From 83e6ad53f108efe99308a62abdb86a965e97a9ce Mon Sep 17 00:00:00 2001 From: "exe.dev user" Date: Tue, 20 Oct 2026 06:09:35 +0000 Subject: [PATCH] Serve safe frontend deep links with SPA fallback --- packages/api/src/static-app.ts | 8 ++++++-- packages/api/test/static-routing.test.ts | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/api/src/static-app.ts b/packages/api/src/static-app.ts index 343a724f..9eebdb0c 100644 --- a/packages/api/src/static-app.ts +++ b/packages/api/src/static-app.ts @@ -2,8 +2,12 @@ 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"]); +const backendNamespaces = ["/api/v1", "/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\/[^/]+$/.test(path); + return pages.has(path) || (!isBackendNamespace(path) && !path.startsWith("/assets/") && path !== "/styles.css"); } export function isStaticAsset(path: string) { return /^\/assets\/[A-Za-z0-9_.-]+$/.test(path) || path === "/styles.css"; @@ -19,7 +23,7 @@ export function securityHeaders(request: Request, response: Response) { if (new URL(request.url).protocol === "https:") headers.set("Strict-Transport-Security", "max-age=31536000; includeSubDomains"); return new Response(response.body, { status: response.status, statusText: response.statusText, headers }); } -/** Explicit routes only. ASSETS never receives a backend path or a write method. */ +/** Safe application fallback. ASSETS never receives a backend path or a write method. */ export async function staticApp(request: Request, env: Env): Promise { const path = new URL(request.url).pathname; if (!isStaticPage(path) && !isStaticAsset(path)) return null; diff --git a/packages/api/test/static-routing.test.ts b/packages/api/test/static-routing.test.ts index 7506ec65..23bfff2f 100644 --- a/packages/api/test/static-routing.test.ts +++ b/packages/api/test/static-routing.test.ts @@ -6,7 +6,7 @@ describe("same-origin static ownership", () => { it("serves explicit deep links 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 ["/", "/jobs", "/jobs/new", "/jobs/id/edit", "/jobs/id/settings", "/projects", "/projects/new", "/projects/id", "/job-runs", "/job-runs/id", "/settings/authorized-clients", "/auth/verify?token=one-time", "/authorize?client_id=test", "/device?user_code=ABCD-2345"]) { + for (const path of ["/", "/jobs", "/jobs/new", "/jobs/id/edit", "/jobs/id/settings", "/projects", "/projects/new", "/projects/id", "/job-runs", "/job-runs/id", "/settings/authorized-clients", "/settings/targets", "/unknown", "/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); @@ -19,14 +19,14 @@ describe("same-origin static ownership", () => { 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, unknown routes or mutations to static hosting", async () => { + 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/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", "/unknown", "/assets/subdir/file.js", "/index.html", "/jobs/id/edit/extra", "/jobs/id/settings/extra", "/jobs/id/unknown", "/projects/id/unknown"]) { + for (const path of ["/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 ["/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(false); + expect(isStaticPage("/settings/unknown")).toBe(true); }); it("caches successful hashed assets immutably but never caches missing chunks", async () => { const fetch = vi.fn(async (request: Request) => new Response("asset", { status: request.url.endsWith("missing.js") ? 404 : 200 }));