Skip to content

feat(hub): add opt-in CORS allowlist for the client API - #17

Merged
5aaee9 merged 3 commits into
mainfrom
feat/hub-cors-allowlist
Nov 26, 2026
Merged

5aaee9 merged 3 commits into
mainfrom
feat/hub-cors-allowlist

Conversation

@5aaee9

@5aaee9 5aaee9 commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Motivation

The Ares slicer web app (for example https://ares-cdh.pages.dev, or http://127.0.0.1:5173 in dev) needs a "send plate to printer" flow through Pandar. To do that it calls the Hub client API straight from the browser with Authorization: Bearer <tenant token>. The Hub has no CORS layer today, so browsers block these cross-origin calls.

Change

New Hub env var PANDAR_CORS_ALLOWED_ORIGINS:

  • Takes a comma-separated list of exact origins, such as https://ares-cdh.pages.dev,http://127.0.0.1:5173. Whitespace around entries and empty entries are ignored.
  • Unset or empty: no CORS layer is installed and no Access-Control-* headers are sent. This is the current behavior.
  • Validated at startup: each entry must be http or https with a host and optional port. *, null, wildcard hosts, paths (including a trailing /), queries, userinfo, and other schemes all fail startup with an error that names the variable.

A tower-http CorsLayer covers only the Bearer-token client API, /api/v1/me and /api/v1/tenants/.... It:

  • allows only the listed origins
  • allows methods GET, POST, PATCH, DELETE, OPTIONS (all methods exposed by client_api_router)
  • allows request headers authorization, content-type
  • never sends Access-Control-Allow-Credentials, and exposes no extra headers
  • answers preflight requests without authentication

Plugin, agent, bootstrap, /api/v1/summary, join-link accept, onboarding, and observability routes have no CORS headers. The client routes now live in their own client_api_router that gets merged into the main router; the route definitions themselves are unchanged.

tower-http was already in the lockfile through reqwest. It is now a workspace dependency with only the cors feature. Also bumped transitive url to 2.5.9 (form_urlencoded 1.2.3) in Cargo.lock so cargo audit clears RUSTSEC-2026-0592.

Security notes

  • No credentials. Without Allow-Credentials, browsers never attach cookies or HTTP auth to these cross-origin requests. A page on a listed origin must supply a tenant token itself, so it can't use ambient credentials (no CSRF-style abuse).
  • Exact origins only, no wildcards. A wildcard or path typo fails startup instead of opening the API more widely than intended.
  • Scope tradeoff. The layer covers the whole /api/v1/tenants/... prefix, allowing every HTTP method exposed by client_api_router (GET, POST, PATCH, DELETE, OPTIONS). Every route still enforces its own Bearer auth, role, and scope checks; CORS only lets a listed origin read responses to requests it already authenticated.
  • No-auth mode. Operators running with PANDAR_HUB_NO_AUTH=true should not set this variable to an untrusted origin.

Tests

New route tests in crates/pandar-hub/src/routes/tests/cors.rs:

  • preflight from a listed origin returns allow-origin, POST/PATCH/DELETE, and authorization/content-type, with no credentials header and no auth needed
  • preflight from an unlisted origin gets no allow-origin
  • unset config sends no access-control-* headers
  • preflight on plugin, bootstrap, and summary routes gets no CORS headers
  • a Bearer tenant-token GET /api/v1/tenants/{id}/printers from a listed origin returns 200 with Access-Control-Allow-Origin and no credentials header
  • invalid allowlist entries (*, null, a wildcard host, a path or trailing slash, a query, userinfo, no scheme, ftp) are rejected

Local results:

  • cargo fmt --check: clean
  • cargo clippy --workspace --all-targets -- -D warnings: clean
  • cargo audit: clean (0 vulnerabilities)
  • cargo nextest run --manifest-path "Cargo.toml" --workspace: passed

Docs: docs/development.md (env var semantics), CHANGELOG.md (Unreleased > Added), docs/roadmap.md. NixOS and Helm pass Hub env vars through extraEnvironment / hub.env, so no new options are needed.

Browser apps on other origins, such as the Ares slicer web app sending a
plate to a printer, need to call the Bearer-token client API directly.

PANDAR_CORS_ALLOWED_ORIGINS takes comma-separated exact http(s) origins;
wildcards, paths, and userinfo fail startup, and unset keeps the previous
no-CORS behavior. Only /api/v1/me and /api/v1/tenants/... answer CORS,
allowing GET/POST/OPTIONS with Authorization and Content-Type and never
sending Access-Control-Allow-Credentials.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T14:16:40.055145Z 151cba8 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 349a19f1eb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/pandar-hub/src/routes.rs Resolved
@5aaee9

5aaee9 commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

@codex review

@5aaee9
5aaee9 merged commit 831fe9e into main Nov 26, 2026
12 checks passed
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 151cba8dac

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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