|
1 | 1 | import http from "node:http"; |
2 | | -import { afterEach, describe, it } from "node:test"; |
| 2 | +import { afterEach, beforeEach, describe, it } from "node:test"; |
3 | 3 | import { expect } from "expect"; |
4 | 4 | import webpack from "webpack"; |
5 | 5 | import Server from "../../lib/Server.js"; |
@@ -369,3 +369,175 @@ describe("cross-origin resource policy header", () => { |
369 | 369 | expect(res.headers["cross-origin-resource-policy"]).toBeUndefined(); |
370 | 370 | }); |
371 | 371 | }); |
| 372 | + |
| 373 | +// State-changing internal endpoints must reject cross-site requests so a page |
| 374 | +// the developer visits cannot trigger them (CSRF). |
| 375 | +describe("cross-site request forgery on state-changing endpoints", () => { |
| 376 | + const devServerPort = port1; |
| 377 | + |
| 378 | + let server; |
| 379 | + |
| 380 | + beforeEach(async () => { |
| 381 | + const compiler = webpack(config); |
| 382 | + server = new Server( |
| 383 | + { port: devServerPort, allowedHosts: "auto" }, |
| 384 | + compiler, |
| 385 | + ); |
| 386 | + |
| 387 | + await server.start(); |
| 388 | + }); |
| 389 | + |
| 390 | + afterEach(async () => { |
| 391 | + if (server) { |
| 392 | + await server.stop(); |
| 393 | + // Allow the port to be fully released before the next test |
| 394 | + await new Promise((resolve) => { |
| 395 | + setTimeout(resolve, 100); |
| 396 | + }); |
| 397 | + server = null; |
| 398 | + } |
| 399 | + }); |
| 400 | + |
| 401 | + function request(path, headers = {}) { |
| 402 | + return new Promise((resolve, reject) => { |
| 403 | + const req = http.get( |
| 404 | + `http://localhost:${devServerPort}${path}`, |
| 405 | + { headers }, |
| 406 | + (res) => { |
| 407 | + let body = ""; |
| 408 | + res.on("data", (chunk) => { |
| 409 | + body += chunk; |
| 410 | + }); |
| 411 | + res.on("end", () => { |
| 412 | + resolve({ status: res.statusCode, body }); |
| 413 | + }); |
| 414 | + }, |
| 415 | + ); |
| 416 | + req.on("error", reject); |
| 417 | + }); |
| 418 | + } |
| 419 | + |
| 420 | + for (const endpoint of [ |
| 421 | + "/webpack-dev-server/invalidate", |
| 422 | + "/webpack-dev-server/open-editor", |
| 423 | + ]) { |
| 424 | + it(`should block cross-site requests to ${endpoint}`, async () => { |
| 425 | + const res = await request(endpoint, { |
| 426 | + "sec-fetch-mode": "cors", |
| 427 | + "sec-fetch-site": "cross-site", |
| 428 | + }); |
| 429 | + |
| 430 | + expect(res.status).toBe(403); |
| 431 | + }); |
| 432 | + |
| 433 | + it(`should allow same-origin requests to ${endpoint}`, async () => { |
| 434 | + const res = await request(endpoint, { |
| 435 | + "sec-fetch-mode": "cors", |
| 436 | + "sec-fetch-site": "same-origin", |
| 437 | + }); |
| 438 | + |
| 439 | + expect(res.status).toBe(200); |
| 440 | + }); |
| 441 | + |
| 442 | + it(`should allow user-initiated navigations to ${endpoint}`, async () => { |
| 443 | + const res = await request(endpoint, { "sec-fetch-site": "none" }); |
| 444 | + |
| 445 | + expect(res.status).toBe(200); |
| 446 | + }); |
| 447 | + } |
| 448 | + |
| 449 | + it("should block requests with a cross-origin Origin and no Sec-Fetch metadata", async () => { |
| 450 | + const res = await request("/webpack-dev-server/invalidate", { |
| 451 | + origin: "http://evil.example", |
| 452 | + }); |
| 453 | + |
| 454 | + expect(res.status).toBe(403); |
| 455 | + }); |
| 456 | + |
| 457 | + it("should allow requests without Sec-Fetch metadata or Origin (e.g. curl)", async () => { |
| 458 | + const res = await request("/webpack-dev-server/invalidate"); |
| 459 | + |
| 460 | + expect(res.status).toBe(200); |
| 461 | + }); |
| 462 | +}); |
| 463 | + |
| 464 | +// Same as above, but driven by a real browser so the cross-site requests carry |
| 465 | +// browser-generated `Sec-Fetch-*` metadata (and the same-origin GET `fetch` |
| 466 | +// omits the `Origin` header, exactly like the overlay client). |
| 467 | +describe("cross-site request forgery on state-changing endpoints (browser)", () => { |
| 468 | + const devServerPort = port1; |
| 469 | + const htmlServerPort = port2; |
| 470 | + const htmlServerHost = "127.0.0.1"; |
| 471 | + |
| 472 | + const openEditorUrl = `http://localhost:${devServerPort}/webpack-dev-server/open-editor`; |
| 473 | + const invalidateUrl = `http://localhost:${devServerPort}/webpack-dev-server/invalidate`; |
| 474 | + |
| 475 | + let server; |
| 476 | + let htmlServer; |
| 477 | + let page; |
| 478 | + let browser; |
| 479 | + |
| 480 | + beforeEach(async () => { |
| 481 | + const compiler = webpack(config); |
| 482 | + server = new Server( |
| 483 | + { port: devServerPort, allowedHosts: "auto" }, |
| 484 | + compiler, |
| 485 | + ); |
| 486 | + |
| 487 | + await server.start(); |
| 488 | + |
| 489 | + htmlServer = http.createServer((req, res) => { |
| 490 | + res.writeHead(200, { "Content-Type": "text/html" }); |
| 491 | + res.end("<!doctype html><html><body></body></html>"); |
| 492 | + }); |
| 493 | + |
| 494 | + await new Promise((resolve) => { |
| 495 | + htmlServer.listen(htmlServerPort, htmlServerHost, resolve); |
| 496 | + }); |
| 497 | + |
| 498 | + ({ page, browser } = await runBrowser()); |
| 499 | + }); |
| 500 | + |
| 501 | + afterEach(async () => { |
| 502 | + await browser.close(); |
| 503 | + htmlServer.close(); |
| 504 | + await server.stop(); |
| 505 | + }); |
| 506 | + |
| 507 | + it("should block a cross-site iframe navigation to /open-editor", async () => { |
| 508 | + await page.goto(`http://${htmlServerHost}:${htmlServerPort}/`); |
| 509 | + |
| 510 | + const responsePromise = page.waitForResponse(openEditorUrl); |
| 511 | + await page.evaluate((url) => { |
| 512 | + const iframe = document.createElement("iframe"); |
| 513 | + iframe.src = url; |
| 514 | + document.body.append(iframe); |
| 515 | + }, openEditorUrl); |
| 516 | + |
| 517 | + expect((await responsePromise).status()).toBe(403); |
| 518 | + }); |
| 519 | + |
| 520 | + it("should block a cross-site iframe navigation to /invalidate", async () => { |
| 521 | + await page.goto(`http://${htmlServerHost}:${htmlServerPort}/`); |
| 522 | + |
| 523 | + const responsePromise = page.waitForResponse(invalidateUrl); |
| 524 | + await page.evaluate((url) => { |
| 525 | + const iframe = document.createElement("iframe"); |
| 526 | + iframe.src = url; |
| 527 | + document.body.append(iframe); |
| 528 | + }, invalidateUrl); |
| 529 | + |
| 530 | + expect((await responsePromise).status()).toBe(403); |
| 531 | + }); |
| 532 | + |
| 533 | + it("should allow the same-origin overlay fetch to /open-editor", async () => { |
| 534 | + await page.goto(`http://localhost:${devServerPort}/`); |
| 535 | + |
| 536 | + const responsePromise = page.waitForResponse(openEditorUrl); |
| 537 | + await page.evaluate(() => { |
| 538 | + fetch("/webpack-dev-server/open-editor").catch(() => {}); |
| 539 | + }); |
| 540 | + |
| 541 | + expect((await responsePromise).status()).toBe(200); |
| 542 | + }); |
| 543 | +}); |
0 commit comments