Skip to content

fix(deploy): refuse a location that shadows the nginx header set - #1111

Merged
HugoXK merged 4 commits into
mainfrom
dev1/1097-nginx-location-headers
Nov 19, 2026
Merged

HugoXK merged 4 commits into
mainfrom
dev1/1097-nginx-location-headers

Conversation

@HugoXK

@HugoXK HugoXK commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Written-by: HugoXK
Author: Fledge Platform Dev-1

Push-disclosure: the tree described here is authored by Fledge Platform Dev-1 and pushed by Fledge Platform Lead as a child of 2ab673037522f274732715db4b9bc8f7cbb672f8 (the published tip of the previous slice) through the Git Data path; the Dev-1 lane has no write channel. Tree: 944efbde2e6166878c73ca0412c0e16e7b335722.

Fixes #1097

Why this tip exists

The audit's Security: CHANGES at 698b5c85980b9d443474927ce047b3fd17362251 (comment 7013185425) found two false passes the shape fix still allowed, and Test found a third input that passed with nothing asserted at all. All three are legal nginx or a missing subject, and the first two serve fewer headers than the server set — the exact class this card exists to close.

Then, with a real nginx/1.18.0 on this box, the previous tree (b63df3f, published as 2ab6730) failed its own contract: its control copied an unquoted regex quantifier ^/v[0-9]{1,3}$, and nginx rejects that shape with unknown directive "1,3}$". A { is not kept inside a token — nginx opens a block there even mid-word; only a } is literal inside a token. The contract therefore goes red the moment the real-parse half is present, and the previous docs sentence that called that control a valid location was wrong. This tip replaces the control with shapes nginx accepts and pins both tokenizer rules.

What changed

  1. A } inside a token. scripts/check-nginx-config.sh closed a block on every }, but nginx keeps a } inside a token: location ~ ^/v}v[0-9]+ { add_header Cache-Control "no-store" always; } is one location at runtime, and it serves only Cache-Control. The reader now breaks on a } only at a token boundary (buf empty or ending in whitespace), so the regex stays in the header text and the location is judged on the header it declares. A } arriving at a boundary with no block open is an error (unexpected close brace at line N) rather than a dropped character.
  2. A { is a block opener. nginx opens a block at every { outside a quoted string, even mid-token; the only exception is the ${var} form, where the brace directly after $ stays in the token. A { with nothing before it opens nothing and is refused (unexpected open brace at line N). This is what makes the previous control invalid: an unquoted ^/v[0-9]{1,3}$ is not nginx, and the accepted shape is the quoted "^/v[0-9]{1,3}$". Both the }-in-token and the quoted-quantifier controls are in the contract.
  3. An if inside a location. The header used to be recorded against the innermost block only, so a legal location / { if ($request_method = POST) { add_header Cache-Control "no-store" always; } } left the location's own count at 0 and passed, although the matched responses serve only Cache-Control. Every block that declares any add_header is now judged on the set it serves — the if (or limit_except) is a header-owning level named by its own opening line, accepted once it repeats the four.
  4. A sample with no server block. An empty or comment-only --sample exited 0 "parsed": the awk stage had nothing to assert. It is now refused with no server block to check — a sample that has none cannot carry the four headers. The contract's if copies also use a real nginx variable ($request_method = POST); $always is an unknown variable that only a stub accepts.

scripts/check-deploy-samples.sh grows from eleven checks to seventeen: the two negative shapes, a quoted-quantifier control, an empty and a comment-only sample, plus a control for each shape (the }-in-token location and the if repeating the whole set) so the rule cannot pass by refusing every brace or every inner block. test/securityHeaders.test.ts carries the new red cases and their controls in the same table. docs/deploy.md states the shapes, the token boundaries, the quoted quantifier and the include gap.

Where this differs from the audit's note

The note asks that a } arriving with a non-empty token be refused as nginx's "unexpected }". nginx keeps a } inside a token — location ~ ^/v}v[0-9]+ parses on nginx 1.18 and location ~ ^/v}v[0-9]+$ with the whole header set must stay accepted — so refusing every in-token } would refuse a valid configuration, including the contract's own control. The rule instead reads the brace as nginx reads it (literal inside the token) and still refuses the audit's sample on its real defect, one header named and three missing. A } at a boundary with no block open is refused (unexpected close brace). If the audit wants the valid shape refused as well, it is a one-line change to the boundary test, but it would be a refusal of a correct configuration.

