diff --git a/packages/api/src/static-app.ts b/packages/api/src/static-app.ts index e3404858..b5d77d8d 100644 --- a/packages/api/src/static-app.ts +++ b/packages/api/src/static-app.ts @@ -1,18 +1,16 @@ import type { Env } from "./types"; export const STATIC_CSP = "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'"; -const pages = new Set(["/", "/jobs", "/jobs/new", "/projects", "/projects/new", "/job-runs", "/settings", "/settings/integrations", "/settings/api-keys", "/settings/api-keys/new", "/settings/authorized-clients", "/settings/password", "/auth/login", "/auth/signup", "/auth/password-reset", "/auth/verify", "/auth/verify/request", "/authorize", "/device"]); +// Browser auth screens share /auth with backend callbacks and form handlers. +const authPages = new Set(["/auth/login", "/auth/signup", "/auth/password-reset", "/auth/verify", "/auth/verify/request"]); const backendNamespaces = ["/api", "/healthz", "/auth", "/oauth", "/webhooks", "/internal", "/mcp", "/.well-known"]; function isBackendNamespace(path: string) { return backendNamespaces.some(namespace => path === namespace || path.startsWith(`${namespace}/`)); } export function isStaticPage(path: string) { - return pages.has(path) || - /^\/jobs\/[^/]+(?:\/edit|\/settings)?$/.test(path) || - /^\/projects\/[^/]+$/.test(path) || - /^\/job-runs\/[^/]+(?:\/(?:trace|info|context|settings))?$/.test(path) || - /^\/skills(?:\/[^/]+)?$/.test(path) || - (path.startsWith("/settings/") && !isBackendNamespace(path)); + if (authPages.has(path)) return true; + // Let the frontend own page matching, including future routes and not-found UI. + return !isBackendNamespace(path) && path !== "/assets" && !path.startsWith("/assets/") && path !== "/styles.css"; } export function isStaticAsset(path: string) { return /^\/assets\/[A-Za-z0-9_.-]+$/.test(path) || path === "/styles.css"; diff --git a/packages/api/test/static-routing.test.ts b/packages/api/test/static-routing.test.ts index a1451d2a..6938256d 100644 --- a/packages/api/test/static-routing.test.ts +++ b/packages/api/test/static-routing.test.ts @@ -3,10 +3,10 @@ import { isStaticPage, staticApp, STATIC_CSP } from "../src/static-app"; import type { Env } from "../src/types"; describe("same-origin static ownership", () => { - it("serves supported frontend deep links with an external-bundle policy and revalidated entry HTML", async () => { + it("serves frontend deep links without a page allowlist with an external-bundle policy and revalidated entry HTML", async () => { const fetch = vi.fn(async (request: Request) => new Response(request.method === "HEAD" ? null : "entry", { headers: { "Content-Type": "text/html" } })); const env = { ASSETS: { fetch } } as unknown as Env; - for (const path of ["/skills", "/skills/id", "/", "/jobs", "/jobs/new", "/jobs/id/edit", "/jobs/id/settings", "/projects", "/projects/new", "/projects/id", "/job-runs", "/job-runs/id", "/job-runs/id/trace", "/settings/authorized-clients", "/settings/targets", "/auth/verify?token=one-time", "/authorize?client_id=test", "/device?user_code=ABCD-2345"]) { + for (const path of ["/projects/id/runs", "/projects/id/settings", "/projects/id/settings/", "/future-feature/nested/page?tab=one", "/unknown", "/api-tools", "/healthz-dashboard", "/skills", "/skills/id", "/", "/jobs", "/jobs/new", "/jobs/id/edit", "/jobs/id/settings", "/projects", "/projects/new", "/projects/id", "/job-runs", "/job-runs/id", "/job-runs/id/trace", "/settings/authorized-clients", "/settings/targets", "/auth/verify?token=one-time", "/authorize?client_id=test", "/device?user_code=ABCD-2345"]) { const response = await staticApp(new Request(`https://app.factorize.sh${path}`), env); expect(response?.status).toBe(200); expect(response?.headers.get("content-security-policy")).toBe(STATIC_CSP); @@ -16,16 +16,15 @@ describe("same-origin static ownership", () => { expect(new URL(fetch.mock.calls.at(-1)![0].url).pathname).toBe("/index.html"); expect(new URL(fetch.mock.calls.at(-1)![0].url).search).toBe(""); } - expect(await staticApp(new Request("https://app.factorize.sh/unknown"), env)).toBeNull(); const head = await staticApp(new Request("https://app.factorize.sh/jobs/id", { method: "HEAD" }), env); expect(await head?.text()).toBe(""); }); it("never forwards backend namespaces or mutations to static hosting", async () => { const fetch = vi.fn(); const env = { ASSETS: { fetch } } as unknown as Env; - for (const path of ["/api", "/assets", "/auth/unknown", "/healthz/unknown", "/api/v1", "/api/v1/unknown", "/api/v1/jobs", "/oauth/token", "/auth/linear", "/auth/github/setup", "/webhooks/github", "/internal/missing", "/mcp", "/healthz", "/.well-known/oauth-authorization-server", "/assets/subdir/file.js"]) { + for (const path of ["/api", "/assets", "/auth", "/oauth", "/webhooks", "/internal", "/.well-known", "/auth/login/unknown", "/auth/unknown", "/healthz/unknown", "/api/v1", "/api/v1/unknown", "/api/v1/jobs", "/oauth/token", "/auth/linear", "/auth/github/setup", "/webhooks/github", "/internal/missing", "/mcp", "/healthz", "/.well-known/oauth-authorization-server", "/assets/subdir/file.js"]) { expect(await staticApp(new Request(`https://app.factorize.sh${path}`), env)).toBeNull(); } - for (const method of ["POST", "PUT", "DELETE", "OPTIONS"]) for (const path of ["/skills", "/unknown", "/jobs", "/authorize", "/device", "/assets/index-hash.js", "/auth/login"]) expect(await staticApp(new Request(`https://app.factorize.sh${path}`, { method }), env)).toBeNull(); + for (const method of ["POST", "PUT", "PATCH", "DELETE", "OPTIONS"]) for (const path of ["/skills", "/unknown", "/jobs", "/authorize", "/device", "/assets/index-hash.js", "/auth/login"]) expect(await staticApp(new Request(`https://app.factorize.sh${path}`, { method }), env)).toBeNull(); expect(fetch).not.toHaveBeenCalled(); expect(isStaticPage("/settings/unknown")).toBe(true); }); diff --git a/scripts/hosting-runtime.test.mjs b/scripts/hosting-runtime.test.mjs index 0f7f1791..cd024ef5 100644 --- a/scripts/hosting-runtime.test.mjs +++ b/scripts/hosting-runtime.test.mjs @@ -41,12 +41,11 @@ test("actual Cloudflare local runtime enforces static/API/protocol ownership, he }); try { await ready; - for (const path of ["/skills", "/skills/id", "/jobs/id/edit", "/jobs/id/settings", "/job-runs", "/job-runs/id", "/settings/authorized-clients", "/auth/verify?token=test-token"]) { + for (const path of ["/projects/id/runs", "/projects/id/settings", "/projects/id/settings/", "/future-feature/nested/page?tab=one", "/unknown", "/skills", "/skills/id", "/jobs/id/edit", "/jobs/id/settings", "/job-runs", "/job-runs/id", "/settings/authorized-clients", "/auth/verify?token=test-token"]) { const page = await read(port, path); assert.equal(page.status, 200); assert.match(page.headers["content-type"], /text\/html/); assert.equal(page.headers["cache-control"], "no-cache, must-revalidate"); assert.match(page.headers["content-security-policy"], /script-src 'self'/); assert.ok(!page.headers["content-security-policy"].includes("unsafe-inline")); assert.equal(page.headers["x-content-type-options"], "nosniff"); const assetPath = page.body.match(/src="(\/assets\/[^"/]+\.js)"/)?.[1]; assert.ok(assetPath); const asset = await read(port, assetPath); assert.equal(asset.status, 200); assert.equal(asset.headers["cache-control"], "public, max-age=31536000, immutable"); } - const unknownPage = await read(port, "/unknown"); assert.equal(unknownPage.status, 404); assert.ok(!unknownPage.headers["content-type"]?.includes("text/html")); const head = await read(port, "/jobs/id", "HEAD"); assert.equal(head.status, 200); assert.equal(head.body, ""); const health = await read(port, "/healthz"); assert.equal(health.status, 200); assert.deepEqual(JSON.parse(health.body), { ok: true }); const session = await read(port, "/api/v1/session"); assert.equal(session.status, 200); assert.equal(session.headers["x-factorize-contract"], "gen-2157-static-v1"); assert.deepEqual(JSON.parse(session.body), { authenticated: false }); diff --git a/scripts/verify-public-routing.mjs b/scripts/verify-public-routing.mjs index 89b9f4d3..9ffa0fa6 100644 --- a/scripts/verify-public-routing.mjs +++ b/scripts/verify-public-routing.mjs @@ -9,7 +9,7 @@ export async function requireApiReady(origin, fetcher = fetch) { } export async function verifyPublicRouting(origin) { await requireApiReady(origin); - for (const path of ["/jobs", "/jobs/route-probe/edit", "/job-runs", "/job-runs/route-probe", "/settings/api-keys", "/settings/api-keys/new", "/auth/password-reset?token=route-probe"]) { + for (const path of ["/projects/route-probe/runs", "/projects/route-probe/settings", "/route-probe-missing", "/route-probe-missing/nested", "/jobs", "/jobs/route-probe/edit", "/job-runs", "/job-runs/route-probe", "/settings/api-keys", "/settings/api-keys/new", "/auth/password-reset?token=route-probe"]) { const response = await fetch(origin + path, { redirect: "manual", signal: AbortSignal.timeout(15_000) }); assert.equal(response.status, 200, path); assert.match(response.headers.get("Content-Type"), /text\/html/); assert.equal(response.headers.get("Cache-Control"), "no-cache, must-revalidate"); @@ -18,7 +18,7 @@ export async function verifyPublicRouting(origin) { const body = await response.text(), assetPath = body.match(/src="(\/assets\/[^"/]+\.js)"/)?.[1]; assert.ok(assetPath, "Expected an external compiled script"); const asset = await fetch(origin + assetPath, { redirect: "manual", signal: AbortSignal.timeout(15_000) }); assert.equal(asset.status, 200); assert.match(asset.headers.get("Content-Type"), /javascript/); assert.equal(asset.headers.get("Cache-Control"), "public, max-age=31536000, immutable"); } - for (const path of ["/api/v1/route-probe-missing", "/oauth/route-probe-missing", "/auth/route-probe-missing", "/internal/route-probe-missing", "/webhooks/route-probe-missing", "/mcp", "/route-probe-missing", "/assets/route-probe-missing.js"]) { + for (const path of ["/api/v1/route-probe-missing", "/oauth/route-probe-missing", "/auth/route-probe-missing", "/internal/route-probe-missing", "/webhooks/route-probe-missing", "/mcp", "/healthz/route-probe-missing", "/api/route-probe-missing", "/assets/route-probe-missing.js"]) { const response = await fetch(origin + path, { redirect: "manual", signal: AbortSignal.timeout(15_000) }); assert.ok([401, 404, 405].includes(response.status), path); assert.ok(!response.headers.get("Content-Type")?.includes("text/html"), path); } const metadata = await (await fetch(origin + "/.well-known/oauth-authorization-server", { signal: AbortSignal.timeout(15_000) })).json(); assert.equal(metadata.issuer, origin); assert.equal(metadata.device_authorization_endpoint, origin + "/oauth/device_authorization");