Repository navigation
feat: deploy same-origin auth on isolated Cloudflare Pages staging #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
84537bc
feat: route auth API through same-origin Pages Functions
zskbot cfee6b8
test: cover Pages same-origin auth routing
zskbot d68b1fc
docs: document isolated Pages auth staging
zskbot 79a99ee
fix: validate auth origin against active Pages hostname
zskbot 0f34cce
test: assert dynamic same-origin validation
zskbot fdf0a6e
docs: clarify dynamic Pages origin validation
zskbot f02e36c
chore: retrigger isolated Pages preview after path filter setup
zskbot f7cb1d2
docs: record provisioned staging auth secrets
zskbot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Cloudflare Pages staging | ||
|
|
||
| The CLI Hub staging host is isolated from the existing OCodeJS production services: | ||
|
|
||
| - Pages project: `ocodejs-cli-hub-staging` | ||
| - Host: https://ocodejs-cli-hub-staging.pages.dev | ||
| - D1 database: `ocodejs-cli-auth-staging` (APAC) | ||
| - D1 binding: `DB` | ||
| - No custom domain or DNS changes | ||
| - The Pages project is not connected to a production branch deployment. | ||
|
|
||
| ## Same-origin auth | ||
|
|
||
| `functions/[[path]].js` sends `/health`, `/auth/*`, and `/api/*` to the existing auth Worker module. Other requests continue to the static Pages asset pipeline. HTML responses set `window.OCODE_AUTH_API` to the current Pages origin. This keeps the HttpOnly session cookie first-party in Safari. | ||
|
|
||
| The staging D1 migration `auth-worker/migrations/0001_init.sql` has been applied and the Pages deployment configuration has the `DB` binding in both preview and production environments. | ||
|
|
||
| ## Required Pages environment variables | ||
|
|
||
| Already configured for both Preview and Production: | ||
|
|
||
| - `SESSION_SECRET` — generated cryptographically and stored as an encrypted Pages secret. | ||
| - `TOKEN_ENCRYPTION_KEY` — generated as a base64-encoded 32-byte key and stored as an encrypted Pages secret. | ||
| - `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` — not configured; only required to enable GitHub OAuth. | ||
|
|
||
| The Pages Function sets `APP_ORIGIN` from the exact active Pages request origin at runtime. This supports both the stable staging hostname and branch-preview hostnames without allowing cross-origin requests. PAT login is backend-configured. GitHub OAuth remains disabled until its dedicated OAuth App credentials are configured. GitHub OAuth additionally requires a dedicated OAuth App whose callback URL is `https://ocodejs-cli-hub-staging.pages.dev/auth/github/callback`. | ||
|
|
||
| Do not copy secrets into GitHub files, commits, issue comments, or chat. Do not attach a custom domain or alter existing `app`, `mcp`, or auth Workers. | ||
|
|
||
|
|
||
| Cloudflare Pages is connected to this repository for the isolated staging project. Only the `feat/cloudflare-pages-same-origin-auth` branch is included for Preview deployments; automatic deployments from `main` are disabled. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { readFile } from "node:fs/promises"; | ||
|
|
||
| const source = await readFile(new URL("../../functions/[[path]].js", import.meta.url), "utf8"); | ||
|
|
||
| test("Pages Functions routes auth endpoints through the same-origin Worker", () => { | ||
| assert.match(source, /AUTH_ROUTE/); | ||
| assert.match(source, /authWorker\.fetch\(request, \{ \.\.\.env, APP_ORIGIN: url\.origin \}\)/); | ||
| assert.match(source, /context\.next\(\)/); | ||
|
zskbot marked this conversation as resolved.
Resolved
|
||
| }); | ||
|
|
||
| test("Pages HTML configures the auth API to use the current origin", () => { | ||
| assert.match(source, /window\.OCODE_AUTH_API = location\.origin/); | ||
| assert.match(source, /contentType\.includes\("text\/html"\)/); | ||
|
zskbot marked this conversation as resolved.
Resolved
|
||
| }); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import authWorker from "../auth-worker/src/index.js"; | ||
|
|
||
| const AUTH_ROUTE = /^\/(?:health|auth(?:\/|$)|api(?:\/|$))/; | ||
|
|
||
| export async function onRequest(context) { | ||
| const { request, env } = context; | ||
| const url = new URL(request.url); | ||
|
|
||
| // Keep authentication and API requests on the Pages origin so Safari treats | ||
| // the HttpOnly session cookie as first-party rather than a third-party cookie. | ||
| if (AUTH_ROUTE.test(url.pathname)) { | ||
| return authWorker.fetch(request, { ...env, APP_ORIGIN: url.origin }); | ||
| } | ||
|
|
||
| // Pass static assets through to the Pages asset pipeline. | ||
| const response = await context.next(); | ||
| const contentType = response.headers.get("content-type") || ""; | ||
| if (!contentType.includes("text/html")) return response; | ||
|
|
||
| const html = await response.text(); | ||
| const configured = html.replace( | ||
| "</head>", | ||
| '<script>window.OCODE_AUTH_API = location.origin;</script></head>' | ||
|
zskbot marked this conversation as resolved.
Resolved
|
||
| ); | ||
| const headers = new Headers(response.headers); | ||
| headers.delete("content-length"); | ||
| headers.delete("content-encoding"); | ||
| headers.delete("etag"); | ||
| headers.set("cache-control", "no-store"); | ||
| return new Response(configured, { | ||
| status: response.status, | ||
|
zskbot marked this conversation as resolved.
Resolved
zskbot marked this conversation as resolved.
Resolved
|
||
| statusText: response.statusText, | ||
| headers | ||
| }); | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.