Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(workers): drop credentials when a redirect changes origin (#3279)

Mohamed Bassem0d31bbada434
diff --git a/apps/workers/network.redirect.test.ts b/apps/workers/network.redirect.test.ts
new file mode 100644
index 00000000..43ae836c
--- /dev/null
+++ b/apps/workers/network.redirect.test.ts
@@ -0,0 +1,167 @@
+import http from "node:http";
+import type { AddressInfo } from "node:net";
+import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
+
+// Loopback is allowlisted so the redirect loop can talk to local test servers.
+vi.mock("@karakeep/shared/config", () => ({
+ default: {
+ allowedInternalHostnames: ["127.0.0.1", "localhost"],
+ crawler: { ipValidation: { dnsResolverTimeoutSec: 1 } },
+ proxy: {
+ httpProxy: undefined,
+ httpsProxy: undefined,
+ noProxy: undefined,
+ },
+ },
+}));
+
+import { fetchWithProxy, resolveValidatedRedirectUrl } from "./network";
+
+interface RecordedRequest {
+ method?: string;
+ url?: string;
+ headers: http.IncomingHttpHeaders;
+ body: string;
+}
+
+function startServer(
+ handler: (req: http.IncomingMessage, res: http.ServerResponse) => void,
+) {
+ const requests: RecordedRequest[] = [];
+ const server = http.createServer((req, res) => {
+ let body = "";
+ req.on("data", (chunk: Buffer) => (body += chunk.toString()));
+ req.on("end", () => {
+ requests.push({
+ method: req.method,
+ url: req.url,
+ headers: req.headers,
+ body,
+ });
+ handler(req, res);
+ });
+ });
+ return { server, requests };
+}
+
+const credentialHeaders = {
+ Authorization: "Bearer webhook-secret",
+ "Proxy-Authorization": "Basic cHJveHk6c2VjcmV0",
+ Cookie: "session=secret",
+ "X-Custom": "kept",
+};
+
+describe("redirect credential handling", () => {
+ // Same host, different ports => different origins.
+ const target = startServer((_req, res) => {
+ res.writeHead(200).end("ok");
+ });
+ let targetOrigin = "";
+
+ const origin = startServer((req, res) => {
+ const [path, query] = (req.url ?? "").split("?");
+ const status = Number(new URLSearchParams(query).get("status") ?? 307);
+ if (path === "/cross") {
+ res.writeHead(status, { Location: `${targetOrigin}/landing` }).end();
+ } else if (path === "/to-localhost") {
+ res
+ .writeHead(307, {
+ Location: `${originUrl.replace("127.0.0.1", "localhost")}/landing`,
+ })
+ .end();
+ } else if (path === "/same") {
+ res.writeHead(status, { Location: "/landing" }).end();
+ } else {
+ res.writeHead(200).end("ok");
+ }
+ });
+ let originUrl = "";
+
+ beforeAll(async () => {
+ await new Promise<void>((r) => target.server.listen(0, "127.0.0.1", r));
+ await new Promise<void>((r) => origin.server.listen(0, "127.0.0.1", r));
+ targetOrigin = `http://127.0.0.1:${(target.server.address() as AddressInfo).port}`;
+ originUrl = `http://127.0.0.1:${(origin.server.address() as AddressInfo).port}`;
+ });
+
+ afterAll(async () => {
+ await new Promise((r) => target.server.close(r));
+ await new Promise((r) => origin.server.close(r));
+ });
+
+ test.each([301, 302, 303, 307, 308])(
+ "drops credentials on a cross-origin %i redirect",
+ async (status) => {
+ target.requests.length = 0;
+
+ const response = await fetchWithProxy(
+ `${originUrl}/cross?status=${status}`,
+ {
+ method: "POST",
+ headers: { ...credentialHeaders, "Content-Type": "application/json" },
+ body: JSON.stringify({ hello: "world" }),
+ },
+ );
+
+ expect(response.status).toBe(200);
+ expect(target.requests).toHaveLength(1);
+ const [received] = target.requests;
+ expect(received.headers.authorization).toBeUndefined();
+ expect(received.headers["proxy-authorization"]).toBeUndefined();
+ expect(received.headers.cookie).toBeUndefined();
+ expect(received.headers["x-custom"]).toBe("kept");
+ },
+ );
+
+ test("keeps credentials on a same-origin redirect", async () => {
+ origin.requests.length = 0;
+
+ const response = await fetchWithProxy(`${originUrl}/same?status=307`, {
+ method: "POST",
+ headers: credentialHeaders,
+ body: "payload",
+ });
+
+ expect(response.status).toBe(200);
+ const landing = origin.requests.find((r) => r.url === "/landing");
+ expect(landing?.headers.authorization).toBe("Bearer webhook-secret");
+ expect(landing?.headers.cookie).toBe("session=secret");
+ expect(landing?.body).toBe("payload");
+ });
+
+ test("treats a different hostname on the same port as cross-origin", async () => {
+ origin.requests.length = 0;
+
+ // 127.0.0.1 -> localhost on the same port reaches the same server but is
+ // a different origin, so only the hostname differs between the two hops.
+ await fetchWithProxy(`${originUrl}/to-localhost`, {
+ headers: credentialHeaders,
+ });
+
+ expect(origin.requests.map((r) => r.url)).toEqual([
+ "/to-localhost",
+ "/landing",
+ ]);
+ const [first, landing] = origin.requests;
+ expect(first.headers.authorization).toBe("Bearer webhook-secret");
+ expect(landing.headers.host).toBe(
+ new URL(originUrl).host.replace("127.0.0.1", "localhost"),
+ );
+ expect(landing.headers.authorization).toBeUndefined();
+ expect(landing.headers.cookie).toBeUndefined();
+ });
+
+ test("resolveValidatedRedirectUrl drops credentials across origins", async () => {
+ target.requests.length = 0;
+
+ const resolved = await resolveValidatedRedirectUrl(
+ `${originUrl}/cross?status=302`,
+ { headers: credentialHeaders },
+ );
+
+ expect(resolved.toString()).toBe(`${targetOrigin}/landing`);
+ expect(target.requests).toHaveLength(1);
+ expect(target.requests[0].headers.authorization).toBeUndefined();
+ expect(target.requests[0].headers.cookie).toBeUndefined();
+ });
+});
diff --git a/apps/workers/network.ts b/apps/workers/network.ts
index 384a140c..6e71a16c 100644
--- a/apps/workers/network.ts
+++ b/apps/workers/network.ts
@@ -390,6 +390,26 @@ function cloneHeaders(init?: HeadersInit): Headers {
return headers;
}
+// Headers that carry credentials for the origin they were configured for (e.g.
+// a webhook's bearer token) and must not follow a redirect to another origin.
+// Mirrors what undici/WHATWG fetch and follow-redirects do.
+const ORIGIN_BOUND_HEADERS = ["authorization", "proxy-authorization", "cookie"];
+
+function stripCredentialsOnCrossOriginRedirect(
+ headers: Headers,
+ from: URL,
+ to: URL,
+): void {
+ // Origin includes the scheme, so an https -> http downgrade on the same host
+ // also counts as cross-origin.
+ if (from.origin === to.origin) {
+ return;
+ }
+ for (const header of ORIGIN_BOUND_HEADERS) {
+ headers.delete(header);
+ }
+}
+
function isRedirectResponse(response: Response): boolean {
return (
response.status === 301 ||
@@ -551,6 +571,7 @@ export const fetchWithProxy = async (
baseHeaders.delete("content-length");
}
+ stripCredentialsOnCrossOriginRedirect(baseHeaders, requestUrl, nextUrl);
currentUrl = nextUrl.toString();
redirectsRemaining -= 1;
}
@@ -622,7 +643,9 @@ export async function resolveValidatedRedirectUrl(
throw new Error(`Too many redirects while resolving ${url}`);
}
- currentUrl = new URL(locationHeader, currentUrl).toString();
+ const nextUrl = new URL(locationHeader, currentUrl);
+ stripCredentialsOnCrossOriginRedirect(baseHeaders, requestUrl, nextUrl);
+ currentUrl = nextUrl.toString();
redirectsRemaining -= 1;
}
}