Skip to content

Commit 4973a56

Browse files
committed
Adapt to new firewall/lists API
1 parent 2c1c524 commit 4973a56

13 files changed

+415
-390
lines changed

library/agent/Agent.test.ts

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,31 @@ wrap(fetch, "fetch", function mock() {
3333
source: "name",
3434
description: "Description",
3535
ips: ["1.3.2.0/24", "fe80::1234:5678:abcd:ef12/64"],
36-
monitor: false,
37-
},
38-
],
39-
blockedUserAgents: [
40-
{
41-
key: "ai_bots",
42-
pattern: "AI2Bot|Bytespider",
43-
monitor: false,
4436
},
4537
],
38+
blockedUserAgents: "AI2Bot|Bytespider",
4639
allowedIPAddresses: shouldOnlyAllowSomeIPAddresses
4740
? [
4841
{
4942
key: "some/key",
5043
source: "name",
5144
description: "Description",
5245
ips: ["4.3.2.1"],
53-
monitor: false,
5446
},
5547
]
5648
: [],
49+
monitoredIPAddresses: [],
50+
monitoredUserAgents: "",
51+
userAgentDetails: [
52+
{
53+
key: "AI2Bot",
54+
pattern: "AI2Bot",
55+
},
56+
{
57+
key: "Bytespider",
58+
pattern: "Bytespider",
59+
},
60+
],
5761
} satisfies Response),
5862
};
5963
};
@@ -1076,51 +1080,36 @@ t.test("it fetches blocked lists", async () => {
10761080

10771081
await setTimeout(0);
10781082

1079-
t.same(agent.getConfig().getBlockedIPAddresses("1.3.2.4"), [
1080-
{
1081-
key: "some/key",
1082-
monitor: false,
1083-
reason: "Description",
1084-
},
1085-
]);
1086-
t.same(agent.getConfig().getBlockedIPAddresses("fe80::1234:5678:abcd:ef12"), [
1087-
{
1088-
key: "some/key",
1089-
monitor: false,
1090-
reason: "Description",
1091-
},
1092-
]);
1083+
t.same(agent.getConfig().isIPAddressBlocked("1.3.2.4"), {
1084+
blocked: true,
1085+
reason: "Description",
1086+
});
1087+
t.same(agent.getConfig().isIPAddressBlocked("fe80::1234:5678:abcd:ef12"), {
1088+
blocked: true,
1089+
reason: "Description",
1090+
});
10931091

10941092
t.same(
10951093
agent
10961094
.getConfig()
1097-
.getBlockedUserAgents(
1095+
.isUserAgentBlocked(
10981096
"Mozilla/5.0 (compatible) AI2Bot (+https://www.allenai.org/crawler)"
10991097
),
1100-
[
1101-
{
1102-
key: "ai_bots",
1103-
monitor: false,
1104-
},
1105-
]
1098+
{
1099+
blocked: true,
1100+
}
11061101
);
11071102

11081103
t.same(
1109-
agent
1110-
.getConfig()
1111-
.getBlockedUserAgents("Mozilla/5.0 (compatible) Bytespider"),
1112-
[
1113-
{
1114-
key: "ai_bots",
1115-
monitor: false,
1116-
},
1117-
]
1104+
agent.getConfig().isUserAgentBlocked("Mozilla/5.0 (compatible) Bytespider"),
1105+
{
1106+
blocked: true,
1107+
}
11181108
);
11191109

1120-
t.same(
1121-
agent.getConfig().getBlockedUserAgents("Mozilla/5.0 (compatible)"),
1122-
[]
1123-
);
1110+
t.same(agent.getConfig().isUserAgentBlocked("Mozilla/5.0 (compatible)"), {
1111+
blocked: false,
1112+
});
11241113
});
11251114

11261115
t.test("it does not fetch blocked IPs if serverless", async () => {
@@ -1134,18 +1123,23 @@ t.test("it does not fetch blocked IPs if serverless", async () => {
11341123

11351124
await setTimeout(0);
11361125

1137-
t.same(agent.getConfig().getBlockedIPAddresses("1.3.2.4"), []);
1126+
t.same(agent.getConfig().isIPAddressBlocked("1.3.2.4"), {
1127+
blocked: false,
1128+
});
1129+
11381130
t.same(agent.getConfig().isAllowedIPAddress("1.3.2.4"), {
11391131
allowed: true,
11401132
});
11411133

11421134
t.same(
11431135
agent
11441136
.getConfig()
1145-
.getBlockedUserAgents(
1137+
.isUserAgentBlocked(
11461138
"Mozilla/5.0 (compatible) AI2Bot (+https://www.allenai.org/crawler)"
11471139
),
1148-
[]
1140+
{
1141+
blocked: false,
1142+
}
11491143
);
11501144
});
11511145

@@ -1160,20 +1154,14 @@ t.test("it only allows some IP addresses", async () => {
11601154

11611155
await setTimeout(0);
11621156

1163-
t.same(agent.getConfig().getBlockedIPAddresses("1.3.2.4"), [
1164-
{
1165-
key: "some/key",
1166-
monitor: false,
1167-
reason: "Description",
1168-
},
1169-
]);
1170-
t.same(agent.getConfig().getBlockedIPAddresses("fe80::1234:5678:abcd:ef12"), [
1171-
{
1172-
key: "some/key",
1173-
monitor: false,
1174-
reason: "Description",
1175-
},
1176-
]);
1157+
t.same(agent.getConfig().isIPAddressBlocked("1.3.2.4"), {
1158+
blocked: true,
1159+
reason: "Description",
1160+
});
1161+
t.same(agent.getConfig().isIPAddressBlocked("fe80::1234:5678:abcd:ef12"), {
1162+
blocked: true,
1163+
reason: "Description",
1164+
});
11771165

11781166
t.same(agent.getConfig().isAllowedIPAddress("1.2.3.4"), {
11791167
allowed: false,

library/agent/Agent.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,20 @@ export class Agent {
380380
}
381381

382382
try {
383-
const { blockedIPAddresses, blockedUserAgents, allowedIPAddresses } =
384-
await fetchBlockedLists(this.token);
383+
const {
384+
blockedIPAddresses,
385+
blockedUserAgents,
386+
allowedIPAddresses,
387+
monitoredIPAddresses,
388+
monitoredUserAgents,
389+
userAgentDetails,
390+
} = await fetchBlockedLists(this.token);
385391
this.serviceConfig.updateBlockedIPAddresses(blockedIPAddresses);
386392
this.serviceConfig.updateBlockedUserAgents(blockedUserAgents);
387393
this.serviceConfig.updateAllowedIPAddresses(allowedIPAddresses);
394+
this.serviceConfig.updateMonitoredIPAddresses(monitoredIPAddresses);
395+
this.serviceConfig.updateMonitoredUserAgents(monitoredUserAgents);
396+
this.serviceConfig.updateUserAgentDetails(userAgentDetails);
388397
} catch (error: any) {
389398
console.error(`Aikido: Failed to update blocked lists: ${error.message}`);
390399
}

library/agent/InspectionStatistics.test.ts

Lines changed: 5 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -581,58 +581,16 @@ t.test("it keeps track of aborted requests", async () => {
581581
clock.uninstall();
582582
});
583583

584-
t.test("it keeps track of blocked IPs and user agents", async () => {
584+
t.test("it keeps track of matched IPs and user agents", async () => {
585585
const clock = FakeTimers.install();
586586

587587
const stats = new InspectionStatistics({
588588
maxPerfSamplesInMemory: 50,
589589
maxCompressedStatsInMemory: 5,
590590
});
591591

592-
stats.onIPAddressMatches([
593-
{ key: "known_threat_actors/public_scanners", monitor: false },
594-
]);
595-
stats.onUserAgentMatches([{ key: "ai_bots", monitor: false }]);
596-
597-
t.same(stats.getStats(), {
598-
operations: {},
599-
startedAt: 0,
600-
requests: {
601-
total: 0,
602-
aborted: 0,
603-
attacksDetected: {
604-
total: 0,
605-
blocked: 0,
606-
},
607-
},
608-
userAgents: {
609-
breakdown: {
610-
// eslint-disable-next-line camelcase
611-
ai_bots: { total: 1, blocked: 1 },
612-
},
613-
},
614-
ipAddresses: {
615-
breakdown: {
616-
"known_threat_actors/public_scanners": { total: 1, blocked: 1 },
617-
},
618-
},
619-
});
620-
621-
clock.uninstall();
622-
});
623-
624-
t.test("it keeps track of monitored IPs and user agents", async () => {
625-
const clock = FakeTimers.install();
626-
627-
const stats = new InspectionStatistics({
628-
maxPerfSamplesInMemory: 50,
629-
maxCompressedStatsInMemory: 5,
630-
});
631-
632-
stats.onIPAddressMatches([
633-
{ key: "known_threat_actors/public_scanners", monitor: true },
634-
]);
635-
stats.onUserAgentMatches([{ key: "ai_data_scrapers", monitor: true }]);
592+
stats.onIPAddressMatches(["known_threat_actors/public_scanners"]);
593+
stats.onUserAgentMatches(["ai_data_scrapers"]);
636594

637595
t.same(stats.getStats(), {
638596
operations: {},
@@ -659,10 +617,8 @@ t.test("it keeps track of monitored IPs and user agents", async () => {
659617
});
660618

661619
// Test multiple occurrences
662-
stats.onIPAddressMatches([
663-
{ key: "known_threat_actors/public_scanners", monitor: true },
664-
]);
665-
stats.onUserAgentMatches([{ key: "ai_data_scrapers", monitor: true }]);
620+
stats.onIPAddressMatches(["known_threat_actors/public_scanners"]);
621+
stats.onUserAgentMatches(["ai_data_scrapers"]);
666622

667623
t.same(stats.getStats(), {
668624
operations: {},
@@ -691,42 +647,6 @@ t.test("it keeps track of monitored IPs and user agents", async () => {
691647
clock.uninstall();
692648
});
693649

694-
t.test("should track multiple matches for the same key", (t) => {
695-
const clock = FakeTimers.install();
696-
697-
const stats = new InspectionStatistics({
698-
maxPerfSamplesInMemory: 100,
699-
maxCompressedStatsInMemory: 10,
700-
});
701-
702-
stats.onIPAddressMatches([
703-
{ key: "known_threat_actors/public_scanners", monitor: true },
704-
{ key: "known_threat_actors/public_scanners", monitor: false },
705-
]);
706-
stats.onUserAgentMatches([
707-
{ key: "ai_data_scrapers", monitor: true },
708-
{ key: "ai_data_scrapers", monitor: false },
709-
]);
710-
711-
const result = stats.getStats();
712-
713-
t.equal(
714-
result.ipAddresses.breakdown["known_threat_actors/public_scanners"].total,
715-
2
716-
);
717-
t.equal(
718-
result.ipAddresses.breakdown["known_threat_actors/public_scanners"].blocked,
719-
1
720-
);
721-
722-
t.equal(result.userAgents.breakdown["ai_data_scrapers"].total, 2);
723-
t.equal(result.userAgents.breakdown["ai_data_scrapers"].blocked, 1);
724-
725-
t.end();
726-
727-
clock.uninstall();
728-
});
729-
730650
t.test("it keeps track of multiple operations of the same kind", async () => {
731651
const clock = FakeTimers.install();
732652

library/agent/InspectionStatistics.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ type UserAgentBotKey = string;
2727
type IPListKey = string;
2828

2929
type UserAgentStats = {
30-
breakdown: Record<UserAgentBotKey, { total: number; blocked: number }>;
30+
breakdown: Record<UserAgentBotKey, number>;
3131
};
3232

3333
type IPAddressStats = {
34-
breakdown: Record<IPListKey, { total: number; blocked: number }>;
34+
breakdown: Record<IPListKey, number>;
3535
};
3636

3737
export class InspectionStatistics {
@@ -111,10 +111,10 @@ export class InspectionStatistics {
111111
};
112112
};
113113
userAgents: {
114-
breakdown: Record<string, { total: number; blocked: number }>;
114+
breakdown: Record<string, number>;
115115
};
116116
ipAddresses: {
117-
breakdown: Record<string, { total: number; blocked: number }>;
117+
breakdown: Record<string, number>;
118118
};
119119
} {
120120
const operations: Record<string, OperationStatsWithoutTimings> = {};
@@ -222,31 +222,23 @@ export class InspectionStatistics {
222222
}
223223
}
224224

225-
onIPAddressMatches(matches: { key: IPListKey; monitor: boolean }[]) {
226-
matches.forEach((match) => {
227-
if (!this.ipAddresses.breakdown[match.key]) {
228-
this.ipAddresses.breakdown[match.key] = { total: 0, blocked: 0 };
225+
onIPAddressMatches(matches: IPListKey[]) {
226+
matches.forEach((key) => {
227+
if (!this.ipAddresses.breakdown[key]) {
228+
this.ipAddresses.breakdown[key] = 0;
229229
}
230230

231-
this.ipAddresses.breakdown[match.key].total += 1;
232-
233-
if (!match.monitor) {
234-
this.ipAddresses.breakdown[match.key].blocked += 1;
235-
}
231+
this.ipAddresses.breakdown[key] += 1;
236232
});
237233
}
238234

239-
onUserAgentMatches(matches: { key: UserAgentBotKey; monitor: boolean }[]) {
240-
matches.forEach((match) => {
241-
if (!this.userAgents.breakdown[match.key]) {
242-
this.userAgents.breakdown[match.key] = { total: 0, blocked: 0 };
235+
onUserAgentMatches(matches: UserAgentBotKey[]) {
236+
matches.forEach((key) => {
237+
if (!this.userAgents.breakdown[key]) {
238+
this.userAgents.breakdown[key] = 0;
243239
}
244240

245-
this.userAgents.breakdown[match.key].total += 1;
246-
247-
if (!match.monitor) {
248-
this.userAgents.breakdown[match.key].blocked += 1;
249-
}
241+
this.userAgents.breakdown[key] += 1;
250242
});
251243
}
252244

0 commit comments

Comments
 (0)