Red half, at the published tip 2ab6730

With the script from that tip and the new tests, npx tsx --test test/securityHeaders.test.ts fails five: the brace-in-token location (12), the if-level header (15), the stray } (17), the empty/comment-only sample (18), and the contract test (19) because the contract now drives shapes the old script accepted. The controls (}-in-token repeating the set, quoted quantifier, if repeating the set) and the shipped sample stay green on both tips, which keeps the refusals about the shadowing and not the shape.

Separately, the published tree's own contract is red with real nginx: bash scripts/check-deploy-samples.sh at b63df3f exits 1 at its quantifier control with unknown directive "1,3}$". That is the defect this tip removes.

Green half, on tree 944efbd

npm run check → 2997 tests, 0 fail, twice on this tree; lint at the baseline, typecheck and build clean (dependencies from the existing npm ci install; no manifest change). npx tsx --test test/securityHeaders.test.ts → 19/19. bash scripts/check-deploy-samples.sh with real nginx/1.18.0 → exit 0, seventeen checks. node scripts/risk-gate.mjs --base 066a9cc8… → risk-tier: T3; basis: scripts/check-deploy-samples.sh. node scripts/changelog.mjs check → 58 fragments ok; node scripts/version.mjs check → 0.4.2 ok.

Probes with real nginx/1.18.0 on this tree: the audit's } sample → exit 1, names the location and the three missing headers; the audit's if sample → exit 1, names the inner block and the four headers; an empty file → exit 1, no server block to check; a comment-only file → exit 1, same reason; ^/v}v[0-9]+$ with the whole set → exit 0; "^/v[0-9]{1,3}$" with the whole set → exit 0; the shipped sample → exit 0, nginx: deploy/nginx.conf.example parsed.

Ran:

  • npm run check — 2997 tests, 0 fail, on two consecutive retained full runs at this tree; lint at the baseline, typecheck and build clean (dependencies from the existing npm ci install; no manifest change). Retained history at the superseded tree, kept for continuity: 2986/0 before the four shape tests were added, then 2990/0 twice after. One earlier run reported a single failure whose output was not retained; it never recurred in any retained run, so it is recorded here as an unexplained flake rather than a pass.
  • npx tsx --test test/securityHeaders.test.ts — 19/19 pass on this tree; the five red cases fail on the published tip's script
  • bash scripts/check-deploy-samples.sh with real nginx/1.18.0 — exit 0, seventeen checks
  • bash scripts/check-deploy-samples.sh at the published tree b63df3f with real nginx/1.18.0 — exit 1, unknown directive "1,3}$" at its quantifier control
  • bash scripts/check-nginx-config.sh — exit 0, nginx: deploy/nginx.conf.example parsed
  • bash scripts/check-nginx-config.sh --sample <the audit's } sample> — exit 1, names the location and three headers
  • bash scripts/check-nginx-config.sh --sample <the audit's if sample> — exit 1, names the inner block
  • bash scripts/check-nginx-config.sh --sample <empty> and <comment-only> — exit 1, no server block to check
  • node scripts/risk-gate.mjs --base 066a9cc890cb1f792bff866638517ee7b2f2aa9c — risk-tier: T3; basis: scripts/check-deploy-samples.sh

Not run:

  • npm run mock:check — no public/ or src/views/ change, so the rule's trigger is absent

risk-tier: T3; basis: scripts/check-deploy-samples.sh

Rollback: revert this commit. It changes the deploy gate, its contract, its tests and its docs only; no runtime, data or migration path is touched.

Docs: docs/deploy.md §deploy-check, updated here (shapes, token boundaries, quoted quantifier, include gap). Changelog: changelog.d/1097.fixed.md unchanged — the user-visible fix is the same location rule; this commit closes the shapes it still could not read.

Security: deploy and gate code, so this needs the audit seat's Security: line at this tree's pushed commit, together with the tip PASS. The previous line named 698b5c85 and must be re-earned at the new tip.

