🪢 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): strip ASCII control characters in getSafeRedirectPath (#14870)
Harsh Kashyap349d0c44041c
diff --git a/web/src/__tests__/redirect.clienttest.ts b/web/src/__tests__/redirect.clienttest.tsindex 74cef0514f..21f5eb5221 100644--- a/web/src/__tests__/redirect.clienttest.ts+++ b/web/src/__tests__/redirect.clienttest.ts@@ -237,6 +237,87 @@ describe("getSafeRedirectPath", () => {expect(getSafeRedirectPath([])).toBe("/");});});++ describe("control character injection", () => {+ beforeEach(() => {+ (env as any).NEXT_PUBLIC_BASE_PATH = undefined;+ });++ it("should strip newlines from paths", () => {+ // Newline injection (HTTP header / log forging) is sanitized+ expect(getSafeRedirectPath("/abc\nevil.com")).toBe("/abcevil.com");+ // CRLF is also stripped+ expect(getSafeRedirectPath("/abc\r\ndef")).toBe("/abcdef");+ // Multiple newlines collapse safely+ expect(getSafeRedirectPath("/a\n\n\nb")).toBe("/ab");+ });++ it("should strip null bytes from paths", () => {+ // Null-byte injection (path-extension confusion) is sanitized+ expect(getSafeRedirectPath("/dashboard\u0000.png")).toBe(+ "/dashboard.png",+ );+ expect(getSafeRedirectPath("/abc\u0000evil")).toBe("/abcevil");+ });++ 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.+ 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 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.+ 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 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("/");+ });+ });});describe("stripBasePath", () => {diff --git a/web/src/utils/redirect.ts b/web/src/utils/redirect.tsindex 3a0dca3177..ee39be4e25 100644--- a/web/src/utils/redirect.ts+++ b/web/src/utils/redirect.ts@@ -42,6 +42,17 @@ export function getSafeRedirectPath(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) {+ return safeDefault;+ }+// Only allow paths starting with "/" but not "//" (protocol-relative URLs)// This blocks:// - Protocol-relative: "//evil.com"@@ -49,18 +60,18 @@ export function getSafeRedirectPath(// - JavaScript URIs: "javascript:alert(1)"// - Data URIs: "data:text/html,..."// - Other schemes: "file://", "ftp://", etc.- if (!trimmed.startsWith("/") || trimmed.startsWith("//")) {+ if (!sanitized.startsWith("/") || sanitized.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 && trimmed.startsWith(basePath)) {- return trimmed;+ if (basePath && sanitized.startsWith(basePath)) {+ return sanitized;}// Prepend basePath if configured- return basePath + trimmed;+ return basePath + sanitized;}/**