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
11 changes: 7 additions & 4 deletions packages/api/src/static-app.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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'";
// 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 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", "/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) {
if (authPages.has(path)) return true;
return !isBackendNamespace(path) && path !== "/assets" && !path.startsWith("/assets/") && path !== "/styles.css";
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));
}
export function isStaticAsset(path: string) {
return /^\/assets\/[A-Za-z0-9_.-]+$/.test(path) || path === "/styles.css";
Expand Down
7 changes: 4 additions & 3 deletions packages/api/test/static-routing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 frontend deep links without a page allowlist with an external-bundle policy and revalidated entry HTML", async () => {
it("serves supported frontend 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 ["/skills", "/skills/new", "/skills/id", "/future-feature/nested/page", "/unknown", "/", "/jobs", "/jobs/new", "/jobs/id/edit", "/jobs/id/settings", "/projects", "/projects/new", "/projects/id", "/job-runs", "/job-runs/id", "/settings/authorized-clients", "/settings/targets", "/auth/verify?token=one-time", "/authorize?client_id=test", "/device?user_code=ABCD-2345"]) {
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"]) {
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);
Expand All @@ -16,6 +16,7 @@ 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("");
});
Expand All @@ -24,7 +25,7 @@ describe("same-origin static ownership", () => {
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"]) {
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", "/future-feature/nested/page", "/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", "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);
});
Expand Down
3 changes: 2 additions & 1 deletion scripts/hosting-runtime.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ test("actual Cloudflare local runtime enforces static/API/protocol ownership, he
});
try {
await ready;
for (const path of ["/skills", "/skills/new", "/skills/id", "/future-feature/nested/page", "/unknown", "/jobs/id/edit", "/jobs/id/settings", "/job-runs", "/job-runs/id", "/settings/authorized-clients", "/auth/verify?token=test-token"]) {
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"]) {
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 });
Expand Down
Loading