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
8 changes: 6 additions & 2 deletions packages/api/src/static-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<Response | null> {
const path = new URL(request.url).pathname;
if (!isStaticPage(path) && !isStaticAsset(path)) return null;
Expand Down
8 changes: 4 additions & 4 deletions packages/api/test/static-routing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 }));
Expand Down
Loading