fix: Improve request scrubbing in error reports (#14820)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-11-30 18:28:16 -05:00
1 parent 654c9c3b1e
commit a143e28edd
2 files changed
+92 -17

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
import type { ErrorEvent } from "@sentry/node";
import { scrubRequest } from "./sentry";
describe("scrubRequest", () => {
it("should filter sensitive query parameters and headers", () => {
const event: ErrorEvent = {
type: undefined,
request: {
url: "https://app.getoutline.com/auth/google.callback?code=secret&state=jwt&iss=google&access_token=abc&sig=xyz",
query_string:
"code=secret&state=jwt&iss=google&access_token=abc&sig=xyz",
headers: { authorization: "Bearer x", "user-agent": "test" },
},
};
scrubRequest(event);
const filtered =
"code=%5BFiltered%5D&state=%5BFiltered%5D&iss=google&access_token=%5BFiltered%5D&sig=%5BFiltered%5D";
expect(event.request).toEqual({
url: `https://app.getoutline.com/auth/google.callback?${filtered}`,
query_string: filtered,
headers: { "user-agent": "test" },
});
});
it("should leave requests without a query string unchanged", () => {
const event: ErrorEvent = {
type: undefined,
request: { url: "https://app.getoutline.com/api/documents.info" },
};
scrubRequest(event);
expect(event.request).toEqual({
url: "https://app.getoutline.com/api/documents.info",
});
});
});
+53 -17
View File
@@ -49,6 +49,43 @@ export function requestErrorHandler(error: any, ctx: AppContext) {
}
}
/**
* Removes the request body, cookies, sensitive query parameters, and sensitive
* headers from an event.
*
* @param event the event to scrub in place.
*/
export function scrubRequest(event: SentryNode.ErrorEvent) {
const { request } = event;
if (!request) {
return;
}
delete request.data;
delete request.cookies;
if (typeof request.query_string === "string") {
request.query_string = filterQueryString(request.query_string);
} else {
delete request.query_string;
}
const queryIndex = request.url?.indexOf("?") ?? -1;
if (request.url && queryIndex !== -1) {
request.url =
request.url.slice(0, queryIndex + 1) +
filterQueryString(request.url.slice(queryIndex + 1));
}
if (request.headers) {
request.headers = Object.fromEntries(
Object.entries(request.headers).filter(
([name]) => !SensitiveHeaderPattern.test(name)
)
);
}
}
export default Sentry;
function loadSentry(dsn: string): typeof SentryNode {
@@ -107,25 +144,24 @@ function loadSentry(dsn: string): typeof SentryNode {
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;
/**
* Removes the request body, cookies, and sensitive headers from an event.
* Replaces the values of sensitive parameters in a query string.
*
* @param event the event to scrub in place.
* @param query the query string, without a leading "?".
* @returns the filtered query string.
*/
function scrubRequest(event: SentryNode.ErrorEvent) {
const { request } = event;
if (!request) {
return;
}
delete request.data;
delete request.cookies;
if (request.headers) {
request.headers = Object.fromEntries(
Object.entries(request.headers).filter(
([name]) => !SensitiveHeaderPattern.test(name)
)
);
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();
}