Skip to content

Commit bcd3ec5

Browse files
committed
Fix tests
1 parent 4973a56 commit bcd3ec5

File tree

8 files changed

+182
-39
lines changed

8 files changed

+182
-39
lines changed

end2end/server/src/handlers/lists.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ module.exports = function lists(req, res) {
1212
const blockedIps = getBlockedIPAddresses(req.app);
1313
const blockedUserAgents = getBlockedUserAgents(req.app);
1414
const allowedIps = getAllowedIPAddresses(req.app);
15+
const monitoredUserAgents = getMonitoredUserAgents(req.app);
16+
const monitoredIps = getMonitoredIPAddresses(req.app);
17+
const userAgentDetails = getUserAgentDetails(req.app);
1518

1619
res.json({
1720
success: true,
@@ -24,20 +27,12 @@ module.exports = function lists(req, res) {
2427
source: "geoip",
2528
description: "geo restrictions",
2629
ips: blockedIps,
27-
monitor: false,
28-
},
29-
]
30-
: [],
31-
blockedUserAgents:
32-
blockedUserAgents.length > 0
33-
? [
34-
{
35-
key: "hackers",
36-
pattern: blockedUserAgents,
37-
monitor: false,
3830
},
3931
]
4032
: [],
33+
blockedUserAgents: blockedUserAgents,
34+
monitoredUserAgents: monitoredUserAgents,
35+
userAgentDetails: userAgentDetails,
4136
allowedIPAddresses:
4237
allowedIps.length > 0
4338
? [
@@ -46,11 +41,19 @@ module.exports = function lists(req, res) {
4641
source: "geoip",
4742
description: "geo restrictions",
4843
ips: allowedIps,
49-
monitor: false,
5044
},
5145
]
5246
: [],
53-
monitoredIPAddresses: [],
54-
monitoredUserAgents: [],
47+
monitoredIPAddresses:
48+
monitoredIps.length > 0
49+
? monitoredIps
50+
: [
51+
{
52+
key: "geoip/Belgium;BE",
53+
source: "geoip",
54+
description: "geo restrictions",
55+
ips: monitoredIps,
56+
},
57+
],
5558
});
5659
};

end2end/server/src/handlers/updateLists.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ const {
22
updateBlockedIPAddresses,
33
updateBlockedUserAgents,
44
updateAllowedIPAddresses,
5+
updateMonitoredUserAgents,
6+
updateMonitoredIPAddresses,
7+
updateUserAgentDetails,
58
} = require("../zen/config");
69

710
module.exports = function updateIPLists(req, res) {
@@ -46,5 +49,26 @@ module.exports = function updateIPLists(req, res) {
4649
updateAllowedIPAddresses(req.app, req.body.allowedIPAddresses);
4750
}
4851

52+
if (
53+
req.body.monitoredUserAgents &&
54+
typeof req.body.monitoredUserAgents === "string"
55+
) {
56+
updateMonitoredUserAgents(req.app, req.body.monitoredUserAgents);
57+
}
58+
59+
if (
60+
req.body.monitoredIPAddresses &&
61+
Array.isArray(req.body.monitoredIPAddresses)
62+
) {
63+
updateMonitoredIPAddresses(req.app, req.body.monitoredIPAddresses);
64+
}
65+
66+
if (
67+
req.body.userAgentDetails &&
68+
typeof req.body.userAgentDetails === "string"
69+
) {
70+
updateUserAgentDetails(req.app, req.body.userAgentDetails);
71+
}
72+
4973
res.json({ success: true });
5074
};

