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
146 changes: 141 additions & 5 deletions apps/web/__tests__/unit/cookie-write-site.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const mocks = vi.hoisted(() => {
update: () => ({ set: () => ({ where: async () => undefined }) }),
};
return {
webUrl: "https://cap.so",
cookieUser,
keyUser,
client,
Expand All @@ -58,7 +59,7 @@ const mocks = vi.hoisted(() => {
vi.mock("server-only", () => ({}));

vi.mock("@cap/env", () => ({
serverEnv: () => ({ WEB_URL: "https://cap.so" }),
serverEnv: () => ({ WEB_URL: mocks.webUrl }),
buildEnv: {
NEXT_PUBLIC_WEB_URL: "https://cap.so",
NEXT_PUBLIC_IS_CAP: "true",
Expand Down Expand Up @@ -103,8 +104,13 @@ const sameSite = {
"sec-fetch-site": "same-site",
origin: "https://www.cap.so",
};
const mediaHost = {
"sec-fetch-site": "same-site",
origin: "https://v.cap.so",
};

beforeEach(() => {
mocks.webUrl = "https://cap.so";
mocks.getServerSession.mockClear();
mocks.getSessionUser.mockClear();
});
Expand Down Expand Up @@ -138,6 +144,94 @@ describe("isCrossSiteCookieWrite", () => {
expect(isCrossSiteCookieWrite(request("POST", headers))).toBe(false);
});

it.each([
["the media host", mediaHost],
[
"another sibling subdomain",
{ "sec-fetch-site": "same-site", origin: "https://uploads.cap.so" },
],
["an opaque origin", { "sec-fetch-site": "same-site", origin: "null" }],
["no Origin", { "sec-fetch-site": "same-site" }],
[
"the www host on another scheme",
{ "sec-fetch-site": "same-site", origin: "http://www.cap.so" },
],
])("flags a same-site write from %s", (_label, headers) => {
expect(isCrossSiteCookieWrite(request("POST", headers))).toBe(true);
});

it("allows a same-site write from the app's own host", () => {
expect(
isCrossSiteCookieWrite(
request(
"POST",
{ "sec-fetch-site": "same-site", origin: "https://www.cap.so" },
"https://www.cap.so/api/test",
),
),
).toBe(false);
});

it("allows a write from the extension's own pages", () => {
expect(
isCrossSiteCookieWrite(
request("POST", {
"sec-fetch-site": "none",
origin: "chrome-extension://abcdefghijklmnopabcdefghijklmnop",
}),
),
).toBe(false);
});

it.each([
[
"its www host",
"https://cap.acme.example",
"https://www.cap.acme.example",
false,
],
[
"its apex host",
"https://www.acme.example",
"https://acme.example",
false,
],
[
"a sibling host",
"https://cap.acme.example",
"https://media.acme.example",
true,
],
])(
"checks a same-site write to a self-hosted install from %s",
(_label, webUrl, origin, flagged) => {
mocks.webUrl = webUrl;
const host = new URL(webUrl).host;
expect(
isCrossSiteCookieWrite(
request(
"POST",
{ "sec-fetch-site": "same-site", origin },
`https://${host}/api/test`,
),
),
).toBe(flagged);
},
);

it("allows a plain-http self-hosted write without fetch metadata", () => {
mocks.webUrl = "http://cap.acme.example";
expect(
isCrossSiteCookieWrite(
request(
"POST",
{ origin: "http://cap.acme.example" },
"http://cap.acme.example/api/test",
),
),
).toBe(false);
});

it("treats the request host as an own origin for custom domains and previews", () => {
expect(
isCrossSiteCookieWrite(
Expand Down Expand Up @@ -241,9 +335,19 @@ describe("Hono cookie fallback", () => {
expect(await response.json()).toEqual({ user: "cookie-user" });
});

it("authenticates a bearer write from any site", async () => {
it("does not authenticate a cookie write from a page on the media host", async () => {
const response = await send("/required", "POST", mediaHost);
expect(response.status).toBe(401);
expect(mocks.getSessionUser).not.toHaveBeenCalled();
});

it.each([
["any site", crossSite],
["the media host", mediaHost],
["a native client", {}],
])("authenticates a bearer write from %s", async (_label, headers) => {
const response = await send("/required", "POST", {
...crossSite,
...headers,
authorization: `Bearer ${API_KEY}`,
});
expect(response.status).toBe(200);
Expand Down Expand Up @@ -273,6 +377,22 @@ describe("App Router cookie writes", () => {
expect(mocks.getSessionUser).not.toHaveBeenCalled();
});

it("does not rename the account from a page on the media host", async () => {
const response = await updateUserName(
new Request("https://cap.so/api/settings/user/name", {
method: "POST",
headers: {
host: "cap.so",
"content-type": "text/plain;charset=UTF-8",
...mediaHost,
},
body: JSON.stringify({ firstName: "A", lastName: "B" }),
}) as never,
);
expect(response.status).toBe(401);
expect(mocks.getSessionUser).not.toHaveBeenCalled();
});

it("authenticates a same-origin cookie write", async () => {
const response = await rename(sameOrigin);
expect(response.status).toBe(200);
Expand Down Expand Up @@ -310,6 +430,7 @@ describe("Effect getCurrentUser", () => {

it.each([
["plain cross-site write", request("POST", crossSite)],
["write from the media host", request("POST", mediaHost)],
[
"cross-site write carrying a bogus bearer",
request("POST", { ...crossSite, authorization: "Bearer other" }),
Expand Down Expand Up @@ -403,9 +524,18 @@ describe("HttpApi auth middleware", () => {
expect(await response.json()).toEqual({ user: "cookie-user" });
});

it("authenticates a bearer write from any site", async () => {
it("does not authenticate a cookie write from the media host", async () => {
const response = await send("POST", mediaHost);
expect(response.status).toBe(401);
});

it.each([
["any site", crossSite],
["the media host", mediaHost],
["a native client", {}],
])("authenticates a bearer write from %s", async (_label, headers) => {
const response = await send("POST", {
...crossSite,
...headers,
authorization: `Bearer ${API_KEY}`,
});
expect(response.status).toBe(200);
Expand Down Expand Up @@ -453,6 +583,12 @@ describe("RPC auth middleware", () => {
expect(JSON.stringify(exit.cause)).toContain("UnauthenticatedError");
});

it("does not authenticate a cookie call from the media host", async () => {
const exit = await call(mediaHost);
expect(exit._tag).toBe("Failure");
expect(JSON.stringify(exit.cause)).toContain("UnauthenticatedError");
});

it("reads fetch metadata from the HTTP request, not the RPC payload headers", async () => {
const exit = await call(crossSite, [
["sec-fetch-site", "same-origin"],
Expand Down
55 changes: 55 additions & 0 deletions apps/web/__tests__/unit/media-processing-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
MEDIA_INPUT_POLICY_STRIKE_WINDOW_MS,
mediaProcessingSuspendedError,
readMediaInputPolicyState,
toCompletedUploadContentType,
toStoredUploadContentType,
unsupportedMediaInputError,
} from "@/lib/media-processing-policy";

Expand Down Expand Up @@ -214,3 +216,56 @@ describe("getMediaProcessingRejection", () => {
);
});
});

describe("toStoredUploadContentType", () => {
it.each([
"video/mp4",
"video/webm;codecs=vp9,opus",
"Video/QuickTime",
"audio/mp4",
"image/png",
"image/jpeg",
])("keeps %s", (contentType) => {
expect(toStoredUploadContentType(contentType)).toBe(contentType);
});

it.each([
"text/html",
" TEXT/HTML ; charset=utf-8",
"application/xhtml+xml",
"image/svg+xml",
"text/xml",
"application/xml",
"text/javascript",
"application/pdf",
"multipart/x-mixed-replace",
"",
])("stores %s as plain bytes", (contentType) => {
expect(toStoredUploadContentType(contentType)).toBe(
"application/octet-stream",
);
});
});

describe("toCompletedUploadContentType", () => {
it.each([
["owner/video/page.html", "application/octet-stream"],
["owner/video/page", "application/octet-stream"],
["owner/video/image.svg", "Application/Octet-Stream"],
])("keeps %s stored as %s a download", (fileKey, stored) => {
expect(toCompletedUploadContentType(fileKey, stored)).toBe(
"application/octet-stream",
);
});

it.each([
["owner/video/result.mp4", "application/octet-stream"],
["owner/video/clip.webm", "application/octet-stream"],
["owner/video/result.mp4", "video/mp4"],
["owner/video/clip.webm", "video/webm"],
["owner/video/result.mp4", undefined],
["owner/video/page.html", undefined],
])("completes %s stored as %s as video/mp4", (fileKey, stored) => {
expect(toCompletedUploadContentType(fileKey, stored)).toBe("video/mp4");
});
});
Loading
Loading