|
15 | 15 |
|
16 | 16 | import { afterEach, describe, expect, test } from "vitest"; |
17 | 17 | import http from "node:http"; |
| 18 | +import net from "node:net"; |
18 | 19 | import { serve } from "../src/adapters/node.ts"; |
19 | 20 | import type { Server } from "../src/types.ts"; |
20 | 21 |
|
@@ -202,3 +203,147 @@ describe("malformed Host header handling", () => { |
202 | 203 | expect(result.body).toContain(String(port)); |
203 | 204 | }); |
204 | 205 | }); |
| 206 | + |
| 207 | +/** |
| 208 | + * RFC 9112 §3.2.2 makes absolute-form (`GET http://host/p HTTP/1.1`) a |
| 209 | + * MUST-accept for origin servers, and §3.3 says its authority supersedes |
| 210 | + * `Host` — but srvx is not a proxy, so the request-line target must not become |
| 211 | + * a way around the guarantees `Host` already gets. These assert the invariants |
| 212 | + * end-to-end on a plaintext listener with the default `trustProxy: false`. |
| 213 | + */ |
| 214 | +describe("absolute-form request target", () => { |
| 215 | + let server: Server; |
| 216 | + |
| 217 | + afterEach(async () => { |
| 218 | + if (server) { |
| 219 | + await server.close(true); |
| 220 | + } |
| 221 | + }); |
| 222 | + |
| 223 | + /** Responses are chunked-encoded on the wire; pull the JSON body out. */ |
| 224 | + function jsonBody(body: string): any { |
| 225 | + return JSON.parse(body.match(/\{.*\}/s)![0]); |
| 226 | + } |
| 227 | + |
| 228 | + /** Send an arbitrary raw request line (http.request rewrites some of these). */ |
| 229 | + function rawLine( |
| 230 | + port: number, |
| 231 | + requestLine: string, |
| 232 | + host = "good.example", |
| 233 | + ): Promise<{ statusCode: number; body: string }> { |
| 234 | + return new Promise((resolve, reject) => { |
| 235 | + const socket = net.connect(port, "127.0.0.1", () => { |
| 236 | + socket.write(`${requestLine}\r\nHost: ${host}\r\nConnection: close\r\n\r\n`); |
| 237 | + }); |
| 238 | + let data = ""; |
| 239 | + socket.on("data", (chunk) => { |
| 240 | + data += chunk.toString(); |
| 241 | + }); |
| 242 | + socket.on("end", () => { |
| 243 | + const statusLine = data.split("\r\n")[0] || ""; |
| 244 | + resolve({ |
| 245 | + statusCode: Number.parseInt(statusLine.split(" ")[1], 10), |
| 246 | + body: data.split("\r\n\r\n").slice(1).join("\r\n\r\n"), |
| 247 | + }); |
| 248 | + }); |
| 249 | + socket.on("error", reject); |
| 250 | + socket.setTimeout(2000, () => { |
| 251 | + socket.destroy(); |
| 252 | + reject(new Error("socket timed out")); |
| 253 | + }); |
| 254 | + }); |
| 255 | + } |
| 256 | + |
| 257 | + async function serveURLInfo(): Promise<number> { |
| 258 | + server = serve({ |
| 259 | + port: 0, |
| 260 | + fetch(request) { |
| 261 | + // `clone()` builds the native Request, which throws on a URL with |
| 262 | + // credentials — a userinfo target used to become an unauthenticated 500. |
| 263 | + request.clone(); |
| 264 | + const url = new URL(request.url); |
| 265 | + return new Response( |
| 266 | + JSON.stringify({ |
| 267 | + url: request.url, |
| 268 | + protocol: url.protocol, |
| 269 | + host: url.host, |
| 270 | + origin: url.origin, |
| 271 | + username: url.username, |
| 272 | + }), |
| 273 | + ); |
| 274 | + }, |
| 275 | + }); |
| 276 | + await server.ready(); |
| 277 | + return getPort(server); |
| 278 | + } |
| 279 | + |
| 280 | + test("cannot claim https: on a plaintext socket", async () => { |
| 281 | + const port = await serveURLInfo(); |
| 282 | + const result = await rawLine(port, "GET https://evil.example.com/x HTTP/1.1"); |
| 283 | + expect(result.statusCode).toBe(200); |
| 284 | + const data = jsonBody(result.body); |
| 285 | + expect(data.protocol).toBe("http:"); |
| 286 | + expect(data.origin).toBe("http://evil.example.com"); |
| 287 | + expect(data.url).toBe("http://evil.example.com/x"); |
| 288 | + }); |
| 289 | + |
| 290 | + test("userinfo is dropped instead of crashing the native Request (500)", async () => { |
| 291 | + const port = await serveURLInfo(); |
| 292 | + const result = await rawLine(port, "GET http://[email protected]/admin HTTP/1.1"); |
| 293 | + expect(result.statusCode).toBe(200); |
| 294 | + const data = jsonBody(result.body); |
| 295 | + expect(data.username).toBe(""); |
| 296 | + expect(data.host).toBe("real.example"); |
| 297 | + expect(data.url).toBe("http://real.example/admin"); |
| 298 | + }); |
| 299 | + |
| 300 | + test("a HOST_RE-invalid request-line authority becomes _invalid_", async () => { |
| 301 | + const port = await serveURLInfo(); |
| 302 | + const result = await rawLine(port, "GET http://evil.example.com./x HTTP/1.1"); |
| 303 | + expect(result.statusCode).toBe(200); |
| 304 | + expect(jsonBody(result.body).host).toBe("_invalid_"); |
| 305 | + }); |
| 306 | + |
| 307 | + test("non-http(s) schemes are rejected with 400", async () => { |
| 308 | + const port = await serveURLInfo(); |
| 309 | + for (const line of [ |
| 310 | + "GET file:///etc/passwd HTTP/1.1", |
| 311 | + "GET file://hehe/x HTTP/1.1", |
| 312 | + "GET ftp://evil.com/ HTTP/1.1", |
| 313 | + "GET zzz://evil/x HTTP/1.1", |
| 314 | + ]) { |
| 315 | + expect((await rawLine(port, line)).statusCode, line).toBe(400); |
| 316 | + } |
| 317 | + }); |
| 318 | + |
| 319 | + test("control: x-forwarded-* stays ignored with trustProxy: false", async () => { |
| 320 | + const port = await serveURLInfo(); |
| 321 | + const result = await new Promise<{ statusCode: number; body: string }>((resolve, reject) => { |
| 322 | + const socket = net.connect(port, "127.0.0.1", () => { |
| 323 | + socket.write( |
| 324 | + "GET /foo HTTP/1.1\r\nHost: good.example\r\n" + |
| 325 | + "X-Forwarded-Host: evil.example.com\r\nX-Forwarded-Proto: https\r\n" + |
| 326 | + "Connection: close\r\n\r\n", |
| 327 | + ); |
| 328 | + }); |
| 329 | + let data = ""; |
| 330 | + socket.on("data", (chunk) => { |
| 331 | + data += chunk.toString(); |
| 332 | + }); |
| 333 | + socket.on("end", () => |
| 334 | + resolve({ |
| 335 | + statusCode: Number.parseInt((data.split("\r\n")[0] || "").split(" ")[1], 10), |
| 336 | + body: data.split("\r\n\r\n").slice(1).join("\r\n\r\n"), |
| 337 | + }), |
| 338 | + ); |
| 339 | + socket.on("error", reject); |
| 340 | + socket.setTimeout(2000, () => { |
| 341 | + socket.destroy(); |
| 342 | + reject(new Error("socket timed out")); |
| 343 | + }); |
| 344 | + }); |
| 345 | + expect(result.statusCode).toBe(200); |
| 346 | + const data = jsonBody(result.body); |
| 347 | + expect(data.url).toBe("http://good.example/foo"); |
| 348 | + }); |
| 349 | +}); |
0 commit comments