Skip to content

Commit 6f84655

Browse files
committed
Set symbol to true and add comment
1 parent cf35e65 commit 6f84655

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

library/sources/HTTPServer.stats.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,11 @@ t.test("it only counts once if multiple listeners", async () => {
162162
});
163163

164164
server.on("request", (req, res) => {
165-
if (res.headersSent) {
166-
return;
167-
}
165+
// This is a second listener
166+
});
168167

169-
res.setHeader("Content-Type", "text/plain");
170-
res.end("OK");
168+
server.on("request", (req, res) => {
169+
// This is a third listener
171170
});
172171

173172
await new Promise<void>((resolve) => {
@@ -178,13 +177,15 @@ t.test("it only counts once if multiple listeners", async () => {
178177
method: "GET",
179178
headers: {
180179
"user-agent": "GPTBot",
180+
"x-forwarded-for": "1.2.3.4",
181181
},
182182
timeoutInMS: 500,
183183
}),
184184
fetch({
185185
url: new URL("http://localhost:3329/test"),
186186
method: "GET",
187187
headers: {
188+
"user-agent": "GPTBot",
188189
"x-forwarded-for": "1.2.3.4",
189190
},
190191
timeoutInMS: 500,
@@ -196,12 +197,12 @@ t.test("it only counts once if multiple listeners", async () => {
196197
t.same(userAgents, {
197198
breakdown: {
198199
// eslint-disable-next-line camelcase
199-
ai_data_scrapers: { total: 1, blocked: 0 },
200+
ai_data_scrapers: { total: 2, blocked: 0 },
200201
},
201202
});
202203
t.same(ipAddresses, {
203204
breakdown: {
204-
"known_threat_actors/public_scanners": { total: 1, blocked: 0 },
205+
"known_threat_actors/public_scanners": { total: 2, blocked: 0 },
205206
},
206207
});
207208
server.close();

library/sources/http-server/checkIfRequestIsBlocked.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import { ipAllowedToAccessRoute } from "./ipAllowedToAccessRoute";
88
const checkedBlocks = Symbol("__zen_checked_blocks__");
99

1010
/**
11-
* Inspects the IP address of the request:
11+
* Inspects the IP address and user agent of the request:
1212
* - Whether the IP address is blocked by an IP blocklist (e.g. Geo restrictions)
1313
* - Whether the IP address is allowed to access the current route (e.g. Admin panel)
14+
* - Whether the user agent is blocked by a user agent blocklist
1415
*/
1516
export function checkIfRequestIsBlocked(
1617
res: ServerResponse & { [checkedBlocks]?: boolean },
@@ -34,7 +35,7 @@ export function checkIfRequestIsBlocked(
3435

3536
// We don't need to check again if the request has already been checked
3637
// Also ensures that the statistics are only counted once
37-
// res[checkedBlocklist] = true;
38+
res[checkedBlocks] = true;
3839

3940
if (!ipAllowedToAccessRoute(context, agent)) {
4041
res.statusCode = 403;

0 commit comments

Comments
 (0)