Skip to content

Reach the UI by IP or Tailscale name, and join Tailscale from settings#153

Merged
spencerhhubert merged 3 commits into
mainfrom
spencer/backend-cors-lan
May 30, 2026
Merged

Reach the UI by IP or Tailscale name, and join Tailscale from settings#153
spencerhhubert merged 3 commits into
mainfrom
spencer/backend-cors-lan

Conversation

@spencerhhubert

Copy link
Copy Markdown
Contributor

Purpose

Make the Sorter UI just work however you connect to it, and make the Tailscale auth-key flow in settings actually usable end-to-end.

Two operator-facing goals:

  1. Load the UI by whatever address you havelocalhost, the machine's hostname, its current LAN IP (http://192.168.x.y:5173), *.local, or its Tailscale name — without hitting CORS errors or having to edit per-machine config.
  2. Paste a Tailscale auth key in settings and have it work — the machine joins the tailnet named (e.g. sorter-dark-red-plate-0ffbef), and the UI keeps working at the new name immediately, with no restart and no manual steps.

What was broken

  • Loading the UI by LAN IP was blocked by CORS (the allowlist never included the machine's own IP).
  • A first attempt fixed that with a broad range-matching regex (all RFC1918 / *.local / *.ts.net / the CGNAT range) — far broader than needed, and it captured the device name once at startup, so a Tailscale rename via the UI left the new name blocked until a backend restart.
  • Joining Tailscale from the UI didn't set a device name, so machines landed in the tailnet under a generic/duplicate name.
  • The Tailscale status the UI displayed was parsed positionally from tailscale status --self, which on the deployed version lists all peers — so it frequently showed the wrong name.
  • Even after the rename was accepted, an identity cache meant the first requests after a join (theme, /health, the WebSocket) intermittently failed CORS for a few seconds.

What this does

  • Device-scoped dynamic CORS — replaces the broad regex with a simple allowlist of this device's own identities: localhost, OS hostname (+.local), its current IP addresses (via hostname -I, so it tracks wifi/DHCP changes), any *.local, and its Tailscale name. Decided per request (a CORSMiddleware subclass) instead of a regex frozen at startup.
  • Name on UI jointailscale up now passes --hostname=sorter-color-piece-mac (same scheme as firstboot), reusing an existing sorter-* name so a re-join doesn't spawn duplicates.
  • Immediate refresh on join/logout — the identity cache is invalidated the moment the name changes, so the UI reloaded at the new name is reachable on the first request (no more intermittent theme/health/WS failures).
  • Robust name parsing — reads Self.DNSName from tailscale status --json instead of splitting peer-list text.

Testing

  • tests/test_server_security.py updated for the device-scoped behavior; all green.
  • Verified on a test Pi: UI loads and CORS passes via localhost, current LAN IP, and sorter.local; 8.8.8.8 denied; Tailscale join from the UI generates a fresh name and the UI keeps working at it without a restart.

🤖 Generated with Claude Code

spencerhhubert and others added 3 commits May 30, 2026 02:30
The UI resolves its backend from window.location, so it works when reached
by hostname (orangepi5:5173 -> orangepi5:8000, an allowed origin). But the
backend's allowed-origins list never included the machine's LAN IP, so
loading the UI by IP (e.g. http://192.168.89.96:5173) was blocked by CORS.

Replace the static allowlist with a dynamic predicate + regex shared by the
FastAPI CORS middleware, the WebSocket guard, and the supervisor: accept
loopback, all RFC1918 private ranges, link-local, the Tailscale CGNAT range
(100.64/10), any *.local / *.ts.net, and this machine's own hostname/Tailscale
name, all on the UI port. SORTER_API_ALLOWED_ORIGINS is now additive rather
than exclusive. Bounded to private/local hosts — public origins never match.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…start

UI 'add auth key' now passes --hostname=sorter-color-piece-mac (same scheme
as firstboot), keeping an existing sorter-* name so a re-join doesn't spawn
duplicate devices. Allow bare single-label origins on the UI port so the
renamed Tailscale MagicDNS name is reachable immediately, with no backend
restart.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
… parsing

Replace the broad range-matching origin regex (all RFC1918 / *.local / *.ts.net
/ CGNAT) with a simple allowlist of *this device's own* identities: localhost,
OS hostname (+.local), current IP addresses (via `hostname -I`, so it tracks
wifi/DHCP changes), and the Tailscale name. Refreshed on a short interval and
invalidated immediately on a Tailscale join/logout so a UI-renamed machine is
reachable at its new name without the stale-cache CORS failures.

CORS now decides per-request via is_ui_origin_allowed (CORSMiddleware subclass)
rather than a regex frozen at startup.

Parse `tailscale status --json` Self.DNSName instead of positionally splitting
`tailscale status --self` (which lists all peers on this version, so the old
code grabbed the wrong token).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@vercel

vercel Bot commented May 30, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
sorter-v2-docs Ready Ready Preview, Comment May 30, 2026 7:29am
sorteros-setup Ready Ready Preview, Comment May 30, 2026 7:29am

@spencerhhubert spencerhhubert merged commit b3c3bfa into main May 30, 2026
3 checks passed
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.

1 participant