lifecycle-audit
v1.0.0
Published
Zero-dependency CLI and API to audit npm, pnpm, and yarn dependencies for install/postinstall lifecycle scripts and risky shell payloads.
Maintainers
Readme
lifecycle-audit
Zero-dependency CLI and TypeScript/JavaScript API to audit node_modules, package-lock.json, npm-shrinkwrap.json, and pnpm-lock.yaml for install/uninstall lifecycle scripts (preinstall, install, postinstall, preuninstall, uninstall, postuninstall, and optional prepare) and flag risky shell payloads.
Why?
npm auditonly checks known CVEs—it does not flag a dependency that runscurl ... | bashin apostinstallscript.lifecycle-auditis read-only, has zero runtime dependencies, and works out of the box withnpx.
Quick Start
npx lifecycle-auditFail CI on any unallowed lifecycle script (including low / medium):
npx lifecycle-audit --strictAllow trusted native build packages and output JSON:
npx lifecycle-audit --allow esbuild,sharp --jsonCLI Options
| Flag | Description | Default |
| --- | --- | --- |
| --dir <path> | Target project directory | . |
| --allow <pkg,...> | Allowlisted package name or name@version (repeatable) | [] |
| --fail-on <level> | Exit 1 on unallowed findings: none, high, medium, any | high |
| --strict | Alias for --fail-on any | false |
| --json | Output machine-readable JSON | false |
| --quiet | Hide allowlisted packages in text output | false |
| --include-root | Also audit the root package.json | false |
| --include-prepare | Also include prepare hooks | false |
| --lockfile-only | Skip node_modules and scan lockfiles only | false |
| -v, --version | Show version | — |
| -h, --help | Show help | — |
Allowlist Configuration
You can persist allowlisted packages in .lifecycle-audit.json:
{
"allow": ["esbuild", "[email protected]"]
}Or inside your project's package.json:
{
"lifecycleAudit": {
"allow": ["esbuild"]
}
}Programmatic API
Works in both ESM and CommonJS with full TypeScript definitions:
import { auditLifecycle, analyzeScript, formatReport } from 'lifecycle-audit';
const report = auditLifecycle({
dir: process.cwd(),
allow: ['esbuild'],
failOn: 'high'
});
if (report.shouldFail) {
console.error(formatReport(report));
process.exit(1);
}
console.log(analyzeScript('curl -fsSL https://example.com/install.sh | bash'));
// { risk: 'high', reasons: ['Invokes network transfer or socket utility', 'Pipes output directly into a shell interpreter'] }Risk Levels
high: Network utilities (curl,wget,iwr,fetch,nc), piping into shells (| sh,| bash), base64/hex obfuscation,eval/new Function, or references to sensitive files (.ssh,.aws,.npmrc,id_rsa).medium: Inline code execution (node -e,python -c,sh -c,cmd /c), shell command substitution/piping, background process spawning, or environment inspection.low: Standard native addon builders (node-gyp,prebuild-install,napi-postinstall), local script invocations (node install.js), or benign messages.unknown: Lockfile reportshasInstallScript: trueorrequiresBuild: true, butnode_modulesis not installed locally so the script body cannot be inspected yet.
License
MIT
