CORS localhost requests are considered invalid host and get blocked

オープン
#5,603 コメント 5 件 リアクション 0 件 担当者 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 を使って動作を確認します。localhost を allowedHosts に明示的に追加しなくても、no-cors の cross-site iframe からの localhost リクエストが成功すれば完了です。

索引モデルが 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時間
マージ済み PR(30日)
8

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

webpack/webpack-dev-server のほかの issue

webpack/webpack-dev-server の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。