Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report stats about blocked requests #505

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion end2end/server/src/handlers/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ module.exports = function lists(req, res) {
},
]
: [],
blockedUserAgents: blockedUserAgents,
blockedUserAgentsV2: Array.isArray(blockedUserAgents)
? blockedUserAgents
: [],
allowedIPAddresses:
allowedIps.length > 0
? [
Expand Down
6 changes: 3 additions & 3 deletions end2end/server/src/handlers/updateLists.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ module.exports = function updateIPLists(req, res) {
updateBlockedIPAddresses(req.app, req.body.blockedIPAddresses);

if (
req.body.blockedUserAgents &&
typeof req.body.blockedUserAgents === "string"
req.body.blockedUserAgentsV2 &&
Array.isArray(req.body.blockedUserAgentsV2)
) {
updateBlockedUserAgents(req.app, req.body.blockedUserAgents);
updateBlockedUserAgents(req.app, req.body.blockedUserAgentsV2);
}

if (
Expand Down
7 changes: 6 additions & 1 deletion end2end/tests/hono-xml-blocklists.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ t.beforeEach(async () => {
},
body: JSON.stringify({
blockedIPAddresses: ["1.3.2.0/24", "e98c:a7ba:2329:8c69::/64"],
blockedUserAgents: "hacker|attacker|GPTBot",
blockedUserAgentsV2: [
{
key: "some/key",
pattern: "hacker|attacker|GPTBot",
},
],
}),
});
t.same(lists.status, 200);
Expand Down
22 changes: 20 additions & 2 deletions library/agent/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Wrapper } from "./Wrapper";
import { Context } from "./Context";
import { createTestAgent } from "../helpers/createTestAgent";
import { setTimeout } from "node:timers/promises";
import type { Response } from "./api/fetchBlockedLists";

let shouldOnlyAllowSomeIPAddresses = false;

Expand All @@ -28,22 +29,33 @@ wrap(fetch, "fetch", function mock() {
body: JSON.stringify({
blockedIPAddresses: [
{
key: "some/key",
source: "name",
description: "Description",
ips: ["1.3.2.0/24", "fe80::1234:5678:abcd:ef12/64"],
},
],
blockedUserAgents: "AI2Bot|Bytespider",
blockedUserAgentsV2: [
{
key: "ai",
pattern: "AI2Bot|SomethingElse",
},
{
key: "spider",
pattern: "Bytespider",
},
],
allowedIPAddresses: shouldOnlyAllowSomeIPAddresses
? [
{
key: "some/key",
source: "name",
description: "Description",
ips: ["4.3.2.1"],
},
]
: [],
}),
} satisfies Response),
};
};
});
Expand Down Expand Up @@ -1066,10 +1078,12 @@ t.test("it fetches blocked lists", async () => {
t.same(agent.getConfig().isIPAddressBlocked("1.3.2.4"), {
blocked: true,
reason: "Description",
key: "some/key",
});
t.same(agent.getConfig().isIPAddressBlocked("fe80::1234:5678:abcd:ef12"), {
blocked: true,
reason: "Description",
key: "some/key",
});

t.same(
Expand All @@ -1079,13 +1093,15 @@ t.test("it fetches blocked lists", async () => {
"Mozilla/5.0 (compatible) AI2Bot (+https://www.allenai.org/crawler)"
),
{
key: "ai",
blocked: true,
}
);

t.same(
agent.getConfig().isUserAgentBlocked("Mozilla/5.0 (compatible) Bytespider"),
{
key: "spider",
blocked: true,
}
);
Expand Down Expand Up @@ -1140,10 +1156,12 @@ t.test("it only allows some IP addresses", async () => {
t.same(agent.getConfig().isIPAddressBlocked("1.3.2.4"), {
blocked: true,
reason: "Description",
key: "some/key",
});
t.same(agent.getConfig().isIPAddressBlocked("fe80::1234:5678:abcd:ef12"), {
blocked: true,
reason: "Description",
key: "some/key",
});

t.same(agent.getConfig().isAllowedIPAddress("1.2.3.4"), {
Expand Down
123 changes: 123 additions & 0 deletions library/agent/InspectionStatistics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ t.test("it resets stats", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand All @@ -54,6 +59,11 @@ t.test("it resets stats", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand All @@ -80,6 +90,11 @@ t.test("it keeps track of amount of calls", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand Down Expand Up @@ -112,6 +127,11 @@ t.test("it keeps track of amount of calls", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand Down Expand Up @@ -144,6 +164,11 @@ t.test("it keeps track of amount of calls", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand All @@ -170,6 +195,11 @@ t.test("it keeps track of amount of calls", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand Down Expand Up @@ -202,6 +232,11 @@ t.test("it keeps track of amount of calls", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand Down Expand Up @@ -234,6 +269,11 @@ t.test("it keeps track of amount of calls", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand Down Expand Up @@ -285,6 +325,11 @@ t.test("it keeps track of amount of calls", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand Down Expand Up @@ -332,6 +377,11 @@ t.test("it keeps track of requests", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand All @@ -347,6 +397,11 @@ t.test("it keeps track of requests", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand All @@ -363,6 +418,11 @@ t.test("it keeps track of requests", async () => {
total: 1,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand All @@ -379,6 +439,11 @@ t.test("it keeps track of requests", async () => {
total: 2,
blocked: 1,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand All @@ -396,6 +461,11 @@ t.test("it keeps track of requests", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand All @@ -420,6 +490,11 @@ t.test("it force compresses stats", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

Expand Down Expand Up @@ -462,6 +537,54 @@ t.test("it keeps track of aborted requests", async () => {
total: 0,
blocked: 0,
},
blocked: {
total: 0,
userAgentList: {},
ipBlocklist: {},
},
},
});

clock.uninstall();
});

t.test("it keeps track of blocked requests", async () => {
const clock = FakeTimers.install();

const stats = new InspectionStatistics({
maxPerfSamplesInMemory: 50,
maxCompressedStatsInMemory: 5,
});

stats.onBlockedRequest({
match: "ipBlocklist",
key: "known_threat_actors/public_scanners",
});
stats.onBlockedRequest({ match: "userAgentList", key: "ai_data_scrapers" });

t.same(stats.getStats(), {
sinks: {},
startedAt: 0,
requests: {
total: 0,
aborted: 0,
attacksDetected: {
total: 0,
blocked: 0,
},
blocked: {
total: 2,
ipBlocklist: {
// eslint-disable-next-line camelcase
"known_threat_actors/public_scanners": 1,
},
userAgentList: {
// eslint-disable-next-line camelcase
ai_data_scrapers: 1,
},
},
},
});

clock.uninstall();
});
Loading