当 npm audit fix / update / shrinkwrap 手动更改不起作用时,如何更新 node-sass 的嵌套包 ansi-regex?

How to update nested package ansi-regex for node-sass, when npm audit fix / update / shrinkwrap manual alteration don't work?

下面 npm audit 有完整的细分。

到目前为止,我们已经尝试 npm audit fix 深度,我们已经尝试收缩包装并手动将相关版本号更改为 GitHub 建议的固定版本 (6.0.1)。

npm install 将软件包重置为 5.0.1,即使在手动删除、重新安装等之后也是如此。

下面是 npm 审计的输出。

     ───────────────┬──────────────────────────────────────────────────────────────┐
    │ Moderate      │  Inefficient Regular Expression Complexity in                │
    │               │ chalk/ansi-regex                                             │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ Package       │ ansi-regex                                                   │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ Patched in    │ >=5.0.1                                                      │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ Dependency of │ node-sass                                                    │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ Path          │ node-sass > sass-graph > yargs > string-width > strip-ansi > │
    │               │ ansi-regex                                                   │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ More info     │ https://github.com/advisories/GHSA-93q8-gq69-wqmw            │
    └───────────────┴──────────────────────────────────────────────────────────────┘
    ┌───────────────┬──────────────────────────────────────────────────────────────┐
    │ Moderate      │  Inefficient Regular Expression Complexity in                │
    │               │ chalk/ansi-regex                                             │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ Package       │ ansi-regex                                                   │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ Patched in    │ >=5.0.1                                                      │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ Dependency of │ node-sass                                                    │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ Path          │ node-sass > sass-graph > yargs > cliui > string-width >      │
    │               │ strip-ansi > ansi-regex                                      │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ More info     │ https://github.com/advisories/GHSA-93q8-gq69-wqmw            │
    └───────────────┴──────────────────────────────────────────────────────────────┘
    ┌───────────────┬──────────────────────────────────────────────────────────────┐
    │ Moderate      │  Inefficient Regular Expression Complexity in                │
    │               │ chalk/ansi-regex                                             │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ Package       │ ansi-regex                                                   │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ Patched in    │ >=5.0.1                                                      │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ Dependency of │ node-sass                                                    │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ Path          │ node-sass > sass-graph > yargs > cliui > wrap-ansi >         │
    │               │ string-width > strip-ansi > ansi-regex                       │
    ├───────────────┼──────────────────────────────────────────────────────────────┤
    │ More info     │ https://github.com/advisories/GHSA-93q8-gq69-wqmw            │
    └───────────────┴──────────────────────────────────────────────────────────────┘

我们如何正确更新这个最好的依赖项以避免 npm 审计问题?

老实说,你最好的办法就是选择不去担心这个。 node-sass 大概是开发依赖项,而不是您要运送给用户的东西。您不会意外地包含一个导致 ansi-regex 效率低下 运行 的字符串。即使您这样做了,也不会关闭您的服务器。这将使您的构建管道花费比您想要的更长的时间。

在撰写本文时,没有其他依赖项的 node-sass(最新版本为 6.0.1)的全新安装仍然会导致安装易受攻击的 ansi-regex。因此,您必须进行一些特殊的恶作剧才能解决问题。虽然这些恶作剧对于在您的生产服务器上安装漏洞的东西来说可能是值得的,但在这种情况下这样做可能意味着需要付出大量努力来为非问题的东西创建一个潜在的脆弱修复。

因此,我强烈建议您等待 node-sass 的下一个版本(6.0.2、6.1.0 或 7.0.0 中的一个)并希望它能解决问题,并且如果没有,请不要担心。

您可以在 package.jsonpreinstall 脚本中使用 npm-force-resolutions 包。来自文档:

This packages modifies package-lock.json to force the installation of specific version of a transitive dependency (dependency of dependency)

这正是为我解决问题的方法(在我用头撞墙几天之后):

package.json中:

...
"scripts": {
  "preinstall": "npx npm-force-resolutions"
},
"resolutions": {
  "ansi-regex": "5.0.1"
},
...

那么 npm i 应该可以毫无漏洞地安装。