From c00a7a4c8d3c77d92fca6108453f252a43cc50ed Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 4 Dec 2026 20:28:01 -0500 Subject: [PATCH] fix: Filter sensitive query parameters in logged strings (#14896) --- server/logging/sanitize.test.ts | 27 ++++++++++++++++++ server/logging/sanitize.ts | 49 +++++++++++++++++++++++++++++++-- server/logging/sentry.ts | 26 +---------------- 3 files changed, 75 insertions(+), 27 deletions(-) diff --git a/server/logging/sanitize.test.ts b/server/logging/sanitize.test.ts index 79c3185ae0..d3e09f9ab1 100644 --- a/server/logging/sanitize.test.ts +++ b/server/logging/sanitize.test.ts @@ -177,4 +177,31 @@ describe("sanitize", () => { input.self = input; expect(() => JSON.stringify(sanitize(input))).not.toThrow(); }); + + it("should filter sensitive query parameters in URLs within strings", () => { + expect( + sanitize({ + error: + "request to https://iframe.ly/api/iframely?url=https%3A%2F%2Fexample.com&api_key=secret failed", + }) + ).toEqual({ + error: + "request to https://iframe.ly/api/iframely?url=https%3A%2F%2Fexample.com&api_key=%5BFiltered%5D failed", + }); + }); + + it("should filter sensitive query parameters in error messages", () => { + const result = sanitize( + new Error("request to https://example.com/?token=secret failed") + ) as unknown as Error; + expect(result.message).toEqual( + "request to https://example.com/?token=%5BFiltered%5D failed" + ); + expect(result.stack).not.toContain("secret"); + }); + + it("should not change URLs without sensitive query parameters", () => { + const text = "see https://example.com/page?id=1&q=test"; + expect(sanitize(text)).toEqual(text); + }); }); diff --git a/server/logging/sanitize.ts b/server/logging/sanitize.ts index 604dbf7f86..78d3666279 100644 --- a/server/logging/sanitize.ts +++ b/server/logging/sanitize.ts @@ -14,8 +14,8 @@ export function sanitize(input: T, level = 0): T { if (input instanceof Error) { return { name: input.name, - message: input.message, - stack: input.stack, + message: filterUrls(input.message), + stack: input.stack && filterUrls(input.stack), } as unknown as T; } @@ -25,6 +25,31 @@ export function sanitize(input: T, level = 0): T { return sanitizeValue(value, level) as T; } +export const SensitiveHeaderPattern = + /auth|cookie|token|secret|key|session|signature/i; + +// OAuth callback codes and state, and signed attachment URL signatures. +const SensitiveQueryParamPattern = /^(code|state|sig)$/i; + +/** + * Replaces the values of sensitive parameters in a query string. + * + * @param query the query string, without a leading "?". + * @returns the filtered query string. + */ +export function filterQueryString(query: string): string { + const params = new URLSearchParams(query); + for (const name of Array.from(params.keys())) { + if ( + SensitiveQueryParamPattern.test(name) || + SensitiveHeaderPattern.test(name) + ) { + params.set(name, "[Filtered]"); + } + } + return params.toString(); +} + /** * Sanitizes a value without calling its toJSON method, as JSON serialization * only calls it once. @@ -34,6 +59,10 @@ export function sanitize(input: T, level = 0): T { * @returns the sanitized value. */ function sanitizeValue(input: unknown, level: number): unknown { + if (typeof input === "string") { + return filterUrls(input); + } + if (typeof input !== "object" || input === null) { return input; } @@ -91,3 +120,19 @@ function hasToJSON(value: unknown): value is { toJSON: () => unknown } { isObject(value) && "toJSON" in value && typeof value.toJSON === "function" ); } + +// The query string of an absolute URL inside text, such as an error message. +const UrlQueryPattern = /(\bhttps?:\/\/[^\s?#]*\?)([^\s#"'<>]+)/gi; + +/** + * Replaces the values of sensitive query parameters in URLs within text. + * + * @param text the text to filter. + * @returns the filtered text. + */ +function filterUrls(text: string): string { + return text.replace( + UrlQueryPattern, + (_match, prefix: string, query: string) => prefix + filterQueryString(query) + ); +} diff --git a/server/logging/sentry.ts b/server/logging/sentry.ts index 2986266204..4a866f6529 100644 --- a/server/logging/sentry.ts +++ b/server/logging/sentry.ts @@ -2,6 +2,7 @@ import cluster from "node:cluster"; import type * as SentryNode from "@sentry/node"; import env from "@server/env"; import type { AppContext } from "@server/types"; +import { filterQueryString, SensitiveHeaderPattern } from "./sanitize"; // Sentry is only loaded in worker processes, as it is comparatively expensive // and the cluster primary only supervises the forked workers. @@ -140,28 +141,3 @@ function loadSentry(dsn: string): typeof SentryNode { }); return sentry; } - -const SensitiveHeaderPattern = - /auth|cookie|token|secret|key|session|signature/i; - -// OAuth callback codes and state, and signed attachment URL signatures. -const SensitiveQueryParamPattern = /^(code|state|sig)$/i; - -/** - * Replaces the values of sensitive parameters in a query string. - * - * @param query the query string, without a leading "?". - * @returns the filtered query string. - */ -function filterQueryString(query: string): string { - const params = new URLSearchParams(query); - for (const name of Array.from(params.keys())) { - if ( - SensitiveQueryParamPattern.test(name) || - SensitiveHeaderPattern.test(name) - ) { - params.set(name, "[Filtered]"); - } - } - return params.toString(); -}