Skip to content

Commit 45f3134

Browse files
committed
Use sliding window for attack waves
1 parent 6c4b812 commit 45f3134

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

library/vulnerabilities/attack-wave-detection/AttackWaveDetector.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type SuspiciousRequest = {
88
};
99

1010
export class AttackWaveDetector {
11-
private suspiciousRequestsCounts: LRUMap<string, number>;
11+
private suspiciousRequestsCounts: LRUMap<string, number[]>;
1212
private suspiciousRequestsSamples: LRUMap<string, SuspiciousRequest[]>;
1313
private sentEventsMap: LRUMap<string, number>;
1414

@@ -80,14 +80,24 @@ export class AttackWaveDetector {
8080
return false;
8181
}
8282

83-
const suspiciousRequests = (this.suspiciousRequestsCounts.get(ip) || 0) + 1;
84-
this.suspiciousRequestsCounts.set(ip, suspiciousRequests);
83+
const currentTime = performance.now();
84+
const requestTimestamps = this.suspiciousRequestsCounts.get(ip) || [];
85+
86+
const filteredTimestamps = requestTimestamps.filter(
87+
(timestamp) => currentTime - timestamp <= this.attackWaveTimeFrame
88+
);
89+
90+
filteredTimestamps.push(currentTime);
91+
92+
this.suspiciousRequestsCounts.set(ip, filteredTimestamps);
8593

8694
this.trackSample(ip, {
8795
method: context.method,
8896
url: context.url,
8997
});
9098

99+
const suspiciousRequests = filteredTimestamps.length;
100+
91101
if (suspiciousRequests < this.attackWaveThreshold) {
92102
return false;
93103
}

0 commit comments

Comments
 (0)