Princeton Courses by TigerApps
From ff255e43a4ddaf7b9700f1b9990b063cf205d59e Mon Sep 17 00:00:00 2001 From: Aikhan Jumashukurov <117706338+aikhanj@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:37:55 -0500 Subject: [PATCH] Fix browser consent origin and OAuth callback policy --- app/src/server/course-consent.ts | 14 ++++++++++---- app/tests/course-mcp.test.ts | 12 +++++++++++- app/tests/course-oauth.test.ts | 7 +++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/src/server/course-consent.ts b/app/src/server/course-consent.ts index 3c8a683..f39157a 100644 --- a/app/src/server/course-consent.ts +++ b/app/src/server/course-consent.ts @@ -5,12 +5,18 @@ export function escapeHtml(value: string): string { return value.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]!); } -export function connectorPage(title: string, body: string, headers = new Headers(), status = 200): Response { +export function connectorPage(title: string, body: string, headers = new Headers(), status = 200, redirectUri?: string): Response { + const callback = redirectUri ? new URL(redirectUri) : undefined; + // Chromium applies form-action to the OAuth redirect too. Include only the + // registered callback's HTTP(S) origin, never raw client text in the policy. + const callbackSource = callback && /^https?:$/.test(callback.protocol) && /^[a-z\d.:[\]-]+$/i.test(callback.host) ? ` ${callback.origin}` : ""; headers.set("content-type", "text/html; charset=utf-8"); headers.set("cache-control", "no-store"); - headers.set("referrer-policy", "no-referrer"); + // Keep Origin on same-origin form POSTs for CSRF checks, without sending + // consent URLs as referrers to the external assistant. + headers.set("referrer-policy", "same-origin"); headers.set("x-frame-options", "DENY"); - headers.set("content-security-policy", "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'"); + headers.set("content-security-policy", `default-src 'none'; style-src 'unsafe-inline'; form-action 'self'${callbackSource}; frame-ancestors 'none'; base-uri 'none'`); return new Response(`
Princeton Courses by TigerApps
Your conversation stays in your assistant. This connection does not import memories, PI chats, Canvas, or calendars.
Access can renew for up to 30 days. Disconnect any time at PI / My apps / Course connector. -`, headers); +`, headers, 200, details.redirectUri); } export function connectionsPage(origin: string, netid: string, grants: GrantSummary[], cursor?: string): Response { diff --git a/app/tests/course-mcp.test.ts b/app/tests/course-mcp.test.ts index 554f4d8..14045c8 100644 --- a/app/tests/course-mcp.test.ts +++ b/app/tests/course-mcp.test.ts @@ -2,7 +2,7 @@ import assert from "node:assert/strict"; import { test } from "node:test"; import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client"; import { COURSE_READ, PLAN_WRITE, createCourseServer, handleCourseMcp, type CourseUpstream } from "../src/server/course-mcp.ts"; -import { consentPage } from "../src/server/course-consent.ts"; +import { connectorPage, consentPage } from "../src/server/course-consent.ts"; const tool = (name: string) => ({ name, inputSchema: { type: "object" as const, properties: {} } }); function fakeEngine() { @@ -111,3 +111,13 @@ test("consent escapes client-controlled text and makes plan editing opt-in", asy assert.match(response.headers.get("content-security-policy")!, /frame-ancestors 'none'/); assert.equal(response.headers.get("cache-control"), "no-store"); }); + +test("consent CSP permits only the callback origin and cannot inject directives", () => { + const policy = (uri: string) => connectorPage("Test", "", undefined, 200, uri).headers.get("content-security-policy")!; + assert.match(policy("https://assistant.test/callback?state=private"), /form-action 'self' https:\/\/assistant\.test;/); + assert.doesNotMatch(policy("https://assistant.test/callback?state=private"), /callback|private/); + assert.match(policy("http://127.0.0.1:4321/callback"), /form-action 'self' http:\/\/127\.0\.0\.1:4321;/); + for (const uri of ["https://evil.test;script-src/callback", "https://*.evil.test/callback", "javascript:alert(1)"]) { + assert.equal(policy(uri), "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'"); + } +}); diff --git a/app/tests/course-oauth.test.ts b/app/tests/course-oauth.test.ts index bc75f5d..97d1355 100644 --- a/app/tests/course-oauth.test.ts +++ b/app/tests/course-oauth.test.ts @@ -67,6 +67,8 @@ async function flow(netid = "student1") { authorize.search = new URLSearchParams({ client_id: client.client_id, redirect_uri: redirectUri, response_type: "code", scope: "courses:read plans:write offline_access", state: "client-state", resource: `${origin}/mcp`, code_challenge: createHash("sha256").update(verifier).digest("base64url"), code_challenge_method: "S256" }).toString(); const page = await request(authorize.href, { headers: { cookie: session(netid) } }); assert.equal(page.status, 200); + assert.equal(page.headers.get("referrer-policy"), "same-origin", "consent forms must preserve the browser's Origin header"); + assert.match(page.headers.get("content-security-policy")!, /form-action 'self' https:\/\/assistant\.test;/); const html = await page.text(); const handle = html.match(/name="handle" value="([^"]+)"/)![1]; const subject = html.match(/name="subject" value="([^"]+)"/)![1]; @@ -118,6 +120,8 @@ test("unsigned browsers return through CAS before connector consent", async () = test("consent rejects CSRF, another browser, and switching accounts", async () => { const f = await flow(); assert.equal((await approve(f, false, { requestOrigin: "https://evil.test" })).status, 403); + assert.equal((await approve(f, false, { requestOrigin: "null" })).status, 403); + assert.equal((await request("/connect/authorize", { method: "POST", headers: { cookie: f.cookie }, body: new URLSearchParams({ handle: f.handle, subject: f.subject, decision: "approve" }) })).status, 403); assert.equal((await approve(f, false, { cookie: session() })).status, 400); const swapped = f.cookie.replace(/^pi_session=[^;]+/, session("student2")); assert.equal((await approve(f, false, { cookie: swapped })).status, 400); @@ -167,6 +171,8 @@ test("optional editing works and per-student grants can be revoked only by their assert.ok(grants.keys.length > 0); // The user-facing management page identifies the grant without exposing tokens. const page = await request("/connect", { headers: { cookie: session() } }); + assert.equal(page.headers.get("referrer-policy"), "same-origin", "disconnect forms must preserve the browser's Origin header"); + assert.match(page.headers.get("content-security-policy")!, /form-action 'self';/); const html = await page.text(); assert.doesNotMatch(html, new RegExp(token.access_token)); const grantRecords = await Promise.all(grants.keys.map(k => kv.get(k.name, "json") as Promise