server/x_accel.ts

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.

const x_accel = create_x_accel_config( '/_facts/', `server { location ^~ /_facts/ { internal; alias /srv/app/facts/; ${fact_security_header_directives().join('\n\t\t')} } }` );
view source

Declarations
#

5 declarations

create_x_accel_config
#

server/x_accel.ts view source

(redirect_prefix: string, nginx_config: string): Branded<{ redirect_prefix: string; }, "XAccelConfig"> import {create_x_accel_config} from '@fuzdev/fuz_app/server/x_accel.js';

Build a validated XAccelConfig, asserting the nginx config serves redirect_prefix only from safe locations (the module-level rules).

The make-impossible-states gate: serving can only emit X-Accel-Redirect into a location proven internal; and header-carrying at boot.

redirect_prefix

the X-Accel redirect prefix, ending in / (e.g. /_facts/)

type string

nginx_config

the nginx config string to validate against

type string

returns

Branded

the validated XAccelConfig carrying redirect_prefix

throws

  • `XAccelConfigError` carrying every violation `validate_facts_internal_location` finds — a prefix not ending in `/`, a missing, public, header-less, re-headered, exact-match, or regex-shadowed facts location, or `ssi` / headers-more reaching it.

NginxFactsValidation
#

server/x_accel.ts view source

NginxFactsValidation import type {NginxFactsValidation} from '@fuzdev/fuz_app/server/x_accel.js';

Result of the facts-location check.

ok

true when every rule passes.

type boolean

errors

Fatal issues, each naming the offending location.

type string[]

validate_facts_internal_location
#

server/x_accel.ts view source

(config: string, facts_location: string): NginxFactsValidation import {validate_facts_internal_location} from '@fuzdev/fuz_app/server/x_accel.js';

Assert the nginx config serves the X-Accel facts prefix only from internal; locations carrying the fact hardening headers. See the module docs for the rules; every violation is collected, so one call reports them all.

config

the nginx config string to check

type string

facts_location

the facts location path the redirect prefix points at (e.g. /_facts/)

type string

returns

NginxFactsValidation

{ok, errors} — ok is true only when every rule passes

XAccelConfig
#

server/x_accel.ts view source

value + type

Branded<{ redirect_prefix: string; }, "XAccelConfig">

type Branded

import {XAccelConfig} from '@fuzdev/fuz_app/server/x_accel.js';

A validated X-Accel redirect configuration — the only handle that enables the X-Accel-Redirect serving path in server/serve_fact_route.ts.

The redirect prefix can be obtained only by passing the nginx config through validate_facts_internal_location (via create_x_accel_config), so X-Accel serving is impossible to enable without proving at boot that the facts location is internal; and carries the fact hardening headers — a public facts location would bypass every cell-visibility check. A branded @fuzdev/fuz_schema object: the brand can't be forged without an explicit cast, so the factory is the only ordinary construction path.

redirect_prefix

The validated redirect prefix the serving path prepends (e.g. /_facts/).

type string

XAccelConfigError
#

server/x_accel.ts view source

import {XAccelConfigError} from '@fuzdev/fuz_app/server/x_accel.js';

A misconfigured X-Accel facts location — a fail-loud boot error thrown by create_x_accel_config.

inheritance

extends: Error

errors

The validator errors that made the location unsafe.

type string[]

readonly

constructor

type new (errors: string[]): XAccelConfigError

errors

type string[]

Depends on
#

Imported by
#