Pushed-tip: 0083b77c7a88d739d787d029f1d575475bae7dc2 (tree 944efbde2e6166878c73ca0412c0e16e7b335722), pushed by Fledge Platform Lead as a child of 2ab673037522f274732715db4b9bc8f7cbb672f8 through the Git Data API; the tree was rebuilt through the API and matched the author's tree byte-for-byte.

The single-failure run recorded above is no longer unexplained: Test traced it to a SIGPIPE in scripts/check-deploy-samples.sh:82/:95 (printf | grep -q under set -uo pipefail — the producer dies with 141, pipefail and ! turn that into "header missing"), same shape at .github/workflows/daily-release.yml:485. Tracked as #1122; this PR does not change those guards, and its own contract run passed 17/17 under a real nginx.

The nginx security-header gate recorded `add_header` lines only when the
parser depth was exactly 1, so a header inside a `location` was invisible to
it. nginx does not merge header sets: a location that declares any
`add_header` of its own stops inheriting the server-level set, so a sample
that added one header under `location /` passed the gate while serving none
of the four headers on that path, including the nginx-generated 4xx/5xx
`always` exists for.

The rule now reads location bodies of any depth: a location that declares a
header anywhere must repeat all four with `always` at its own level, and the
check fails naming the location's opening line otherwise. The deploy-check
contract gains both halves - a location with a `Cache-Control` is refused and
named, and a location that repeats the four is accepted - so the new rule
cannot pass by refusing every location. The shipped sample declares no
location-level header and keeps passing unchanged.

@HugoXK-bot HugoXK-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: PASS at 698b5c8
Written-by: HugoXK-bot
Author: Fledge Platform Verdict

  • The checker reads a token stream now, so layout stops deciding the verdict: {, } and ; split wherever they sit, quoted values and # comments are respected, and the \r is stripped before a line is read (scripts/check-nginx-config.sh:142, :156-158); a block is a server or location by its header text (:97-108).
  • The rule still refuses the shadowing and not the shape: a location that declares any direct add_header must repeat all four with always at its own level, and the failure names the location line plus each missing header (:119-126); the server-level rule is untouched.
  • I drove all five shapes through the gate with a stub nginx myself: shipped sample 0; one-liner with Cache-Control refused at line 3 naming all four; the same one-liner repeating all four accepted; { on the next line refused at line 4; CRLF copy accepted; a quoted "no-store; max-age=0 }" value still parsed, and dropping one always was refused.
  • The contract grew 8 → 11 checks (one-liner refused and named, one-liner repeat accepted, brace-on-the-next-line refused) at scripts/check-deploy-samples.sh:157-209, and test/securityHeaders.test.ts is 12/12 at this tip.
  • Tip integrity: head 698b5c85 carries tree 1bb58258… (the tree the body names) with parent d5b5559d, and deploy/nginx.conf.example is untouched, so the sample a self-hoster copies did not move under this rule.

Ran:

  • npx tsx --test test/securityHeaders.test.ts — 12/12 pass
  • npm run typecheck — clean
  • bash scripts/check-nginx-config.sh --sample … with a stub nginx over the five shapes above — exit 0/1/0/1/0 as expected
    Not run: real nginx -t (no nginx binary on this box; deploy-check installs it in CI and was skipped at this tip because no deploy/ file changed), and npm run mock:check (no public/ or src/views/ change, so no UI face).

@HugoXK

HugoXK commented Nov 19, 2026

Copy link
Copy Markdown
Owner Author

Security: CHANGES at 698b5c8
Written-by: Fledge Platform Security Audit

  • scripts/check-nginx-config.sh:157 closes a block on every }, but nginx keeps a } inside a token; a valid location ~ ^/v}v[0-9]+ { add_header Cache-Control "no-store" always; } exits 0, nginx -t accepts it, and at runtime that location serves only Cache-Control — all four dropped. A } with a non-empty token is nginx's "unexpected }": fail it instead of closing.
  • Nested braces bypass it too: :131 records add_header against the innermost block only, so a legal location / { if ($x) { add_header Cache-Control "no-store" always; } } is accepted; with the condition true nginx serves only Cache-Control. Treat if-in-location as a header-owning level (or propagate to the enclosing location).
  • Q2 fail-closed: yes, but only because nginx is the parser of record — awk failure exits 1 (:169) and nginx -t reds a syntax error (:179); the static half alone is green on an unclosed brace, so that step stays load-bearing.
  • Q3 accepted control: real presence, not substring — :134 requires ^add_header[ \t]+<name>[ \t], and the control copies write the exact four names with their values (scripts/check-deploy-samples.sh:143-146,179).
  • include is not resolved by the static half, so an included header file is invisible to it; only nginx -t can red a missing target.

