Skip to content

Commit

Permalink
test: remove global pollution
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy1339 committed Feb 14, 2025
1 parent 28c09b5 commit a7cd200
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,30 @@ const userAgent = `octokit-request.js/0.0.0-development ${getUserAgent()}`;
const stringToArrayBuffer = require("string-to-arraybuffer");

describe("request()", () => {
it("Test ReDoS - attack string", () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url, options) => {
const response = await originalFetch(url, options);
it("Test ReDoS - attack string", () => {
const fakeFetch = async (url, options) => {
const response = await fetch(url, options);
const fakeHeaders = new Headers(response.headers);
fakeHeaders.set("link", "<".repeat(100000) + ">");
fakeHeaders.set("deprecation", "true");
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: fakeHeaders
headers: fakeHeaders,
});
};
const startTime = performance.now();
request("GET /repos/octocat/hello-world");
request("GET /repos/octocat/hello-world", {
request: { fetch: fakeFetch },
});
const endTime = performance.now();
const elapsedTime = endTime - startTime;
const reDosThreshold = 2000;
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.`);
console.warn(
`🚨 Potential ReDoS Attack! getDuration method took ${elapsedTime.toFixed(2)} ms, exceeding threshold of ${reDosThreshold} ms.`,
);
}
});

Expand Down

0 comments on commit a7cd200

Please sign in to comment.