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
4 changes: 3 additions & 1 deletion packages/app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ app.use("*", async (c, next) => {
c.set("cspNonce", nonce);
await next();
c.header("Content-Security-Policy", `default-src 'self'; script-src 'nonce-${nonce}'; style-src 'self'; img-src 'self' data:; connect-src 'self'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'`);
c.header("Referrer-Policy", "no-referrer");
// Keep Origin on same-origin form POSTs; no-referrer makes it "null" in
// browsers and breaks the auth CSRF check. External requests still omit Referer.
c.header("Referrer-Policy", "same-origin");
c.header("X-Content-Type-Options", "nosniff");
c.header("X-Frame-Options", "DENY");
c.header("Permissions-Policy", "camera=(), microphone=(), geolocation=()");
Expand Down
41 changes: 41 additions & 0 deletions packages/app/test/auth-origin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it } from "vitest";
import app from "../src/index";
import type { Env } from "../src/types";

const env = { APP_ORIGIN: "https://factorize.test", SESSION_SIGNING_SECRET: "test-secret" } as Env;

describe("browser auth form origin protection", () => {
it("keeps same-origin form origins while suppressing external referrers", async () => {
const page = await app.request(`${env.APP_ORIGIN}/auth/login`, {}, env);
expect(page.status).toBe(200);
expect(page.headers.get("Referrer-Policy")).toBe("same-origin");
});

it("accepts a same-origin logout POST and clears the cookie", async () => {
const response = await app.request(`${env.APP_ORIGIN}/auth/logout`, {
method: "POST", headers: { Origin: env.APP_ORIGIN, "Sec-Fetch-Site": "same-origin" },
}, env);
expect(response.status).toBe(302);
expect(response.headers.get("Location")).toBe("/");
expect(response.headers.get("Set-Cookie")).toContain("factorize_session=; Max-Age=0; Path=/");
});

it.each([
{ Origin: "https://evil.test", "Sec-Fetch-Site": "cross-site" },
{ Origin: "https://evil.factorize.test", "Sec-Fetch-Site": "same-site" },
{ Origin: "null", "Sec-Fetch-Site": "same-origin" },
{ "Sec-Fetch-Site": "same-origin" },
{ Origin: env.APP_ORIGIN, "Sec-Fetch-Site": "cross-site" },
] as Record<string, string>[])("rejects unsafe logout origins: %j", async (headers) => {
const response = await app.request(`${env.APP_ORIGIN}/auth/logout`, { method: "POST", headers }, env);
expect(response.status).toBe(403);
expect(await response.text()).toBe("Invalid request origin");
expect(response.headers.get("Set-Cookie")).toBeNull();
});

it("does not log out through a GET request", async () => {
const response = await app.request(`${env.APP_ORIGIN}/auth/logout`, {}, env);
expect(response.status).toBe(404);
expect(response.headers.get("Set-Cookie")).toBeNull();
});
});
Loading