Skip to content

feat(console): serve the admin console from a deployment sub-path - #443

Open
misswell wants to merge 1 commit into
astaxie:mainfrom
misswell:codex/console-subpath-deploy
Open

misswell wants to merge 1 commit into
astaxie:mainfrom
misswell:codex/console-subpath-deploy

Conversation

@misswell

Copy link
Copy Markdown

Summary

Operators sometimes publish the admin console and the backend API under path prefixes of one shared hostname (for example https://console.example.com/gateway/tokenhub and .../gateway/tokenhub-api). Today that layout breaks in several places: Next.js has no basePath, absolute asset URLs escape the prefix, new URL() rejects a relative API base, the OAuth binding cookie is scoped to a fixed path, and CORS compares a browser Origin (which has no path) against the full public base URL. This PR makes sub-path deployment work with configuration only.

Related Issue

N/A

Changes

  • Add build-time TOKENHUB_FRONTEND_BASE_PATH support via basePath in frontend/next.config.ts, plus root redirects that keep bare URLs working and never rewrite already-prefixed ones.
  • Add frontend/lib/base-path.ts and apply it to hard-coded /brand/... and /provider-icons/... asset references, which Next does not rewrite.
  • Build the OAuth start URL and the callback forward URL as strings so a relative TOKENHUB_API_BASE_URL resolves against the page origin instead of throwing.
  • Backend: derive the public OAuth callback from the configured base URL path and scope the browser binding cookie to the actual callback path, so deleting it still works if configuration changes mid-flow.
  • Backend: derive the CORS allowlist entry from the public base URL origin, and keep an unparsable value restricted instead of letting the dev fallback authorize it.
  • Plumb the base path through backend/Dockerfile, all three Compose files, start.sh, and both .env.example files; register the variable in tools/env-contract.mjs.
  • Document the layout, the required variable combination, and the shared-origin trust trade-off in English, Simplified Chinese, and Japanese.

Type of Change

  • Bug fix
  • New feature
  • Refactor or maintenance
  • Documentation
  • Deployment or configuration

Verification

  • go build ./...
  • go test ./internal/server/ -run 'TestOAuthSubpath|TestCORSDerivesOriginFromPrefixedPublicBaseURL|TestOAuthCallbackURLUsesOnlyConfiguredOrTrustedOrigins' -count=1
  • node --test tools/*.test.mjs — 205 passed
  • node tools/check-env-contract.mjs
  • node tools/check-doc-translations.mjs --base origin/main --head HEAD
  • npm run test:domain — 198 passed; npm run typecheck; npm run lint
  • TOKENHUB_FRONTEND_BASE_PATH=/gateway/tokenhub npm run build — build succeeds and .next/required-server-files.json reports the expected basePath
  • git diff --check

Not covered here: the RSC prefetch/proxy change from the same internal branch, which is a separate concern.

Compatibility, Security, and Operations

TOKENHUB_FRONTEND_BASE_PATH is build-time only; a prebuilt image cannot pick it up at runtime, so the image must be rebuilt to change it. Leaving it empty preserves current behavior in every code path touched here.

The OAuth callback cookie is now scoped to the stored redirect URI's path rather than a fixed constant. For deployments without a path prefix the computed value is identical to the previous constant, so existing flows and cookies are unaffected.

CORS behavior changes only when TOKENHUB_PUBLIC_BASE_URL carries a path: the allowlist entry becomes the origin, which is what browsers send. An unparsable TOKENHUB_PUBLIC_BASE_URL now fails closed instead of being treated as a literal allowed origin in dev.

Serving two applications from one origin means they share the browser security boundary; a sibling application's script can read same-origin storage in the same tab. The docs call this out and recommend a separate hostname when that trust is unacceptable.

Checklist

  • The PR title and body are written in English.
  • Tests were added or updated for behavior changes, or the reason they are unnecessary is documented.
  • No credentials, local .env files, databases, backups, or runtime logs are included.
  • Environment variable changes are synchronized across examples, Compose, start.sh, and deployment documentation where applicable.
  • Shared user-facing behavior is documented consistently in English, Simplified Chinese, and Japanese where applicable.
  • data/model-catalog.yaml remains tracked and catalog changes were reviewed where applicable.
  • git diff --check passes.

@misswell

Copy link
Copy Markdown
Author

CI shows action_required instead of running — that's the first-time-contributor approval gate, so no job has started yet. Could a maintainer approve the workflow runs?

I pre-ran the equivalent checks locally at this exact head (84c6bd5f):

  • repository gates: node --test tools/*.test.mjs, doc/UI translation, env contract (80 vars), source-line check, git diff --check — all pass
  • frontend: npm ci, lint, typecheck, npm test (820 passed), and TOKENHUB_FRONTEND_BASE_PATH=/tokenhub npm run build
  • backend: gofmt -l clean, go vet ./..., 4-shard suite via node tools/test-backend-shard.mjs N 4
  • Migration CI paths touched by deploy/**: go test ./internal/migration/... -race and go vet pass

Happy to adjust anything if you'd rather see a different shape.

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