GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: reject cross-site requests to open-editor and invalidate endpoints by bjohansebas · Pull Request #5691 · webpack/webpack-dev-server

fix: reject cross-site requests to open-editor and invalidate endpoints #5691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
bjohansebas merged 2 commits into main from protect-routes
Jul 2, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

fix: reject cross-site requests to open-editor and invalidate endpoints #5691

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
f90b108
fix: reject cross-site requests to open-editor and invalidate endpoints
bjohansebas Jun 30, 2026
f4a545d
fix: reject cross-site requests to internal open-editor and invalidat…
bjohansebas Jun 30, 2026
File filter
Filter by extension


Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

5 changes: 5 additions & 0 deletions 5 .changeset/fix-cross-site-request-forgery.md
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"webpack-dev-server": patch
---

Reject cross-site requests to the internal `open-editor` and `invalidate` endpoints. They performed state-changing actions (opening a file in the editor, forcing a recompilation) on any GET request, so a page the developer visited could trigger them. They now require a same-origin request, validated via `Sec-Fetch-Site` with an `Origin`/`Host` fallback.
42 changes: 42 additions & 0 deletions 42 lib/Server.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,12 @@ class Server {
return;
}

if (!this.#isSameOriginRequest(req)) {
res.statusCode = 403;
res.end("Cross-Origin request blocked");
return;
}

this.invalidate();

res.end();
Expand All @@ -2105,6 +2111,12 @@ class Server {
return;
}

if (!this.#isSameOriginRequest(req)) {
res.statusCode = 403;
res.end("Cross-Origin request blocked");
return;
}

if (!req.url) {
next();
return;
Expand Down Expand Up @@ -3288,6 +3300,36 @@ class Server {
return origin === host;
}

/**
* Determines whether a request was initiated from the dev server's own
* origin, to reject cross-site request forgery on state-changing endpoints.
* @param {Request} req request
* @returns {boolean} true when the request can be trusted as same-origin
*/
#isSameOriginRequest(req) {
const headers =
/** @type {{ [key: string]: string | undefined }} */
(req.headers);

const secFetchSite = headers["sec-fetch-site"];

// Prefer `Sec-Fetch-Site`: browsers send it even for same-origin GET
// `fetch`es (which omit `Origin`), and a cross-site page cannot forge it.
// `none` is a user-initiated navigation.
if (typeof secFetchSite === "string") {
return secFetchSite === "same-origin" || secFetchSite === "none";
}

// Without `Sec-Fetch-*` metadata (non-browser/legacy clients): a request
// with no `Origin` header was not initiated by another origin's script;
// otherwise fall back to the `Origin`/`Host` comparison.
if (typeof headers.origin !== "string") {
return true;
Comment thread
Comment on lines +3325 to +3327

@bjohansebas bjohansebas Jun 30, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I've tested, the browser doesn't send an Origin header in this case

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@bjohansebas bjohansebas Jul 2, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, without this, we could break a simple curl request, which isn't a good idea.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
}

return this.isSameOrigin(headers);
}
Comment thread
bjohansebas marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


