Allow multiple trusted origins for form submissions - #142
Merged
kittendevv merged 1 commit intoSep 7, 2026
Conversation
Adds an optional TRUSTED_ORIGINS env var so self-hosted deployments can accept form POSTs from more than one hostname (e.g. Umbrel .local plus Tailscale MagicDNS or LAN IP). Existing installs with only ORIGIN set are unaffected. - Move CSRF origin gating from SvelteKit's build-time check into hooks.server.ts so the allowlist is configurable at runtime - Support exact origins, glob patterns (e.g. http://*.ts.net:8003), and "*" for trusted networks - When neither env var is set, allow same-host form posts (Origin host matches request Host, scheme ignored) to fix HTTP deployments where adapter-node would otherwise default to https:// origin - Skip the check in dev to match SvelteKit's built-in behavior - Add Bun unit tests for the matcher Fixes kittendevv#133 Co-authored-by: Cursor <cursoragent@cursor.com>
Owner
|
Thanks for this awesome work |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an optional
TRUSTED_ORIGINSenv var so self-hosted deployments can accept form POSTs from more than one hostname (e.g. Umbrel.localplus Tailscale MagicDNS or LAN IP), while keeping CSRF protection enabled.Existing installs with only
ORIGINset are unaffected —TRUSTED_ORIGINSis purely additive.Fixes #133
Problem
When Invio is accessed over a hostname different from the configured
ORIGIN, login and other form actions fail with:{\"message\":\"Cross-site POST form submissions are forbidden\"}Common example (Umbrel):
http://umbrel.local:8003— workshttp://umbrel-home.<tailnet>.ts.net:8003— rejectedSvelteKit's built-in CSRF only compares
Originagainst one server-side origin, andcsrf.trustedOriginsinsvelte.config.jsis build-time — not configurable per install.Solution
Move the CSRF origin gate into
hooks.server.tsso the allowlist can be set at runtime:ORIGIN— canonical origin (unchanged; still respected)TRUSTED_ORIGINS— optional comma-separated extra originshttp://tailscale.local:8000http://*.ts.net:8003*to allow all (trusted networks only)https://origin.Backward compatibility
ORIGIN=http://localhost:8000only (Docker)ORIGIN=http://\${DEVICE_DOMAIN_NAME}:8003only (Umbrel)vite devwithout env varsTRUSTED_ORIGINSTesting
frontend/src/lib/csrf.test.tscovering exact, glob, wildcard, missing origin, and Host-header fallback (bun run test)bun run check— 0 errors (4 pre-existing warnings unrelated to this PR)/login?/loginfrom multiple hostnames viacurl --resolve, all scenarios pass (canonical, extra trusted origin, glob, wildcard, empty-env same-host, blocked cross-host)For Umbrel packaging (separate PR)
After this ships, the
umbrel-appsinvio/docker-compose.ymlfrontend service can add:Files
frontend/svelte.config.js— disable built-in origin gate (delegated to hook)frontend/src/hooks.server.ts— production-only runtime CSRF checkfrontend/src/lib/csrf.ts— matcher (exact/glob/wildcard + host-header fallback)frontend/src/lib/csrf.test.ts— 13 unit testsfrontend/package.json— addtestscript and@types/bundevDependency.env.example— documentTRUSTED_ORIGINSMade with Cursor