-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathhono-xml-monitored-lists.test.js
More file actions
151 lines (135 loc) · 3.6 KB
/
hono-xml-monitored-lists.test.js
File metadata and controls
151 lines (135 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const t = require("tap");
const { spawn } = require("child_process");
const { resolve } = require("path");
const timeout = require("../timeout");
const pathToApp = resolve(__dirname, "../../sample-apps/hono-xml", "app.js");
const testServerUrl = "http://localhost:5874";
let token;
t.beforeEach(async () => {
const response = await fetch(`${testServerUrl}/api/runtime/apps`, {
method: "POST",
});
const body = await response.json();
token = body.token;
const lists = await fetch(`${testServerUrl}/api/runtime/firewall/lists`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: token,
},
body: JSON.stringify({
blockedIPAddresses: [],
monitoredIPAddresses: ["1.3.2.0/24", "e98c:a7ba:2329:8c69::/64"],
monitoredUserAgents: "monitored-bot",
userAgentDetails: [
{
key: "monitored-bot",
pattern: "monitored-bot",
},
],
}),
});
t.same(lists.status, 200);
});
t.test("it does not block monitored IPs", (t) => {
const server = spawn(`node`, [pathToApp, "4005"], {
env: {
...process.env,
AIKIDO_DEBUG: "true",
AIKIDO_BLOCKING: "true",
AIKIDO_TOKEN: token,
AIKIDO_ENDPOINT: testServerUrl,
},
});
server.on("close", () => {
t.end();
});
server.on("error", (err) => {
t.fail(err);
});
let stdout = "";
server.stdout.on("data", (data) => {
stdout += data.toString();
});
let stderr = "";
server.stderr.on("data", (data) => {
stderr += data.toString();
});
// Wait for the server to start
timeout(2000)
.then(async () => {
// Test IPv4 monitoring
const resp1 = await fetch("http://127.0.0.1:4005/add", {
method: "POST",
body: "<cat><name>Njuska</name></cat>",
headers: {
"Content-Type": "application/xml",
"X-Forwarded-For": "1.3.2.4",
},
signal: AbortSignal.timeout(5000),
});
t.same(resp1.status, 200);
t.same(await resp1.text(), JSON.stringify({ success: true }));
// Test IPv6 monitoring
const resp2 = await fetch("http://127.0.0.1:4005/add", {
method: "POST",
body: "<cat><name>Harry</name></cat>",
headers: {
"Content-Type": "application/xml",
"X-Forwarded-For": "e98c:a7ba:2329:8c69:a13a:8aff:a932:13f2",
},
signal: AbortSignal.timeout(5000),
});
t.same(resp2.status, 200);
t.same(await resp2.text(), JSON.stringify({ success: true }));
})
.catch((error) => {
t.fail(error);
})
.finally(() => {
server.kill();
});
});
t.test("it does not block monitored user agents", (t) => {
const server = spawn(`node`, [pathToApp, "4006"], {
env: {
...process.env,
AIKIDO_DEBUG: "true",
AIKIDO_BLOCKING: "true",
AIKIDO_TOKEN: token,
AIKIDO_ENDPOINT: testServerUrl,
},
});
server.on("close", () => {
t.end();
});
server.on("error", (err) => {
t.fail(err);
});
let stdout = "";
server.stdout.on("data", (data) => {
stdout += data.toString();
});
let stderr = "";
server.stderr.on("data", (data) => {
stderr += data.toString();
});
// Wait for the server to start
timeout(2000)
.then(async () => {
// Test monitored user agent
const resp1 = await fetch("http://127.0.0.1:4006/", {
headers: {
"User-Agent": "monitored-bot",
},
signal: AbortSignal.timeout(5000),
});
t.same(resp1.status, 200);
})
.catch((error) => {
t.fail(error);
})
.finally(() => {
server.kill();
});
});