pnpm audit

检查已安装程序包的已知安全问题。

如果发现安全问题,请尝试通过 pnpm update 更新你的依赖项。如果简单的更新不能解决所有问题,请使用 overrides 强制使用不易受攻击的版本。例如,如果 lodash@<2.1.0 易受攻击,可用这个 overrides 来强制使用 lodash@^2.1.0:

pnpm-workspace.yaml
overrides:
  "lodash@<2.1.0": "^2.1.0"

或者运行 pnpm audit --fix。

如果你想容忍一些不影响项目的漏洞,可以使用 audit.ignore 设置。

自 v11 起,pnpm audit 会查询注册表的 /-/npm/v1/security/advisories/bulk 端点。响应中不包含 CVE 标识符,因此公告会按 GitHub 公告 ID (GHSA) 进行筛选。如果你此前在 auditConfig.ignoreCves 下列出了 CVE,请把每个条目替换为 audit.ignore 下对应的 GHSA-xxxx-xxxx-xxxx 值(该值显示在 pnpm audit 输出的 More info 列中)。

The command exits with code 1 when an advisory at or above the audit level remains after ignored advisories are removed. This also applies to --ignore and --ignore-unfixable, which save the IDs first.

命令#

signatures#

添加于:v11.1.0

pnpm audit signatures

验证已安装软件包的 ECDSA 注册表签名是否与每个注册表在 /-/npm/v1/keys 发布的公钥一致。通过 registries 配置的作用域注册源将被遵守;未发布签名密钥的注册表将被跳过。

Signatures are verified against the package integrity recorded in the lockfile. A package without recorded integrity cannot pass verification.

如果任何软件包的签名无效,或者注册源声明了签名密钥,但发布的软件包没有签名,则该命令将以代码 1 退出。结合使用 --json 参数可获得机器可读的输出。

配置项#

--audit-level <severity>#

  • Type: info, low, moderate, high, critical
  • 默认值:low

仅打印严重性大于或等于 "severity" 的警告。

这也可以通过 pnpm-workspace.yaml 中的 audit.level 来设置。

--fix#

Add overrides to the pnpm-workspace.yaml file to force non-vulnerable versions of the dependencies.

使用 --fix=update(在 v11.0.0 中添加)通过更新锁文件中的软件包来修复漏洞,而不是添加覆盖。

当设置了 minimumReleaseAge 时,--fix 还会将每个安全公告的最小补丁版本添加到 pnpm-workspace.yaml 中的 minimumReleaseAgeExclude 中,以便无需等待发布期限窗口即可安装安全修复程序。

--interactive, -i#

添加于:v11.0.0

审查 --fix 选择的建议,并选择要应用的建议。 Implies --fix=override when --fix is not given.

--json#

以 JSON 格式输出审查报告。

patched_versions is null for an advisory with no published fix, so tooling can tell "no fix available" from "fix available at version X".

--dev, -D#

仅审查开发依赖项。

--prod, -P#

仅审查生产依赖项。

--no-optional#

不审查 optionalDependencies。

--ignore-registry-errors#

如果注册源响应了非 200 状态码,则进程应以 0 退出。所以只有当注册源真正成功响应发现的漏洞时,该进程才会执行失败。

--ignore-unfixable#

添加于:v10.11.0

忽略所有没有解决方式的警报。

自 v11 版本起,无法修复的漏洞公告由 GHSA 而非 CVE 进行跟踪。

Their IDs are saved in pnpm-workspace.yaml the same way as with --ignore.

--ignore <vulnerability>#

添加于:v10.11.0

按 GitHub 公告 ID (GHSA) 忽略漏洞。 v11 版本之前,此标志位接受 CVE 标识符。

The ID is saved in pnpm-workspace.yaml, which is created if missing, and no audit report is printed. It is added to audit.ignore if that list is already set, otherwise to auditConfig.ignoreGhsas.

配置#

pnpm audit 在 pnpm-workspace.yaml 的 audit 部分中配置(添加于 v11.16.0):

pnpm-workspace.yaml
audit:
  level: high
  ignore:
    - GHSA-42xw-2xvc-qx8m

audit.level#

  • 默认值:low
  • Type: info, low, moderate, high, critical

仅打印严重程度等于或高于此级别的警告。等同于 --audit-level 标志。

audit.ignore#

会被 pnpm audit 命令忽略的 GHSA 代码列表。

pnpm-workspace.yaml
audit:
  ignore:
    - GHSA-42xw-2xvc-qx8m
    - GHSA-4w2v-q235-vp99
    - GHSA-cph5-m8f7-6c5x
    - GHSA-vh95-rmgr-6w4m

Since v11.26.0, ignored advisories are left out of the vulnerability total and the per-severity counts, and reported separately. When every advisory found is ignored, the summary says so rather than reporting a clean audit.

audit.ignorePrune#

Added in: v11.25.0

  • 默认值: false
  • 类型:Boolean

When true, pnpm audit --fix removes the audit.ignore entries whose GHSA no longer appears in the audit report, so a list of tolerated advisories doesn't accumulate entries for dependencies that are long gone.

pnpm-workspace.yaml
audit:
  ignorePrune: true

提示

在 v11.16.0 之前,这些设置名为 auditLevel 和 auditConfig.ignoreGhsas。这些已废弃的名称在下一个主版本之前仍可工作;两者都设置时,audit 部分优先,并会打印警告。

在 v11 之前,auditConfig.ignoreCves 用于按 CVE 标识符过滤安全公告。该设置已不再被识别。