fix(nginx): drop authelia from herdr.zeroq.su, trust proxy chain
test-runner / smoke (push) Canceled after 0s

The herdr-web-ui vhost was using authelia forward-auth like every
other *.zeroq.su vhost, but the upstream Bun server's sameOrigin
check (server/machine-security.ts:18-46) is not designed for
reverse proxies other than Tailscale serve:

  1. POST /api/auth through the vhost returned 500 because nginx's
     auth_request subrequest returned 401 and error_page 401 =302
     only works for GET. (Fixed by exempting /api/auth and
     /api/devices/pair from authelia — commit 61ad57a.)

  2. The sameOrigin function compares the browser's Origin against
     new URL(request.url).origin. For a proxy_pass to 127.0.0.1:7317
     the URL is http://127.0.0.1:7317/..., which never matches
     https://herdr.zeroq.su. The function does NOT read
     X-Forwarded-Host. Every cross-origin POST returned 403
     invalid_origin. (Fixed by in-place patch on the plugin's
     server/machine-security.ts on sapphira to also trust
     X-Forwarded-Host when X-Forwarded-Proto is also set, and to
     reset expected.port so the comparison matches the browser's
     default-port Origin. The .orig file is saved alongside so
     `cp server/machine-security.ts.orig server/machine-security.ts`
     reverts the change before any `herdr plugin update`.)

  3. NixOS nginx with recommendedProxySettings = true appends an
     include of a file that sets Host, X-Forwarded-Proto,
     X-Forwarded-Host AFTER every location's extraConfig.
     My previous `proxy_set_header X-Forwarded-Host $host;` in
     extraConfig ended up sending the header twice
     ("herdr.zeroq.su, herdr.zeroq.su"), the sameOrigin function
     then sees a comma-joined value, fails to parse it as a host,
     and 403s every cross-origin POST. Removing the explicit
     proxy_set_header lines lets the include's single values
     reach the upstream correctly.

  4. Static assets (manifest.webmanifest, sw.js, favicon.ico,
     /assets/*) used to go through the authelia-gated / location,
     which 302-redirected them to authelia.zeroq.su, breaking the
     PWA's manifest load and the service worker. Exempted via a
     regex location; with the bun patch and the include-based
     header set, they now load with the right Host.

The herdr.zeroq.su vhost now serves the SPA with only the
herdr_web_token gate. Authelia stays in front of every other
*.zeroq.su vhost. To bring authelia back in front of herdr-web-ui
the cleanest long-term fix is for the upstream to merge the
X-Forwarded-Host patch into the released herdr-web-ui
(server/machine-security.ts); until then, the in-place patch on
sapphira must be reapplied after any `herdr plugin update
devswha.herdr-web-ui`, or the plugin should be installed via
`herdr plugin link` against a local fork that carries the patch.
This commit is contained in:
oqyude committed 2026-10-15 12:37:37 +03:00
1 parent 61ad57a102
commit 9bf9407c9d
1 file changed
+65 -54
+65 -54
View File
@@ -238,69 +238,80 @@ in
# devswha/herdr-web-ui` (one shot, then the plugin's update mechanism # devswha/herdr-web-ui` (one shot, then the plugin's update mechanism
# takes over; the user is expected to handle it — see # takes over; the user is expected to handle it — see
# https://herdrweb.dev/ for the install guide and Tailscale pairing). # https://herdrweb.dev/ for the install guide and Tailscale pairing).
# Same auth_request / 401→302 wiring as vtimeline/tty below — the #
# wildcard rule `*.zeroq.su` in modules/server/authelia.nix already # NO authelia here. herdr-web-ui's CSRF check (sameOrigin in
# covers this subdomain under `one_factor`, so no policy change is # server/machine-security.ts:18-46) compares the browser's
# needed. # `Origin: https://herdr.zeroq.su` against `new URL(request.url).origin`,
# and `request.url` for a proxy_pass is `http://127.0.0.1:7317/...`
# — the function does NOT read X-Forwarded-Host, only request.url +
# x-forwarded-proto. Every cross-origin POST through this vhost
# would therefore return 403 `invalid_origin`. Setting `Host $host`
# makes the right Host header reach the Bun server but does not
# change request.url, so the sameOrigin check still fails. There is
# no env var or config flag in the Bun server to opt into
# "trust this proxy" mode (verified by reading server/index.ts and
# the PROXY_HEADERS table in server/access.ts:105).
#
# Auth model: the herdr_web_token cookie / Authorization: Bearer
# header is the only gate. The token is 64 hex chars (256 bits of
# entropy), kept at /home/oqyude/.config/herdr/plugins/config/
# devswha.herdr-web-ui/env mode 0600. Brute-force is rate-limited
# by the per-IP and shared-proxy budgets in server/auth.ts
# (AUTH_FAILURE_BUDGET=5, AUTH_BACKOFF_MS=1_000, doubling up to
# 60 s, 1024-entry LRU). An unauthenticated visitor sees only
# the SPA's "enter token" / "enter code" screen — the Bun server
# rejects every /api/* except the three gate endpoints with
# 401+JSON until a valid token is presented. To bring Authelia
# back in front, the upstream Bun server needs a `sameOrigin`
# patch that reads X-Forwarded-Host (fork + plugin link) — the
# same-origin check assumes direct loopback or Tailscale serve,
# both of which give request.url a host that matches the browser.
"herdr.zeroq.su" = { "herdr.zeroq.su" = {
forceSSL = true; forceSSL = true;
enableACME = true; enableACME = true;
locations = { locations = {
# /api/auth, /api/devices/pair and /api/health are herdr-web-ui's # Static assets served by the Bun server (manifest, service
# own gate: /api/auth is the form that sets the herdr_web_token # worker, favicon, bundled JS/CSS under /assets/). A regex
# cookie, /api/devices/pair is the 6-digit-code flow, /api/health # match here keeps the PWA manifest and service worker on
# is a plain status probe. All three must reach the Bun server # the vhost rather than the `/` prefix location, so the
# without an upstream authelia check — the user is not signed in # browser can fetch them before any cookie is set. There is
# yet when they POST the token, and the POST would otherwise # no auth_request or any other gate — the Bun server enforces
# trigger `authelia` → 401 → `error_page 401 =302`, but the # the herdr_web_token on every /api/* and on the SPA shell
# 302-rewrite only works for GET: a POST request through that # itself.
# chain returns 500 ("auth request unexpected status" in the #
# nginx error log), and the client never reaches the Bun server. # CRITICAL: do NOT add `proxy_set_header Host $host;`,
# Exempting these three from the `authelia` subrequest lets the # `X-Forwarded-Proto $scheme;` or `X-Forwarded-Host $host;`
# form post work. The Bun server's own auth.ts keeps /api/auth, # in this extraConfig. The NixOS module's
# /api/devices/pair and /api/health open in `requiresAuth()` # `services.nginx.recommendedProxySettings = true` already
# (server/auth.ts:79-85) so a missing or wrong token is rejected # appends an `include
# there with 401 + JSON body, never silently served. # ...-nginx-recommended-proxy_set_header-headers.conf;` to
"~ ^/api/(auth|devices/pair|health)$" = { # every proxy location; that include sets the same three
# headers. nginx's `proxy_set_header` REPLACES the inherited
# value in the same scope, so the include's `proxy_set_header
# X-Forwarded-Host $host;` would normally override ours — but
# because the include is appended AFTER our extraConfig in
# the generated location, BOTH directives end up active and
# nginx sends the header twice (`herdr.zeroq.su, herdr.zeroq.su`).
# The Bun server's sameOrigin() then sees the comma-joined
# value, fails to parse it as a host, and returns 403 on
# every cross-origin POST. The same trap applies to
# `recommendedProxySettings` for every other vhost on this
# host; nothing else sets X-Forwarded-* explicitly in extraConfig.
"~ ^/(manifest\\.webmanifest|sw\\.js|favicon\\.ico|assets/.*)$" = {
proxyPass = "http://127.0.0.1:7317"; proxyPass = "http://127.0.0.1:7317";
proxyWebsockets = true;
extraConfig = ''
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Method $request_method;
'';
}; };
# Everything else: the SPA shell (`/`) and every /api/* call
# that requires auth. `proxyWebsockets = true` upgrades the
# `/ws` SSE feed and the chat WebSocket from the Bun server.
# Same header caveat as the regex location above: the
# `recommendedProxySettings` include already sets Host,
# X-Forwarded-Proto, X-Forwarded-Host correctly; the patch in
# server/machine-security.ts (applied in-place on sapphira)
# trusts those headers, so the sameOrigin check lets the
# browser POST /api/auth and the cookie gets set.
"/" = { "/" = {
proxyPass = "http://127.0.0.1:7317"; proxyPass = "http://127.0.0.1:7317";
proxyWebsockets = true; proxyWebsockets = true;
extraConfig = ''
auth_request /authelia;
auth_request_set $authelia_user $upstream_http_remote_user;
# Same 401→302 trick as vtimeline/tty: a bare 302 from
# Authelia surfaces to the client as a 500 ("auth request
# unexpected status"), so we rewrite the response status to
# a 302 pointing at the authelia login UI with an absolute
# `$scheme://$host$request_uri` so the post-login `rd`
# lands the user back on herdr.zeroq.su, not on
# authelia.zeroq.su/<path>.
error_page 401 =302 https://authelia.zeroq.su/?rd=$scheme://$host$request_uri;
'';
};
"= /authelia" = {
extraConfig = ''
internal;
proxy_pass http://127.0.0.1:9091/api/authz/forward-auth;
proxy_set_header X-Original-URL $request_uri;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Method $request_method;
proxy_set_header X-Forwarded-Uri $request_uri;
proxy_set_header X-Forwarded-For $remote_addr;
# Same Accept-forces-401 trick as vtimeline.zeroq.su — see
# the comment there for why Authelia's default 302 is
# harmful here.
proxy_set_header Accept "application/json";
'';
}; };
}; };
}; };