🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(web): Resolve redirect paths with the URL constructor (#16928)
Ben Bachem2ecafed00e67
diff --git a/web/src/__tests__/redirect.clienttest.ts b/web/src/__tests__/redirect.clienttest.tsindex 1e90d48ca3..1a58c0746a 100644--- a/web/src/__tests__/redirect.clienttest.ts+++ b/web/src/__tests__/redirect.clienttest.ts@@ -112,6 +112,7 @@ describe("getSafeRedirectPath", () => {expect(getSafeRedirectPath("./dashboard")).toBe("/");expect(getSafeRedirectPath("../dashboard")).toBe("/");expect(getSafeRedirectPath("evil.com")).toBe("/");+ expect(getSafeRedirectPath("\\dashboard")).toBe("/");});it("should handle URL-encoded attack attempts", () => {@@ -122,6 +123,36 @@ describe("getSafeRedirectPath", () => {// Note: The function receives already-decoded input in real usage// since router.query.targetPath is already decoded by Next.js});++ it("should block inputs the WHATWG parser would resolve off-origin", () => {+ // `new URL(path, origin)` — the same parse as location.assign.+ expect(getSafeRedirectPath("/\\evil.com")).toBe("/");+ expect(getSafeRedirectPath("/\\evil.com/path")).toBe("/");+ expect(getSafeRedirectPath("/\\\\evil.com")).toBe("/");+ expect(getSafeRedirectPath("\\\\evil.com")).toBe("/");+ expect(getSafeRedirectPath(decodeURIComponent("%2F%5Cevil.com"))).toBe(+ "/",+ );+ // Tab between the slashes is dropped by the URL parser, leaving `//`.+ expect(getSafeRedirectPath("/\t/evil.com")).toBe("/");+ expect(getSafeRedirectPath("/\t\\evil.com")).toBe("/");+ });++ it("should reject absolute URLs that match the dummy parse origin", () => {+ expect(getSafeRedirectPath("https://langfuse.invalid/phishing")).toBe(+ "/",+ );+ expect(getSafeRedirectPath("https:/evil.com")).toBe("/");+ });++ it("should reject paths that normalize to protocol-relative", () => {+ // `/x/..//evil.com` stays on the dummy origin, but pathname is+ // `//evil.com`. Returning that would reopen the redirect.+ expect(getSafeRedirectPath("/x/..//evil.com")).toBe("/");+ expect(getSafeRedirectPath("/..//evil.com")).toBe("/");+ expect(getSafeRedirectPath("/x/..//evil.com/path")).toBe("/");+ expect(getSafeRedirectPath("/x/%2e%2e//evil.com")).toBe("/");+ });});});@@ -170,6 +201,8 @@ describe("getSafeRedirectPath", () => {expect(getSafeRedirectPath("//evil.com")).toBe("/my-app/");expect(getSafeRedirectPath("http://evil.com")).toBe("/my-app/");expect(getSafeRedirectPath("javascript:alert(1)")).toBe("/my-app/");+ expect(getSafeRedirectPath("/\\evil.com")).toBe("/my-app/");+ expect(getSafeRedirectPath("/x/..//evil.com")).toBe("/my-app/");});it("should not double-prepend basePath when path already includes it", () => {@@ -197,6 +230,12 @@ describe("getSafeRedirectPath", () => {"/my-app/some/my-app/path",);});++ it("should only skip prepending when the path is a basePath segment", () => {+ expect(getSafeRedirectPath("/my-application")).toBe(+ "/my-app/my-application",+ );+ });});describe("edge cases", () => {@@ -210,8 +249,9 @@ describe("getSafeRedirectPath", () => {});it("should handle paths with special characters", () => {+ // The URL serializer percent-encodes spaces in the pathname.expect(getSafeRedirectPath("/path/with spaces")).toBe(- "/path/with spaces",+ "/path/with%20spaces",);expect(getSafeRedirectPath("/path/with-dashes")).toBe("/path/with-dashes",@@ -221,6 +261,14 @@ describe("getSafeRedirectPath", () => {);});+ it("should return the WHATWG-resolved path for otherwise-safe inputs", () => {+ expect(getSafeRedirectPath("/project\\123")).toBe("/project/123");+ expect(getSafeRedirectPath("/dashboard\\tab?view=traces")).toBe(+ "/dashboard/tab?view=traces",+ );+ expect(getSafeRedirectPath("/foo/../bar")).toBe("/bar");+ });+it("should handle paths with encoded characters", () => {expect(getSafeRedirectPath("/path%20with%20spaces")).toBe("/path%20with%20spaces",@@ -254,69 +302,41 @@ describe("getSafeRedirectPath", () => {expect(getSafeRedirectPath("/a\n\n\nb")).toBe("/ab");});- it("should strip null bytes from paths", () => {- // Null-byte injection (path-extension confusion) is sanitized+ it("should percent-encode null bytes and DEL in paths", () => {expect(getSafeRedirectPath("/dashboard\u0000.png")).toBe(- "/dashboard.png",+ "/dashboard%00.png",);- expect(getSafeRedirectPath("/abc\u0000evil")).toBe("/abcevil");+ expect(getSafeRedirectPath("/abc\u0000evil")).toBe("/abc%00evil");+ expect(getSafeRedirectPath("/abc\u007Fdef")).toBe("/abc%7Fdef");+ expect(getSafeRedirectPath("/abc\u001Fdef")).toBe("/abc%1Fdef");});- it("should strip DEL and other control characters from the C0 range", () => {- // DEL (U+007F) is the boundary control char; stripped.- expect(getSafeRedirectPath("/abc\u007Fdef")).toBe("/abcdef");- // Tab (U+0009) is in the C0 range; stripped.+ it("should strip tabs that the URL parser treats as ignorable", () => {expect(getSafeRedirectPath("/abc\tdef")).toBe("/abcdef");- // Unit-separator (U+001F) is the upper edge of the C0 range;- // stripped.- expect(getSafeRedirectPath("/abc\u001Fdef")).toBe("/abcdef");- });-- it("should NOT strip Unicode bidi-formatting characters outside the C0 range", () => {- // The C0 control-char strip is intentionally scoped to 0x00-0x1F- // and 0x7F (ASCII control characters per RFC 3986). Unicode bidi- // chars like RTL-override (U+202E) are out of scope for THIS fix;- // they require a separate, larger change. This test pins the- // current scope so future regressions are visible.- // Note: when this PR is merged, file a follow-up issue for- // bidi-formatting-character handling.- const pathWithRtl = "/abc\u202eevil";- const result = getSafeRedirectPath(pathWithRtl);- // The current implementation does not strip U+202E, so the- // character passes through.- expect(result).toContain("\u202e");+ });++ it("should percent-encode Unicode bidi-formatting characters", () => {+ expect(getSafeRedirectPath("/abc\u202eevil")).toBe("/abc%E2%80%AEevil");});it("should fall back to safe default when path is only control characters", () => {- // A path that is nothing but ASCII control characters is fully- // stripped to empty, so the safe default is returned.expect(getSafeRedirectPath("\n")).toBe("/");expect(getSafeRedirectPath("\u0000\u0000")).toBe("/");});- it("should fall back to safe default for path with no leading slash after stripping", () => {- // U+202E (RTL-override) is not stripped by this fix, but it- // also doesn't start with "/" so the safe default is returned.- // This case is documented separately from the control-character- // case above because the rejection happens at the startWith("/")- // guard, not in the strip step.+ it("should fall back to safe default for path with no leading slash", () => {+ // U+202E is not path-absolute, so it is rejected even though+ // the URL parser would resolve it same-origin as /%E2%80%AE.expect(getSafeRedirectPath("\u202e")).toBe("/");});- it("should preserve internal whitespace", () => {- // Tabs, spaces, etc. that are NOT control chars (i.e. U+0020- // SPACE) are preserved. This is a regression test for the strip- // being scoped to control characters only.- expect(getSafeRedirectPath("/abc def")).toBe("/abc def");- expect(getSafeRedirectPath("/a b c")).toBe("/a b c");+ it("should percent-encode internal spaces", () => {+ expect(getSafeRedirectPath("/abc def")).toBe("/abc%20def");+ expect(getSafeRedirectPath("/a b c")).toBe("/a%20b%20c");});it("should keep protocol-relative and absolute-URL guards working with control characters", () => {- // Regression: the existing // guard still wins after the strip.- // "//\nevil.com" → strip newline → "//evil.com" → still matches- // startsWith("//") → safe default.expect(getSafeRedirectPath("//\nevil.com")).toBe("/");- // "http:\n//evil.com" → "http://evil.com" → safe default.expect(getSafeRedirectPath("http:\n//evil.com")).toBe("/");});});diff --git a/web/src/utils/redirect.ts b/web/src/utils/redirect.tsindex dc9737699f..aa705284bd 100644--- a/web/src/utils/redirect.ts+++ b/web/src/utils/redirect.ts@@ -1,15 +1,24 @@import { env } from "@/src/env.mjs";+/**+ * Dummy https origin used only so the WHATWG URL parser can resolve the+ * input the same way a browser would. Never returned to callers.+ */+const REDIRECT_ORIGIN = "https://langfuse.invalid";+/*** Validates and sanitizes a redirect path to prevent open redirect attacks.** Security Requirements:- * - Only allows relative paths starting with "/" (e.g., "/dashboard", "/project/123")- * - Blocks protocol-relative URLs (e.g., "//evil.com")- * - Blocks absolute URLs (e.g., "http://evil.com", "https://evil.com")- * - Blocks javascript: and data: URIs+ * - Only allows path-absolute relative paths that start with "/"+ * - Parses with the WHATWG URL constructor and rejects off-origin results+ * (protocol-relative, backslash-normalized, absolute http(s), other schemes)+ * - Rejects serialized output that starts with "//" after dot-segment resolution* - Automatically prepends NEXT_PUBLIC_BASE_PATH if configured*+ * Returned paths are URL-serialized: spaces and control characters are+ * percent-encoded, and dot segments are resolved.+ ** @param targetPath - The path to validate (typically from query params or user input)* @returns A safe redirect path with basePath prepended, or "/" (with basePath) if invalid*@@ -30,48 +39,36 @@ export function getSafeRedirectPath(const basePath = env.NEXT_PUBLIC_BASE_PATH ?? "";const safeDefault = basePath ? `${basePath}/` : "/";- // Handle empty/null/undefined- if (!targetPath || typeof targetPath !== "string") {+ if (typeof targetPath !== "string") {return safeDefault;}- // Trim whitespace- const trimmed = targetPath.trim();+ const input = targetPath.trim();- if (!trimmed) {+ if (!input.startsWith("/")) {return safeDefault;}- // Strip ASCII control characters (0x00-0x1F, 0x7F) to defend against- // newline injection (HTTP header / log forging) and null-byte injection- // (path-extension confusion). These characters are not valid in URL- // paths per RFC 3986. Unicode bidi-formatting characters (e.g.- // U+202E RTL-override) are intentionally out of scope for this fix.- const sanitized = trimmed.replace(/[\x00-\x1F\x7F]/g, "");-- if (!sanitized) {+ let url: URL;+ try {+ url = new URL(input, REDIRECT_ORIGIN);+ } catch {return safeDefault;}- // Only allow paths starting with "/" but not "//" (protocol-relative URLs)- // This blocks:- // - Protocol-relative: "//evil.com"- // - Absolute URLs: "http://evil.com", "https://evil.com"- // - JavaScript URIs: "javascript:alert(1)"- // - Data URIs: "data:text/html,..."- // - Other schemes: "file://", "ftp://", etc.- if (!sanitized.startsWith("/") || sanitized.startsWith("//")) {+ const path = `${url.pathname}${url.search}${url.hash}`;++ // Origin rejects `/\evil.com` (HTTPS parser resolves it off-site).+ // Leading `//` after serialization rejects `/x/..//evil.com`.+ if (url.origin !== REDIRECT_ORIGIN || path.startsWith("//")) {return safeDefault;}- // If basePath is configured, check if the path already starts with it- // This prevents double-prepending when the path already includes the base path- if (basePath && sanitized.startsWith(basePath)) {- return sanitized;- }+ const includesBasePath =+ basePath &&+ (url.pathname === basePath || url.pathname.startsWith(`${basePath}/`));- // Prepend basePath if configured- return basePath + sanitized;+ return includesBasePath ? path : basePath + path;}/**@@ -92,8 +89,8 @@ export function stripBasePath(path: string): string {return path;}- // Strip ASCII control characters (0x00-0x1F, 0x7F) before further- // processing. See getSafeRedirectPath for the rationale.+ // Strip ASCII control characters (0x00-0x1F, 0x7F) so a newline or+ // null byte cannot split the basePath prefix from the remainder.const cleaned = path.replace(/[\x00-\x1F\x7F]/g, "");const stripped = cleaned.slice(basePath.length) || "/";