Skip to content

fix(server): treat a loopback request with a foreign Host or a forwarding header as proxied - #252

Merged
devswha merged 5 commits into
mainfrom
fix/proxy-without-forwarded-for
Oct 1, 2026
Merged

devswha merged 5 commits into
mainfrom
fix/proxy-without-forwarded-for

Conversation

@devswha

@devswha devswha commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Closes #250

What was wrong

A loopback request with no X-Forwarded-For counts as this computer and gets full access. A reverse proxy on the same PC that does not send that header made every visitor look local, so with no token set, pairing a device did not close the server. Reproduced with nginx 1.31.6; the issue has the table from before this change.

What changes

  • Server (server/access.ts, one call in server/index.ts): a loopback request is treated as proxied when it carries any forwarding header (X-Forwarded-For, X-Forwarded-Host, X-Forwarded-Proto, X-Real-IP, Forwarded, Via) or a Host that is not a name for this machine (localhost, *.localhost, 127.x.x.x, [::1]). Claiming to be proxied only removes trust, so a forged header gains nothing. This closes the configuration people end up with once the app works behind nginx (they must keep Host, or the browser gets invalid_origin). It also stops a page on another hostname that resolves to 127.0.0.1 from being taken for this computer.
  • Guide: a "Behind a reverse proxy" section with Caddy and nginx examples that keep Host, send X-Forwarded-For and X-Forwarded-Proto, and pass the WebSocket upgrade. It says plainly that a proxy sending neither signal makes every visitor count as this computer, so set a token first. The FAQ bullet points there.

What is not fixed, and why

  • nginx's bare proxy_pass (rows A and B below) is still taken for this computer. It rewrites Host to 127.0.0.1:7317 and adds no header, so the request is byte-for-byte what a local client sends. Only a token closes it; the guide and changelog say so.
  • A startup or log warning: rejected. At startup the server cannot know a proxy exists (it is bound to loopback either way). At request time, every case it could warn about it can now refuse instead, and the one it cannot refuse it cannot see.
  • A Settings connection check is out of scope.

Reachability after this change

nginx 1.31.6 and Caddy 2.11.4 (docker, host network) in front of createServer({ port: 0, hostname: "127.0.0.1", token, stateDir }), requests sent to the proxy as app.example.test with no cookie or token unless the row says so. "+Origin" sends Origin and Sec-Fetch-Site: same-origin like a browser on the proxy's address. "WS + input" opens /ws, attaches a pane and sends an input frame.

state via GET session GET pane/read POST pane/input POST pane/input +Origin POST workspace/create POST workspace/create +Origin POST devices/pair (wrong code) WS + input WS + input +Origin
no token, nothing paired direct to 127.0.0.1, no proxy 200 200 200 200 200 200 401 invalid_code 101, input typed 101, input typed
no token, nothing paired A: nginx, bare proxy_pass 200 200 200 403 invalid_origin 200 403 invalid_origin 401 invalid_code 426 403
no token, nothing paired B: A + WebSocket upgrade 200 200 200 403 invalid_origin 200 403 invalid_origin 401 invalid_code 101, input typed 403
no token, nothing paired C: B + browser's Host 200 200 200 200 200 200 401 invalid_code 101, input typed 101, input typed
no token, nothing paired D: nginx, guide example, TLS 200 200 200 200 200 200 401 invalid_code 101, input typed 101, input typed
no token, nothing paired E: Caddy, guide example, TLS 200 200 200 200 200 200 401 invalid_code 101, input typed 101, input typed
no token, one device paired direct to 127.0.0.1, no proxy 200 200 200 200 200 200 401 invalid_code 101, input typed 101, input typed
no token, one device paired A: nginx, bare proxy_pass 200 200 200 403 invalid_origin 200 403 invalid_origin 401 invalid_code 426 403
no token, one device paired B: A + WebSocket upgrade 200 200 200 403 invalid_origin 200 403 invalid_origin 401 invalid_code 101, input typed 403
no token, one device paired C: B + browser's Host 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 invalid_code 401 401
no token, one device paired D: nginx, guide example, TLS 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 invalid_code 401 401
no token, one device paired E: Caddy, guide example, TLS 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 invalid_code 401 401
token set direct to 127.0.0.1, no proxy 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 invalid_code 401 401
token set A: nginx, bare proxy_pass 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 invalid_code 401 401
token set B: A + WebSocket upgrade 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 invalid_code 401 401
token set C: B + browser's Host 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 invalid_code 401 401
token set D: nginx, guide example, TLS 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 invalid_code 401 401
token set E: Caddy, guide example, TLS 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 invalid_code 401 401
no token, nothing paired, Tailscale login known direct to 127.0.0.1, no proxy 200 200 200 200 200 200 401 invalid_code 101, input typed 101, input typed
no token, nothing paired, Tailscale login known A: nginx, bare proxy_pass 200 200 200 403 invalid_origin 200 403 invalid_origin 401 invalid_code 426 403
no token, nothing paired, Tailscale login known B: A + WebSocket upgrade 200 200 200 403 invalid_origin 200 403 invalid_origin 401 invalid_code 101, input typed 403
no token, nothing paired, Tailscale login known C: B + browser's Host 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 invalid_code 401 401
no token, nothing paired, Tailscale login known D: nginx, guide example, TLS 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 invalid_code 401 401
no token, nothing paired, Tailscale login known E: Caddy, guide example, TLS 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 unauthorized 401 invalid_code 401 401
token set A: nginx, bare proxy_pass, with the token 200 200 200 403 invalid_origin 200 403 invalid_origin 401 invalid_code 426 403
token set B: A + WebSocket upgrade, with the token 200 200 200 403 invalid_origin 200 403 invalid_origin 401 invalid_code 101, input typed 403
token set C: B + browser's Host, with the token 200 200 200 200 200 200 401 invalid_code 101, input typed 101, input typed
token set D: nginx, guide example, TLS, with the token 200 200 200 200 200 200 401 invalid_code 101, input typed 101, input typed
token set E: Caddy, guide example, TLS, with the token 200 200 200 200 200 200 401 invalid_code 101, input typed 101, input typed

