【问题标题】:How to check for only high vulnerabilities when using "npm audit"?使用“npm audit”时如何只检查高漏洞?
【发布时间】:2018-10-23 03:43:46
【问题描述】:

当我使用新的 npm 6 执行 npm install 时 我收到一条消息,告诉我我有一些漏洞

[!] 发现 75 个漏洞 [审核了 4867 个包]

严重性:66 低 | 4 中等 | 5高

运行npm audit了解更多详情

我运行了npm audit,但得到了一个截断的漏洞列表。

如何仅检查漏洞列表?

谢谢

【问题讨论】:

标签: node.js npm npm-audit


【解决方案1】:

不是您正在寻找的答案,但它会做同样的事情:

npm audit | grep -B 1 -A 10 High

【讨论】:

  • 谢谢,但正如你所说,这不是我要找的,一些High vulns 有一个建议,这个解决方案省略了它们。必须有一个 paramaudit 来过滤结果或至少逐页显示它们
  • 同时您可以尝试调整grep 参数。我认为-B 2 应该包含这些建议。
  • 这个选项现在内置在 npm 中,见stackoverflow.com/a/64312068/20774
【解决方案2】:

这个包可能是你要找的:

https://www.npmjs.com/package/audit-filter

它允许您按咨询编号进行过滤,这比按级别过滤要好。

$ cat .nsprc
{
  "exceptions": [
    "https://npmjs.com/advisories/532",
    "https://npmjs.com/advisories/577"
   ]
}

将其与npm config for audit level 结合起来,您就是金子了。

【讨论】:

    【解决方案3】:

    只计算高点:

    npm audit | grep 'High' | wc -l | rev
    

    【讨论】:

    • 这似乎不需要,因为npm install 已经在末尾列出了这个概述(并且是彩色的!;))
    【解决方案4】:

    这个对我有用:

    仅显示高位

    npm audit | grep -E "(High)" -B3 -A10
    

    同时显示关键和高问题

    npm audit | grep -E "(High | Critical)" -B3 -A10
    

    查看提出此解决方案的 issue 讨论。

    【讨论】:

      【解决方案5】:

      如果您希望在 Powershell 中执行此操作,只需使用以下命令(改编自 @stayingcool 的回答):

      仅显示高位

      npm audit | Select-String -Pattern "High" -Context 0,10
      

      同时显示高和关键

      npm audit | Select-String -Pattern "(High | Critical)" -Context 0,10
      

      【讨论】:

        【解决方案6】:

        编辑:我推荐这个(更好的)答案:https://stackoverflow.com/a/58056454/88111

        它不是那么漂亮,但你可以这样做:

        npm audit --parseable | grep high
        

        另外一个缺点是任何包含"high" 的包/问题元数据也会被打印出来。

        【讨论】:

        • 仅当grep 可用时才有效,例如在 *nix 系统中。
        【解决方案7】:

        --audit-level=high 标志不会改变 npm audit 的输出。

        我将其发送到 html 以用于报告目的,因此希望进一步清理它:

        npm audit | grep -E "(High | Critical)" -B3 -A11 --color=always | grep -E '┌|│|├|└' --color=never
        

        但这会失去标题,以及底部的“发现的漏洞”。我发现最简单的方法是运行几次 npm audit 并将我需要的位附加到文件中。

        最后是这样的:

        npm audit | grep '===' --color=never > temp.txt
        npm audit | grep -E "(High | Critical)" -B3 -A11 --color=never | grep -E '┌|│|├|└' --color=never >> temp.txt
        npm audit | grep -E "(found|scanned packages)" --color=never >> temp.txt
        cat temp.txt
        

        或者作为一个吸引人的衬里(大声笑),它也删除了 temp.txt 文件:

        npm audit | grep '=== npm audit' --color=never > temp.txt; npm audit | grep -E "(High | Critical)" -B3 -A11 --color=never | grep -E '┌|│|├|└' --color=never >> temp.txt; npm audit | grep -E "(found|scanned packages)" --color=never >> temp.txt; cat temp.txt; rm temp.txt;
        

        这条线很丑,但在一堆不同的存储库中运行良好,前提是您只需要终端中的输出。

        输出到文件时,npm audit 包含 ansi 颜色代码,无法关闭。这对我的报告来说是个问题!可以使用 Sed 删除它们:

        sed -i '' $'s,\x1b\\[[0-9;]*[a-zA-Z],,g' temp.txt
        

        【讨论】:

          【解决方案8】:

          将此行放入您的审计脚本中:

          "audit": "level=$(npm audit --parseable | grep -E 'high|critical' | wc -l | rev); [ $level == 0 ] && exit 0"
          

          这段代码确实检查了npm audit 的输出。如果没有高危或危急漏洞,该进程将不会因错误而退出。

          【讨论】:

          • 改进。谢谢
          猜你喜欢
          • 1970-01-01
          • 2020-04-28
          • 2020-07-05
          • 2020-02-15
          • 2019-12-06
          • 2021-09-09
          • 2021-06-30
          • 2018-10-24
          • 2021-08-14
          相关资源
          最近更新 更多