Add files via upload

add test_apicache_ReDos.js
This commit is contained in:
DayShift 2025-01-19 21:16:56 +08:00 committed by GitHub
parent 02f4cba9c6
commit e243a1a3a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,22 @@
const semver = require("semver");
let test;
const nodeVersion = process.versions.node;
if (semver.satisfies(nodeVersion, ">= 18")) {
test = require("node:test");
} else {
test = require("test");
}
const apicacheModule = require("../../server/modules/apicache/apicache.js");
const assert = require("node:assert");
test("Test ReDos - attack string", async (t) => {
const getDuration = apicacheModule.getDuration;
const str = "" + "00".repeat(100000) + "\u0000";
const startTime = performance.now();
getDuration(str);
const endTime = performance.now();
const elapsedTime = endTime - startTime;
const reDosThreshold = 9000;
assert(elapsedTime <= reDosThreshold, `🚨 可能存在 ReDoS 攻击getDuration 方法耗时 ${elapsedTime.toFixed(2)} 毫秒,超过阈值 ${reDosThreshold} 毫秒。`);
});