CORS localhost requests are considered invalid host and get blocked

未关闭
#5,603 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Jeston10 已经在做这个了。

开始于 2026年1月25日。

  • #5627 来自 @VeerRaj-007 —— 已关闭,未合并
  • #5636 来自 @Jeston10 —— 已关闭,未合并

评估

难度
3/5
预计耗时
1-2 天
新手友好度
52/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
javascript, node.js, webpack
领域
backend, tooling

调研方向

从 lib/Server.js 中的 cross-origin-header-check 中间件开始,然后跟踪 isValidHost、isHostAllowed 以及 issue 中提到的 validateHost 路径。使用链接的最小复现及其 README 验证行为;当来自 no-cors 跨站 iframe 的 localhost 请求能够成功,且无需将 localhost 显式添加到 allowedHosts 时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

Bug Description

If webpack-dev-server gets "no-cors cross-site" requests, localhost is not considered an allowed host and a 403 Forbidden is returned.

By explicitly adding allowedHosts: ['localhost'] to the webpack config, the bug is circumvented.

Link to Minimal Reproduction and step to reproduce

In the following project, an iframe is constructed from a blob HTML string. The iframe contains a script tag which refers to a resource URL which is hosted by webpack dev server.

https://github.com/RedMser/bug-webpack-dev-server-corb

See the readme in the repo for reproduction steps.

Expected Behavior

Requests to localhost should succeed, even if they originate from a "no-cors cross-site" context.

Actual Behavior

The request fails with a 403 Forbidden response.

Image
Environment
System:
    OS: Windows 11 10.0.26100
    CPU: (4) x64 Intel(R) Xeon(R) Platinum 8462Y+
    Memory: 2.84 GB / 19.00 GB
  Binaries:
    Node: 24.11.1 - C:\nvm4w\nodejs\node.EXE
    npm: 11.6.2 - C:\nvm4w\nodejs\npm.CMD
  Browsers:
    Chrome: 141.0.7390.123
    Edge: Chromium (141.0.3537.57)
    Firefox: 140.5.0 - C:\Program Files\Mozilla Firefox\firefox.exe
    Internet Explorer: 11.0.26100.1882
  Packages:
    webpack: ^5.102.1 => 5.102.1
    webpack-cli: ^6.0.1 => 6.0.1
    webpack-dev-server: ^5.2.2 => 5.2.2
Is this a regression?

None

Last Working Version

No response

Additional Context

Responsible is the cross-origin-header-check middleware.
In particular, isValidHost calls isHostAllowed which contains following comment:

https://github.com/webpack/webpack-dev-server/blob/3f3d124bc656197d83a57a422792d80d25137c49/lib/Server.js#L3143-L3145

According to this outdated comment, localhost should be always allowed, but there is no code that actually ensures this here!
isValidHost does check for localhost among other things, but validateHost == false in this particular instance.

It might be caused by the refactor in 6045b1e9d63078fb24cac52eb361b7356944cddd but I did not investigate further. This used to work in the past so it's likely a regression, let me know if more info is needed.

主要语言
JavaScript
星标
7.9k
派生
1.5k
平均合并
2 天 12 小时
30 天内合并 PR
8

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

webpack/webpack-dev-server 的其他 Issue

查看 webpack/webpack-dev-server 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。