-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathFetch.localhost.test.ts
More file actions
179 lines (167 loc) · 4.65 KB
/
Fetch.localhost.test.ts
File metadata and controls
179 lines (167 loc) · 4.65 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
/* eslint-disable prefer-rest-params */
import * as t from "tap";
import { createServer, Server } from "http";
import { Token } from "../agent/api/Token";
import { Context, runWithContext } from "../agent/Context";
import { createTestAgent } from "../helpers/createTestAgent";
import { Fetch } from "./Fetch";
function createContext({
url,
hostHeader,
body,
additionalHeaders = {},
}: {
url: string;
hostHeader: string;
body: unknown;
additionalHeaders?: Record<string, string>;
}): Context {
return {
url: url,
method: "GET",
headers: {
host: hostHeader,
connection: "keep-alive",
"cache-control": "max-age=0",
"sec-ch-ua":
'"Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"macOS"',
"upgrade-insecure-requests": "1",
"user-agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"sec-fetch-site": "none",
"sec-fetch-mode": "navigate",
"sec-fetch-user": "?1",
"sec-fetch-dest": "document",
"accept-encoding": "gzip, deflate, br, zstd",
"accept-language": "nl,en;q=0.9,en-US;q=0.8",
...additionalHeaders,
},
route: "/",
query: {},
source: "express",
routeParams: {},
cookies: {},
remoteAddress: "127.0.0.1",
subdomains: [],
body: body,
};
}
const agent = createTestAgent({
token: new Token("123"),
});
agent.start([new Fetch()]);
const port = 1341;
const serverUrl = `http://localhost:${port}`;
const hostHeader = `localhost:${port}`;
let server: Server;
t.before(async () => {
server = createServer((_, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello World\n");
});
return new Promise<void>((resolve) => {
server.listen(port, resolve);
server.unref();
});
});
t.test(
"it does not block request to localhost with same port",
{ skip: !global.fetch ? "fetch is not available" : false },
async (t) => {
await runWithContext(
createContext({
url: serverUrl,
hostHeader: hostHeader,
body: {},
}),
async () => {
// Server doing a request to itself
const response = await fetch(`${serverUrl}/favicon.ico`);
// The server should respond with a 200
t.same(response.status, 200);
}
);
}
);
t.test(
"it does not block request to localhost with same port using the origin header",
{ skip: !global.fetch ? "fetch is not available" : false },
async (t) => {
await runWithContext(
createContext({
url: serverUrl,
hostHeader: "",
body: {},
additionalHeaders: {
origin: serverUrl,
},
}),
async () => {
// Server doing a request to itself
const response = await fetch(`${serverUrl}/favicon.ico`);
// The server should respond with a 200
t.same(response.status, 200);
}
);
}
);
t.test(
"it does not block request to localhost with same port using the referer header",
{ skip: !global.fetch ? "fetch is not available" : false },
async (t) => {
await runWithContext(
createContext({
url: serverUrl,
hostHeader: "",
body: {},
additionalHeaders: {
referer: serverUrl,
},
}),
async () => {
// Server doing a request to itself
const response = await fetch(`${serverUrl}/favicon.ico`);
// The server should respond with a 200
t.same(response.status, 200);
}
);
}
);
t.test(
"it blocks requests to other ports",
{ skip: !global.fetch ? "fetch is not available" : false },
async (t) => {
const error = await t.rejects(async () => {
await runWithContext(
createContext({
url: `http://localhost:${port + 1}`,
hostHeader: `localhost:${port + 1}`,
body: {
url: `${serverUrl}/favicon.ico`,
},
}),
async () => {
// Server doing a request to localhost but with a different port
// This should be blocked
await fetch(`${serverUrl}/favicon.ico`);
// This should not be called
t.fail();
}
);
});
t.ok(error instanceof Error);
if (error instanceof Error) {
t.same(
error.message,
"Zen has blocked a server-side request forgery: fetch(...) originating from body.url"
);
}
}
);
t.after(() => {
server.close();
});