|
| 1 | +--- |
| 2 | +name: fix-audit-vulnerabilities |
| 3 | +description: 'Identify and fix high-severity npm/pnpm security vulnerabilities in the saved-views monorepo. Use when asked to "fix audit vulnerabilities", "run pnpm audit", "update audit", "fix security issues", "address CVEs", or when security advisories need to be resolved. Runs pnpm audit --audit-level high, diagnoses affected packages, applies dependency overrides or upgrades, then verifies fixes with build and tests.' |
| 4 | +--- |
| 5 | + |
| 6 | +# Fix Audit Vulnerabilities |
| 7 | + |
| 8 | +Workflow for identifying, fixing, and verifying high-severity security vulnerabilities in the saved-views pnpm monorepo. |
| 9 | + |
| 10 | +## When to Use This Skill |
| 11 | + |
| 12 | +- User asks to "fix audit vulnerabilities" or "run pnpm audit" |
| 13 | +- User mentions "security issues", "CVEs", or "dependency vulnerabilities" |
| 14 | +- CI audit check is failing |
| 15 | +- User wants to "update audit" or "resolve security advisories" |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +- pnpm >= 10 installed |
| 20 | +- Node >= 20 |
| 21 | +- Run commands from the **repository root** (`e:\saved-views_1\saved-views\`) |
| 22 | +- On Windows, prefix commands with `cmd /c "cd /d <repo-root> && <command> 2>&1"` |
| 23 | + |
| 24 | +## Step-by-Step Workflow |
| 25 | + |
| 26 | +### Step 1: Run the Audit |
| 27 | + |
| 28 | +Run [run-audit.ps1](./scripts/run-audit.ps1) or execute directly: |
| 29 | + |
| 30 | +```powershell |
| 31 | +Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process |
| 32 | +cd e:\saved-views_1\saved-views |
| 33 | +pnpm audit 2>&1 |
| 34 | +``` |
| 35 | + |
| 36 | +> Note: Run without `--audit-level high` so that moderate vulnerabilities are also visible — they may be fixed just as easily. |
| 37 | +
|
| 38 | +Read the output carefully. Note: |
| 39 | +- **Package name** with the vulnerability |
| 40 | +- **Severity** (high or critical) |
| 41 | +- **Via** chain (which of your direct deps pulled it in) |
| 42 | +- **Fix available** — whether a non-breaking fix exists |
| 43 | + |
| 44 | +### Step 2: Diagnose the Affected Packages |
| 45 | + |
| 46 | +For each vulnerable package, determine which workspace package(s) depend on it: |
| 47 | + |
| 48 | +```powershell |
| 49 | +pnpm why <vulnerable-package> |
| 50 | +``` |
| 51 | + |
| 52 | +> **Caveat:** `pnpm why` only searches the current workspace root, not sub-packages. If it returns nothing, the package is still likely installed as a transitive dep — confirm by checking `pnpm-lock.yaml` directly: |
| 53 | +> ```powershell |
| 54 | +> Select-String -Path pnpm-lock.yaml -Pattern "^ <vulnerable-package>@" |
| 55 | +> ``` |
| 56 | +
|
| 57 | +Check the sub-package `package.json` files in: |
| 58 | +- `packages/saved-views-client/package.json` |
| 59 | +- `packages/saved-views-react/package.json` |
| 60 | +- `packages/test-app-frontend/package.json` |
| 61 | +- `packages/test-app-backend/package.json` |
| 62 | +- Root `package.json` |
| 63 | +
|
| 64 | +### Step 3: Choose a Fix Strategy |
| 65 | +
|
| 66 | +Consult [audit-fix-guide.md](./references/audit-fix-guide.md) for strategy details. |
| 67 | +
|
| 68 | +| Situation | Strategy | |
| 69 | +|-----------|----------| |
| 70 | +| Direct dependency, non-breaking update available | Bump version in the relevant `package.json` | |
| 71 | +| Transitive dependency, fix available | Add `pnpm.overrides` in root `package.json` | |
| 72 | +| No fix available yet | Add `pnpm.overrides` to pin to the least-vulnerable version | |
| 73 | +| Breaking major version bump needed | Update code for compatibility before bumping | |
| 74 | +
|
| 75 | +### Step 4: Apply the Fix |
| 76 | +
|
| 77 | +**Option A — Bump a direct dependency:** |
| 78 | +Edit the relevant `package.json` and update the version range, then: |
| 79 | +```powershell |
| 80 | +cmd /c "cd /d e:\saved-views_1\saved-views && pnpm install 2>&1" |
| 81 | +``` |
| 82 | +
|
| 83 | +**Option B — Add/update a pnpm override** (root `package.json`): |
| 84 | +```json |
| 85 | +{ |
| 86 | + "pnpm": { |
| 87 | + "overrides": { |
| 88 | + "vulnerable-package@<fixed-version": ">=fixed-version" |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | +Then run `pnpm install`. |
| 94 | + |
| 95 | +> **Override key syntax:** Always use comparison-operator ranges on the key (e.g. `"pkg@<1.2.3"`, `"pkg@>=4.0.0 <5.0.5"`) rather than caret/tilde ranges (e.g. `"pkg@^4"`). Caret/tilde ranges in keys may not match correctly. |
| 96 | +> |
| 97 | +> **Override value syntax:** Use `">=fixed-version"` (not a pinned exact version) so future patches are still resolved. For the value, `>=` works correctly even though it looks unbounded — pnpm resolves the minimum satisfying version. |
| 98 | +> |
| 99 | +> **Scope overrides precisely:** If a vulnerability only affects one major version range, restrict the key accordingly (e.g. `"picomatch@>=4.0.0 <4.0.4": "4.0.4"`) rather than a bare `"picomatch"` which would force all consumers, including those on a safe `^2.x` range, to use the overridden version. |
| 100 | +
|
| 101 | +### Step 5: Consider Deleting the Lockfile for a Fresh Resolve |
| 102 | + |
| 103 | +If some overrides don't seem to take effect, delete `pnpm-lock.yaml` and re-install to force a full dependency re-resolution: |
| 104 | +```powershell |
| 105 | +Remove-Item pnpm-lock.yaml |
| 106 | +pnpm install |
| 107 | +``` |
| 108 | +This ensures all overrides are applied from scratch rather than reusing cached resolutions. |
| 109 | + |
| 110 | +### Step 6: Verify the Fix |
| 111 | + |
| 112 | +Re-run the audit to confirm vulnerabilities are resolved: |
| 113 | +```powershell |
| 114 | +pnpm audit 2>&1 |
| 115 | +``` |
| 116 | + |
| 117 | +### Step 7: Try to Reduce Overrides |
| 118 | + |
| 119 | +Before finishing, check whether any override can be eliminated by bumping a direct dependency instead. For each override, trace the chain with `pnpm why` and check if the direct dep that pulls it in has a newer version with the vulnerability fixed: |
| 120 | + |
| 121 | +```powershell |
| 122 | +pnpm view <direct-dep>@latest dependencies --json |
| 123 | +``` |
| 124 | + |
| 125 | +Removing an override is always preferable to keeping one — it means the fix is self-maintaining. |
| 126 | + |
| 127 | +### Step 8: Confirm Nothing is Broken |
| 128 | + |
| 129 | +Run build, tests, and lint to make sure the fix didn't break anything: |
| 130 | +```powershell |
| 131 | +pnpm run build 2>&1 |
| 132 | +pnpm test 2>&1 |
| 133 | +pnpm run lint -- --max-warnings 0 2>&1 |
| 134 | +``` |
| 135 | + |
| 136 | +> **Warning — ESLint plugin upgrades:** If you upgraded `@typescript-eslint/eslint-plugin` or `@typescript-eslint/parser` as part of the fix, major version bumps (e.g. v7 → v8) can remove or rename rules, causing lint to break even if audit passes. Common v8 breakages: |
| 137 | +> - Formatting rules removed: `@typescript-eslint/comma-dangle`, `@typescript-eslint/quotes`, `@typescript-eslint/member-delimiter-style` → replace with base eslint equivalents (`comma-dangle`, `quotes`) and drop `member-delimiter-style` |
| 138 | +> - `@typescript-eslint/ban-types` removed → use `@typescript-eslint/no-empty-object-type` or disable it |
| 139 | +> - `no-unused-expressions` default changed → add `["error", { "allowShortCircuit": true, "allowTernary": true }]` to restore previous behavior |
| 140 | +> - New rules added to `recommended-type-checked` may flag existing code (e.g. `no-duplicate-type-constituents`, `only-throw-error`) |
| 141 | +> |
| 142 | +> Always run lint immediately after any `@typescript-eslint` upgrade and before considering the fix complete. |
| 143 | +
|
| 144 | +If type errors appear, run typecheck for details: |
| 145 | +```powershell |
| 146 | +pnpm run typecheck 2>&1 |
| 147 | +``` |
| 148 | + |
| 149 | +## Important Notes |
| 150 | + |
| 151 | +- **Never use `npm`** — this project blocks npm (`"npm": "<0"` in engines). Always use `pnpm`. |
| 152 | +- **pnpm overrides** live under the `"pnpm"` key in the root `package.json`, not `"resolutions"`. |
| 153 | +- After editing any `package.json`, always run `pnpm install` to regenerate `pnpm-lock.yaml`. |
| 154 | +- If a fix requires a major version bump of a published package (`saved-views-client` or `saved-views-react`), check for API breaking changes before applying. |
| 155 | + |
| 156 | +## References |
| 157 | + |
| 158 | +- [Audit Fix Strategy Guide](./references/audit-fix-guide.md) |
| 159 | +- [Run Audit Script](./scripts/run-audit.ps1) |
| 160 | +- [pnpm audit docs](https://pnpm.io/cli/audit) |
| 161 | +- [pnpm overrides docs](https://pnpm.io/package_json#pnpmoverrides) |
0 commit comments