@@ -30,6 +30,7 @@ function newAttackWaveDetector() {
3030 attackWaveTimeFrame : 60 * 1000 ,
3131 minTimeBetweenEvents : 60 * 60 * 1000 ,
3232 maxLRUEntries : 10_000 ,
33+ maxSamplesPerIP : 5 ,
3334 } ) ;
3435}
3536
@@ -150,3 +151,55 @@ t.test("a slow web scanner that triggers in the third interval", async (t) => {
150151
151152 clock . uninstall ( ) ;
152153} ) ;
154+
155+ t . test ( "it collects samples correctly" , async ( t ) => {
156+ const detector = newAttackWaveDetector ( ) ;
157+ const ip = "::1" ;
158+ detector . check ( getTestContext ( ip , "/wp-config.php" , "GET" ) ) ;
159+ detector . check ( getTestContext ( ip , "/wp-config.php.bak" , "GET" ) ) ;
160+ detector . check ( getTestContext ( ip , "/.git/config" , "GET" ) ) ;
161+ detector . check ( getTestContext ( ip , "/.env" , "GET" ) ) ;
162+ detector . check ( getTestContext ( ip , "/.htaccess" , "GET" ) ) ;
163+
164+ detector . check ( getTestContext ( ip , "/.htaccess" , "GET" ) ) ; // Duplicate
165+ detector . check ( getTestContext ( "::2" , "/test/.env" , "GET" ) ) ; // Different IP
166+
167+ const samples = detector . getSamplesForIP ( ip ) ;
168+ t . equal ( samples . length , 5 , "should have collected 5 samples" ) ;
169+
170+ t . same (
171+ samples ,
172+ [
173+ { method : "GET" , url : "http://localhost:4000/wp-config.php" } ,
174+ { method : "GET" , url : "http://localhost:4000/wp-config.php.bak" } ,
175+ { method : "GET" , url : "http://localhost:4000/.git/config" } ,
176+ { method : "GET" , url : "http://localhost:4000/.env" } ,
177+ { method : "GET" , url : "http://localhost:4000/.htaccess" } ,
178+ ] ,
179+ "should have collected the correct samples"
180+ ) ;
181+ } ) ;
182+
183+ t . test ( "it limits samples correctly" , async ( t ) => {
184+ const detector = newAttackWaveDetector ( ) ;
185+ const ip = "::1" ;
186+
187+ for ( let i = 0 ; i < 10 ; i ++ ) {
188+ detector . check ( getTestContext ( ip , `/${ i } /.env` , "GET" ) ) ;
189+ }
190+
191+ const samples = detector . getSamplesForIP ( ip ) ;
192+ t . equal ( samples . length , 5 , "should have collected maximum 5 samples" ) ;
193+
194+ t . same (
195+ samples ,
196+ [
197+ { method : "GET" , url : "http://localhost:4000/0/.env" } ,
198+ { method : "GET" , url : "http://localhost:4000/1/.env" } ,
199+ { method : "GET" , url : "http://localhost:4000/2/.env" } ,
200+ { method : "GET" , url : "http://localhost:4000/3/.env" } ,
201+ { method : "GET" , url : "http://localhost:4000/4/.env" } ,
202+ ] ,
203+ "should have collected the correct samples"
204+ ) ;
205+ } ) ;
0 commit comments