nginx-proxy-manager/test/nginx-semantics/echo.js
Tom Winnerl 7bca4cc170 fix(review): run cached assets of a location through its whole advanced config
The nested asset location of a custom location with "Cache Assets" only
repeated the top-level `set` statements of the advanced config. nginx runs
none of a parent's rewrite-phase directives in a nested location, so an
`if (...) { return 403; }` guard, a `rewrite` or a `set` inside an `if` were
skipped for assets: `^~ /private/` with `set $backend app; if
($http_x_token != secret) { return 403; }` served /private/file.css without
the token.

The asset location now repeats the location's whole body (templates/
_location_body.conf): the advanced config at the same place before the proxy
directives, the access list, headers, exploit / SSL / HSTS includes and the
proxy settings, plus the cache settings. assetSetDirectives() is gone.

- top-level `location` blocks of the advanced config are left out of the
  copy (nginx rejects a prefix location inside a regex location); their lines
  stay as empty lines so error lines still map to the advanced config
- a cache directive the advanced config sets itself at the top level
  (expires, proxy_cache, proxy_read_timeout, ...) is left out of the cache
  block: nginx rejects most of them twice, and the location's value applies
- an advanced config with a top-level `include` is not repeated as a whole:
  the included file is unknown here, and a second copy of it fails nginx -t
  (Authelia's proxy.conf: "proxy_read_timeout" directive is duplicate; an
  included `expires`; an included `location`, "is outside location"). The
  nested location then repeats the advanced config without the includes and
  without the list settings a nested location only inherits while it sets
  none of its own (proxy_set_header, add_header, more_set_headers, allow /
  deny, error_page, auth_request_set, limit_req, set_real_ip_from,
  proxy_redirect, valid_referers, mirror, error_log, *_types, ...), and sets
  no proxy_set_header / add_header / access / websocket / proxy_redirect
  lines of its own, so it inherits the location's lists, the included
  entries too (auth_request, headers, access list, Authelia's
  "proxy_redirect http:// $scheme://;"). Everything else
  is repeated, so a guard reading a variable of set_by_lua_block or set-misc
  (set_if_empty, set_unescape_uri, ...) applies to assets as well. A
  proxy_hide_header written in the advanced config is repeated (the asset
  block has its own list); rewrite lines and proxy_hide_header inside an
  included file don't apply to assets (documented)
- a top-level proxy_store (not off) leaves proxy_cache out of the cache block
  like a cache directive of the advanced config: nginx -t rejects both in one
  location ("is incompatible")
- the tokenizer records statement offsets, ends and block ends for that

Docs: assets behave like the location; the `set` limitation and the `map`
advice (map is not allowed in a server block) are removed.

Tests: unit tests for the copy, nested locations and own cache directives;
harness cases for a 403 guard on cached assets (also after a cached hit), a
rewrite, a set inside an if, a nested location block and an access list;
unit and harness cases for includes (Authelia-like proxy.conf with cache
directives and proxy_set_header / add_header, an auth_request snippet with
nested locations, an included guard, set_by_lua_block and set_if_empty
guards next to an include, proxy_store with and without an include, an
included proxy_redirect, valid_referers both included and direct).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 09:03:56 +01:00

14 lines
1,015 B
JavaScript

// Answers with the URI and the Host header it received. Port 9001 is the main upstream of the cached hosts
// (gen.mjs), it adds "upstream":"main".
const http = require("http");
const handler = (main) => (req, res) => {
if (req.url.startsWith("/auth")) { res.writeHead(req.headers["x-ok"] ? 200 : 401); return res.end(); }
const h = { "content-type": "application/json" };
if (req.url.includes("redir=abs")) { h.location = "http://echo:9000" + req.url.split("?")[0] + "/next"; res.writeHead(302, h); return res.end(); }
if (req.url.includes("redir=rel")) { h.location = req.url.split("?")[0] + "/next"; res.writeHead(302, h); return res.end(); }
// x-sem-in: a proxy_set_header of an included snippet (run.sh), shown as "in"
const body = { url: req.url, host: req.headers.host, in: req.headers["x-sem-in"] };
res.writeHead(200, h); res.end(JSON.stringify(main ? { ...body, upstream: "main" } : body));
};
http.createServer(handler(false)).listen(9000);
http.createServer(handler(true)).listen(9001);