Skip to content

Commit

Permalink
fix: ReDos regex vulnerability, reported by @dayshift
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiyuBanzhou authored and nickfloyd committed Feb 14, 2025
1 parent 3af20bd commit 6f009fa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class RequestError extends Error {
if (options.request.headers.authorization) {
requestCopy.headers = Object.assign({}, options.request.headers, {
authorization: options.request.headers.authorization.replace(
/ .*$/,
/(?<! ) .*$/,
" [REDACTED]",
),
});
Expand Down
34 changes: 34 additions & 0 deletions test/request-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,40 @@ const mockOptions: RequestErrorOptions = {
};

describe("RequestError", () => {
test("Test ReDoS - attack string", () => {
const startTime = performance.now();
const error = new RequestError("Oops", 500, {
request: {
method: "POST",
url: "https://api.github.com/foo",
body: {
bar: "baz",
},
headers: {
authorization: ""+" ".repeat(100000)+"\n@",
},
},
response: {
status: 500,
url: "https://api.github.com/foo",
headers: {
"x-github-request-id": "1:2:3:4",
},
data: {
foo: "bar",
},
},
});
const endTime = performance.now();
const elapsedTime = endTime - startTime;
const reDosThreshold = 2000;

expect(elapsedTime).toBeLessThanOrEqual(reDosThreshold);
if (elapsedTime > reDosThreshold) {
console.warn(`🚨 Potential ReDoS Attack! getDuration method took ${elapsedTime.toFixed(2)} ms, exceeding threshold of ${reDosThreshold} ms.`);
}
});

test("inherits from Error", () => {
const error = new RequestError("test", 123, mockOptions);
expect(error).toBeInstanceOf(Error);
Expand Down

0 comments on commit 6f009fa

Please sign in to comment.