headplane icon indicating copy to clipboard operation
headplane copied to clipboard

Unable to save ACLs and logout after upgrading from 0.7 to 0.8

Open JasonHK opened this issue 1 month ago • 5 comments

Description

When I trying to save ACLs or logout after upgraded to 0.8.0, they broke with the following errors. Release notes didn't seems to mentioned any migration steps needed.

Error: Bad Request
    at singleFetchAction (file:///app/build/server/index.js:14265:44)
    at handleSingleFetchRequest (file:///app/build/server/index.js:14640:50)
    at requestHandler (file:///app/build/server/index.js:14544:21)
    at requestHandler (file:///app/build/server/index.js:14610:10)
    at file:///app/build/server/index.js:14822:10
    at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
    at async Object.requestListener (file:///app/build/server/index.js:5376:15)
Error: Bad Request
    at singleFetchAction (file:///app/build/server/index.js:14265:44)
    at handleSingleFetchRequest (file:///app/build/server/index.js:14640:50)
    at requestHandler (file:///app/build/server/index.js:14544:21)
    at requestHandler (file:///app/build/server/index.js:14610:10)
    at file:///app/build/server/index.js:14822:10
    at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
    at async Object.requestListener (file:///app/build/server/index.js:5376:15)
Error: Bad Request
    at singleFetchAction (file:///app/build/server/index.js:14265:44)
    at handleSingleFetchRequest (file:///app/build/server/index.js:14640:50)
    at requestHandler (file:///app/build/server/index.js:14544:21)
    at requestHandler (file:///app/build/server/index.js:14610:10)
    at file:///app/build/server/index.js:14822:10
    at async Object.requestListener (file:///app/build/server/index.js:5376:15)
Error: Bad Request
    at singleFetchAction (file:///app/build/server/index.js:14265:44)
    at handleSingleFetchRequest (file:///app/build/server/index.js:14640:50)
    at requestHandler (file:///app/build/server/index.js:14544:21)
    at requestHandler (file:///app/build/server/index.js:14610:10)
    at file:///app/build/server/index.js:14822:10
    at async Object.requestListener (file:///app/build/server/index.js:5376:15)
Image

Headplane Version

v0.8.0

Headscale Version

v0.29.5

JasonHK avatar Oct 14 '26 04:10 JasonHK

Root cause

The Error: Bad Request is React Router's action-origin guard (throwIfPotentialCSRFAttack), which runs for every non-GET single-fetch action. Its stack (singleFetchAction → handleSingleFetchRequest → requestHandler) is exactly the trace in the report. So this is not specific to ACLs or logout — every action POST is rejected, and logout simply happens to be one of them. Loaders are unaffected, which is why pages still render.

The guard compares the browser's Origin header against request.url:

  • react-router 8.3.0 compared only the host: new URL(origin).host === new URL(request.url).host.
  • react-router 8.3.1 tightened it in remix-run/react-router#15419 to compare the full origin (scheme + host + port). 8.4.0 carries the same code.
  • Headplane 0.7.1 pinned react-router 8.3.0; 0.8.0 bumped it to 8.4.0 (verified by diffing v0.7.1 → v0.8.0 in package.json and pnpm-lock.yaml). That bump is the regression.

When Headplane runs behind a TLS-terminating reverse proxy — the documented, recommended setup — the socket to Headplane is plain HTTP, so @react-router/node builds the request URL as http://<host>/admin/... while the browser sends Origin: https://<host>. Same host, different scheme, so the guard returns a 400 that the UI renders as "Unexpected Server Error". Headplane's runtime does not consult X-Forwarded-Proto, and @react-router/node's createRequestListener doesn't expose trustProxy, so there is nothing to reconcile the two today.

I confirmed the installed [email protected] code (dist/development/lib/actions.js) and compared it with the published 8.3.0 build. This is a known upstream change that other projects have hit; upstream later fixed only the dev-server path (#15466).

Fix plan

Goal: make same-host action requests pass the schemeful check while still rejecting genuinely cross-origin submissions. No new required config.

  1. Align the request origin for same-host actions. When the Origin host matches the request Host, the request URL should use the browser's scheme. Since the host is already correct behind a proxy, this is purely a scheme reconciliation:
    • Preferred: construct the React Router request with the resolved scheme in a small Headplane-owned listener in app/server/app.ts, replacing the @react-router/node createRequestListener. Use @remix-run/node-fetch-server's createRequest/sendResponse (add it as a direct dependency — it's currently only transitive) and keep passing the socket address to getLoadContext so proxy_auth's trusted_proxy_cidrs logic is unchanged.
    • Minimal alternative: in runtime/http.ts's composeListener, normalize the incoming Origin header to the scheme Headplane actually sees when the Origin host equals Host (React Router copies request headers into the Request). Cross-host Origins are left untouched and still rejected. ~10 lines, no new deps, keeps all of @react-router/node's abort/stream handling.
  2. Optionally honour X-Forwarded-Proto/X-Forwarded-Host as a complementary signal, since the docs already instruct proxies to set them. Do not enable blanket trustProxy, because @react-router/node doesn't forward it and doing so through node-fetch-server would change the client address and break proxy_auth.
  3. Optional defense in depth: set build.allowedActionOrigins at runtime from server.base_url's host (React Router's documented escape hatch, per its Config docs). This is a few lines but only helps when server.base_url is the public URL — note config.example.yaml defaults it to http://localhost:3000, so it is not sufficient on its own.

Files

  • app/server/app.ts — request listener / origin handling (shared by prod and dev, so one fix covers both).
  • runtime/http.ts — only if we take the composeListener shim route.
  • package.json / pnpm-lock.yaml — add @remix-run/node-fetch-server if we replace the listener.
  • tests/unit/server/... (new) — unit-test the origin-resolution helper: same-host Origin: https://host + Host: host resolves to https; Origin: https://evil.com is not allowed.
  • CHANGELOG.md — add a ## Fixes line under # Next, in prose, closing #657.
  • docs/configuration/index.md (Reverse Proxying) — note the X-Forwarded-Proto/Host expectation so proxy setups are documented end to end.

Verification

  • pnpm run typecheck and pnpm run lint.
  • New unit test for the origin helper, plus a manual check: run the production build behind a TLS-terminating proxy and confirm Save (ACL) and Logout now succeed while a cross-origin POST still returns 400.

Immediate workaround for affected users

server.base_url alone does not fix 0.8.0. Options:

  • Configure the reverse proxy to rewrite the Origin request header to the scheme Headplane sees, e.g. nginx proxy_set_header Origin http://$host; (only the scheme differs, so this is safe).
  • Or terminate TLS in-process with server.tls_cert_path/server.tls_key_path (from 0.7.0) and point the proxy at HTTPS, so the socket is genuinely HTTPS.
  • Or pin/downgrade react-router to 8.3.0 (not recommended long-term).

No implementation has been started yet, per the request.

Task list (3/3 completed)
  • [x] Gather context on ACL save and logout flow
  • [x] Identify root cause of Bad Request after 0.7->0.8 upgrade
  • [x] Produce actionable plan and post via report_progress

Pullfrog  | Implement plan ➔ | View workflow run | via Pullfrog | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

pullfrog[bot] avatar Oct 14 '26 04:10 pullfrog[bot]

HP-601

linear-code[bot] avatar Oct 14 '26 04:10 linear-code[bot]

Thanks, I implement workaround "Configure the reverse proxy to rewrite the Origin request header ..." in my traefik reverse proxy labels with success!

EiTiFuzzi avatar Oct 14 '26 08:10 EiTiFuzzi

Thanks, I implement workaround "Configure the reverse proxy to rewrite the Origin request header ..." in my traefik reverse proxy labels with success!

~~My Caddy doesn't seem to work with this workaround...~~ (Edit: It works now)

JasonHK avatar Oct 14 '26 08:10 JasonHK

I have updated a system with headplane 0.6.1 and headscale 0.27.0. With this headplane was working in all scenarios I had.

But after updatting to heaplane 0.8.2 with headscale v0.29.7 I had the same issue as described above.

In one scenario (reverse proxy and headscale/headplane on same VM and headplane uses a different domain and port 8443) it only works with this:

proxy_set_header Origin "";

Without this seeting I get:

Error: Session cookie is empty at decodeCookie (file:///app/build/server/index.js:177816:19) at async resolve (file:///app/build/server/index.js:177896:19) at async apiForRequest (file:///app/build/server/index.js:178174:21) at async loader$16 (file:///app/build/server/index.js:178305:18)

But maybe this is not a bug but a matter of nginx configuration.

I can remove this setting by changing this:

# Preserves domain + port (:8443) so login/CSRF works
# proxy_set_header Host $host;
proxy_set_header Host $http_host;

Here the complete working configuration:

location / {
      proxy_pass http://172.18.0.2:3000;

      # Preserves domain + port (:8443) so login/CSRF works
      proxy_set_header Host $http_host;

      # Tells Headplane the original client connection is HTTPS
      proxy_set_header X-Forwarded-Proto $scheme;

      # Passes client IP for accurate logs
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Real-IP $remote_addr;

      # Disables proxy buffering so real-time events stream immediately
      proxy_buffering off;
      proxy_cache off;

      # Extends timeout so idle streams aren't killed prematurely (e.g. 24h)
      proxy_read_timeout 86400s;
      proxy_send_timeout 86400s;

      # Required for persistent SSE & WebSocket streams
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
    }

fansari avatar Nov 03 '26 16:11 fansari