api-key-guard 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -61
- package/package.json +2 -2
- package/src/readmeGenerator.js +1 -1
- package/TESTING.md +0 -111
- package/USAGE.md +0 -126
- package/config/patterns.json +0 -49
package/README.md
CHANGED
|
@@ -1,61 +1,84 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
## Features
|
|
23
|
-
|
|
24
|
-
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
1
|
+
# 🔐 api-key-guard
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/api-key-guard)
|
|
4
|
+
[](https://www.npmjs.com/package/api-key-guard)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
## Project Title & Description
|
|
8
|
+
|
|
9
|
+
**api-key-guard** is a comprehensive Node.js CLI tool designed to help developers and teams detect, prevent, and manage API key leaks within their codebases. It provides powerful scanning capabilities, integrates seamlessly with Git hooks, and even offers AI-powered assistance for project documentation, ensuring your secrets stay secret.
|
|
10
|
+
|
|
11
|
+
## ⚠️ Problem Statement: The Silent Threat of API Key Leaks
|
|
12
|
+
|
|
13
|
+
Accidentally committing API keys, private tokens, or sensitive credentials to a public or even private repository is a widespread and critical security vulnerability. A single leaked key can lead to:
|
|
14
|
+
|
|
15
|
+
* **Data Breaches:** Unauthorized access to sensitive user data.
|
|
16
|
+
* **Financial Loss:** Misuse of cloud resources, payment gateways, or premium services.
|
|
17
|
+
* **Reputational Damage:** Erosion of trust from customers and partners.
|
|
18
|
+
* **Service Interruptions:** Malicious actors disabling or disrupting your services.
|
|
19
|
+
|
|
20
|
+
Traditional methods often rely on manual reviews or simplistic grep commands, which are prone to error and can't keep pace with rapid development cycles. **api-key-guard** provides an automated, intelligent solution to proactively safeguard your repositories against this silent, yet devastating, threat.
|
|
21
|
+
|
|
22
|
+
## ✨ Features List
|
|
23
|
+
|
|
24
|
+
**api-key-guard** comes packed with features to make API key management robust and effortless:
|
|
25
|
+
|
|
26
|
+
* 🔍 **Advanced API Key Detection:**
|
|
27
|
+
* Utilizes a combination of regex patterns and entropy analysis to identify a wide range of common API keys (AWS, Google Cloud, Stripe, GitHub, etc.) and high-entropy strings that might be custom secrets.
|
|
28
|
+
* Scans various file types including JavaScript, TypeScript, Python, Ruby, JSON, YAML, Markdown, and more.
|
|
29
|
+
* 🎣 **Seamless Git Hooks Integration:**
|
|
30
|
+
* Set up pre-commit hooks to automatically scan staged files before every commit, preventing secrets from ever reaching your repository.
|
|
31
|
+
* Provides clear feedback and blocks commits if a leak is detected.
|
|
32
|
+
* 🚀 **Powerful CLI Commands:**
|
|
33
|
+
* `scan`: On-demand scanning of entire directories or specific files.
|
|
34
|
+
* `setup-hooks`: Automates the installation of Git pre-commit hooks.
|
|
35
|
+
* `readme`: Leverages AI to generate comprehensive `README.md` files based on your project's structure and existing documentation.
|
|
36
|
+
* 🤖 **AI-Powered README Generation:**
|
|
37
|
+
* A unique feature that helps you quickly generate professional and informative `README.md` files, improving project documentation and onboarding.
|
|
38
|
+
* 📁 **Multiple File Format Support:**
|
|
39
|
+
* Intelligently parses and scans a broad spectrum of text-based file formats, ensuring no stone is left unturned.
|
|
40
|
+
* ⚙️ **Configurable Ignore Patterns:**
|
|
41
|
+
* Define custom ignore rules in configuration files or via CLI flags to exclude specific files, directories (e.g., `node_modules`, `dist`), or even patterns of "false positive" keys, reducing noise.
|
|
42
|
+
* 🌈 **Colorful and Clear Output:**
|
|
43
|
+
* Leverages `chalk` to provide easy-to-read, color-coded output in the terminal, highlighting detected keys and scan results.
|
|
44
|
+
|
|
45
|
+
## 🛠️ Installation
|
|
46
|
+
|
|
47
|
+
**Prerequisites:**
|
|
48
|
+
|
|
49
|
+
* Node.js (v14 or higher)
|
|
50
|
+
* npm (v6 or higher)
|
|
51
|
+
|
|
52
|
+
Install `api-key-guard` globally using npm:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npm install -g api-key-guard
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Verify the installation by checking the version:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
api-key-guard --version
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## 🚀 CLI Usage Examples
|
|
65
|
+
|
|
66
|
+
`api-key-guard` provides a simple yet powerful command-line interface.
|
|
67
|
+
|
|
68
|
+
### 1. `api-key-guard scan` - Scan your codebase for API keys
|
|
69
|
+
|
|
70
|
+
Scan a directory or specific files for potential API key leaks.
|
|
71
|
+
|
|
72
|
+
#### Basic Scan of Current Directory:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
api-key-guard scan .
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
#### Scan a Specific File:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
api-key-guard scan src/utils/api.js
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### Scan a Specific Directory with
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-key-guard",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A comprehensive tool to detect, prevent, and manage API key leaks in your codebase with AI-powered README generation",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -28,4 +28,4 @@
|
|
|
28
28
|
"engines": {
|
|
29
29
|
"node": ">=14.0.0"
|
|
30
30
|
}
|
|
31
|
-
}
|
|
31
|
+
}
|
package/src/readmeGenerator.js
CHANGED
|
@@ -97,7 +97,7 @@ async function generateWithGemini(apiKey, projectContext) {
|
|
|
97
97
|
|
|
98
98
|
try {
|
|
99
99
|
const response = await axios.post(
|
|
100
|
-
`https://generativelanguage.googleapis.com/
|
|
100
|
+
`https://generativelanguage.googleapis.com/v1/models/gemini-2.5-flash:generateContent?key=${apiKey}`,
|
|
101
101
|
{
|
|
102
102
|
contents: [{
|
|
103
103
|
parts: [{
|
package/TESTING.md
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
# Testing the API Key Guard CLI
|
|
2
|
-
|
|
3
|
-
## Demo Instructions
|
|
4
|
-
|
|
5
|
-
This guide helps you test the API Key Guard CLI tool with the AI-powered README generator.
|
|
6
|
-
|
|
7
|
-
### Step 1: Install Dependencies
|
|
8
|
-
```bash
|
|
9
|
-
cd api-key-guard
|
|
10
|
-
npm install
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
### Step 2: Test Basic Commands
|
|
14
|
-
|
|
15
|
-
#### Test the help system:
|
|
16
|
-
```bash
|
|
17
|
-
node bin/cli.js --help
|
|
18
|
-
node bin/cli.js readme --help
|
|
19
|
-
node bin/cli.js scan --help
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
#### Test the scanner:
|
|
23
|
-
```bash
|
|
24
|
-
node bin/cli.js scan
|
|
25
|
-
node bin/cli.js scan --verbose
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
### Step 3: Test API Key Detection
|
|
29
|
-
Create a test file with fake API keys:
|
|
30
|
-
```javascript
|
|
31
|
-
// test-keys.js
|
|
32
|
-
const config = {
|
|
33
|
-
api_key: "sk-1234567890abcdef1234567890abcdef12345678",
|
|
34
|
-
aws_key: "AKIA1234567890ABCDEF",
|
|
35
|
-
github_token: "ghp_abcdefghijklmnopqrstuvwxyz1234567890"
|
|
36
|
-
};
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
Then scan:
|
|
40
|
-
```bash
|
|
41
|
-
node bin/cli.js scan --verbose
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
### Step 4: Test README Generator
|
|
45
|
-
|
|
46
|
-
#### Without API key (should show error):
|
|
47
|
-
```bash
|
|
48
|
-
node bin/cli.js readme --output test-readme.md
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
#### With API key (requires real Gemini API key):
|
|
52
|
-
1. Get API key from [Google AI Studio](https://makersuite.google.com/app/apikey)
|
|
53
|
-
2. Set environment variable:
|
|
54
|
-
```bash
|
|
55
|
-
# Windows PowerShell
|
|
56
|
-
$env:GEMINI_API_KEY = "your_actual_api_key"
|
|
57
|
-
|
|
58
|
-
# Then run
|
|
59
|
-
node bin/cli.js readme --force --output ai-generated-readme.md
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
### Step 5: Test Git Hooks
|
|
63
|
-
```bash
|
|
64
|
-
# Initialize git (if not already done)
|
|
65
|
-
git init
|
|
66
|
-
|
|
67
|
-
# Install hooks
|
|
68
|
-
node bin/cli.js setup-hooks
|
|
69
|
-
|
|
70
|
-
# Test the hook by trying to commit a file with API keys
|
|
71
|
-
echo 'const key = "AKIA1234567890ABCDEF";' > bad-file.js
|
|
72
|
-
git add bad-file.js
|
|
73
|
-
git commit -m "test commit" # Should be blocked
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
## Expected Outputs
|
|
77
|
-
|
|
78
|
-
### Successful README Generation
|
|
79
|
-
When you have a valid API key, you should see:
|
|
80
|
-
```
|
|
81
|
-
🤖 Generating README with AI...
|
|
82
|
-
📝 Analyzing project structure...
|
|
83
|
-
🚀 Generating README with Gemini AI...
|
|
84
|
-
💾 Writing README.md...
|
|
85
|
-
✅ Successfully generated README.md!
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### API Key Detection
|
|
89
|
-
When scanning files with API keys:
|
|
90
|
-
```
|
|
91
|
-
🔍 Scanning for API key leaks...
|
|
92
|
-
🚨 Found X potential API key leak(s):
|
|
93
|
-
📄 filename.js:line_number
|
|
94
|
-
Pattern: detected_pattern
|
|
95
|
-
⚠️ Please review these findings and secure any exposed API keys!
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
### Error Handling
|
|
99
|
-
Without API key:
|
|
100
|
-
```
|
|
101
|
-
Error generating README: GEMINI_API_KEY environment variable is required. Please set your Gemini API key.
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
## Cleanup After Testing
|
|
105
|
-
```bash
|
|
106
|
-
# Remove test files
|
|
107
|
-
rm test-keys.js bad-file.js ai-generated-readme.md
|
|
108
|
-
|
|
109
|
-
# Remove git hooks if needed
|
|
110
|
-
rm .git/hooks/pre-commit
|
|
111
|
-
```
|
package/USAGE.md
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
# API Key Guard - Usage Guide
|
|
2
|
-
|
|
3
|
-
## Quick Setup
|
|
4
|
-
|
|
5
|
-
1. **Install the package:**
|
|
6
|
-
```bash
|
|
7
|
-
npm install -g api-key-guard
|
|
8
|
-
```
|
|
9
|
-
|
|
10
|
-
2. **Get a Gemini API key:**
|
|
11
|
-
- Visit [Google AI Studio](https://makersuite.google.com/app/apikey)
|
|
12
|
-
- Create a new API key
|
|
13
|
-
- Copy the API key
|
|
14
|
-
|
|
15
|
-
3. **Set up environment variable:**
|
|
16
|
-
```bash
|
|
17
|
-
# Windows (PowerShell)
|
|
18
|
-
$env:GEMINI_API_KEY = "your_gemini_api_key_here"
|
|
19
|
-
|
|
20
|
-
# Windows (Command Prompt)
|
|
21
|
-
set GEMINI_API_KEY=your_gemini_api_key_here
|
|
22
|
-
|
|
23
|
-
# macOS/Linux
|
|
24
|
-
export GEMINI_API_KEY=your_gemini_api_key_here
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
## Using the AI-Powered README Generator
|
|
28
|
-
|
|
29
|
-
### Basic Usage
|
|
30
|
-
```bash
|
|
31
|
-
# Generate README.md with AI
|
|
32
|
-
api-key-guard readme
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
### With Options
|
|
36
|
-
```bash
|
|
37
|
-
# Force overwrite existing README.md
|
|
38
|
-
api-key-guard readme --force
|
|
39
|
-
|
|
40
|
-
# Output to a different file
|
|
41
|
-
api-key-guard readme --output DOCUMENTATION.md
|
|
42
|
-
|
|
43
|
-
# Combine options
|
|
44
|
-
api-key-guard readme --force --output NEW_README.md
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### What the AI Generates
|
|
48
|
-
The README generator creates a comprehensive README.md that includes:
|
|
49
|
-
|
|
50
|
-
- **Project title and description**
|
|
51
|
-
- **Problem statement** explaining API key leak issues
|
|
52
|
-
- **Features list** showcasing all capabilities
|
|
53
|
-
- **Installation instructions** for npm
|
|
54
|
-
- **CLI usage examples** for all commands
|
|
55
|
-
- **Git hooks setup guide**
|
|
56
|
-
- **Configuration options**
|
|
57
|
-
- **Security best practices**
|
|
58
|
-
- **Contributing guidelines**
|
|
59
|
-
- **License information**
|
|
60
|
-
|
|
61
|
-
## Other Commands
|
|
62
|
-
|
|
63
|
-
### Scan for API Key Leaks
|
|
64
|
-
```bash
|
|
65
|
-
# Scan current directory
|
|
66
|
-
api-key-guard scan
|
|
67
|
-
|
|
68
|
-
# Scan specific path
|
|
69
|
-
api-key-guard scan --path ./src
|
|
70
|
-
|
|
71
|
-
# Verbose output
|
|
72
|
-
api-key-guard scan --verbose
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### Setup Git Hooks
|
|
76
|
-
```bash
|
|
77
|
-
# Install pre-commit hook
|
|
78
|
-
api-key-guard setup-hooks
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
## Error Handling
|
|
82
|
-
|
|
83
|
-
The tool provides clear error messages for common issues:
|
|
84
|
-
|
|
85
|
-
- **Missing API key**: Clear instructions on how to set GEMINI_API_KEY
|
|
86
|
-
- **Network issues**: Helpful network troubleshooting information
|
|
87
|
-
- **Invalid API responses**: Detailed API error messages
|
|
88
|
-
- **File permissions**: Clear permission error messages
|
|
89
|
-
|
|
90
|
-
## Security Features
|
|
91
|
-
|
|
92
|
-
- **No API key storage**: API keys are read from environment variables only
|
|
93
|
-
- **Pattern-based detection**: Uses regex patterns to detect various API key formats
|
|
94
|
-
- **Configurable ignore patterns**: Respects .gitignore-style patterns
|
|
95
|
-
- **Git hook integration**: Prevents commits with detected API keys
|
|
96
|
-
|
|
97
|
-
## Supported API Key Formats
|
|
98
|
-
|
|
99
|
-
The scanner detects these common API key patterns:
|
|
100
|
-
- Generic API keys (`api_key`, `secret_key`, `access_token`)
|
|
101
|
-
- AWS Access Keys (`AKIA...`)
|
|
102
|
-
- GitHub Personal Access Tokens (`ghp_...`)
|
|
103
|
-
- Google API Keys (`AIza...`)
|
|
104
|
-
- Bearer tokens
|
|
105
|
-
- Custom patterns (configurable)
|
|
106
|
-
|
|
107
|
-
## Configuration
|
|
108
|
-
|
|
109
|
-
Create a `api-key-guard.config.json` file to customize:
|
|
110
|
-
|
|
111
|
-
```json
|
|
112
|
-
{
|
|
113
|
-
"ignorePatterns": [
|
|
114
|
-
"node_modules",
|
|
115
|
-
".git",
|
|
116
|
-
"dist",
|
|
117
|
-
"*.log"
|
|
118
|
-
],
|
|
119
|
-
"customPatterns": [
|
|
120
|
-
{
|
|
121
|
-
"name": "Custom API Key",
|
|
122
|
-
"pattern": "custom_[0-9a-f]{32}"
|
|
123
|
-
}
|
|
124
|
-
]
|
|
125
|
-
}
|
|
126
|
-
```
|
package/config/patterns.json
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"apiKeyPatterns": [
|
|
3
|
-
{
|
|
4
|
-
"name": "Generic API Key",
|
|
5
|
-
"pattern": "(?i)api[_-]?key[\\s]*[=:][\\s]*['\"]?([a-z0-9]{20,})",
|
|
6
|
-
"description": "Generic API key pattern"
|
|
7
|
-
},
|
|
8
|
-
{
|
|
9
|
-
"name": "Generic Secret Key",
|
|
10
|
-
"pattern": "(?i)secret[_-]?key[\\s]*[=:][\\s]*['\"]?([a-z0-9]{20,})",
|
|
11
|
-
"description": "Generic secret key pattern"
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
"name": "Access Token",
|
|
15
|
-
"pattern": "(?i)access[_-]?token[\\s]*[=:][\\s]*['\"]?([a-z0-9]{20,})",
|
|
16
|
-
"description": "Generic access token pattern"
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
"name": "AWS Access Key ID",
|
|
20
|
-
"pattern": "AKIA[0-9A-Z]{16}",
|
|
21
|
-
"description": "AWS Access Key ID"
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
"name": "GitHub Token",
|
|
25
|
-
"pattern": "ghp_[0-9a-zA-Z]{36}",
|
|
26
|
-
"description": "GitHub personal access token"
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
"name": "Google API Key",
|
|
30
|
-
"pattern": "AIza[0-9A-Za-z\\-_]{35}",
|
|
31
|
-
"description": "Google API key"
|
|
32
|
-
}
|
|
33
|
-
],
|
|
34
|
-
"ignorePatterns": [
|
|
35
|
-
"node_modules",
|
|
36
|
-
".git",
|
|
37
|
-
"dist",
|
|
38
|
-
"build",
|
|
39
|
-
"*.log",
|
|
40
|
-
"*.min.js",
|
|
41
|
-
"package-lock.json",
|
|
42
|
-
".env.example"
|
|
43
|
-
],
|
|
44
|
-
"fileExtensions": [
|
|
45
|
-
".js", ".ts", ".jsx", ".tsx", ".json", ".md", ".txt", ".yml", ".yaml",
|
|
46
|
-
".xml", ".html", ".css", ".scss", ".sass", ".less", ".env", ".config",
|
|
47
|
-
".py", ".java", ".php", ".rb", ".go", ".rs", ".cpp", ".c", ".h"
|
|
48
|
-
]
|
|
49
|
-
}
|