Skip to content

fix(invalid_regex): validate $N references in map values and across the whole scope; read $N as a single digit#67

Merged
MegaManSec merged 2 commits into
masterfrom
fix/invalid_regex-coverage
Jun 9, 2026
Merged

fix(invalid_regex): validate $N references in map values and across the whole scope; read $N as a single digit#67
MegaManSec merged 2 commits into
masterfrom
fix/invalid_regex-coverage

Conversation

@MegaManSec

Copy link
Copy Markdown
Owner

Previously only rewrite replacements and set-inside-if were checked, so a $1 against a captureless regex went undetected in map values, server_name/location regex contexts, return, proxy_pass, add_header, more_set_headers and try_files. Two new checks:

  • map entries (exact): a map regex evaluation resets the request's numbered captures — verified against nginx at runtime — so $N in a regex entry's value can only come from that entry's own pattern.
  • whole-scope (conservative): for other value-carrying directives, flag $N only when no regex that could run for the request — the enclosing server's location/if/rewrite/server_name patterns, plus any map regex key — defines group N, making the reference provably empty. Any unparseable provider pattern silences the check.

Also fix the reference tokenizer: nginx reads exactly one digit after $ (ngx_http_script_compile), so $12 is capture $1 followed by a literal 2; the old [1-9]\d* tokenizer reported $12 as a nonexistent group 12 (false positive).

…he whole scope; read $N as a single digit

Previously only rewrite replacements and set-inside-if were checked, so
a $1 against a captureless regex went undetected in map values,
server_name/location regex contexts, return, proxy_pass, add_header,
more_set_headers and try_files. Two new checks:

- map entries (exact): a map regex evaluation resets the request's
  numbered captures — verified against nginx at runtime — so $N in a
  regex entry's value can only come from that entry's own pattern.
- whole-scope (conservative): for other value-carrying directives, flag
  $N only when no regex that could run for the request — the enclosing
  server's location/if/rewrite/server_name patterns, plus any map regex
  key — defines group N, making the reference provably empty. Any
  unparseable provider pattern silences the check.

Also fix the reference tokenizer: nginx reads exactly one digit after
$ (ngx_http_script_compile), so $12 is capture $1 followed by a
literal 2; the old [1-9]\d* tokenizer reported $12 as a nonexistent
group 12 (false positive).
… the provider scan

- Map regex keys are providers for directives at http level too (an
  add_header there is evaluated per request, after any lazily-evaluated
  map has run), so drop the base-is-not-root condition that silently
  excluded them outside server scope.
- Skip map/geo entries in _provider_patterns: they carry their key
  string as .name, so a key spelled like a directive ('rewrite', ...)
  would have been misread as a capture provider. Correct the _audit_map
  comment that claimed entries share the name 'map', and reuse the
  Regexp that MapDirective already parses instead of re-deriving
  pattern and case-sensitivity from src_val.
- Repair proxy_pass_normalized's missing_variable*_fp.conf, which the
  whole-scope check rightly flagged: 'rewrite ^ $request_uri;' alone
  defines no captures and loops (verified: 500 internal-redirect cycle
  on nginx 1.27), so $1 was provably empty. Use the full raw-URI relay
  idiom with a second capturing 'rewrite ... break' (verified: backend
  receives the raw /%2F path).
@MegaManSec
MegaManSec merged commit e92ac3e into master Jun 9, 2026
23 of 24 checks passed
@MegaManSec
MegaManSec deleted the fix/invalid_regex-coverage branch June 9, 2026 23:02
MegaManSec added a commit that referenced this pull request Jun 9, 2026
…anged

The first commit pinned the NEW behaviors; these pin the behaviors that
were already correct before #62-#67 and had no test keeping them that
way:

- return_bypasses_allow_deny: a flagless rewrite with a URI replacement
  is not a bypass; the named-location pruning applies to redirecting
  rewrites just as it does to return.
- unanchored_regex: prefix (non-regex) locations are never flagged;
  '~*' case-insensitive regexes still are; a PCRE-only pattern the
  vendored parser cannot read stays quiet instead of crashing.
- allow_without_deny: auth alone (no 'satisfy any') still warns; a
  sibling location's satisfy/auth does not leak into another location's
  allow.
- invalid_regex: $12 against a captureless pattern reports $1 (single
  digit), not $12; the whole-scope union spans sibling locations of the
  same server; static/default map values are never validated against
  their key.
- status_page_exposed: stub_status directly in server context is still
  audited.
MegaManSec added a commit that referenced this pull request Jun 9, 2026
…es (#70)

* tests: pin probe-verified behaviors of the recently merged plugin fixes

Each of these was verified by hand (CLI probes, and runtime checks
against nginx 1.27 in docker) while reviewing #62-#67 but had no test
pinning it:

- status_page_exposed: http-level allow/deny inheritance; a location
  whose own partial rules replace the inherited ones (all-or-nothing
  merge) is flagged again.
- return_bypasses_allow_deny: 'rewrite ... break' with a URI
  replacement reaches the access phase (not flagged); a $scheme://
  replacement redirects even under 'break' (flagged); the absolute-URL
  prefix check is case-sensitive like nginx's, so 'HTTP://' does not
  count.
- unanchored_regex: \A/\Z string anchors; a lookahead carrying the
  anchor; min>=1 repeats anchor while min=0 repeats do not; an escaped
  backslash before 'z' is not a \z anchor.
- allow_without_deny: auth_jwt participates in the satisfy-any
  suppression; an explicit 'satisfy all' still warns.
- invalid_regex: the set-outside-if fallback path (flag without a
  provider, stay quiet with one); an unparseable provider regex
  silences the whole-scope check; add_header and try_files are audited;
  a static map key spelled 'map' is skipped without crashing while the
  sibling regex entry is still validated.

* tests: guard pre-existing behaviors the recent fixes must not have changed

The first commit pinned the NEW behaviors; these pin the behaviors that
were already correct before #62-#67 and had no test keeping them that
way:

- return_bypasses_allow_deny: a flagless rewrite with a URI replacement
  is not a bypass; the named-location pruning applies to redirecting
  rewrites just as it does to return.
- unanchored_regex: prefix (non-regex) locations are never flagged;
  '~*' case-insensitive regexes still are; a PCRE-only pattern the
  vendored parser cannot read stays quiet instead of crashing.
- allow_without_deny: auth alone (no 'satisfy any') still warns; a
  sibling location's satisfy/auth does not leak into another location's
  allow.
- invalid_regex: $12 against a captureless pattern reports $1 (single
  digit), not $12; the whole-scope union spans sibling locations of the
  same server; static/default map values are never validated against
  their key.
- status_page_exposed: stub_status directly in server context is still
  audited.
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.

1 participant