-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.test.js
More file actions
324 lines (273 loc) · 12 KB
/
function.test.js
File metadata and controls
324 lines (273 loc) · 12 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { describe, it, expect } from "vitest";
import { handler } from "./function.js";
function makeEvent({ uri = "/", userAgent = "Mozilla/5.0" } = {}) {
const headers = {};
if (userAgent !== null) {
headers["user-agent"] = { value: userAgent };
}
return { request: { uri, headers } };
}
// =====================================================
// Always-allow paths
// =====================================================
describe("always-allow paths", () => {
it("allows /robots.txt", () => {
const event = makeEvent({ uri: "/robots.txt" });
expect(handler(event)).toEqual(event.request);
});
it("allows /ads.txt", () => {
const event = makeEvent({ uri: "/ads.txt" });
expect(handler(event)).toEqual(event.request);
});
it("allows /robots.txt even with a blocked user-agent (always-allow wins)", () => {
const event = makeEvent({ uri: "/robots.txt", userAgent: "CCBot/2.0" });
expect(handler(event)).toEqual(event.request);
});
it("normalises URI whitespace before checking (trim)", () => {
const event = makeEvent({ uri: " /robots.txt " });
expect(handler(event)).toEqual(event.request);
});
it("normalises URI case before checking (lowercase)", () => {
const event = makeEvent({ uri: "/ROBOTS.TXT" });
expect(handler(event)).toEqual(event.request);
});
});
// =====================================================
// /.well-known/traffic-advice — Chrome Private Prefetch Proxy
// =====================================================
describe("/.well-known/traffic-advice", () => {
it("returns 200", () => {
const result = handler(makeEvent({ uri: "/.well-known/traffic-advice" }));
expect(result.statusCode).toBe(200);
});
it("returns application/trafficadvice+json content-type", () => {
const result = handler(makeEvent({ uri: "/.well-known/traffic-advice" }));
expect(result.headers["content-type"].value).toBe("application/trafficadvice+json");
});
it("sets a Traffic-Advice: 1.0 header", () => {
const result = handler(makeEvent({ uri: "/.well-known/traffic-advice" }));
expect(result.headers["traffic-advice"].value).toBe("1.0");
});
it("response body is valid JSON containing prefetch-proxy entry", () => {
const result = handler(makeEvent({ uri: "/.well-known/traffic-advice" }));
const parsed = JSON.parse(result.body);
expect(parsed.some((e) => e.user_agent === "prefetch-proxy")).toBe(true);
});
it("sets a long cache-control header", () => {
const result = handler(makeEvent({ uri: "/.well-known/traffic-advice" }));
expect(result.headers["cache-control"].value).toContain("max-age=");
});
it("sets the permissions-policy header", () => {
const result = handler(makeEvent({ uri: "/.well-known/traffic-advice" }));
expect(result.headers["permissions-policy"].value).toContain("browsing-topics=");
});
});
// =====================================================
// Security scan blocking — PHP files → 404
// =====================================================
describe("PHP file blocking", () => {
it("blocks a .php file at the root", () => {
const result = handler(makeEvent({ uri: "/wp-login.php" }));
expect(result.statusCode).toBe(404);
});
it("blocks a .php file in a sub-directory", () => {
const result = handler(makeEvent({ uri: "/path/to/script.php" }));
expect(result.statusCode).toBe(404);
});
it("PHP block is case-insensitive due to URI normalisation", () => {
const result = handler(makeEvent({ uri: "/Shell.PHP" }));
expect(result.statusCode).toBe(404);
});
it("does not block a path that merely contains 'php' as a substring", () => {
const event = makeEvent({ uri: "/php-info" });
expect(handler(event)).toEqual(event.request);
});
it("blocks a .php5 file", () => {
expect(handler(makeEvent({ uri: "/shell.php5" })).statusCode).toBe(404);
});
it("blocks a .php7 file", () => {
expect(handler(makeEvent({ uri: "/shell.php7" })).statusCode).toBe(404);
});
it("blocks a .phtml file", () => {
expect(handler(makeEvent({ uri: "/page.phtml" })).statusCode).toBe(404);
});
it("blocks a .phar file", () => {
expect(handler(makeEvent({ uri: "/app.phar" })).statusCode).toBe(404);
});
});
// =====================================================
// Security scan blocking — bad folder prefixes → 404
// =====================================================
describe("bad folder blocking", () => {
const cases = [
["/images/logo.png", "images"],
["/image/logo.png", "image (singular)"],
["/img/logo.png", "img"],
["/wp-includes/js/jquery.js", "wp-includes"],
["/static/app.js", "static"],
["/wp/xmlrpc.php", "wp"],
["/wordpress/index.php", "wordpress"],
["/old/site/index.html", "old"],
["/new/site/index.html", "new"],
["/blog/post/1", "blog"],
["/backup/db.sql", "backup"],
["/cgi-bin/test.cgi", "cgi-bin"],
];
it.each(cases)("blocks %s (%s)", (uri) => {
expect(handler(makeEvent({ uri })).statusCode).toBe(404);
});
it("blocks a bad folder path with no trailing content (bare folder)", () => {
expect(handler(makeEvent({ uri: "/cgi-bin" })).statusCode).toBe(404);
});
it("does not block a path that shares a prefix but is a different folder", () => {
// /images2 or /blog-post should NOT be caught — regex anchors with (\/|$)
const event = makeEvent({ uri: "/images2/logo.png" });
expect(handler(event)).toEqual(event.request);
});
it("blocking is case-insensitive due to URI normalisation", () => {
const result = handler(makeEvent({ uri: "/WP-INCLUDES/load.php" }));
expect(result.statusCode).toBe(404);
});
it("blocks /ip (server IP disclosure probe)", () => {
expect(handler(makeEvent({ uri: "/ip" })).statusCode).toBe(404);
});
});
// =====================================================
// Scrapper bot user-agent blocking → 404
// =====================================================
describe("scrapper bot blocking by user-agent", () => {
const blockedAgents = [
[
"Mozilla/5.0 (Linux; Android 7.1.1; MI MAX 2 Build/NMF26F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36 YaApp_Android/10.61 YaSearchBrowser/10.61",
"YaApp_Android full UA",
],
["YaApp_Android/10.61", "YaApp_Android token"],
["YaSearchBrowser/10.61", "YaSearchBrowser token"],
["Seamus The Search Engine/1.0", "Seamus the search engine"],
["DataForSEOBot/1.0", "DataForSEO bot"],
["ev-crawler/1.0", "ev-crawler"],
["Mozilla/5.0 ptst/1.0", "ptst scraper token"],
["Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/120.0.6099.119 Mobile/15E148 Safari/604.1", "Chrome for iOS (CriOS)"],
["Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/120.0 Mobile/15E148 Safari/604.1", "Firefox for iOS (FxiOS)"],
["Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko", "Internet Explorer (Trident)"],
["Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.18", "Opera legacy (Presto)"],
];
it.each(blockedAgents)("blocks '%s' (%s)", (userAgent) => {
const result = handler(makeEvent({ userAgent }));
expect(result.statusCode).toBe(404);
});
it("scrapper bot matching is case-insensitive", () => {
const result = handler(makeEvent({ userAgent: "YAAPP_ANDROID/10.61" }));
expect(result.statusCode).toBe(404);
});
});
// =====================================================
// Null or empty user-agent → 404
// =====================================================
describe("null or empty user-agent blocking", () => {
it("blocks a request with no user-agent header", () => {
const result = handler(makeEvent({ uri: "/about", userAgent: null }));
expect(result.statusCode).toBe(404);
});
it("blocks a request with an empty user-agent value", () => {
const result = handler(makeEvent({ uri: "/about", userAgent: "" }));
expect(result.statusCode).toBe(404);
});
it("blocks a request with a whitespace-only user-agent value", () => {
const result = handler(makeEvent({ uri: "/about", userAgent: " " }));
expect(result.statusCode).toBe(404);
});
it("blocks even for robots.txt when user-agent is absent", () => {
const result = handler(makeEvent({ uri: "/robots.txt", userAgent: null }));
expect(result.statusCode).toBe(404);
});
});
// =====================================================
// Percent-encoded URI bypass prevention
// =====================================================
describe("percent-encoded URI handling", () => {
it("blocks a .php file with an encoded dot (%2E)", () => {
const result = handler(makeEvent({ uri: "/wp-login%2Ephp" }));
expect(result.statusCode).toBe(404);
});
it("blocks a bad folder with an encoded character (%77p-includes)", () => {
const result = handler(makeEvent({ uri: "/%77p-includes/load.php" }));
expect(result.statusCode).toBe(404);
});
it("blocks cgi-bin with an encoded hyphen (%2D)", () => {
const result = handler(makeEvent({ uri: "/cgi%2Dbin/test" }));
expect(result.statusCode).toBe(404);
});
it("returns 404 for a malformed percent-encoded URI", () => {
const result = handler(makeEvent({ uri: "/%zz/path" }));
expect(result.statusCode).toBe(404);
});
});
// =====================================================
// Security scan blocking — .env and .git URIs → 404
// =====================================================
describe(".env and .git URI blocking", () => {
it("blocks /.env", () => {
expect(handler(makeEvent({ uri: "/.env" })).statusCode).toBe(404);
});
it("blocks /.env.local", () => {
expect(handler(makeEvent({ uri: "/.env.local" })).statusCode).toBe(404);
});
it("blocks /config/.env inside a subdirectory", () => {
expect(handler(makeEvent({ uri: "/config/.env" })).statusCode).toBe(404);
});
it("blocks /.git/config", () => {
expect(handler(makeEvent({ uri: "/.git/config" })).statusCode).toBe(404);
});
it("blocks /.git (bare)", () => {
expect(handler(makeEvent({ uri: "/.git" })).statusCode).toBe(404);
});
});
// =====================================================
// Security scan blocking — .sql and .bak extensions → 404
// =====================================================
describe(".sql and .bak file blocking", () => {
it("blocks a .sql file", () => {
expect(handler(makeEvent({ uri: "/dump.sql" })).statusCode).toBe(404);
});
it("blocks a .bak file", () => {
expect(handler(makeEvent({ uri: "/config.bak" })).statusCode).toBe(404);
});
});
// =====================================================
// Security scan blocking — admin folder variants → 404
// =====================================================
describe("admin folder blocking", () => {
const cases = [
["/admin/login", "admin"],
["/administrator/index.php", "administrator"],
["/wp-admin/admin-ajax.php", "wp-admin"],
["/phpmyadmin/index.php", "phpmyadmin"],
["/pma/index.php", "pma"],
];
it.each(cases)("blocks %s (%s)", (uri) => {
expect(handler(makeEvent({ uri })).statusCode).toBe(404);
});
});
// =====================================================
// Always-allow paths with blocked user-agents
// =====================================================
describe("always-allow paths bypass UA checks", () => {
it("allows /ads.txt even with a blocked user-agent", () => {
const event = makeEvent({ uri: "/ads.txt", userAgent: "CCBot/2.0" });
expect(handler(event)).toEqual(event.request);
});
});
// =====================================================
// Pass-through for normal traffic
// =====================================================
describe("pass-through", () => {
it("returns the request object unchanged for a normal path", () => {
const event = makeEvent({ uri: "/about", userAgent: "Mozilla/5.0" });
expect(handler(event)).toEqual(event.request);
});
it("returns the request object unchanged for the root path", () => {
const event = makeEvent({ uri: "/" });
expect(handler(event)).toEqual(event.request);
});
});