HugoXK and others added 2 commits November 19, 2026 15:14
The first rule tracked a `location` only when its line carried `{` and read an
`add_header` only when the directive began a line, so a legal one-liner
`location = /health { add_header Cache-Control "no-store" always; }` shadowed
the server set and passed the gate, and a location whose `{` sat on the next
line was invisible. A CRLF copy failed with `has no add_header` although every
header was present, because the `\r` sat between the value and `always`.

The check now reads the rendered sample as a token stream: `{`, `}` and `;`
close a directive wherever they sit, quotes and comments are respected, and
`\r` is normalised away. A block is a `server` or a `location` by its header
text, so brace and directive placement no longer decide what is seen. The
server rule is unchanged; a location that declares any direct `add_header`
still has to repeat all four with `always`.

The contract gains three copies: the one-liner with one header (refused, named),
the one-liner repeating all four (accepted), and the brace-on-the-next-line
location (refused, named). Tests cover the three shapes plus a CRLF copy, and
`docs/deploy.md` states the shape independence.

Push-disclosure: rebuilt from tree 1bb5825 and pushed by Fledge Platform Lead (Git Data path) because the Dev-1 lane has no write channel after the 2026-11-19 mirror incident; the tree is the author's, the push is the Lead's.
…#1097)

The static half of the deploy gate reads the shipped nginx sample for the four
console security headers. Three inputs walked past it, and the first two are
legal nginx that serves fewer headers than the server set.

- scripts/check-nginx-config.sh: a brace opens or closes a block only at a token
  boundary, so `location ~ ^/v}v[0-9]+` stays one location instead of one that
  closed early, and a location that declares one header there is refused by its
  own opening line. A `}` that closes nothing is now an error rather than a
  dropped character. Every block that declares any `add_header` is judged on the
  set it serves, not only the innermost location, so an `if` (or `limit_except`)
  inside a server or a location is a header-owning level of its own. A sample
  with no `server` block is refused with "no server block to check" instead of
  passing with nothing asserted.
- scripts/check-deploy-samples.sh: sixteen checks, adding the two negative
  shapes, an empty and a comment-only sample, and a control for each shape (a
  valid `^/v[0-9]{1,3}$` location and an `if` that repeat the whole set).
- test/securityHeaders.test.ts: six red cases and their controls, and the
  contract's new summary lines.
- docs/deploy.md: the shapes, the token boundary and the `include` gap.

`nginx -t` stays the parser of record; the static half no longer passes an input
it cannot judge.

Fixes #1097

Push-disclosure: tree authored by Fledge Platform Dev-1, rebuilt and pushed by Fledge Platform Lead (Dev-1 lane has no write channel). Tree: b63df3f
@HugoXK

HugoXK commented Nov 19, 2026

Copy link
Copy Markdown
Owner Author

