39

所以,到目前为止,似乎没有yarn audit --fix,所以我试图弄清楚如何解决我的yarn audit错误。

我已经尝试过yarn upgrade修复了一些错误(这很好),但仍然存在一些错误。

然后我尝试了yarn add <package>@latest剩余的高漏洞,但它升级了我的版本package.json,当我认为问题来自我正在使用的包的依赖项时。

这是我的一些剩余错误的示例:

┌───────────────┬──────────────────────────────────────────────────────────────┐
│ high          │ Regular Expression Denial of Service                         │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package       │ minimatch                                                    │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in    │ >=3.0.2                                                      │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ gulp                                                         │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path          │ gulp > vinyl-fs > glob-stream > glob > minimatch             │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info     │ https://nodesecurity.io/advisories/118                       │
└───────────────┴──────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ high          │ Regular Expression Denial of Service                         │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package       │ minimatch                                                    │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in    │ >=3.0.2                                                      │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ gulp                                                         │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path          │ gulp > vinyl-fs > glob-stream > minimatch                    │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info     │ https://nodesecurity.io/advisories/118                       │
└───────────────┴──────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ high          │ Regular Expression Denial of Service                         │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package       │ minimatch                                                    │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in    │ >=3.0.2                                                      │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ gulp                                                         │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path          │ gulp > vinyl-fs > glob-watcher > gaze > globule > glob >     │
│               │ minimatch                                                    │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info     │ https://nodesecurity.io/advisories/118                       │
└───────────────┴──────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ high          │ Regular Expression Denial of Service                         │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package       │ minimatch                                                    │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in    │ >=3.0.2                                                      │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ gulp                                                         │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path          │ gulp > vinyl-fs > glob-watcher > gaze > globule > minimatch  │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info     │ https://nodesecurity.io/advisories/118                       │
└───────────────┴──────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ moderate      │ Prototype Pollution                                          │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package       │ lodash                                                       │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in    │ >=4.17.11                                                    │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ gulp                                                         │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path          │ gulp > vinyl-fs > glob-watcher > gaze > globule > lodash     │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info     │ https://nodesecurity.io/advisories/782                       │
└───────────────┴──────────────────────────────────────────────────────────────┘
4

2 回答 2

39

在 yarn 中解决这个问题的方法称为选择性版本解析,它基本上定义resolutionspackage.json.

是依赖项的transitive dependencies依赖项。

{
   "resolutions": { "**/**/lodash": "^4.17.12" }
}

因此,即使 lodash 不是您的包的直接依赖项,您的包中的依赖包也会使用解决方案中定义的版本。也可以提供具体的解决方案。更多信息在这里

于 2019-07-16T11:41:58.903 回答
15

虽然resolutions工作,但它不是最佳解决方案,因为:

  • 你把package.json传递依赖的解决方案弄得一团糟
  • 你用你认为可以工作的版本覆盖实际需要的版本。假设取决于并且您更新 B 并将其解析为. 一段时间后 A 得到更新并需要,但您仍然将 B 解析为,这不再兼容。AB@^4.0.0^4.3.2B@^5.0.0^4.3.2

这是更新传递依赖项的另一种方法:

  1. 删除要从中更新的依赖项的版本yarn.lock
  2. yarn install

通过这种方式,您可以强制 yarn 再次解决依赖关系,并且在大多数情况下,yarn 将安装您从中删除的内容的更新版本yarn.lock

示例:假设您要更新易受攻击minimist@0.0.8的,那么您需要从您的以下条目中删除这样的条目yarn.lock

minimist@0.0.8:
  version "0.0.8"
  resolved "http://10.0.0.1/repository/npm-registry/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
  integrity sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=

然后运行yarn install

如果这没有帮助:

尝试更新依赖链中更高的依赖:

  1. 运行yarn why <dependency>找出哪些包拉它
  2. 上链,尝试从链中删除上层依赖,yarn.lock然后运行yarn install

例子:

这是一个示例,我们在其中更新传递依赖项minimist

$ yarn why minimist
.....
=> Found "mkdirp#minimist@0.0.8"
info This module exists because "eslint#mkdirp" depends on it.
=> Found "optimist#minimist@0.0.10"
info This module exists because "jest#jest-cli#@jest#core#@jest#reporters#istanbul-reports#handlebars#optimist" depends on it.
.....
  1. minimist从 yarn.lock 中删除条目并运行yarn install-> 这没有帮助,大概是因为mkdirpand optimistrequireminimist@0.0.8minimist@0.0.10
  2. minimist从 yarn.lock:mkdirp和中删除“直接父母” optimist
  3. 运行yarn install
  4. 再次运行yarn why minimist

    $ yarn why minimist
    .....
    => Found "mkdirp#minimist@1.2.5"
    info This module exists because "eslint#mkdirp" depends on it.
    => Found "optimist#minimist@0.0.10"
    info This module exists because "jest#jest-cli#@jest#core#@jest#reporters#istanbul-reports#handlebars#optimist" depends on it.
    .....
    

    在这里,我们看到它minimist@0.0.8已更新为minimist@1.2.5,但minimist@0.0.10仍然存在。

  5. 删除依赖链中的下一个依赖yarn.lockhandlebars

  6. yarn install
  7. 运行yarn why minimist- 没有任何改变,minimist@0.0.10仍然存在。
  8. 删除链中的下一个依赖项yarn.lockistanbul-reports
  9. yarn install
  10. 运行yarn why minimistminimist@0.0.10不再存在,因为istanbul-reports已更新。
于 2020-03-24T14:01:38.460 回答