【问题标题】:yarn upgrade to fix yarn audit errors纱线升级以修复纱线审计错误
【发布时间】:2019-08-03 13:00:41
【问题描述】:

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

【问题讨论】:

    标签: javascript npm gulp yarnpkg npm-audit


    【解决方案1】:

    虽然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. 运行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. 从 yarn.lock 中删除 minimist 条目并运行 yarn install -> 这没有帮助,大概是因为 mkdirpoptimist 恰好需要 minimist@0.0.8minimist@0.0.10
    2. 从 yarn.lock 中删除 minimist 的“直接父母”:mkdirpoptimist
    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 minimistminimist@0.0.10 不再存在,因为istanbul-reports 已更新。

    【讨论】:

    • 不推荐手动编辑yarn.lock。难道我们不能简单地删除最顶层的库并再次添加它吗?
    • 手动编辑yarn.lock 是最后的手段。你应该删除yarn.lock 并运行yarn install 来生成一个新的。这将根据您的package.json 更新所有依赖项。如果这样的更新破坏了您的项目,那么 package.json 中的版本选择器并不能真正反映您的依赖要求,您应该修复它。
    【解决方案2】:

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

    transitive dependencies是依赖的依赖。

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

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

    【讨论】:

      猜你喜欢
      • 2021-11-09
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      相关资源
      最近更新 更多