end2end/server/src/zen/config.js

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ function updateAppConfig(app, newConfig) {
4040
const blockedIPAddresses = [];
4141
const blockedUserAgents = [];
4242
const allowedIPAddresses = [];
43+
const monitoredUserAgents = [];
44+
const monitoredIPAddresses = [];
45+
const userAgentDetails = [];
4346

4447
function updateBlockedIPAddresses(app, ips) {
4548
let entry = blockedIPAddresses.find((ip) => ip.serviceId === app.serviceId);
@@ -110,7 +113,79 @@ function getBlockedUserAgents(app) {
110113
return entry.userAgents;
111114
}
112115

113-
return { serviceId: app.serviceId, userAgents: [] };
116+
return "";
117+
}
118+
119+
function updateMonitoredUserAgents(app, uas) {
120+
let entry = monitoredUserAgents.find((e) => e.serviceId === app.serviceId);
121+
122+
if (entry) {
123+
entry.userAgents = uas;
124+
} else {
125+
entry = { serviceId: app.serviceId, userAgents: uas };
126+
monitoredUserAgents.push(entry);
127+
}
128+
129+
// Bump lastUpdatedAt
130+
updateAppConfig(app, {});
131+
}
132+
133+
function getMonitoredUserAgents(app) {
134+
const entry = monitoredUserAgents.find((e) => e.serviceId === app.serviceId);
135+
136+
if (entry) {
137+
return entry.userAgents;
138+
}
139+
140+
return "";
141+
}
142+
143+
function updateMonitoredIPAddresses(app, ips) {
144+
let entry = monitoredIPAddresses.find((e) => e.serviceId === app.serviceId);
145+
146+
if (entry) {
147+
entry.ipAddresses = ips;
148+
} else {
149+
entry = { serviceId: app.serviceId, ipAddresses: ips };
150+
monitoredIPAddresses.push(entry);
151+
}
152+
153+
// Bump lastUpdatedAt
154+
updateAppConfig(app, {});
155+
}
156+
157+
function getMonitoredIPAddresses(app) {
158+
const entry = monitoredIPAddresses.find((e) => e.serviceId === app.serviceId);
159+
160+
if (entry) {
161+
return entry.ipAddresses;
162+
}
163+
164+
return [];
165+
}
166+
167+
function updateUserAgentDetails(app, uas) {
168+
let entry = userAgentDetails.find((e) => e.serviceId === app.serviceId);
169+
170+
if (entry) {
171+
entry.userAgents = uas;
172+
} else {
173+
entry = { serviceId: app.serviceId, userAgents: uas };
174+
userAgentDetails.push(entry);
175+
}
176+
177+
// Bump lastUpdatedAt
178+
updateAppConfig(app, {});
179+
}
180+
181+
function getUserAgentDetails(app) {
182+
const entry = userAgentDetails.find((e) => e.serviceId === app.serviceId);
183+
184+
if (entry) {
185+
return entry.userAgents;
186+
}
187+
188+
return [];
114189
}
115190

116191
module.exports = {
@@ -122,4 +197,10 @@ module.exports = {
122197
getBlockedUserAgents,
123198
getAllowedIPAddresses,
124199
updateAllowedIPAddresses,
200+
updateMonitoredUserAgents,
201+
getMonitoredUserAgents,
202+
updateMonitoredIPAddresses,
203+
getMonitoredIPAddresses,
204+
updateUserAgentDetails,
205+
getUserAgentDetails,
125206
};

library/agent/InspectionStatistics.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,12 @@ t.test("it keeps track of matched IPs and user agents", async () => {
606606
userAgents: {
607607
breakdown: {
608608
// eslint-disable-next-line camelcase
609-
ai_data_scrapers: { total: 1, blocked: 0 },
609+
ai_data_scrapers: 1,
610610
},
611611
},
612612
ipAddresses: {
613613
breakdown: {
614-
"known_threat_actors/public_scanners": { total: 1, blocked: 0 },
614+
"known_threat_actors/public_scanners": 1,
615615
},
616616
},
617617
});
@@ -634,12 +634,12 @@ t.test("it keeps track of matched IPs and user agents", async () => {
634634
userAgents: {
635635
breakdown: {
636636
// eslint-disable-next-line camelcase
637-
ai_data_scrapers: { total: 2, blocked: 0 },
637+
ai_data_scrapers: 2,
638638
},
639639
},
640640
ipAddresses: {
641641
breakdown: {
642-
"known_threat_actors/public_scanners": { total: 2, blocked: 0 },
642+
"known_threat_actors/public_scanners": 2,
643643
},
644644
},
645645
});

library/agent/ServiceConfig.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,25 +368,38 @@ t.test("it clears RegExp when updating with empty pattern", async (t) => {
368368

369369
config.updateBlockedUserAgents("");
370370
config.updateMonitoredUserAgents("");
371+
config.updateUserAgentDetails([]);
371372

372-
t.same(config.getMatchingUserAgentKeys("googlebot"), []);
373+
t.same(config.isMonitoredUserAgent("googlebot"), false);
374+
t.same(config.isUserAgentBlocked("googlebot"), { blocked: false });
373375
t.same(config.getMatchingUserAgentKeys("googlebot"), []);
374376
});
375377

376378
t.test(
377379
"it does not throw error when updating user agent lists with invalid patterns",
378380
async (t) => {
379381
const config = new ServiceConfig([], 0, [], [], false, [], []);
382+
383+
config.updateBlockedUserAgents("googlebot");
384+
config.updateMonitoredUserAgents("googlebot");
385+
config.updateUserAgentDetails([
386+
{
387+
key: "googlebot",
388+
pattern: "googlebot",
389+
},
390+
]);
391+
380392
config.updateBlockedUserAgents("[");
381393
config.updateMonitoredUserAgents("[");
382394
config.updateUserAgentDetails([
383395
{
384396
key: "googlebot",
385-
pattern: "googlebot",
397+
pattern: "[",
386398
},
387399
]);
388400

389-
t.same(config.getMatchingUserAgentKeys("googlebot"), []);
401+
t.same(config.isMonitoredUserAgent("googlebot"), false);
402+
t.same(config.isUserAgentBlocked("googlebot"), { blocked: false });
390403
t.same(config.getMatchingUserAgentKeys("googlebot"), []);
391404
}
392405
);

library/agent/ServiceConfig.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,21 @@ export class ServiceConfig {
162162
return this.monitoredIPAddresses.some((list) => list.list.has(ip));
163163
}
164164

165+
private safeCreateRegExp(pattern: string, flags: string): RegExp | undefined {
166+
try {
167+
return new RegExp(pattern, flags);
168+
} catch {
169+
// Don't throw errors when the regex is invalid
170+
return undefined;
171+
}
172+
}
173+
165174
updateBlockedUserAgents(blockedUserAgents: string) {
166175
if (!blockedUserAgents) {
167176
this.blockedUserAgentRegex = undefined;
168177
return;
169178
}
170-
this.blockedUserAgentRegex = new RegExp(blockedUserAgents, "i");
179+
this.blockedUserAgentRegex = this.safeCreateRegExp(blockedUserAgents, "i");
171180
}
172181

173182
isUserAgentBlocked(ua: string): { blocked: boolean } {
@@ -178,10 +187,16 @@ export class ServiceConfig {
178187
}
179188

180189
private setUserAgentDetails(userAgentDetails: UserAgentDetails[]) {
181-
this.userAgentDetails = userAgentDetails.map((userAgentDetails) => ({
182-
key: userAgentDetails.key,
183-
pattern: new RegExp(userAgentDetails.pattern, "i"),
184-
}));
190+
this.userAgentDetails = [];
191+
for (const detail of userAgentDetails) {
192+
const pattern = this.safeCreateRegExp(detail.pattern, "i");
193+
if (pattern) {
194+
this.userAgentDetails.push({
195+
key: detail.key,
196+
pattern,
197+
});
198+
}
199+
}
185200
}
186201

187202
updateUserAgentDetails(userAgentDetails: UserAgentDetails[]) {
@@ -193,14 +208,17 @@ export class ServiceConfig {
193208
this.monitoredUserAgentRegex = undefined;
194209
return;
195210
}
196-
this.monitoredUserAgentRegex = new RegExp(monitoredUserAgent, "i");
211+
this.monitoredUserAgentRegex = this.safeCreateRegExp(
212+
monitoredUserAgent,
213+
"i"
214+
);
197215
}
198216

199-
isMonitoredUserAgent(ua: string): { monitored: boolean } {
217+
isMonitoredUserAgent(ua: string): boolean {
200218
if (this.monitoredUserAgentRegex) {
201-
return { monitored: this.monitoredUserAgentRegex.test(ua) };
219+
return this.monitoredUserAgentRegex.test(ua);
202220
}
203-
return { monitored: false };
221+
return false;
204222
}
205223

206224
getMatchingUserAgentKeys(ua: string): string[] {

library/sources/HTTPServer.stats.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ wrap(fetchBlockedLists, "fetchBlockedLists", function fetchBlockedLists() {
4343
userAgentDetails: [
4444
{
4545
key: "ai_data_scrapers",
46-
pattern: "GPTBot|Google-Extended",
46+
pattern: "GPTBot",
47+
},
48+
{
49+
key: "google_extended",
50+
pattern: "Google-Extended",
4751
},
4852
],
4953
} satisfies Response;
@@ -99,7 +103,9 @@ t.test("it tracks monitored user agents", async () => {
99103
t.same(stats.userAgents, {
100104
breakdown: {
101105
// eslint-disable-next-line camelcase
102-
ai_data_scrapers: { total: 2, blocked: 0 },
106+
ai_data_scrapers: 1,
107+
// eslint-disable-next-line camelcase
108+
google_extended: 1,
103109
},
104110
});
105111
t.same(stats.ipAddresses, {
@@ -146,7 +152,7 @@ t.test("it tracks monitored IP addresses", async () => {
146152
});
147153
t.same(stats.ipAddresses, {
148154
breakdown: {
149-
"known_threat_actors/public_scanners": { total: 1, blocked: 0 },
155+
"known_threat_actors/public_scanners": 1,
150156
},
151157
});
152158
server.close();
@@ -198,12 +204,12 @@ t.test("it only counts once if multiple listeners", async () => {
198204
t.same(userAgents, {
199205
breakdown: {
200206
// eslint-disable-next-line camelcase
201-
ai_data_scrapers: { total: 2, blocked: 0 },
207+
ai_data_scrapers: 2,
202208
},
203209
});
204210
t.same(ipAddresses, {
205211
breakdown: {
206-
"known_threat_actors/public_scanners": { total: 2, blocked: 0 },
212+
"known_threat_actors/public_scanners": 2,
207213
},
208214
});
209215
server.close();

0 commit comments

Comments
 (0)