Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): run same-site check on the cookie-auth fallback regardless of Authorization header

Richie McIlroy59c0f5a3aaa9
diff --git a/apps/web/__tests__/unit/cookie-write-site.test.ts b/apps/web/__tests__/unit/cookie-write-site.test.ts
index dc8457e91..564ff3db0 100644
--- a/apps/web/__tests__/unit/cookie-write-site.test.ts
+++ b/apps/web/__tests__/unit/cookie-write-site.test.ts
@@ -170,12 +170,17 @@ describe("isCrossSiteCookieWrite", () => {
).toBe(false);
});
- it("leaves requests with an Authorization header alone", () => {
+ it("still flags a cross-site write that carries an Authorization header", () => {
expect(
isCrossSiteCookieWrite(
request("POST", { ...crossSite, authorization: `Bearer ${API_KEY}` }),
),
- ).toBe(false);
+ ).toBe(true);
+ expect(
+ isCrossSiteCookieWrite(
+ request("POST", { ...crossSite, authorization: "Bearer bogus" }),
+ ),
+ ).toBe(true);
});
it.each(["GET", "HEAD", "OPTIONS"])("leaves %s alone", (method) => {
@@ -203,12 +208,30 @@ describe("Hono cookie fallback", () => {
expect(mocks.getSessionUser).not.toHaveBeenCalled();
});
+ it("does not authenticate a cross-site cookie write carrying a bogus bearer", async () => {
+ const response = await send("/required", "POST", {
+ ...crossSite,
+ authorization: "Bearer bogus",
+ });
+ expect(response.status).toBe(401);
+ expect(mocks.getSessionUser).not.toHaveBeenCalled();
+ });
+
it("keeps optional endpoints working anonymously for cross-site writes", async () => {
const response = await send("/optional", "POST", crossSite);
expect(response.status).toBe(200);
expect(await response.json()).toEqual({ user: null });
});
+ it("keeps optional endpoints anonymous for a cross-site write with a bogus bearer", async () => {
+ const response = await send("/optional", "POST", {
+ ...crossSite,
+ authorization: "Bearer bogus",
+ });
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual({ user: null });
+ });
+
it.each([
["same-origin", sameOrigin],
["same-site", sameSite],
@@ -285,8 +308,14 @@ describe("Effect getCurrentUser", () => {
),
);
- it("returns no user for a cross-site cookie write", async () => {
- expect(await run(request("POST", crossSite))).toEqual(Option.none());
+ it.each([
+ ["plain cross-site write", request("POST", crossSite)],
+ [
+ "cross-site write carrying a bogus bearer",
+ request("POST", { ...crossSite, authorization: "Bearer other" }),
+ ],
+ ])("returns no user for a %s", async (_label, web) => {
+ expect(await run(web)).toEqual(Option.none());
expect(mocks.getServerSession).not.toHaveBeenCalled();
});
@@ -294,10 +323,6 @@ describe("Effect getCurrentUser", () => {
["same-origin write", request("POST", sameOrigin)],
["same-site write", request("POST", sameSite)],
["cross-site GET", request("GET", crossSite)],
- [
- "cross-site write with an Authorization header",
- request("POST", { ...crossSite, authorization: "Bearer other" }),
- ],
["call outside a request", undefined],
])("returns the cookie user for a %s", async (_label, web) => {
expect(await run(web)).toEqual(Option.some("cookie-user"));
@@ -361,6 +386,14 @@ describe("HttpApi auth middleware", () => {
expect(response.status).toBe(401);
});
+ it("does not authenticate a cross-site cookie write carrying a bogus bearer", async () => {
+ const response = await send("POST", {
+ ...crossSite,
+ authorization: "Bearer bogus",
+ });
+ expect(response.status).toBe(401);
+ });
+
it.each([
["same-origin", sameOrigin],
["same-site", sameSite],
diff --git a/packages/database/auth/request-site.ts b/packages/database/auth/request-site.ts
index 40e17f46a..c9e82a039 100644
--- a/packages/database/auth/request-site.ts
+++ b/packages/database/auth/request-site.ts
@@ -5,7 +5,7 @@ const READ_ONLY_METHODS = new Set(["GET", "HEAD", "OPTIONS"]);
export type SiteCheckedRequest = {
method: string;
url: string;
- headers: Pick<Headers, "get" | "has">;
+ headers: Pick<Headers, "get">;
};
const parseUrl = (value: string | null | undefined) => {
@@ -34,9 +34,12 @@ const isOwnOrigin = (origin: URL, request: SiteCheckedRequest) => {
return parseUrl(webUrl)?.origin === origin.origin;
};
+// Callers must only consult this on the cookie-auth path: a successfully
+// authenticated bearer token returns before the check is reached. The presence
+// of an Authorization header is not proof of bearer auth, so it is ignored
+// here — an invalid bearer that falls back to the cookie still gets checked.
export const isCrossSiteCookieWrite = (request: SiteCheckedRequest) => {
if (READ_ONLY_METHODS.has(request.method.toUpperCase())) return false;
- if (request.headers.has("authorization")) return false;
// Sec-Fetch-Site is set by the browser and cannot be forged by page script,
// so it decides when present. Origin is the fallback for clients that