api-key-guard 1.0.2 → 1.0.5

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 CHANGED
@@ -1,84 +1,450 @@
1
- # 🔐 api-key-guard
1
+ # 🔐 API Key Guard
2
2
 
3
3
  [![npm version](https://badge.fury.io/js/api-key-guard.svg)](https://www.npmjs.com/package/api-key-guard)
4
4
  [![npm downloads](https://img.shields.io/npm/dm/api-key-guard.svg)](https://www.npmjs.com/package/api-key-guard)
5
5
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
6
 
7
- ## Project Title & Description
7
+ A comprehensive CLI tool for detecting, preventing, and managing API key leaks in your codebase with AI-powered documentation generation.
8
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.
9
+ ## Quick Start
10
10
 
11
- ## ⚠️ Problem Statement: The Silent Threat of API Key Leaks
11
+ ```bash
12
+ # Install globally
13
+ npm install -g api-key-guard
12
14
 
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:
15
+ # Scan for API key leaks
16
+ api-key-guard scan
14
17
 
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.
18
+ # Setup git hooks for automatic scanning
19
+ api-key-guard setup-hooks
19
20
 
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
+ # Generate AI-powered README (requires GEMINI_API_KEY)
22
+ export GEMINI_API_KEY=your_api_key_here
23
+ api-key-guard readme
24
+ ```
21
25
 
22
- ## Features List
26
+ ## 🚨 The Problem
23
27
 
24
- **api-key-guard** comes packed with features to make API key management robust and effortless:
28
+ API key leaks in code repositories are a critical security vulnerability that can lead to:
25
29
 
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.
30
+ - **Data breaches** and unauthorized access
31
+ - **Financial losses** from misused cloud resources
32
+ - **Service disruptions** and security incidents
33
+ - **Reputational damage** from exposed credentials
44
34
 
45
- ## 🛠️ Installation
35
+ ## Features
46
36
 
47
- **Prerequisites:**
37
+ - 🔍 **Smart Detection**: Advanced regex patterns detect AWS keys, GitHub tokens, Google API keys, and more
38
+ - 🔒 **Git Hooks Integration**: Automatic pre-commit scanning to prevent leaks
39
+ - 🤖 **AI-Powered README**: Generate professional documentation using Google's Gemini API
40
+ - ⚡ **Fast Scanning**: Efficient file parsing with configurable ignore patterns
41
+ - 🌈 **Clear Output**: Color-coded results with detailed reporting
42
+ - 📋 **Multiple Formats**: Support for JS, TS, Python, JSON, YAML, ENV files, and more
48
43
 
49
- * Node.js (v14 or higher)
50
- * npm (v6 or higher)
44
+ ## 📋 CLI Commands
51
45
 
52
- Install `api-key-guard` globally using npm:
46
+ ### Scan for API Keys
47
+ ```bash
48
+ # Scan current directory
49
+ api-key-guard scan
53
50
 
51
+ # Scan specific path
52
+ api-key-guard scan --path ./src
53
+
54
+ # Verbose output with pattern details
55
+ api-key-guard scan --verbose
56
+ ```
57
+
58
+ ### Git Hooks Setup
54
59
  ```bash
55
- npm install -g api-key-guard
60
+ # Install pre-commit hook
61
+ api-key-guard setup-hooks
56
62
  ```
57
63
 
58
- Verify the installation by checking the version:
64
+ ### AI README Generation
65
+ ```bash
66
+ # Generate README.md
67
+ api-key-guard readme
68
+
69
+ # Force overwrite existing file
70
+ api-key-guard readme --force
71
+
72
+ # Custom output file
73
+ api-key-guard readme --output DOCUMENTATION.md
74
+ ```
75
+
76
+ ## 🛠️ Installation
77
+
78
+ **Prerequisites:** Node.js 14+ and npm
79
+
80
+ ```bash
81
+ # Global installation (recommended)
82
+ npm install -g api-key-guard
59
83
 
84
+ # Local project installation
85
+ npm install --save-dev api-key-guard
86
+ ```
87
+
88
+ **Verify installation:**
60
89
  ```bash
61
90
  api-key-guard --version
62
91
  ```
63
92
 
93
+ ## 🔑 API Key Types Detected
94
+
95
+ - **AWS Access Keys**: `AKIA...`
96
+ - **GitHub Tokens**: `ghp_...`, `github_pat_...`
97
+ - **Google API Keys**: `AIza...`
98
+ - **Generic Patterns**: `api_key`, `secret_key`, `access_token`
99
+ - **Bearer Tokens**: `Bearer ...`
100
+ - **Custom Patterns**: High-entropy strings
101
+
102
+ ## 🤖 AI README Generation
103
+
104
+ The `readme` command uses Google's Gemini API to generate comprehensive documentation:
105
+
106
+ ### Setup
107
+ 1. Get API key from [Google AI Studio](https://makersuite.google.com/app/apikey)
108
+ 2. Set environment variable:
109
+ ```bash
110
+ export GEMINI_API_KEY=your_api_key_here
111
+ ```
112
+
113
+ ### Generated Content
114
+ - Project overview and description
115
+ - Installation instructions
116
+ - Usage examples and CLI documentation
117
+ - Security best practices
118
+ - Contributing guidelines
119
+
120
+ ## ⚙️ Configuration
121
+
122
+ Create `.api-key-guard.json` in your project root:
123
+
124
+ ```json
125
+ {
126
+ "ignorePatterns": [
127
+ "node_modules/**",
128
+ "dist/**",
129
+ "*.min.js",
130
+ "test/**/*.fixture.js"
131
+ ],
132
+ "customPatterns": [
133
+ {
134
+ "name": "Custom API Key",
135
+ "pattern": "custom_key_[0-9a-f]{32}"
136
+ }
137
+ ]
138
+ }
139
+ ```
140
+
141
+ ## 🔐 Git Hooks
142
+
143
+ The `setup-hooks` command creates a pre-commit hook that:
144
+
145
+ 1. Scans staged files for API keys
146
+ 2. Blocks commits if leaks are detected
147
+ 3. Provides clear feedback on detected patterns
148
+ 4. Allows bypass with `--no-verify` if needed
149
+
150
+ ## 🛡️ Security Features
151
+
152
+ - **Zero Storage**: API keys are never stored or logged
153
+ - **Environment Variables**: Secure handling of authentication tokens
154
+ - **Pattern Matching**: Regular expressions detect common key formats
155
+ - **Entropy Analysis**: Identifies high-entropy strings that may be secrets
156
+ - **Configurable Scanning**: Customize patterns and ignore rules
157
+
158
+ ## 📊 Usage Examples
159
+
160
+ **Basic Scanning:**
161
+ ```bash
162
+ api-key-guard scan
163
+ # ✅ No potential API key leaks detected!
164
+ ```
165
+
166
+ **With API Key Detection:**
167
+ ```bash
168
+ api-key-guard scan --verbose
169
+ # 🚨 Found 2 potential API key leak(s):
170
+ # 📄 src/config.js:15
171
+ # Pattern: AKIA1234567890ABCDEF
172
+ # 📄 .env.example:3
173
+ # Pattern: sk-1234567890abcdef...
174
+ ```
175
+
176
+ ## 🤝 Contributing
177
+
178
+ 1. Fork the repository
179
+ 2. Create a feature branch: `git checkout -b feature/amazing-feature`
180
+ 3. Commit changes: `git commit -m 'Add amazing feature'`
181
+ 4. Push to branch: `git push origin feature/amazing-feature`
182
+ 5. Open a Pull Request
183
+
184
+ ## 📄 License
185
+
186
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
187
+
188
+ ## 🙋‍♂️ Support
189
+
190
+ - 📖 [Documentation](https://www.npmjs.com/package/api-key-guard)
191
+ - 🐛 [Report Issues](https://github.com/yourusername/api-key-guard/issues)
192
+ - 💬 [Discussions](https://github.com/yourusername/api-key-guard/discussions)
193
+
194
+ ---
195
+
196
+ **Made with ❤️ for developer security**
197
+ npm install --save-dev api-key-guard
198
+ ```
199
+
64
200
  ## 🚀 CLI Usage Examples
65
201
 
66
- `api-key-guard` provides a simple yet powerful command-line interface.
202
+ `api-key-guard` provides several commands for different use cases.
67
203
 
68
- ### 1. `api-key-guard scan` - Scan your codebase for API keys
204
+ ### 🔍 `api-key-guard scan` - Detect API Keys
69
205
 
70
- Scan a directory or specific files for potential API key leaks.
206
+ Scans your project for potential API key leaks.
71
207
 
72
- #### Basic Scan of Current Directory:
208
+ **Basic Scan (current directory):**
73
209
 
74
210
  ```bash
75
211
  api-key-guard scan .
76
212
  ```
77
213
 
78
- #### Scan a Specific File:
214
+ **Scan a specific directory:**
215
+
216
+ ```bash
217
+ api-key-guard scan src/
218
+ ```
219
+
220
+ **Scan a specific file:**
221
+
222
+ ```bash
223
+ api-key-guard scan config/secrets.js
224
+ ```
225
+
226
+ **Verbose output (shows more details about detection):**
227
+
228
+ ```bash
229
+ api-key-guard scan . --verbose
230
+ ```
231
+
232
+ **Fail on leak (exit with a non-zero code if leaks are found, useful for CI/CD):**
233
+
234
+ ```bash
235
+ api-key-guard scan . --fail-on-leak
236
+ ```
237
+
238
+ **Include specific file types (comma-separated):**
79
239
 
80
240
  ```bash
81
- api-key-guard scan src/utils/api.js
241
+ api-key-guard scan . --include "*.js,*.ts,*.env"
82
242
  ```
83
243
 
84
- #### Scan a Specific Directory with
244
+ **Exclude specific file types (comma-separated):**
245
+
246
+ ```bash
247
+ api-key-guard scan . --exclude "*.min.js,*.lock"
248
+ ```
249
+
250
+ **Ignore files or directories using patterns (shell-like glob patterns):**
251
+
252
+ ```bash
253
+ api-key-guard scan . --ignore-pattern "node_modules/**" --ignore-pattern "dist/*"
254
+ ```
255
+
256
+ **Use a `.gitignore` file for ignore patterns:**
257
+
258
+ ```bash
259
+ api-key-guard scan . --ignore-file .gitignore
260
+ ```
261
+
262
+ **Combine options:**
263
+
264
+ ```bash
265
+ api-key-guard scan src/ --fail-on-leak --verbose --ignore-file .gitignore --include "*.js,*.ts"
266
+ ```
267
+
268
+ ### 🎣 `api-key-guard setup-hooks` - Integrate Git Hooks
269
+
270
+ Sets up Git pre-commit and/or pre-push hooks to automatically scan for keys before commits or pushes.
271
+
272
+ **Set up pre-commit hook (default):**
273
+
274
+ ```bash
275
+ api-key-guard setup-hooks
276
+ ```
277
+
278
+ This will configure your local `.git/hooks/pre-commit` script to run `api-key-guard scan --fail-on-leak` on staged files.
279
+
280
+ **Set up pre-push hook:**
281
+
282
+ ```bash
283
+ api-key-guard setup-hooks --hook pre-push
284
+ ```
285
+
286
+ This will configure your local `.git/hooks/pre-push` script to run `api-key-guard scan --fail-on-leak` on all changes being pushed.
287
+
288
+ **Remove existing hooks (if you need to clean up):**
289
+
290
+ ```bash
291
+ api-key-guard setup-hooks --remove
292
+ ```
293
+
294
+ ### 📝 `api-key-guard readme` - Generate READMEs with AI
295
+
296
+ Leverages AI to help you generate a professional README.md for your project. (Requires an active internet connection and potentially an API key for the AI service, configured via environment variables).
297
+
298
+ **Generate a README with a prompt:**
299
+
300
+ ```bash
301
+ api-key-guard readme "Generate a README for a Node.js CLI tool that detects API keys, focusing on its security benefits and ease of use."
302
+ ```
303
+
304
+ **Generate and save to a specific file:**
305
+
306
+ ```bash
307
+ api-key-guard readme --output docs/PROJECT_README.md "Generate a simple README for a web application using React and Node.js."
308
+ ```
309
+
310
+ ## 🎣 Git Hooks Usage
311
+
312
+ Once you've set up Git hooks using `api-key-guard setup-hooks`, the tool will automatically run during your `git commit` or `git push` operations.
313
+
314
+ **Example of a pre-commit hook in action:**
315
+
316
+ 1. You have `api-key-guard` pre-commit hook enabled.
317
+ 2. You accidentally add a file containing a hardcoded API key:
318
+ ```javascript
319
+ // src/config.js
320
+ const API_KEY = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // BAD PRACTICE!
321
+ ```
322
+ 3. You stage the file:
323
+ ```bash
324
+ git add src/config.js
325
+ ```
326
+ 4. You try to commit:
327
+ ```bash
328
+ git commit -m "Add new config"
329
+ ```
330
+ 5. `api-key-guard` will detect the leak, prevent the commit, and display a warning:
331
+ ```
332
+ 🚨 api-key-guard: Potential API key leak detected in staged files! 🚨
333
+ Path: src/config.js
334
+ Line 2: const API_KEY = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
335
+
336
+ Commit aborted. Please remove or secure the sensitive information.
337
+ If you need to bypass, use 'git commit --no-verify'.
338
+ ```
339
+
340
+ To bypass the hook for a single commit (use with extreme caution!):
341
+
342
+ ```bash
343
+ git commit -m "Temporary commit, will fix secrets later" --no-verify
344
+ ```
345
+
346
+ ## ⚙️ Configuration
347
+
348
+ `api-key-guard` can be configured using a `api-key-guard.config.json` file at the root of your project. This allows you to define custom rules, ignore patterns, and scanner settings.
349
+
350
+ **Example `api-key-guard.config.json`:**
351
+
352
+ ```json
353
+ {
354
+ "scanPaths": [
355
+ "src/",
356
+ "config/",
357
+ "server/"
358
+ ],
359
+ "ignorePatterns": [
360
+ "node_modules/**",
361
+ "dist/**",
362
+ "*.min.js",
363
+ "*.log",
364
+ "testdata/**"
365
+ ],
366
+ "ignoreFiles": [
367
+ ".gitignore",
368
+ ".dockerignore"
369
+ ],
370
+ "includeFileTypes": [
371
+ "*.js",
372
+ "*.ts",
373
+ "*.env",
374
+ "*.json",
375
+ "*.yaml",
376
+ "*.yml"
377
+ ],
378
+ "excludeFileTypes": [
379
+ "*.lock",
380
+ "package-lock.json"
381
+ ],
382
+ "customRules": [
383
+ {
384
+ "name": "Custom-API-Key",
385
+ "regex": "MY_CUSTOM_API_KEY_[a-zA-Z0-9]{32,64}",
386
+ "description": "Detects specific internal API keys."
387
+ }
388
+ ],
389
+ "failOnLeak": true,
390
+ "verbose": false
391
+ }
392
+ ```
393
+
394
+ * **`scanPaths`**: An array of glob patterns for directories or files to explicitly scan. If empty, the current directory (`.`) is scanned.
395
+ * **`ignorePatterns`**: An array of glob patterns for files or directories to ignore during scanning.
396
+ * **`ignoreFiles`**: An array of filenames (e.g., `.gitignore`) whose contents will be used as additional ignore patterns.
397
+ * **`includeFileTypes`**: An array of glob patterns for file types to explicitly include. If specified, only these types will be scanned.
398
+ * **`excludeFileTypes`**: An array of glob patterns for file types to explicitly exclude.
399
+ * **`customRules`**: An array of custom regex rules for detecting specific patterns. Each rule should have a `name`, `regex`, and `description`.
400
+ * **`failOnLeak`**: Boolean. If `true`, the CLI will exit with a non-zero code if any leaks are found.
401
+ * **`verbose`**: Boolean. If `true`, more detailed output will be provided.
402
+
403
+ ## 🔒 Security Best Practices
404
+
405
+ While `api-key-guard` is a powerful tool, it's part of a broader security strategy. Always adhere to these best practices:
406
+
407
+ * **🚫 Never Hardcode Secrets:** The golden rule. Avoid placing API keys, passwords, or sensitive tokens directly in your code.
408
+ * **🌳 Use Environment Variables:** For development and deployment, load secrets from environment variables (e.g., using `.env` files locally and proper environment variable injection in production).
409
+ * **🔐 Employ Secret Management Services:** For production environments, utilize dedicated secret management services like AWS Secrets Manager, Azure Key Vault, Google Secret Manager, HashiCorp Vault, or similar.
410
+ * **🔑 Implement Least Privilege:** Grant API keys only the minimum necessary permissions required for their function.
411
+ * **🔄 Rotate Keys Regularly:** Periodically change your API keys, especially if they are long-lived.
412
+ * **📚 Educate Your Team:** Ensure all developers understand the risks of secret exposure and the proper procedures for handling sensitive information.
413
+ * **⚙️ Integrate into CI/CD:** Incorporate `api-key-guard` scans into your Continuous Integration/Continuous Deployment pipelines to catch leaks before deployment.
414
+
415
+ `api-key-guard` helps you catch mistakes, but proactive secure coding practices are paramount.
416
+
417
+ ## 🛣️ Future Roadmap
418
+
419
+ We are continuously working to enhance `api-key-guard`. Here are some planned features:
420
+
421
+ * **Advanced Entropy Analysis:** Improve detection of generic high-entropy strings that might indicate secrets without specific patterns.
422
+ * **Machine Learning-Based Detection:** Explore ML models for more intelligent and adaptive secret detection.
423
+ * **Cloud Provider Integrations:** Direct integrations with AWS, Azure, GCP for scanning cloud-specific credential formats.
424
+ * **Reporting & Alerting:** Generate detailed reports and integrate with alerting systems (e.g., Slack, email) when leaks are detected in CI/CD.
425
+ * **Support for More Secret Types:** Expand detection to include private keys, database connection strings, access tokens, etc.
426
+ * **IDE Extensions:** Develop extensions for popular IDEs (VS Code, IntelliJ) for real-time feedback.
427
+ * **Web UI for Centralized Management:** A future goal for larger teams to manage configurations and view scan results centrally.
428
+
429
+ ## 🤝 Contributing
430
+
431
+ We welcome contributions to `api-key-guard`! Whether it's bug reports, feature requests, or code contributions, your help is valuable.
432
+
433
+ 1. **Report Bugs:** If you find a bug, please open an issue on GitHub, providing detailed steps to reproduce, expected behavior, and actual behavior.
434
+ 2. **Suggest Features:** Have an idea for a new feature or improvement? Open an issue to discuss it.
435
+ 3. **Code Contributions:**
436
+ * Fork the repository.
437
+ * Create a new branch (`git checkout -b feature/your-feature-name` or `fix/bug-fix-description`).
438
+ * Make your changes.
439
+ * Write tests for your changes.
440
+ * Ensure your code adheres to the project's coding style.
441
+ * Commit your changes (`git commit -m "feat: Add new feature"`) using conventional commits.
442
+ * Push to your fork (`git push origin feature/your-feature-name`).
443
+ * Open a Pull Request to the `main` branch of the original repository.
444
+
445
+ Please adhere to our Code of Conduct.
446
+
447
+ ## 📄 License
448
+
449
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
450
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-key-guard",
3
- "version": "1.0.2",
3
+ "version": "1.0.5",
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": {
@@ -108,7 +108,7 @@ async function generateWithGemini(apiKey, projectContext) {
108
108
  temperature: 0.7,
109
109
  topK: 40,
110
110
  topP: 0.95,
111
- maxOutputTokens: 2048,
111
+ maxOutputTokens: 8192,
112
112
  },
113
113
  safetySettings: [
114
114
  {