Skip to content

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

fix(shared): reject redirect paths that browsers treat as off-site (#3277)

Mohamed Bassem3977ba74bfc5
diff --git a/packages/shared/utils/redirectUrl.test.ts b/packages/shared/utils/redirectUrl.test.ts
index 97d52cf2..87418bd9 100644
--- a/packages/shared/utils/redirectUrl.test.ts
+++ b/packages/shared/utils/redirectUrl.test.ts
@@ -28,6 +28,46 @@ describe("validateRedirectUrl", () => {
expect(validateRedirectUrl("//evil.com/path")).toBe(undefined);
});
+ it("should reject paths that browsers treat as protocol-relative", () => {
+ // Browsers normalize "\" to "/" and drop tabs/newlines in URLs.
+ expect(validateRedirectUrl("/\\evil.com")).toBe(undefined);
+ expect(validateRedirectUrl("/\\/evil.com")).toBe(undefined);
+ expect(validateRedirectUrl("/\\\\evil.com/path")).toBe(undefined);
+ expect(validateRedirectUrl("/\t/evil.com")).toBe(undefined);
+ expect(validateRedirectUrl("/\n/evil.com")).toBe(undefined);
+ expect(validateRedirectUrl("/\r\n/evil.com")).toBe(undefined);
+ });
+
+ it("should reject paths that normalize to a protocol-relative URL", () => {
+ expect(validateRedirectUrl("/a/..//evil.com")).toBe(undefined);
+ expect(validateRedirectUrl("/.//evil.com")).toBe(undefined);
+ expect(validateRedirectUrl("/a/../\\/evil.com")).toBe(undefined);
+ expect(validateRedirectUrl("//redirect-check.invalid//evil.com")).toBe(
+ undefined,
+ );
+ expect(validateRedirectUrl("/\\redirect-check.invalid//evil.com")).toBe(
+ undefined,
+ );
+ expect(validateRedirectUrl("//REDIRECT-CHECK.INVALID:80//evil.com")).toBe(
+ undefined,
+ );
+ });
+
+ it("should keep percent-encoded slashes encoded", () => {
+ // Browsers don't decode %2F/%5C in paths, so these stay on-site.
+ expect(validateRedirectUrl("/%2F/evil.com")).toBe("/%2F/evil.com");
+ expect(validateRedirectUrl("/%5Cevil.com")).toBe("/%5Cevil.com");
+ });
+
+ it("should return the normalized same-origin path", () => {
+ // Even when the placeholder origin is named, only the path survives.
+ expect(validateRedirectUrl("/\\redirect-check.invalid/dashboard")).toBe(
+ "/dashboard",
+ );
+ expect(validateRedirectUrl("/a/../dashboard")).toBe("/dashboard");
+ expect(validateRedirectUrl("/a\\b")).toBe("/a/b");
+ });
+
it("should allow karakeep:// scheme for mobile app", () => {
expect(validateRedirectUrl("karakeep://")).toBe("karakeep://");
expect(validateRedirectUrl("karakeep://callback")).toBe(
diff --git a/packages/shared/utils/redirectUrl.ts b/packages/shared/utils/redirectUrl.ts
index c2adffc0..cb7793d5 100644
--- a/packages/shared/utils/redirectUrl.ts
+++ b/packages/shared/utils/redirectUrl.ts
@@ -1,7 +1,11 @@
+// Placeholder origin used to resolve relative paths; any path that resolves to
+// a different origin would leave the site.
+const SAME_ORIGIN_BASE = "http://redirect-check.invalid";
+
/**
* Validates a redirect URL to prevent open redirect attacks.
* Only allows:
- * - Relative paths starting with "/" (but not "//" to prevent protocol-relative URLs)
+ * - Relative paths that stay on the current origin (returned normalized)
* - The karakeep:// scheme for the mobile app
*
* @returns The validated URL if valid, otherwise undefined.
@@ -13,9 +17,29 @@ export function validateRedirectUrl(
return undefined;
}
- // Allow relative paths starting with "/" but not "//" (protocol-relative URLs)
- if (url.startsWith("/") && !url.startsWith("//")) {
- return url;
+ // Allow same-origin relative paths. A plain "doesn't start with //" check
+ // isn't enough: browsers treat "\" as "/" and drop tabs and newlines, so
+ // "/\evil.com" or "/\t/evil.com" are protocol-relative URLs too. Resolve the
+ // path the way a browser would, and return the normalized result so that
+ // what we validated is exactly what gets redirected to.
+ if (url.startsWith("/")) {
+ let resolved: URL;
+ try {
+ resolved = new URL(url, SAME_ORIGIN_BASE);
+ } catch {
+ return undefined;
+ }
+ if (resolved.origin !== SAME_ORIGIN_BASE) {
+ return undefined;
+ }
+ // Dot-segment removal can turn "/a/..//evil.com" (or "//<base>//evil.com")
+ // into the path "//evil.com", which is protocol-relative once returned
+ // without its origin. After parsing, the path has no "\" or whitespace
+ // left, so a leading "//" is the only remaining way to leave the site.
+ if (resolved.pathname.startsWith("//")) {
+ return undefined;
+ }
+ return `${resolved.pathname}${resolved.search}${resolved.hash}`;
}
// Allow karakeep:// scheme for mobile app deep links