prompt-cop
v2.0.0
Published
A lightweight security tool to detect potential prompt injection vulnerabilities in code files
Maintainers
Readme
Prompt Cop
A light weight library prompt-cop scans text files in your project for potential prompt injection vulnerabilities.
Use it from the command line or as a library in your tooling.
Quick Terminal Demo
Prompt Injection Example in GPT
Features
- Scan files or directories recursively
- Works with Markdown, YAML, JSON, JS/TS, and more
- Detect hidden comments, obfuscation, Unicode tricks, and other injection patterns
- Optional Hugging Face AI detection of prompt injections
- Output results as color-coded text or JSON
- Customize include/exclude patterns and severity filtering
Installation
Requires Node.js 14 or higher.
npm install -g prompt-copOr as a development dependency:
npm install --save-dev prompt-copAI Detection Setup (Optional)
To enable AI-powered detection using Hugging Face models:
- Sign up for a free account at huggingface.co
- Generate an access token at Settings > Access Tokens
- Set the environment variable:
export HF_ACCESS_TOKEN=hf_your_token_here - Use the
--aiflag orai: trueoption to enable AI detection
Usage
Command Line Interface (CLI)
Basic usage:
prompt-cop ./srcScan a specific file:
prompt-cop README.mdAdvanced options:
# Output as JSON
prompt-cop ./src --json
# Only show medium and high severity issues
prompt-cop ./src --severity medium
# Include only specific file types
prompt-cop ./src --include .md .yml
# Exclude directories
prompt-cop . --exclude node_modules dist
# Non-recursive scan
prompt-cop ./src --no-recursive
# Use AI detection (requires HF_ACCESS_TOKEN environment variable)
prompt-cop ./src --aiCLI Options
-r, --no-recursive- Do not scan directories recursively-j, --json- Output results as JSON-i, --include <extensions...>- File extensions to include (e.g., .md .yml)-e, --exclude <patterns...>- Patterns to exclude (e.g., node_modules)-s, --severity <level>- Minimum severity level to report (low, medium, high)-a, --ai- Use Hugging Face model for detection (requires HF_ACCESS_TOKEN)
Programmatic API
const { scan, scanContent, scanContentAI, SEVERITY } = require('prompt-cop');
// Scan a file or directory
async function checkVulnerabilities() {
try {
const results = await scan('./src', {
recursive: true,
exclude: ['node_modules', 'dist'],
include: ['.md', '.yml'],
json: true,
ai: true
});
console.log(`Files scanned: ${results.filesScanned}`);
console.log(`Vulnerabilities found: ${results.vulnerabilities.length}`);
results.vulnerabilities.forEach(vuln => {
console.log(`${vuln.file}:${vuln.line} - ${vuln.reason}`);
});
} catch (error) {
console.error('Scan failed:', error);
}
}
// Scan text content directly
const content = '<!-- Hidden comment --> Some text';
const vulnerabilities = scanContent(content, 'example.md');
const aiVulnerabilities = await scanContentAI(content, 'example.md');Examples of Detected Vulnerabilities
Refer to Prompt Injection Examples
Integration with CI/CD
Use prompt-cop in your CI/CD pipeline to automatically check for vulnerabilities:
GitHub Actions Example
name: Security Check
on: [push, pull_request]
jobs:
prompt-injection-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- run: npm install -g prompt-cop
- run: prompt-cop . --exclude node_modules --severity mediumPre-commit Hook
{
"husky": {
"hooks": {
"pre-commit": "prompt-cop . --exclude node_modules"
}
}
}Exit Codes
0- No vulnerabilities found1- Vulnerabilities detected or error occurred
Development
Running Tests
npm testRunning Tests with Coverage
npm run test:coverageContributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT
