fix: Filter sensitive query parameters in logged strings (#14896)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-12-04 20:28:01 -05:00
1 parent 43eca25a1e
commit c00a7a4c8d
3 files changed
+75 -27

No files matched your search

+27
View File
@@ -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);
});
});
+47 -2
View File
@@ -14,8 +14,8 @@ export function sanitize<T>(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<T>(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<T>(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)
);
}
+1 -25
View File
@@ -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();
}