LoginSignup
0
0

More than 1 year has passed since last update.

npm Scriptで使っているbrowser-syncのセキュリティ脆弱性解消にチャレンジしたときのメモ

Posted at

ウェブ制作のときのscssコンパイルやベンダープレフィクスつけるのに、npm Script を使っています。先輩フリーランスにもらったものを、dartsassにしたくらいは自分で入れましたが、あとはそのまままるっと頂いたものです。browser-syncを使った、ホットリロード機能も備えているので、とても便利に使っています。

ただ、1つ気になるのが、セキュリティの脆弱性の警告が出ることです。
今回は、その解消にチャレンジしたときの記録を記事にしておこうと思います。

問題とその状況

npm auditで状況を確認すると以下の感じ

# npm audit report

async  <2.6.4
Severity: high
Prototype Pollution in async - https://github.com/advisories/GHSA-fwr7-v2mv-hh25
fix available via `npm audit fix --force`
Will install browser-sync@1.9.2, which is a breaking change
node_modules/async
  portscanner  0.1.1 - 2.1.1
  Depends on vulnerable versions of async
  node_modules/portscanner
    browser-sync  <=0.6.2 || >=2.0.0-rc1
    Depends on vulnerable versions of portscanner
    node_modules/browser-sync

3 high severity vulnerabilities

To address all issues (including breaking changes), run:

過去にやったこと

このときはだいたい、npm audit fixまたは'npm audit fix --force'をして下さいとメッセージが出るので、そのとおりにすると、もっとたくさんの警告が出てしまいます。

npm audit fix --force                                                                +[main]
npm WARN using --force Recommended protections disabled.
npm WARN audit Updating browser-sync to 1.9.2,which is a SemVer major change.
npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated graceful-fs@1.2.3: please upgrade to graceful-fs 4 for compatibility with current and future versions of Node.js
npm WARN deprecated json3@3.3.2: Please use the native JSON object instead of JSON 3
npm WARN deprecated minimatch@0.2.14: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated debug@4.1.1: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
npm WARN deprecated axios@0.19.0: Critical security vulnerability fixed in v0.21.1. For more information, see https://github.com/axios/axios/pull/3410

added 95 packages, removed 41 packages, changed 38 packages, and audited 461 packages in 21s

68 packages are looking for funding
  run `npm fund` for details

# npm audit report

async  <2.6.4
Severity: high
Prototype Pollution in async - https://github.com/advisories/GHSA-fwr7-v2mv-hh25
fix available via `npm audit fix --force`
Will install browser-sync@2.27.9, which is a breaking change
node_modules/async
  portscanner  0.1.1 - 2.1.1
  Depends on vulnerable versions of async
  node_modules/portscanner
    portscanner-plus  >=0.1.0
    Depends on vulnerable versions of portscanner
    node_modules/portscanner-plus
      browser-sync  <=2.26.13
      Depends on vulnerable versions of dev-ip
      Depends on vulnerable versions of eazy-logger
      Depends on vulnerable versions of foxy
      Depends on vulnerable versions of glob-watcher
      Depends on vulnerable versions of lodash
      Depends on vulnerable versions of object-path
      Depends on vulnerable versions of opt-merger
      Depends on vulnerable versions of portscanner-plus
      Depends on vulnerable versions of resp-modifier
      Depends on vulnerable versions of socket.io
      Depends on vulnerable versions of tfunk
      node_modules/browser-sync

axios  <=0.21.1
Severity: high
Incorrect Comparison in axios - https://github.com/advisories/GHSA-cph5-m8f7-6c5x
Server-Side Request Forgery in Axios - https://github.com/advisories/GHSA-4w2v-q235-vp99
Depends on vulnerable versions of follow-redirects
fix available via `npm audit fix`
node_modules/axios
  localtunnel  1.9.0 - 2.0.1
  Depends on vulnerable versions of axios
  Depends on vulnerable versions of yargs
  node_modules/localtunnel

debug  <2.6.9
Regular Expression Denial of Service in debug - https://github.com/advisories/GHSA-gxpj-cx7g-858c
fix available via `npm audit fix --force`
Will install browser-sync@2.27.9, which is a breaking change
node_modules/engine.io-client/node_modules/debug
node_modules/engine.io/node_modules/debug
node_modules/socket.io-adapter/node_modules/debug
node_modules/socket.io-client/node_modules/debug
node_modules/socket.io-parser/node_modules/debug
node_modules/socket.io/node_modules/debug
  engine.io  <=4.0.0-alpha.1
  Depends on vulnerable versions of debug
  node_modules/engine.io
    socket.io  <=2.4.1
    Depends on vulnerable versions of debug
    Depends on vulnerable versions of engine.io
    Depends on vulnerable versions of socket.io-parser
    node_modules/socket.io
      browser-sync  <=2.26.13
      Depends on vulnerable versions of dev-ip
      Depends on vulnerable versions of eazy-logger
      Depends on vulnerable versions of foxy
      Depends on vulnerable versions of glob-watcher
      Depends on vulnerable versions of lodash
      Depends on vulnerable versions of object-path
      Depends on vulnerable versions of opt-merger
      Depends on vulnerable versions of portscanner-plus
      Depends on vulnerable versions of resp-modifier
      Depends on vulnerable versions of socket.io
      Depends on vulnerable versions of tfunk
      node_module
・・・・以下けっこう長い😅

で、この後、もう一度'npm audit fix --force'をすると、最初の状況になります。

理解したこと

browser-sync が使用しているportscannerというパッケージのバージョンが古いのでセキュリティ的に危険です!だからなんとかして下さい。
こう言われていると理解しました。

参考記事とやってみたこと

と、いうことは、package.jsonでバージョンを指定すればいいのか?と思ったら、パッケージが依存しているパッケージなので、そこには書いていない。では、package.lock.jsonを書き換えるかなと思って、portscannerを検索すると、8箇所くらいでてきたので、なんとかできそう。でも、これは勝手に作られるから、結局、npm i するときにはもとにもどってしまうのではないだろうか。根本的な解決にはならない。

browser-syncのリポジトリを確認してみた

issueにあがっていました。
async portscanner vulnerability 2.1.1 #1959

issueの最後は次の文章
Could you please update portscanner up to the latest version?

パッケージの上書きができる

ということは、portscannerのバージョンを変更すればこの警告が消えるのでは無いかと考えて、依存関係のパッケージを上書きできないかと検索。
npm override あたりで検索して、以下の記事を発見。
How do I override nested NPM dependency versions?

package.jsonに以下の記述を追加

{
"overrides": {
		"portscanner": "2.2.0"
	}
}

node_modulesを削除して、再度 npm install

結果

結論、結果は変わらずでした。
しかし、package.lock.jsonを検索してみると、中の記述の1つだけ、portscannerのバージョンが2.0.0になっていました。

他のところにもたくさんportscannerの記述があり、その1つ以外は前のバージョンのまま。やはり、browser-syncの中を修正しないと行けないのか、あるいは、他のパッケージの依存関係でも使われているのか。

今回はここで時間切れとします。

なにか認識違いや解決方法などあれば、ご教示お願いします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0