From 61ad57a102f427a6ed425ec043c97fb6bd40ac5f Mon Sep 17 00:00:00 2001 From: oqyude Date: Thu, 15 Oct 2026 04:56:55 +0300 Subject: [PATCH] fix(nginx): exempt herdr-web-ui /api/auth and /api/devices/pair from authelia MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The herdr-web-ui token entry form (POST /api/auth with JSON body {"token": "..."}) and the 6-digit pairing flow (POST /api/devices/pair) were returning 500 through the vhost instead of 204/401. nginx's auth_request subrequest hits authelia first; authelia returns 401 because the visitor is not signed in yet (no authelia cookie); error_page 401 =302 then tries to redirect the original POST, but nginx's 302 rewrite only works cleanly for GET — the POST round trips to a 500 ("auth request unexpected status" in the nginx error log). Same root cause as the working vtimeline/tty vhosts, where the form is GET-only and the redirect works. Fix: add a regex location '~ ^/api/(auth|devices/pair|health)$' that proxies these three paths to 127.0.0.1:7317 WITHOUT the authelia auth_request. The Bun server's own auth.ts keeps the same three paths open in requiresAuth() (server/auth.ts:79-85), so a missing or wrong token is rejected there with 401 + JSON body — the gate is enforced one layer down, just not by authelia. Verified after the rebuild: POST /api/auth (body=token) -> 204 + set-cookie herdr_web_token POST /api/devices/pair (code=...) -> 401 invalid_code (not 500) GET /api/health -> 200 Real-browser flow now: visitor opens herdr.zeroq.su -> authelia login -> web UI shows pair/token form -> submit POST /api/auth works -> HttpOnly cookie set -> app loads. --- modules/server/nginx.nix | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/modules/server/nginx.nix b/modules/server/nginx.nix index 72d29cb..38954ee 100644 --- a/modules/server/nginx.nix +++ b/modules/server/nginx.nix @@ -246,6 +246,30 @@ in forceSSL = true; enableACME = true; locations = { + # /api/auth, /api/devices/pair and /api/health are herdr-web-ui's + # own gate: /api/auth is the form that sets the herdr_web_token + # cookie, /api/devices/pair is the 6-digit-code flow, /api/health + # is a plain status probe. All three must reach the Bun server + # without an upstream authelia check — the user is not signed in + # yet when they POST the token, and the POST would otherwise + # trigger `authelia` → 401 → `error_page 401 =302`, but the + # 302-rewrite only works for GET: a POST request through that + # chain returns 500 ("auth request unexpected status" in the + # nginx error log), and the client never reaches the Bun server. + # Exempting these three from the `authelia` subrequest lets the + # form post work. The Bun server's own auth.ts keeps /api/auth, + # /api/devices/pair and /api/health open in `requiresAuth()` + # (server/auth.ts:79-85) so a missing or wrong token is rejected + # there with 401 + JSON body, never silently served. + "~ ^/api/(auth|devices/pair|health)$" = { + 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; + ''; + }; "/" = { proxyPass = "http://127.0.0.1:7317"; proxyWebsockets = true;