From 84537bc639d5df74b55712982b0410df5fd0f797 Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:46:28 +0700 Subject: [PATCH 1/8] feat: route auth API through same-origin Pages Functions --- functions/[[path]].js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 functions/[[path]].js diff --git a/functions/[[path]].js b/functions/[[path]].js new file mode 100644 index 0000000..890363d --- /dev/null +++ b/functions/[[path]].js @@ -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); + } + + // 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( + "", + '' + ); + 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, + statusText: response.statusText, + headers + }); +} From cfee6b8bbe03eeaf7694e488e86ae11d625a8520 Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:47:03 +0700 Subject: [PATCH 2/8] test: cover Pages same-origin auth routing --- auth-worker/test/pages-function.test.mjs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 auth-worker/test/pages-function.test.mjs diff --git a/auth-worker/test/pages-function.test.mjs b/auth-worker/test/pages-function.test.mjs new file mode 100644 index 0000000..ca45568 --- /dev/null +++ b/auth-worker/test/pages-function.test.mjs @@ -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\)/); + assert.match(source, /context\.next\(\)/); +}); + +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"\)/); +}); From d68b1fc34e6f910628fcbc17dd81b546a092c0e5 Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:47:06 +0700 Subject: [PATCH 3/8] docs: document isolated Pages auth staging --- auth-worker/PAGES_STAGING.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 auth-worker/PAGES_STAGING.md diff --git a/auth-worker/PAGES_STAGING.md b/auth-worker/PAGES_STAGING.md new file mode 100644 index 0000000..3340a85 --- /dev/null +++ b/auth-worker/PAGES_STAGING.md @@ -0,0 +1,28 @@ +# 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 + +Set these in Cloudflare Dashboard → Workers & Pages → `ocodejs-cli-hub-staging` → Settings → Variables and Secrets. Add them to both Preview and Production environments: + +- `SESSION_SECRET` — Secret; use a cryptographically random value of at least 48 bytes. +- `TOKEN_ENCRYPTION_KEY` — Secret; base64-encoded 32-byte random key. +- `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` — optional until GitHub OAuth is enabled. + +`APP_ORIGIN` is already configured as the staging Pages origin. PAT login remains unavailable until the two required secrets are entered. 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. From 79a99eeb7288fc99fc044c7dfa84eaa10a126419 Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:48:27 +0700 Subject: [PATCH 4/8] fix: validate auth origin against active Pages hostname --- functions/[[path]].js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/[[path]].js b/functions/[[path]].js index 890363d..95adb0d 100644 --- a/functions/[[path]].js +++ b/functions/[[path]].js @@ -9,7 +9,7 @@ export async function onRequest(context) { // 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); + return authWorker.fetch(request, { ...env, APP_ORIGIN: url.origin }); } // Pass static assets through to the Pages asset pipeline. From 0f34ccef25e224afa931b9f9391be5b53ba3292b Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:48:37 +0700 Subject: [PATCH 5/8] test: assert dynamic same-origin validation --- auth-worker/test/pages-function.test.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auth-worker/test/pages-function.test.mjs b/auth-worker/test/pages-function.test.mjs index ca45568..2172fdc 100644 --- a/auth-worker/test/pages-function.test.mjs +++ b/auth-worker/test/pages-function.test.mjs @@ -6,7 +6,7 @@ const source = await readFile(new URL("../../functions/[[path]].js", import.meta test("Pages Functions routes auth endpoints through the same-origin Worker", () => { assert.match(source, /AUTH_ROUTE/); - assert.match(source, /authWorker\.fetch\(request, env\)/); + assert.match(source, /authWorker\.fetch\(request, \{ \.\.\.env, APP_ORIGIN: url\.origin \}\)/); assert.match(source, /context\.next\(\)/); }); From fdf0a6ef5945bee33ed65cbf0e05ce2d231bb3cc Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:48:40 +0700 Subject: [PATCH 6/8] docs: clarify dynamic Pages origin validation --- auth-worker/PAGES_STAGING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auth-worker/PAGES_STAGING.md b/auth-worker/PAGES_STAGING.md index 3340a85..5661079 100644 --- a/auth-worker/PAGES_STAGING.md +++ b/auth-worker/PAGES_STAGING.md @@ -23,6 +23,6 @@ Set these in Cloudflare Dashboard → Workers & Pages → `ocodejs-cli-hub-stagi - `TOKEN_ENCRYPTION_KEY` — Secret; base64-encoded 32-byte random key. - `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` — optional until GitHub OAuth is enabled. -`APP_ORIGIN` is already configured as the staging Pages origin. PAT login remains unavailable until the two required secrets are entered. GitHub OAuth additionally requires a dedicated OAuth App whose callback URL is `https://ocodejs-cli-hub-staging.pages.dev/auth/github/callback`. +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 remains unavailable until the two required secrets are entered. 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. From f02e36cb1c350c4f1b67ba04289359e60a6fcba9 Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:49:13 +0700 Subject: [PATCH 7/8] chore: retrigger isolated Pages preview after path filter setup --- auth-worker/PAGES_STAGING.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/auth-worker/PAGES_STAGING.md b/auth-worker/PAGES_STAGING.md index 5661079..7249fff 100644 --- a/auth-worker/PAGES_STAGING.md +++ b/auth-worker/PAGES_STAGING.md @@ -26,3 +26,6 @@ Set these in Cloudflare Dashboard → Workers & Pages → `ocodejs-cli-hub-stagi 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 remains unavailable until the two required secrets are entered. 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. From f7cb1d28bfe36f7481200db919031aca96f37e47 Mon Sep 17 00:00:00 2001 From: zskbot <241179063+zskbot@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:51:04 +0700 Subject: [PATCH 8/8] docs: record provisioned staging auth secrets --- auth-worker/PAGES_STAGING.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/auth-worker/PAGES_STAGING.md b/auth-worker/PAGES_STAGING.md index 7249fff..158ffde 100644 --- a/auth-worker/PAGES_STAGING.md +++ b/auth-worker/PAGES_STAGING.md @@ -17,13 +17,13 @@ The staging D1 migration `auth-worker/migrations/0001_init.sql` has been applied ## Required Pages environment variables -Set these in Cloudflare Dashboard → Workers & Pages → `ocodejs-cli-hub-staging` → Settings → Variables and Secrets. Add them to both Preview and Production environments: +Already configured for both Preview and Production: -- `SESSION_SECRET` — Secret; use a cryptographically random value of at least 48 bytes. -- `TOKEN_ENCRYPTION_KEY` — Secret; base64-encoded 32-byte random key. -- `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` — optional until GitHub OAuth is enabled. +- `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 remains unavailable until the two required secrets are entered. GitHub OAuth additionally requires a dedicated OAuth App whose callback URL is `https://ocodejs-cli-hub-staging.pages.dev/auth/github/callback`. +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.