/**
* @param {ClientConnection[]} clients clients
* @param {string} type type
Expand Down
16 changes: 8 additions & 8 deletions 16 package-lock.json
Show comments
View file
Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

2 changes: 1 addition & 1 deletion 2 package.json
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"graceful-fs": "^4.2.11",
"http-proxy-middleware": "^4.1.1",
"ipaddr.js": "^2.3.0",
"launch-editor": "^2.13.2",
"launch-editor": "^2.14.1",
"open": "^11.0.0",
"p-retry": "^8.0.0",
"schema-utils": "^4.3.3",
Expand Down
174 changes: 173 additions & 1 deletion 174 test/e2e/cross-origin-request.test.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import http from "node:http";
import { afterEach, describe, it } from "node:test";
import { afterEach, beforeEach, describe, it } from "node:test";
import { expect } from "expect";
import webpack from "webpack";
import Server from "../../lib/Server.js";
Expand Down Expand Up @@ -369,3 +369,175 @@ describe("cross-origin resource policy header", () => {
expect(res.headers["cross-origin-resource-policy"]).toBeUndefined();
});
});

// State-changing internal endpoints must reject cross-site requests so a page
// the developer visits cannot trigger them (CSRF).
describe("cross-site request forgery on state-changing endpoints", () => {
const devServerPort = port1;

let server;

beforeEach(async () => {
const compiler = webpack(config);
server = new Server(
{ port: devServerPort, allowedHosts: "auto" },
compiler,
);

await server.start();
});

afterEach(async () => {
if (server) {
await server.stop();
// Allow the port to be fully released before the next test
await new Promise((resolve) => {
setTimeout(resolve, 100);
});
server = null;
}
});

function request(path, headers = {}) {
return new Promise((resolve, reject) => {
const req = http.get(
`http://localhost:${devServerPort}${path}`,
{ headers },
(res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
resolve({ status: res.statusCode, body });
});
},
);
req.on("error", reject);
});
}

for (const endpoint of [
"/webpack-dev-server/invalidate",
"/webpack-dev-server/open-editor",
]) {
it(`should block cross-site requests to ${endpoint}`, async () => {
const res = await request(endpoint, {
"sec-fetch-mode": "cors",
"sec-fetch-site": "cross-site",
});

expect(res.status).toBe(403);
});

it(`should allow same-origin requests to ${endpoint}`, async () => {
const res = await request(endpoint, {
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
});

expect(res.status).toBe(200);
});

it(`should allow user-initiated navigations to ${endpoint}`, async () => {
const res = await request(endpoint, { "sec-fetch-site": "none" });

expect(res.status).toBe(200);
});
}

it("should block requests with a cross-origin Origin and no Sec-Fetch metadata", async () => {
const res = await request("/webpack-dev-server/invalidate", {
origin: "http://evil.example",
});

expect(res.status).toBe(403);
});

it("should allow requests without Sec-Fetch metadata or Origin (e.g. curl)", async () => {
const res = await request("/webpack-dev-server/invalidate");

expect(res.status).toBe(200);
});
});

// Same as above, but driven by a real browser so the cross-site requests carry
// browser-generated `Sec-Fetch-*` metadata (and the same-origin GET `fetch`
// omits the `Origin` header, exactly like the overlay client).
describe("cross-site request forgery on state-changing endpoints (browser)", () => {
const devServerPort = port1;
const htmlServerPort = port2;
const htmlServerHost = "127.0.0.1";

const openEditorUrl = `http://localhost:${devServerPort}/webpack-dev-server/open-editor`;
const invalidateUrl = `http://localhost:${devServerPort}/webpack-dev-server/invalidate`;

let server;
let htmlServer;
let page;
let browser;

beforeEach(async () => {
const compiler = webpack(config);
server = new Server(
{ port: devServerPort, allowedHosts: "auto" },
compiler,
);

await server.start();

htmlServer = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/html" });
res.end("<!doctype html><html><body></body></html>");
});

await new Promise((resolve) => {
htmlServer.listen(htmlServerPort, htmlServerHost, resolve);
});

({ page, browser } = await runBrowser());
});

afterEach(async () => {
await browser.close();
htmlServer.close();
await server.stop();
});

it("should block a cross-site iframe navigation to /open-editor", async () => {
await page.goto(`http://${htmlServerHost}:${htmlServerPort}/`);

const responsePromise = page.waitForResponse(openEditorUrl);
await page.evaluate((url) => {
const iframe = document.createElement("iframe");
iframe.src = url;
document.body.append(iframe);
}, openEditorUrl);

expect((await responsePromise).status()).toBe(403);
});

it("should block a cross-site iframe navigation to /invalidate", async () => {
await page.goto(`http://${htmlServerHost}:${htmlServerPort}/`);

const responsePromise = page.waitForResponse(invalidateUrl);
await page.evaluate((url) => {
const iframe = document.createElement("iframe");
iframe.src = url;
document.body.append(iframe);
}, invalidateUrl);

expect((await responsePromise).status()).toBe(403);
});

it("should allow the same-origin overlay fetch to /open-editor", async () => {
await page.goto(`http://localhost:${devServerPort}/`);

const responsePromise = page.waitForResponse(openEditorUrl);
await page.evaluate(() => {
fetch("/webpack-dev-server/open-editor").catch(() => {});
});

expect((await responsePromise).status()).toBe(200);
});
});
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.