antongolub/yarn-audit-fix

GitHub: antongolub/yarn-audit-fix

yarn-audit-fix 是一个用于自动修复 Yarn 依赖项漏洞的工具。

Stars: 194 | Forks: 8

Yarn audit fix

yarn-audit-fix

[![CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/11e04a4be5034319.svg)](https://github.com/antongolub/yarn-audit-fix/actions/workflows/ci.yaml) [![Maintainability](https://api.codeclimate.com/v1/badges/1ace18434c46fe1a47fe/maintainability)](https://codeclimate.com/github/antongolub/yarn-audit-fix/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/1ace18434c46fe1a47fe/test_coverage)](https://codeclimate.com/github/antongolub/yarn-audit-fix/test_coverage) [![Sonar](https://sonarcloud.io/api/project_badges/measure?project=antongolub_yarn-audit-fix&metric=alert_status)](https://sonarcloud.io/dashboard?id=antongolub_yarn-audit-fix) [![Known Vulnerabilities](https://snyk.io/test/github/antongolub/yarn-audit-fix/badge.svg)](https://snyk.io/test/github/antongolub/yarn-audit-fix) [![Downloads](https://img.shields.io/npm/dt/yarn-audit-fix)](https://www.npmjs.com/package/yarn-audit-fix) [![npm (tag)](https://img.shields.io/npm/v/yarn-audit-fix)](https://www.npmjs.com/package/yarn-audit-fix) 缺失的 `yarn audit fix` - [摘要](#digest) - [问题](#problem) - [解决方案](#solution) - [主要功能](#key-features) - [入门](#getting-started) - [要求](#requirements) - [安装](#install) - [CLI](#cli) - [ENV](#env) - [JS API](#js-api) - [迁移说明](#migration-notes) - [^11.0.0](#1100) - [^10.0.0](#1000) - [^9.0.0](#900) - [^8.0.0](#800) - [^7.0.0](#700) - [^6.0.0](#600) - [^4.0.0](#400) - [⚠️ 故障排除](#troubleshooting) - [贡献](#contributing) - [许可证](#license) ## 摘要 ### 问题 1. `yarn audit` 检测到漏洞但无法修复。作者建议使用 [Dependabot](https://dependabot.com/) 或 [Snyk](https://snyk.io/),这在许多设置中不方便。讨论:[yarn/issues/7075](https://github.com/yarnpkg/yarn/issues/7075). 2. `yarn audit` 不支持自定义(内部)注册表——参见此 [问题](https://github.com/yarnpkg/yarn/issues/7012) & [PR](https://github.com/yarnpkg/yarn/pull/6484),尚未合并。 ### 解决方案 yarn-audit-fix 通过 `@antongolub/lockfile` 直接修补 `yarn/npm audit --json` 告警和补丁。 (kudos to [G. Kosev](https://github.com/spion), [code reference](https://github.com/hfour/yarn-audit-fix-ng/blob/main/src/index.ts))。 完整描述:[dev.to/yarn-audit-fix-for-yarn-2-berry](https://dev.to/antongolub/the-missing-yarn-audit-fix-for-yarn-2-berry-1p8) ### 主要功能 * 支持野外所有的 yarn 锁文件模式:Yarn 1 Classic,Yarn 2/3 (berry v4–v6) 和 **Yarn 4+** (berry v8/v9/v10),通过 `@antongolub/lockfile` 自动检测。 * 通过直接修补锁文件图来修复漏洞 * macOS / Linux / Windows * CLI 和 JS API * TypeScript 类型定义 #### 锁文件兼容性 | Yarn | 锁文件模式 | 支持 | |-------|---------------------------|:---------:| | 1.x | `yarn-classic` | ✅ | | 2.x | `yarn-berry-v4` | ✅ | | 3.0 | `yarn-berry-v5` | ✅ | | 3.1+ | `yarn-berry-v6` | ✅ | | 4.0–4.13 | `yarn-berry-v8` | ✅ | | 4.14+ | `yarn-berry-v9` | ✅ | | 5.x dev | `yarn-berry-v10` | ✅ | ## 入门 ### 要求 Node.js: `>=16.0.0` ### 安装 ``` yarn add yarn-audit-fix -D ``` 或直接运行: ``` npm_config_yes=true npx yarn-audit-fix ``` ### 命令行界面

$ yarn-audit-fix [--opts]



验证包结构...

准备临时资源...

使用审计数据修补 yarn.lock...

安装依赖更新...

调用 yarn install --update-checksums

[1/4] 🔍  解决包...

[2/4] 🚚  获取包...

[3/4] 🔗  链接依赖...

[4/4] 🔨  重建所有包...

success 保存锁文件。

完成

| 选项                | 描述                                                                                                                                                             | 默认                                    |
|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------|
| `--audit-level`       | 包含定义或更高级别的漏洞。支持的值:low,moderate,high,critical                                                              | `low`                                      |
| `--cwd`               | 当前工作目录                                                                                                                                                     | `process.cwd()`                            |
| `--dry-run`           | 获取审计修复将执行的操作的概览                                                                                                                                   |                                            |
| `--force`             | 让审计修复安装顶级依赖项的 semver-major 更新,而不仅仅是 semver 兼容的更新                                                                   | `false`                                    |
| `--help/-h`           | 打印帮助信息                                                                                                                                                      |                                            |
| `--npm-path`          | 切换到项目的本地 **npm** 版本而不是系统默认版本。或者提供自定义路径。`system / local / `                                         | `system`                                   |
| `--registry`          | 自定义注册表 URL                                                                                                                                                 |                                            |
| `--silent`            | 禁用日志输出                                                                                                                                                      | `false`                                    |
| `--symlink`           | `node_modules` 引用的符号链接类型                                                                                                                                     | `junction` for Windows, `dir` otherwise    |
| `--temp`              | 临时资源的目录                                                                                                                                          | `/node_modules/.cache/yarn-audit-fix` |
| `--verbose`           | 将日志级别切换到详细/调试                                                                                                                                       | `false`                                    |
| `--ignore-engines`    | 在 `yarn install` 上忽略引擎检查                                                                                                                                  | `false`                                    |
| `--exclude`           | 要排除的包的 glob 模式数组                                                                                                                |                                            |
| `--ignore`            | 要在审计报告中忽略的告警 ID 的 glob 模式数组                                                                                                    |                                            |

### 环境变量

任何 CLI 选项都可以通过以 `YAF` 为前缀的 env 变量设置。例如:

* `YAF_FORCE` — `--force`
* `YAF_AUDIT_LEVEL=high` — `--audit-level=high`

### JavaScript API

**yarn-audit-fix** 暴露其内部结构,因此您可以调整步骤或构建自己的流程。

Typedoc:[antongolub.github.io/yarn-audit-fix/modules](https://antongolub.github.io/yarn-audit-fix/modules/)


```
import { run, runSync } from 'yarn-audit-fix'



// NOTE actually it's promisified `run.sync`

await run({

   verbose: true

})



// `runSync` is an alias for `run.sync`

await runSync({

  verbose: true

})
```


单个阶段(`resolveBins`、`patchLockfile`、`yarnInstall`、…)也被导出,因此如果您需要,可以组合自己的管道。

## 迁移说明

### ^11.0.0

**重大变更**:移除了传统的 `convert` 流程,以及 `--flow` 开关(及其基于 `synp` 的双向锁文件转换和已废弃的 `--package-lock-only` / `--legacy-peer-deps` / `--loglevel` / `--only` 标志)。现在只有直接图修补流程。它更易于控制并支持每个 yarn 模式。

使用单个流程,流程抽象本身也消失了:`getFlow`、`TFlow` / `TStage` 类型以及 `run` / `runSync` 的可选自定义流程参数都被移除了。调用 `run(flags)` / `runSync(flags)`——修补管道被内联。如果您想组装自己的,单个阶段仍然被导出。

添加了 Yarn 4+ 的第一类支持 ([#248](https://github.com/antongolub/yarn-audit-fix/issues/248))。定制的 v1/v2 锁文件适配器被替换为 `@antongolub/lockfile`,它自动检测每个 yarn 模式(classic + berry v4–v10)。审计解析器处理 yarn 2/3 的 `{advisories: …}` 形状和 yarn 4 的 NDJSON,当字段不存在时,从 `Vulnerable Versions` 推导 `patched_versions`。条目通过图边重定向而不是就地重写进行修补;合并描述符键(例如,`"lodash@npm:4.17.21, lodash@npm:4.17.20":`)通过后续的 `yarn install` 进行协调。

### ^10.0.0

v10 将包依赖项提升到 NodeJS v14。

### ^9.0.0

v9 添加了实验性的 Yarn 2+ 锁文件支持并更改了锁文件的检测方式(不再通过解析失败进行)。

### ^8.0.0

从 v8 开始,库不再捆绑 **npm**,因此使用系统默认版本。如果需要,您可以通过以下方式:

* 安装所需的 npm 版本并通过 CLI / ENV / JS API 提供自定义路径
* 使用一丁点 **npx** 魔法:`npm_config_yes=true YAF_NPM_PATH=local npx -p yarn-audit-fix -p npm@8 -c yarn-audit-fix`

### ^7.0.0

转换为 ESM 以及其依赖项,因此 v7 中弃用了传统的 `require` API。使用 `import` 代替,或者尝试 [esm-hook](https://www.npmjs.com/package/@qiwi/esm)。CLI 的工作方式与之前相同。


```
// const {run} = require('yarn-audit-fix') turns into

import {run} from 'yarn-audit-fix'
```


### ^6.0.0

默认修复策略 [已更改](https://github.com/antongolub/yarn-audit-fix/releases/tag/v6.0.0) 为直接使用 `yarn audit --json` 数据修补锁文件。之前的 _传统_ `convert` 流程通过 `--flow=convert` 选项进行选择,直到 v11,那时它被完全移除。

### ^4.0.0

`--npm-v7` 标志是多余的。从 v4.0.0 开始,默认使用包自己的 **npm**。您仍然可以使用 `--npm-path=system` 选择系统默认版本,或者使用 `--npm-path=/another/npm/bin` 选择自定义版本。

## 故障排除

### yarn-audit-fix 版本 x.x.x 已过时


```
npm_config_yes=true npx yarn-audit-fix --audit-level=moderate

Runtime digest

yarn-audit-fix version 4.3.6 is out of date. Install the latest 6.0.0 for better results
```


**npx** 缓存之前加载的包,因此您需要以下之一:

1. 锁定版本:`npx yarn-audit-fix@6.0.0`
2. 重置 npx 缓存。在 macOS/Linux 上:`rm -rf ~/.npm/_npx`

### yarn-audit-fix 命令未找到

安装后,二进制文件可能找不到——通常是由于 `$PATH` 问题定位 `node_modules/.bin` ([npm/issues/957](https://github.com/npm/npm/issues/957))。有两种简单的方法可以解决这个问题:

* 通过 **yarn** 运行:`yarn yarn-audit-fix`
* 直接调用脚本:`node_modules/.bin/yarn-audit-fix`

### 对于某些告警没有可用的修复

某些告警无法自动修复——没有已发布的版本满足消费者的声明范围,因此跳过了提升(重新运行带有 `--force` 的选项以应用跨主要更新)。


```
npm_config_yes=true npx yarn-audit-fix --audit-level=moderate
```



```
Patching yarn.lock with audit data...

invoke yarn audit --json --level moderate

Can't find patched version that satisfies postcss@^7.0.0 in >=8.2.10

Can't find patched version that satisfies postcss@^7.0.1 in >=8.2.10

Can't find patched version that satisfies postcss@^7.0.27 in >=8.2.10

Can't find patched version that satisfies ws@^7.2.3 in >=6.2.2 <7.0.0 || >=7.4.6

Upgraded deps: 

invoke yarn install --update-checksums
```


并非所有内容都可以修复。

### 尽管在正确的 node 版本上,也无法安装包

yarn-audit-fix 与支持 ESM 的任何 NodeJS 版本兼容,但嵌套包可以定义自己的引擎要求。


```
pkg-dir@7.0.0: The engine "node" is incompatible with this module. Expected version ">=14.16". Got "14.15.1"
```


_推荐_ 的方法是更新运行时版本。作为临时解决方案,您可以直接传递 `--ignore-engines` 标志。


```
yarn add yarn-audit-fix -D --ignore-engines
```


### 响应代码:400(错误请求)

在某些情况下,**yarn npm audit** 失败是因为 `yarn.lock` 包含格式不可读的传递依赖项:


```
  'example-dependency': 'npm:example-dependency@1.0.0'
```


这导致:


```
invoke yarn npm audit --all --json --recursive

➤ YN0035: Bad Request

➤ YN0035:   Response Code: 400 (Bad Request)

➤ YN0035:   Request Method: POST

➤ YN0035:   Request URL: https://registry.yarnpkg.com/-/npm/v1/security/audits/quick
```


https://github.com/yarnpkg/berry/issues/4117

使用 `exclude` 选项解决:

1. 将项目 **yarn** 更新到 >=3.3.0(早期版本不支持此标志用于 **yarn npm audit**)。
2. 运行 `npx yarn-audit-fix --exclude example-dependency`,这样 **yarn** 在构建审计报告时会跳过 `example-dependency`。

## 贡献

请随意打开任何问题:错误、功能请求或其他问题。

您始终欢迎提出 PR。只需分叉此存储库,编写一些代码,添加一些测试,然后推送您的更改。

任何反馈都受到欢迎。

## 许可证

[MIT](./LICENSE)
标签:Anchore, Dependabot, DNS解析, MITM代理, npm, Snyk, Yarn, 代码审查, 依赖管理, 包管理, 安全防护, 开源项目, 测试覆盖率, 漏洞修复, 维护性, 网络安全培训, 自动化攻击