|
| 1 | +# Dependency Pinning Check 📌 |
| 2 | + |
| 3 | +This security script verifies that all dependencies in `package.json` files use exact version numbers (pinned versions) rather than version ranges. |
| 4 | + |
| 5 | +## Why Pinning Matters |
| 6 | + |
| 7 | +Using version ranges (`^`, `~`, `>=`, etc.) allows package managers to automatically install newer versions, which can introduce: |
| 8 | + |
| 9 | +- **Supply chain attacks**: Malicious code in updated packages |
| 10 | +- **Breaking changes**: Unexpected behavior from dependency updates |
| 11 | +- **Inconsistent builds**: Different environments may install different versions |
| 12 | + |
| 13 | +### Historical Examples: |
| 14 | + |
| 15 | +- `event-stream@3.3.6` - Bitcoin wallet stealer injected via compromised dependency |
| 16 | +- `ua-parser-js@0.7.29` - Cryptominer backdoor in specific version |
| 17 | +- `coa@2.0.3` - Password stealer affecting 9M downloads/week |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Run Locally |
| 22 | + |
| 23 | +```bash |
| 24 | +# From repository root |
| 25 | +pnpm run check-pinned-deps |
| 26 | + |
| 27 | +# Or directly |
| 28 | +node .github/scripts/check-pinned-dependencies.js |
| 29 | +``` |
| 30 | + |
| 31 | +### In CI/CD |
| 32 | + |
| 33 | +The check runs automatically as part of the CI checks matrix (`.github/workflows/ci-checks.yaml`) on: |
| 34 | + |
| 35 | +- All pull requests (opened, synchronized, reopened) |
| 36 | +- Pushes to `changeset-release/main` branch |
| 37 | + |
| 38 | +### Example Output |
| 39 | + |
| 40 | +**All dependencies pinned (✅):** |
| 41 | + |
| 42 | +``` |
| 43 | +Checking for pinned dependencies... |
| 44 | +
|
| 45 | +✓ apps/greenhouse/package.json |
| 46 | +✓ packages/oauth/package.json |
| 47 | +✓ package.json |
| 48 | +
|
| 49 | +====================================================================== |
| 50 | +Dependency Pinning Check Summary |
| 51 | +====================================================================== |
| 52 | +
|
| 53 | +Files checked: 17 |
| 54 | +✓ All dependencies are properly pinned! |
| 55 | +``` |
| 56 | + |
| 57 | +**Unpinned dependencies found (❌):** |
| 58 | + |
| 59 | +``` |
| 60 | +Checking for pinned dependencies... |
| 61 | +
|
| 62 | +✓ apps/greenhouse/package.json |
| 63 | +✗ Found unpinned dependencies |
| 64 | +
|
| 65 | +====================================================================== |
| 66 | +Dependency Pinning Check Summary |
| 67 | +====================================================================== |
| 68 | +
|
| 69 | +Files checked: 17 |
| 70 | +
|
| 71 | +✗ Found 3 unpinned dependencies: |
| 72 | +
|
| 73 | +apps/example/package.json: |
| 74 | + ✗ dependencies.lodash: "^4.17.23" |
| 75 | + ✗ dependencies.react: "~19.0.0" |
| 76 | + ✗ devDependencies.vite: ">=7.0.0" |
| 77 | +
|
| 78 | +ERROR: Unpinned dependencies found! |
| 79 | +
|
| 80 | +To fix: Remove version range prefixes (^, ~, >=, etc.) from package.json |
| 81 | +Example: Change "^1.2.3" to "1.2.3" |
| 82 | +``` |
| 83 | + |
| 84 | +## What's Checked |
| 85 | + |
| 86 | +### Dependency Fields (Checked): |
| 87 | + |
| 88 | +- ✅ `dependencies` |
| 89 | +- ✅ `devDependencies` |
| 90 | +- ✅ `optionalDependencies` |
| 91 | + |
| 92 | +### Fields Skipped: |
| 93 | + |
| 94 | +- ⏭️ `peerDependencies` (ranges expected here) |
| 95 | +- ⏭️ `peerDependenciesMeta` |
| 96 | + |
| 97 | +### Allowed Patterns: |
| 98 | + |
| 99 | +- `workspace:*` - Monorepo workspace dependencies |
| 100 | +- `npm:` - npm protocol references |
| 101 | +- `file:` - Local file dependencies |
| 102 | +- `link:` - Linked dependencies |
| 103 | +- `patch:` - Patched dependencies (pnpm) |
| 104 | + |
| 105 | +### Unpinned Patterns (Blocked): |
| 106 | + |
| 107 | +- `^1.2.3` - Caret (allows minor/patch updates) |
| 108 | +- `~1.2.3` - Tilde (allows patch updates) |
| 109 | +- `>=1.2.0` - Greater than or equal |
| 110 | +- `<=2.0.0` - Less than or equal |
| 111 | +- `>1.0.0` - Greater than |
| 112 | +- `<3.0.0` - Less than |
| 113 | +- `*` - Any version |
| 114 | + |
| 115 | +## Configuration |
| 116 | + |
| 117 | +Edit `.github/scripts/check-pinned-dependencies.js` to customize: |
| 118 | + |
| 119 | +```javascript |
| 120 | +const config = { |
| 121 | + // Allow specific packages to have ranges |
| 122 | + allowedUnpinnedPackages: ["some-internal-package"], |
| 123 | + |
| 124 | + // Ignore specific packages completely |
| 125 | + ignorePackages: ["example-package"], |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +## Fixing Unpinned Dependencies |
| 130 | + |
| 131 | +### Automatic Fix (Recommended): |
| 132 | + |
| 133 | +Add to `.npmrc`: |
| 134 | + |
| 135 | +```ini |
| 136 | +save-exact=true |
| 137 | +``` |
| 138 | + |
| 139 | +This ensures new packages are automatically pinned when installed. |
| 140 | + |
| 141 | +### Manual Fix: |
| 142 | + |
| 143 | +```json |
| 144 | +{ |
| 145 | + "dependencies": { |
| 146 | + "lodash": "^4.17.23", // ❌ Unpinned |
| 147 | + "lodash": "4.17.23" // ✅ Pinned |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +### Update All Dependencies: |
| 153 | + |
| 154 | +```bash |
| 155 | +# 1. Remove version prefixes from package.json |
| 156 | +# 2. Regenerate lock file |
| 157 | +pnpm install |
| 158 | + |
| 159 | +# Or use sed to remove prefixes automatically |
| 160 | +sed -i '' 's/"\^/"/g' package.json |
| 161 | +sed -i '' 's/"~/"/g' package.json |
| 162 | +``` |
| 163 | + |
| 164 | +## Additional Security |
| 165 | + |
| 166 | +This script is part of a defense-in-depth strategy: |
| 167 | + |
| 168 | +1. **Pinned dependencies** (this script) - Prevent auto-updates |
| 169 | +2. **Lock file** (`pnpm-lock.yaml`) - Lock transitive dependencies |
| 170 | +3. **`--frozen-lockfile`** - Prevent lock file changes in CI |
| 171 | +4. **npm audit** - Check for known vulnerabilities |
| 172 | +5. **Dependabot/Renovate** - Automated, reviewed updates |
| 173 | +6. **Socket.dev/Snyk** - Supply chain monitoring |
| 174 | + |
| 175 | +## Related Files |
| 176 | + |
| 177 | +- `.github/workflows/ci-checks.yaml` - Integrated into CI checks matrix (line 80-81) |
| 178 | +- `.npmrc` - Contains `save-exact=true` for auto-pinning |
| 179 | +- `pnpm-lock.yaml` - Locks all transitive dependencies |
| 180 | + |
| 181 | +## References |
| 182 | + |
| 183 | +- [npm docs: save-exact](https://docs.npmjs.com/cli/v10/using-npm/config#save-exact) |
| 184 | +- [pnpm: Pinning dependencies](https://pnpm.io/continuous-integration#pinning-dependencies) |
| 185 | +- [OWASP: Supply Chain Security](https://owasp.org/www-community/vulnerabilities/Supply_Chain_Attack) |
0 commit comments