纱线升级以修复纱线审计错误

yarn upgrade to fix yarn audit errors

所以,到目前为止,似乎没有 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                       │
└───────────────┴──────────────────────────────────────────────────────────────┘

yarn 中这个问题的解决方案称为 selective version resolutions,它基本上是为 package.json.

中的传递依赖定义 resolutions

transitive dependencies是依赖的依赖。

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

所以这里即使 lodash 不是你包的直接依赖,你包中的依赖包也会使用决议中定义的版本。也可以提供具体的决议。更多信息 here.

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

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

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

  1. yarn.lock
  2. 中删除要更新的依赖版本
  3. 运行 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删除链中的上层依赖然后运行ning 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. 从 yarn.lock 和 运行 yarn install 中删除 minimist 条目 -> 这没有帮助,大概是因为 mkdirpoptimist需要 minimist@0.0.8minimist@0.0.10
  2. 从 yarn.lock 中删除 "direct parents" 的 minimistmkdirpoptimist
  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.lock删除依赖链中的下一个依赖:handlebars

  6. 运行 yarn install
  7. 运行 yarn why minimist - 没有任何变化,minimist@0.0.10 仍然存在。
  8. yarn.lock 中删除链中的下一个依赖项:istanbul-reports
  9. 运行 yarn install
  10. 运行 yarn why minimist: minimist@0.0.10 不存在了,因为 istanbul-reports 已更新。