Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions auth-worker/PAGES_STAGING.md
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)
Comment thread
zskbot marked this conversation as resolved.
Resolved
- 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.
16 changes: 16 additions & 0 deletions auth-worker/test/pages-function.test.mjs
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\(\)/);
Comment thread
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"\)/);
Comment thread
zskbot marked this conversation as resolved.
Resolved
});
35 changes: 35 additions & 0 deletions functions/[[path]].js
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>'
Comment thread
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,
Comment thread
zskbot marked this conversation as resolved.
Resolved
Comment thread
zskbot marked this conversation as resolved.
Resolved
statusText: response.statusText,
headers
});
}
Loading