Skip to content

Commit ac65adb

Browse files
authored
Merge pull request #835 from AikidoSec/bypass-attack-wave
Don't report attack waves for bypass IPs
2 parents 2f39052 + 28d7d7a commit ac65adb

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

end2end/tests-new/hono-pg-esm.test.mjs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const testServerUrl = "http://localhost:5874";
1414
const port = await getRandomPort();
1515
const port2 = await getRandomPort();
1616
const port3 = await getRandomPort();
17+
const port4 = await getRandomPort();
1718

1819
test("it blocks request in blocking mode", async () => {
1920
const server = spawn(
@@ -237,3 +238,86 @@ test("it reports packages in heartbeat with ESM instrumentation", async () => {
237238
server.kill();
238239
}
239240
});
241+
242+
test("if bypass IP is set, attack waves are ignored for that IP", async () => {
243+
const response = await fetch(`${testServerUrl}/api/runtime/apps`, {
244+
method: "POST",
245+
});
246+
const body = await response.json();
247+
const token = body.token;
248+
249+
await fetch(`${testServerUrl}/api/runtime/config`, {
250+
method: "POST",
251+
headers: {
252+
"Content-Type": "application/json",
253+
Authorization: token,
254+
},
255+
body: JSON.stringify({
256+
allowedIPAddresses: ["1.2.3.4"],
257+
}),
258+
});
259+
260+
const server = spawn(
261+
`node`,
262+
["--require", "@aikidosec/firewall/instrument", "./app.js", port4],
263+
{
264+
cwd: pathToAppDir,
265+
env: {
266+
...process.env,
267+
AIKIDO_TOKEN: token,
268+
AIKIDO_ENDPOINT: testServerUrl,
269+
AIKIDO_DEBUG: "true",
270+
},
271+
}
272+
);
273+
274+
try {
275+
server.on("error", (err) => {
276+
fail(err);
277+
});
278+
279+
let stdout = "";
280+
server.stdout.on("data", (data) => {
281+
stdout += data.toString();
282+
});
283+
284+
let stderr = "";
285+
server.stderr.on("data", (data) => {
286+
stderr += data.toString();
287+
});
288+
289+
// Wait for the server to start
290+
await timeout(2000);
291+
292+
await Promise.all(
293+
Array.from({ length: 15 }).map(() =>
294+
fetch(`http://localhost:${port4}/.env`, {
295+
headers: {
296+
"x-forwarded-for": "1.2.3.4",
297+
},
298+
})
299+
)
300+
);
301+
302+
// Wait for the attack wave event to be sent
303+
await timeout(2000);
304+
305+
const eventsResponse = await fetch(`${testServerUrl}/api/runtime/events`, {
306+
method: "GET",
307+
headers: {
308+
Authorization: token,
309+
},
310+
signal: AbortSignal.timeout(5000),
311+
});
312+
313+
const events = await eventsResponse.json();
314+
const attackWaveEvents = events.filter(
315+
(event) => event.type === "detected_attack_wave"
316+
);
317+
equal(attackWaveEvents.length, 0);
318+
} catch (err) {
319+
fail(err);
320+
} finally {
321+
server.kill();
322+
}
323+
});

library/sources/http-server/createRequestListener.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ function onFinishRequestHandler(
113113
agent.onRouteRateLimited(context.rateLimitedEndpoint);
114114
}
115115

116-
if (agent.getAttackWaveDetector().check(context)) {
116+
if (
117+
context.remoteAddress &&
118+
!agent.getConfig().isBypassedIP(context.remoteAddress) &&
119+
agent.getAttackWaveDetector().check(context)
120+
) {
117121
agent.onDetectedAttackWave({ request: context, metadata: {} });
118122
agent.getInspectionStatistics().onAttackWaveDetected();
119123
}

library/sources/http-server/http2/createStreamListener.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ function discoverRouteFromStream(
7676
agent.onRouteRateLimited(context.rateLimitedEndpoint);
7777
}
7878

79-
if (agent.getAttackWaveDetector().check(context)) {
79+
if (
80+
context.remoteAddress &&
81+
!agent.getConfig().isBypassedIP(context.remoteAddress) &&
82+
agent.getAttackWaveDetector().check(context)
83+
) {
8084
agent.onDetectedAttackWave({ request: context, metadata: {} });
8185
agent.getInspectionStatistics().onAttackWaveDetected();
8286
}

0 commit comments

Comments
 (0)