feat(server): configurable CORS origins for external hosts#30
Merged
Conversation
Expose extra allowed origins to the API guard via a repeatable `--allowed-origin` flag on `serve` and a `NULLHUB_ALLOWED_ORIGINS` environment variable so a hub reached through a Tailscale name or other external host is no longer rejected with "forbidden origin". Built-in bind-host and local alias handling are unchanged; extras are validated (scheme + host, no trailing slash) and matched case-insensitively against the request's `Origin` header. Closes #26
Review follow-ups for the configurable CORS origins change: - `cli.appendOriginIfValid` now dupes each accepted origin and `appendOriginsFromCsv` returns the count of rejected entries, so `main.resolveAllowedOrigins` can free the env-var buffer and surface invalid `NULLHUB_ALLOWED_ORIGINS` values on stderr. - `parseServe` warns on malformed `--allowed-origin` flags and on the (OOM) drop path instead of failing silently. - Outer allowed-origin slice is freed on shutdown via `freeResolvedOrigins`, eliminating the leak flagged in review. - Added a regression test covering the `0.0.0.0` bind + extra origin path to document current behavior.
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
--allowed-origin ORIGIN(repeatable) tonullhub serveandNULLHUB_ALLOWED_ORIGINS(comma-separated) so a hub reached via a Tailscale name or other external host is no longer rejected with "forbidden origin".extra_allowed_originslist through the existing CORS guard (requestOriginAllowed/isAllowedCorsOrigin/ CORS headers); built-in bind-host and local-alias handling are unchanged.Originheader case-insensitively, and surface diagnostics for malformed CLI/env values.Closes #26
Test plan
zig build test --summary allgreen (new cases: extras accepted for non-local and0.0.0.0binds, empty/invalid entries ignored, CSV copies survive freeing the source buffer,requestOriginAllowedhonors configured extras).0.0.0.0, add Tailscale MagicDNS origin via--allowed-origin https://hub.tailnet.ts.net, confirm Hub Web UI works through Tailscale.NULLHUB_ALLOWED_ORIGINS="https://hub.tailnet.ts.net, bad"and confirm the single invalid entry is logged and the valid one is accepted.