Skip to content

Enforce the environment whitelist for YAML files, failing closed (#157) - #164

Merged
jrosskopf merged 3 commits into
mainfrom
fix/gh-157-environment-whitelist
Sep 29, 2026
Merged

jrosskopf merged 3 commits into
mainfrom
fix/gh-157-environment-whitelist

Conversation

@jrosskopf

@jrosskopf jrosskopf commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

{{env.NAME}} in a YAML config file is documented as requiring NAME to match template.environment-whitelist. It never did. The whitelist was only read for SQL templates. For YAML files the parser had an empty whitelist that meant "allow everything": I measured a variable substituting identically with a whitelist that excluded it, one that included it, and none at all. Closes #157.

The shipped S3/GCS/Azure examples made it worse by putting environment-whitelist: at the top level, where nothing reads it either, so anyone copying them believed they had restricted which variables a config could read.

Behaviour, per your decisions

  • An unlisted variable stops startup, naming every such variable and the key to add it to. Never a silent literal: a literal {{env.DB_PASSWORD}} would quietly become the password.
  • An empty or missing whitelist allows no variables, the rule SQL templates already had. The two implementations no longer disagree.
  • A top-level environment-whitelist: is an error naming template.environment-whitelist.

How

The whitelist lives in the file being substituted, so ConfigManager::applyEnvironmentPolicy() reads it first, from the raw text (each {{env.X}} neutralised, include directives dropped), so a config that is only valid YAML after substitution still yields its whitelist. It validates it (a list of strings, each a valid regex, reported by pattern) and applies it to the shared ExtendedYamlParser. That parser also loads every endpoint file, so the rule covers the main config, endpoint files and runtime reloads. Content pulled in with {{include}} is not scanned; that is a pre-existing gap, see #165.

Found along the way

Include conditions bypassed it {{include ... if env.X}} called getenv directly, so a config could probe whether any variable was set. Now whitelisted.
Comments are scanned A comment that merely mentioned {{env.NAME}} would fail startup. My own explanatory comment stopped the shipped example starting. Full-line comments are now skipped; trailing ones are still checked (telling one from a # inside a quoted value needs a tokenizer, and failing loudly is the safe way to be wrong).
Debug log leaked values Each substituted variable's value was written to the debug log. It logs the name now.
Docs stated the bug as a feature "If the whitelist is empty or omitted, all environment variables are allowed." Also ^DB_ listed as matching DB_HOST (patterns are full matches), and ${VAR} shown as the standard syntax, which was never supported. Passages this PR rewrites are fixed; the other ~20 ${VAR} examples are #163.
A test fixture had the same bug test/integration/api_configuration/flapi.yaml. My first static guard silently skipped it (unquoted {{env.X}} isn't valid YAML until substituted); the guard now neutralises references and has a test that it sees that file.

Review

Two rounds, both requested changes; every finding was reproduced before acting.

Round 1 (Codex, correctness + usability): fixed

  • The probe needed valid YAML, so a config that is only valid after substitution (an env variable in an include path, a bare reference plus trailing text) lost its whitelist and had listed variables rejected: a regression on configs that work on main.
  • A # line inside a block scalar (a Markdown heading in a prompt) was treated as a comment, silently leaving a literal {{env.X}}. The comment check is now block-scalar-aware.
  • The config-service file browser used its own parser with the old empty whitelist.

Round 2 (security, Codex + Muse): fixed

  • Matching was case-insensitive, but variable names are case-sensitive on Linux/macOS, so listing AWS_REGION also permitted reading a separate aws_region. Now case-sensitive, like the SQL side.
  • The file browser exposed values. With the old empty whitelist a url-path: /x-{{env.SECRET}} put any variable's value in the response for a config-service token holder. It no longer substitutes at all.

Not fixed here, with reasons

Breaking change

A config that uses {{env.X}} in YAML without a matching whitelist will no longer start. The changelog leads with the fix (a template.environment-whitelist block) and notes --validate-config reports it, so CI catches it before a deploy. Held for the release: not tagged yet, by your call, so it ships together with #153.

Test plan

  • Red first: 12 of 18 new integration tests failed on the unfixed binary; the 6 fences passed.
  • Static guard: every {{env.X}} in every shipped config is covered by that config's own whitelist
  • 12 new unit tests (803 total). Two older parser tests had only passed because the default was allow-all; they whitelist explicitly now.
  • 814 integration tests passed, 0 failed on the round-1 binary; re-running on the final one: every file that starts its own server
  • The two files needing a fixed :8080 (test_mcp_integration.py, test_mcp_sessions.py) run in CI; an unrelated service holds that port locally
  • Reviewed in two rounds (see above); each fix was red first

Not in this PR

#163 (${VAR} docs), #160 (warn on any unrecognised top-level key), #156, #158, #159, #162.

🤖 Generated with Claude Code

`{{env.NAME}}` in a YAML configuration file is documented as requiring NAME to
match a pattern in `template.environment-whitelist`. It never did. The whitelist
was read only for SQL templates; the parser that substitutes variables into
config files was built with an EMPTY whitelist that it treated as "allow
everything". Measured before changing anything: a variable substituted
identically with a whitelist that excluded it, one that included it, and none
at all. Meanwhile SQL templates treated an empty whitelist as "allow nothing" -
two implementations of one control, disagreeing, one comment contradicting its
own code.

The shipped S3/GCS/Azure examples, and a test fixture, made it worse by putting
`environment-whitelist:` at the TOP level, where nothing reads it either, so
anyone copying them believed they had restricted which variables a config could
read.

Now, by decision:

  - a variable that is not whitelisted STOPS startup, naming every such
    variable and the key to add it to. It is never left as literal text: a
    literal `{{env.DB_PASSWORD}}` would silently become the password;
  - an empty or missing whitelist allows NO variables - the rule SQL templates
    already had, now the only one;
  - a top-level `environment-whitelist:` is an error naming the right key.

The whitelist lives in the file being substituted, so ConfigManager now reads it
first: a probe parse with substitution switched OFF lifts out
`template.environment-whitelist`, validates it (a list of strings, each a valid
regex, reported naming the pattern), and applies it to the shared parser. That
parser also loads every endpoint file, so the rule holds for endpoint files,
includes and runtime endpoint reloads too.

Found and fixed along the way:

  - `{{include ... if env.X}}` conditions read the environment directly, so a
    config could still probe any variable's presence, whitelist or not. They
    obey it now (an error where enforced, "unset" where not);
  - the substitution loop scans YAML comments, so a comment that merely
    MENTIONED {{env.NAME}} - which is how this change's own explanatory comment
    stopped the shipped example starting - would fail startup. A full-line
    comment cannot reference anything and is skipped; a trailing comment is
    still checked, because telling it from a `#` inside a quoted value needs a
    tokenizer and failing loudly is the safe way to be wrong;
  - the debug log printed each substituted variable's VALUE, writing every
    secret a config pulled from the environment to the log. It logs the name;
  - the docs stated the bug as a feature ("If the whitelist is empty or
    omitted, all environment variables are allowed"), listed `^DB_` as matching
    DB_HOST although patterns are full matches, and showed `${VAR}` as the
    "standard syntax", which is left literal and was never supported. The
    passages this change rewrites are corrected, and the Azure example, which
    would have sent a literal `${AZURE_SQL_USER}` as a username, now works. The
    other ~20 `${VAR}` examples are #163.

Shipped configs adjusted: examples/flapi.yaml and flapi-test.yaml widen their
whitelist to exactly the variables their endpoint files read; the S3/GCS/Azure
examples and test/integration/api_configuration/flapi.yaml move theirs under
`template:`.

Red first: 12 of 18 new integration tests failed on the unfixed binary, the 6
fences passed. A static guard checks that every `{{env.X}}` in every shipped
config is covered by that config's own whitelist - and its first version
silently skipped a fixture that is not valid YAML until substituted, which is
how that fixture's misplaced whitelist escaped the first sweep; it now
neutralises references before parsing and has a test that it sees the file.
Two existing parser tests had passed only because the default was allow-all;
they whitelist explicitly now.

806 integration tests (every file that starts its own server), 803 unit tests.
The two files needing a fixed :8080 run in CI.

Closes #157
… as content

From the first Codex review of this PR (correctness and usability, both
requested changes). Each finding was reproduced before fixing.

1. The whitelist probe needed the whole config to be valid YAML before
   substitution. Many are not - `{{env.DIR}}/sqls`, a bare reference followed by
   text, and above all an env variable inside an include path, which the docs
   themselves show. The probe failed, the real parse then ran with an EMPTY
   whitelist, and rejected variables that WERE listed: a regression on configs
   that work on main. Reproduced on three shapes (a bare reference plus trailing
   text, and an env-based include path each rejected a whitelisted variable; a
   bare reference on its own happened to load, and is kept as a fence).

   The whitelist is now read from the raw text: each `{{env.NAME}}` becomes an
   inert scalar and include directives are dropped (in that order, so a directive
   carrying a reference in its path has no nested braces left). Included files
   are not read - the whitelist has to be in the main file, which is now
   documented.

2. A line starting with `#` inside a block scalar (`key: |`, `>-`, `- |`) is
   content - a Markdown heading in a prompt - not a comment. Skipping it left a
   literal {{env.X}} in the text with no error: the silent failure this change
   exists to prevent. The comment check is now a per-input mask that tracks block
   scalars (a block runs to the last line that is blank or indented deeper than its
   header) and, where that is ambiguous, errs toward "content", whose only cost
   is a loud, easy-to-fix error. It is also built once per input, not once per
   match.

3. GET /api/v1/_config/filesystem parses YAML for metadata with its own parser,
   which kept the old empty whitelist. Once empty means none it would have shown
   a literal `{{env.X}}` for a variable that is whitelisted. It takes the
   configured whitelist now, and does not fail on an unlisted variable - it is a
   read-only view.

Considered and not changed: values reaching debug logs through fields flAPI logs
itself. Project secrets are already masked in its logging (`Secret Key:
*****[n]`) and connection properties are never printed, so only a contrived
`project-name: '{{env.DB_PASSWORD}}'` shows a value. The changelog now says the
SUBSTITUTION step does not log values, rather than implying none ever appear.
The authentication guide's `${ADMIN_PASSWORD}` is #163.

Red first: 4 new integration tests and 4 unit-test sections failed on the
previous commit; the fences passed. 803 unit tests, 50 integration tests in the
affected files.
…ser shows no values

From the security review (round 2) of this PR. Findings that were reproduced
and are in scope are fixed; the rest are filed, with reasons.

Fixed:

  - Whitelist patterns were matched with icase, but environment variable names
    are case-sensitive on Linux and macOS: listing AWS_REGION also let a
    config read a SEPARATE, unlisted `aws_region`. A confidentiality bypass,
    if a narrow one. The SQL-template matcher was already case-sensitive and
    the docs, copied from the YAML side, wrongly said otherwise - both now
    agree. Red first: the case-variant test and the lowercase-pattern test
    failed without the change; the exact-case test passed as a fence.

  - GET /api/v1/_config/filesystem parsed YAML for labelling with its own
    parser and an empty whitelist, which used to mean "allow everything": a
    `url-path: /x-{{env.SECRET}}` put ANY variable's value in the response for
    whoever held the config-service token. The previous commit narrowed it to
    whitelisted values; a directory listing has no business showing even those,
    so it no longer substitutes at all and shows the file as written. This
    reverses last round's choice, deliberately: that fix answered a usability
    concern, this one a disclosure.

Found and NOT fixed here, on purpose:

  - {{env.NAME}} inside a file pulled in with {{include}} is never substituted
    or checked (loadYamlFile is a bare YAML::Load). Reproduced. It is not a
    regression - identical on main - and not a confidentiality problem, but it
    made this PR's docs and changelog claim more than it delivers, so they now
    say included files are not scanned. Fixing it is not a one-liner: the shipped
    customers-rest.yaml includes `auth` from customer-common.yaml, whose twelve
    {{env.CUSTOMER_API_*}} references are literal today, and substituting an
    UNSET variable yields an empty string - empty credentials on an
    authenticated endpoint. That needs a decision, not a patch: #165.

  - A debug config dump after a YAML exception, secret values retained in
    resolved_variables_, the environment listing treating patterns as names,
    per-call regex compilation, two comment detectors that disagree, and
    near-miss syntax such as `{{ env.X }}` staying literal: #166.

Checked and found safe by the reviewers, and by reading the code: second-order
substitution (single pass, no rescan of inserted values), the include-condition
existence oracle, fail-open paths (an unreadable probe leaves an empty whitelist
with enforcement ON, so it fails closed), config-service endpoint create/update
(in memory only, so a token holder cannot plant a file that breaks the next
restart), and bundle mode.
@jrosskopf
jrosskopf merged commit d301660 into main Sep 29, 2026
23 checks passed
@jrosskopf
jrosskopf deleted the fix/gh-157-environment-whitelist branch September 29, 2026 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

environment-whitelist is set at the top level in the S3/GCS/Azure examples, where nothing reads it

1 participant