Repository navigation
fix: harden Pages same-origin auth and session CSRF - #5
Conversation
|
ⓘ Qodo reviews are paused because your trial has ended. Ask your workspace admin to add credits to resume reviews. Manage billing |
Reviewer's GuideAPIEnforce same-origin checks on Pages auth routes, allow the GitHub OAuth callback navigation, synthesize Origin for eligible GETs, and preserve bodyless asset responses.
ImplementationDerive CSRF tokens deterministically from the session ID and migrate legacy stored hashes so session reads do not invalidate other tabs.
DocumentationDocument the Pages origin protections, OAuth callback exception, stable CSRF migration, and current staging review branch.
TestsAdd behavioral coverage for Pages routing, origin enforcement, OAuth callback navigation, HTML configuration, and bodyless responses.
Guard against regressions in session expiry handling and stable CSRF token behavior.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Deploying ocodejs-cli-hub-staging with
|
| Latest commit: |
46b0075
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://ce03f438.ocodejs-cli-hub-staging.pages.dev |
| Branch Preview URL: | https://fix-auth-session-and-pages-r.ocodejs-cli-hub-staging.pages.dev |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Deployment failed for project cli with the following error: Learn More: https://vercel.com/ocodejs-co?upgradeToPro=build-rate-limit |
|
Deployment failed for project cli-hub-auth-server with the following error: Learn More: https://vercel.com/ocodejs-co?upgradeToPro=build-rate-limit |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b5c5f0f6c3
ℹ️ 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".
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="functions/[[path]].js" line_range="21" />
<code_context>
+ });
+ }
+ const headers = new Headers(request.headers);
+ if (!origin) headers.set("Origin", url.origin);
+ const sameOriginRequest = new Request(request, { headers });
+ return authWorker.fetch(sameOriginRequest, { ...env, APP_ORIGIN: url.origin });
</code_context>
<issue_to_address>
**Victim gets attacker’s account**
When a cross-site POST to `/auth/pat` reaches Pages without `Origin` or `Sec-Fetch-Site`, `onRequest` supplies the Pages origin, so `originAllowed` accepts the request and `/auth/pat` creates a session for the submitted GitHub account. The victim ends up signed in to the attacker’s account.
Do not fabricate a trusted Origin for cross-site POSTs; reject auth POSTs that lack a valid Origin or other CSRF protection.
</issue_to_address>
### Comment 2
<location path="functions/[[path]].js" line_range="14" />
<code_context>
- return authWorker.fetch(request, { ...env, APP_ORIGIN: url.origin });
+ const origin = request.headers.get("Origin");
+ const fetchSite = request.headers.get("Sec-Fetch-Site");
+ if ((origin && origin !== url.origin) || fetchSite === "cross-site") {
+ return new Response(JSON.stringify({ error: "origin_not_allowed" }), {
+ status: 403,
</code_context>
<issue_to_address>
**GitHub sign-in cannot complete**
When gitHub OAuth is enabled and the browser follows GitHub’s cross-site redirect to the callback with `Sec-Fetch-Site: cross-site`, `onRequest` returns 403 before the auth Worker can validate the OAuth state or exchange the code, so the user cannot complete sign-in.
Allow the OAuth callback through the Pages guard so the auth Worker can validate its state and complete the sign-in.
</issue_to_address>
### Comment 3
<location path="auth-worker/src/index.js" line_range="179" />
<code_context>
- const csrf = randomToken();
- await env.DB.prepare("UPDATE auth_sessions SET csrf_hash=? WHERE id_hash=?").bind(await hmac(csrf, env.SESSION_SECRET), session.id_hash).run();
+ const sessionId = cookies(request)[COOKIE];
+ const csrf = await csrfForSession(sessionId, env);
return json({ connected: true, user: publicUser(session), csrfToken: csrf });
}
</code_context>
<issue_to_address>
**Existing sessions fail protected requests**
When an unexpired session created before this change is read after deployment, `csrfForSession` returns a token derived from the session ID, but the row still stores the hash of the old random token. `requireSession` rejects the derived token, so protected requests—including API calls, logout, and disconnect—fail with 403.
Update the stored `csrf_hash` to the HMAC of the derived token when returning it for an existing session.
</issue_to_address>
### Comment 4
<location path="auth-worker/test/pages-function.test.mjs" line_range="44" />
<code_context>
+ next: async () => { throw new Error("static asset pipeline must not run"); }
+ });
+ assert.equal(response.status, 200);
+ assert.equal((await response.json()).service, "ocodejs-auth-worker");
+});
+
</code_context>
<issue_to_address>
**Health routing regression passes**
When the Pages handler returns a hard-coded response with `service: "ocodejs-auth-worker"`, the `Pages routes health requests through the auth Worker` test checks only `service` at line 44; a Pages handler that returns that field directly without delegating still passes, so a broken Worker route appears healthy.
Make the Worker call observable in the test, for example through an injectable Worker dependency that can be spied on.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 4 findings to address first, and the Pages Function rejects a mismatched Origin, but it also supplies the Pages origin whenever Origin is absent, for every method—not just browser GETs. If an auth mutation relies on Origin as a guard and lacks an independent CSRF check, a request without Origin could be treated as same-origin; reverting restores the prior behavior, but any access or data exposure before then cannot be undone.
Blocking findings: functions/[[path]].js:21, functions/[[path]].js:14, auth-worker/src/index.js:179, auth-worker/test/pages-function.test.mjs:44
Summary
Review findings
s.expires_at > ?; regression coverage preserves it.Cloudflare staging
ocodejs-cli-hub-stagingce03f438-e846-4c73-875d-97b08fd8fa16: all five stages succeeded.Validation and hosting fallback
Safety