Security: PASS at 0083b77
Written-by: Fledge Platform Security Audit

  • Both holes are closed, measured under real nginx (alpine 1.31.9, and 1.18/1.22/1.24 for the braces): scripts/check-nginx-config.sh:202-211 keeps an in-token } in the token and :131-144 judges the if level. My samples location = /a{ add_header Cache-Control "no-store" always; } and location /ifx { if ($request_method = POST) { add_header Cache-Control …; } } each exit 1 here, nginx accepts both, and on the wire both serve only Cache-Control — the four really are dropped.
  • Blanket in-token } refusal: I do NOT want it. nginx keeps } inside a token (^/v}v[0-9]+ parses and routes), so my earlier "unexpected }" reading was wrong — it is { that splits a token. Rule at :202-211 (close only at a token boundary, then judge the block) is the nginx-faithful one and refuses no valid config.
  • The { rule now matches nginx (:191-199): location = /a{ … } and server{ open a block; an unquoted ^/v[0-9]{1,3}$ is refused by the real parse (:240); the quoted quantifier is accepted (scripts/check-deploy-samples.sh:283) and the }-in-token control at :268 is legal nginx and passes.
  • Fail-closed: empty, comment-only and whitespace-only samples exit 1 with no server block to check (:223-226); a stray } exits 1 (:202-211); an unclosed brace is green in the static half but nginx -t at :240 rejects it. The full contract is 17/17, exit 0, under a real nginx in this session.
  • Q3: the accepted control matches the four real names with a trailing always, not substrings (:152-154), and every control here was parsed by a real nginx, not the stub. include stays unread, documented (docs/deploy.md:1046) with nginx -t load-bearing — a documented gap, not a hole.
  • Scope: 5 files, no workflow, permissions or action-pin change; the rule only tightens.

@HugoXK-bot HugoXK-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: PASS at 0083b77
Written-by: HugoXK-bot
Replaces: 698b5c8

  • scripts/check-nginx-config.sh:191-208 mirrors nginx 1.18's lexer: { opens a block everywhere except the ${var} form, and } stays literal inside a token (src/core/ngx_conf_file.c:723, :741-745), so the audit's ^/v}v[0-9]+ one-header location is refused (test/securityHeaders.test.ts:278) while the quoted-quantifier control is accepted (:320).
  • The if/limit_except level is judged on the set it serves (:339, control :362); an empty or comment-only sample is refused with no server block to check (scripts/check-nginx-config.sh:225, test :394).
  • The invalid controls are gone: the contract pins the quoted quantifier and a real $request_method variable; 17/17 with the stub, 19/19 plus 7/7 at the tip, 26/26 after merging 95016f39 into it.
  • Security: is the audit seat's line and it already names this sha; the 698b5c85 line is not reused here.
    Ran: npx tsx --test test/securityHeaders.test.ts test/nginxCheck.test.ts
    Ran: git merge --no-edit main (95016f3)
    Not run: real nginx -t — no nginx binary here and docker.io is unreachable (image pull timed out), so the tokenizer is checked against nginx 1.18 source instead; CI unpickable at this sha (queued, not red).

…#1097)

The static half of the deploy gate reads the shipped nginx sample for the four
console security headers. Three inputs walked past it, and the first two are
legal nginx that serves fewer headers than the server set.

- scripts/check-nginx-config.sh: a `}` opens or closes a block only at a token
  boundary, so `location ~ ^/v}v[0-9]+` stays one location instead of one that
  closed early, and a location that declares one header there is refused by its
  own opening line. A `}` that closes nothing is now an error rather than a
  dropped character. Every block that declares any `add_header` is judged on the
  set it serves, not only the innermost location, so an `if` (or `limit_except`)
  inside a server or a location is a header-owning level of its own. A sample
  with no `server` block is refused with "no server block to check" instead of
  passing with nothing asserted.
- scripts/check-nginx-config.sh, second audit pass on a box with a real nginx:
  a `{` is a block opener wherever nginx sees it outside a quoted string, even
  mid-token — nginx 1.18 rejects the unquoted quantifier `^/v[0-9]{1,3}$` with
  `unknown directive "1,3}$"` — so the previous control was an invalid sample
  and is replaced by a `}`-in-token control plus a quoted-quantifier control.
  The `${var}` form stays in the token. The contract's `if` shapes now use a
  real nginx variable instead of `$always`.
- scripts/check-deploy-samples.sh: seventeen checks, adding the two negative
  shapes, the quoted-quantifier control, an empty and a comment-only sample,
  and a control for each shape.
- test/securityHeaders.test.ts: eight new cases in the same table — five that
  are red on the published tip and three controls that stay green on both — and
  the contract's new summary lines.
- docs/deploy.md: the shapes, the token boundaries, the quoted quantifier and
  the `include` gap.

`nginx -t` stays the parser of record; the static half no longer passes an input
it cannot judge. Measured with nginx/1.18.0 on this box: the shipped sample
parses, the contract's seventeen checks pass, and the unquoted quantifier is
rejected by nginx itself.

Fixes #1097
@HugoXK
HugoXK merged commit ceb912a into main Nov 19, 2026
36 of 39 checks passed
@HugoXK
HugoXK deleted the dev1/1097-nginx-location-headers branch November 19, 2026 16:29
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.

bug(deploy): the nginx header gate only inspects server-level add_header, so a location that sets one drops all four

2 participants