Changed by this PR: the two C rows "one device paired" and "Tailscale login known" went from 200 / input typed everywhere to 401. Every other cell is the same as before.

D is the guide's nginx example (TLS listener, $scheme = https) and E is the guide's Caddy example with tls internal; both were run as written apart from ports, certificate paths and host name. The PC list stream (/api/machines/events) was also read through D.

Compatibility

  • http://localhost:7317, http://127.0.0.1:7317, *.localhost: still local (contract test).
  • Vite dev proxy: it keeps the browser's Host (localhost:5173) and adds no header: still local. Opened from another machine with vite --host, the visitor is no longer taken for this computer.
  • tailscale serve: already sent X-Forwarded-For; unchanged.
  • Remote bridge: authenticates with its token on a loopback address; unchanged.
  • A custom name for this PC in /etc/hosts (say http://mybox:7317 → 127.0.0.1) is no longer local: it is open until a device is paired and needs pairing after. http://localhost:7317 always works.

Commands

bun run typecheck                      # clean
bun run test:unit                      # 673 pass, 0 fail
bun test ./server/api.contract.test.ts ./server/device-access.contract.test.ts --timeout 15000   # 73 pass, 0 fail

Unverified

  • Real certificates (Let's Encrypt) and a public domain: tested with a self-signed certificate and Caddy's internal CA on loopback only.
  • Other proxies (Apache, Traefik, HAProxy, cloudflared): not run. Whether they are covered depends on whether they send a forwarding header or keep Host.
  • A real browser through the proxies: requests were sent with curl and a Bun WebSocket carrying browser-like headers.

@coderabbitai

coderabbitai Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: Repository: devswha/herdr-web-ui/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: c113f5a4-c77c-4edd-9d9e-e289d1a6c03f

📥 Commits

Reviewing files that changed from the base of the PR and between 2a7bbe3 and b7eb289.

📒 Files selected for processing (6)
  • CHANGELOG.md
  • docs/guide.md
  • server/access.test.ts
  • server/access.ts
  • server/api.contract.test.ts
  • server/index.ts
 ____________________________________________________________________________________________________________________________________________________________________________________________
< The object-oriented model makes it easy to build up programs by accretion. What this often means, in practice, is that it provides a structured way to write spaghetti code. - Paul Graham >
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


Comment @coderabbitai help to get the list of available commands.

…no Host as proxied

From an adversarial review of the proxy check:
- Host went through a URL parser, so `public.example@localhost` and
  `localhost/[email protected]` came out as localhost and a visitor was taken for this
  computer. Host is now matched as a name or bracketed address with an optional port.
- A request without Host (HTTP/1.0 through a proxy) was taken for local.
- `localhost.` and `[::ffff:127.0.0.1]` are names for this machine too.

Guide: both proxy examples drop a visitor's Tailscale-User-Login (run against real nginx
and Caddy: passed through without the line, gone with it), and the check to run is a 401
on /api/session, since /api/health shows no `via` once a token is set.
@devswha
devswha merged commit 94f54b3 into main Oct 1, 2026
10 of 11 checks passed
@devswha
devswha deleted the fix/proxy-without-forwarded-for branch October 1, 2026 10:09
devswha added a commit that referenced this pull request Oct 1, 2026
Release v0.3.40: moves the Unreleased notes under 0.3.40 and bumps
`package.json` and `herdr-plugin.toml`.

What's in it:
- Fixes from #249, #251, #252, #263, #264, #265, #267.
- The iPhone CJK and text-symbol fix (#272).
- Emoji cell widths matching herdr (#273).

Every changelog entry already ends with its PR link. All these PRs are
by @devswha, so no credits to add.

No remote bundle change: `REMOTE_BUNDLE_VERSION` stays 9. The Windows PC
part of #264 and #265 reaches a PC with the next remote bundle, as its
changelog entry says.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A reverse proxy on the same PC that sends no X-Forwarded-For makes every visitor count as this computer

1 participant