Fail-loud validation of the X-Accel facts nginx location.
The X-Accel-Redirect serving path's confidentiality depends on the facts
nginx location being internal; — only the authz'd handler's redirect (an
internal subrequest) may reach it. A *public* facts location would serve any
fact's bytes to anyone who guesses the <shard>/<rest> path, bypassing every
cell-visibility check. A location with its own add_header also stops the
server-level headers from applying, so the facts location must carry the fact
hardening headers itself (fact_security_header_directives in
server/fact_security_headers.ts).
XAccelConfig makes these assertions structural: a consumer can only obtain
the redirect prefix by building an XAccelConfig, which runs
validate_facts_internal_location and throws loudly on any violation — it
cannot silently fall closed.
Rules
The config is tokenized as nginx reads it (ngx_conf_read_token): whitespace
is exactly space, tab, CR and LF; an unquoted word ends only at those or at
; / { (a } inside a word is part of it, and so is a { straight after
$); # starts a comment only where a token would start; quoted strings are
one word, with nginx's backslash escapes. It is then parsed into nested
blocks, and every location at any depth, in every server block, is
classified against the prefix (trailing slashes ignored). A location *serves
the prefix* when it is a prefix-type location (no modifier, or ^~) whose
path is the prefix or below it. Each one must:
- use
^~ — a plain prefix location is refused; - be a direct child of a
server block — one nested in another location is
refused; - carry, as top-level directives of its own body, an exact
internal; and
each fact_security_header_directives line (token-equal: whitespace and
quoting are free, the words are not); - set those headers with no other
add_header — one naming a
FACT_SECURITY_HEADERS header (ASCII case-insensitive) that is not
token-equal to its required line is refused, since nginx sends both values
and a client may read only the first (sniff, nosniff); - hold only simple directives — any nested block (
location, if,
limit_except, …) is refused, and so is include, since included files
are not followed.
The first two rules are what keep regex locations out: nginx never evaluates
regex locations for a path whose longest prefix match is a ^~ location at
server level, and because every prefix location at or below the prefix must
be such a location, the longest match for any redirected path always is. (A
longer plain location below the prefix can't exist — the ^~ rule refuses
it.)
The prefix may appear only in its own location lines and their bodies:
internal; admits every internal redirect and subrequest, so any other
directive with a word naming the prefix (trailing slash trimmed, so
/_facts$uri counts) is refused, in any block at any depth — a rewrite,
try_files, error_page, index, add_before_body, or a set / map
value that a redirect later reads. The only exemptions are each location's
own modifier and path words and the bodies of the locations serving the
prefix.
The prefix itself must be an absolute path ending in exactly one / — the
server appends <shard>/<rest> straight onto it, so /_facts would redirect
to /_factsab/…, outside the validated location. At least one location
serving the prefix must sit exactly at it (/_facts or /_facts/): one only
below it (location ^~ /_facts/00/) leaves every other redirect path to
whatever location matches next, and nginx compares location paths
byte-for-byte, so /_facts// sits below it too.
Module directives that undo those guarantees are refused too:
ssi with any value but off, anywhere in the config. SSI `include
virtual is a subrequest, which internal;` admits, so an SSI-processed
response — a fact inheriting ssi on from http or server, or any page
carrying user content — could include another fact's bytes.more_set_headers / more_clear_headers (headers-more) everywhere except
inside a location that doesn't serve the prefix (or a block within one):
they run after add_header and can strip the hardening headers, so they
are refused in the facts location and at every level above it, including
any block there (if, another server).
Also refused: an = location on the prefix or below it (it can't serve the
nested paths the redirect emits); a regex location (~ / ~*) whose pattern
spells out the prefix (after unescaping \/, ASCII case-folded for ~*) —
dead under the rules above, refused for the clearer message; a config with no
location at the prefix; and blocks nested more than 64 deep (a parse error, so
the walks can't exhaust the stack). include at server level is allowed, but
its file is not read — keep facts-related locations and the directives above
out of included files.
The threat model is operator mistakes, not an adversarial config author: the
prefix check is textual, so a value assembled from pieces (/_fac$1) evades
it. Beyond the config text:
- The validator checks URL paths only — another
root or alias that maps
onto the facts directory would serve its files publicly, so keep the facts
directory outside every other location's root. - nginx honours
X-Accel-Redirect from any proxied upstream (proxy_pass,
fastcgi_pass, uwsgi_pass, scgi_pass, grpc_pass), so every upstream in
the server is trusted to point into the prefix; an untrusted one needs
proxy_ignore_headers X-Accel-Redirect; (or the fastcgi_ / uwsgi_ /
scgi_ equivalent). - Other module code can redirect internally too (njs
internalRedirect, perl,
echo), or strip the hardening headers as headers-more can (njs
js_header_filter, lua-nginx-module header_filter_by_lua*), and the
check sees none of it, so run trusted modules and content only.
The Rust twin is fuz_fact_serving's nginx.rs, held to the same rules and
the same fixture corpus.