Industry-grade security scanner for AI-generated code with AST analysis, taint tracking, and comprehensive vulnerability detection
Project description
๐ก๏ธ AI Code Guard Pro
Industry-grade security scanner for AI-generated code with AST analysis, taint tracking, and LLM-specific vulnerability detection.
AI coding assistants (GitHub Copilot, Claude, ChatGPT, Cursor) are revolutionizing developmentโbut they can introduce security vulnerabilities that slip past code review. AI Code Guard Pro is a next-generation security scanner specifically designed to catch these issues.
๐ Key Improvements Over Basic Scanners
| Feature | Basic Scanners | AI Code Guard Pro |
|---|---|---|
| Analysis Method | Regex matching | AST parsing + taint tracking |
| False Positives | High | Reduced via context awareness |
| Secret Detection | Pattern only | Pattern + Shannon entropy |
| Prompt Injection | โ Not detected | โ Direct + indirect detection |
| Supply Chain | Basic | Typosquatting + dependency confusion |
| Output Formats | Limited | Console, JSON, SARIF, Markdown |
| CI/CD Integration | Basic | Native SARIF for GitHub Security |
๐ฏ What It Detects
๐ Secrets & Credentials
- API Keys: OpenAI, Anthropic, AWS, GCP, GitHub, Stripe, and 15+ providers
- Private Keys: RSA, SSH, PGP, EC
- Database Credentials: Connection strings, passwords
- High-Entropy Strings: AI placeholder secrets
๐ Injection Vulnerabilities
- SQL Injection: f-strings, .format(), concatenation in queries
- Command Injection: os.system, subprocess with shell=True
- Code Execution: eval(), exec() with user input
- SSRF: User-controlled URLs in requests
๐ค AI/LLM-Specific Issues
- Direct Prompt Injection: User input in system prompts
- Indirect Injection: RAG/retrieval injection risks
- Unsafe Deserialization: pickle, yaml.load without SafeLoader
๐ฆ Supply Chain Attacks
- Typosquatting: Similar names to popular packages
- Dependency Confusion: Internal package name patterns
- Known Malicious Packages: Database of suspicious packages
๐ฆ Installation
pip install ai-code-guard
Or with development dependencies:
pip install ai-code-guard[dev]
๐ง Quick Start
# Scan a directory
ai-code-guard scan ./src
# Scan with specific output format
ai-code-guard scan ./src --format sarif -o results.sarif
# Quick CI check
ai-code-guard check ./src
# List all rules
ai-code-guard rules
# Create config file
ai-code-guard init
๐ Example Output
๐ก๏ธ AI Code Guard Pro v1.0.0
Scanning ./my-project...
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ด CRITICAL: SQL Injection Vulnerability โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ src/db/queries.py:42 โ
โ โ
โ SQL query constructed using f-string interpolation. User-controlled โ
โ data may be interpolated directly into the query, enabling SQL โ
โ injection attacks. โ
โ โ
โ Code: query = f"SELECT * FROM users WHERE id = {user_id}" โ
โ โ
โ โ
Fix: Use parameterized queries: โ
โ cursor.execute('SELECT * FROM users WHERE id = ?', (user_id,)) โ
โ โ
โ CWE: CWE-89 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ HIGH: Prompt Injection Vulnerability โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ src/api/chat.py:23 โ
โ โ
โ User input directly embedded in LLM prompt via f-string. Attackers โ
โ can inject malicious instructions to manipulate the AI's behavior. โ
โ โ
โ Code: prompt = f"You are a helper. User says: {user_input}" โ
โ โ
โ โ
Fix: โ
โ 1. Separate system prompts from user content using message roles โ
โ 2. Sanitize user input (remove control characters, limit length) โ
โ 3. Use structured output formats to detect injection attempts โ
โ โ
โ CWE: CWE-74 | OWASP: LLM01 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ SUMMARY
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Files scanned 47
Issues found 3
Scan time 127ms
๐ด CRITICAL: 1 ๐ HIGH: 2 ๐ก MEDIUM: 0 ๐ต LOW: 0
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ๏ธ Configuration
Create .ai-code-guard.yaml in your project root:
# Minimum severity to report
min_severity: low # critical, high, medium, low, info
# Patterns to ignore
ignore:
- "tests/**"
- "**/test_*.py"
- "examples/**"
- "docs/**"
# Rules to disable
disable_rules: []
# - "SEC001" # If using example API keys
# - "PRI001" # If false positives on prompt construction
# Secret detection tuning
entropy_threshold: 4.5 # Shannon entropy threshold
min_secret_length: 16
# AI-specific detection
detect_placeholder_secrets: true
detect_prompt_injection: true
# Performance
max_file_size_kb: 1024
parallel_workers: 4
๐ CI/CD Integration
GitHub Actions
name: Security Scan
on: [push, pull_request]
jobs:
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- run: pip install ai-code-guard
- name: Run security scan
run: ai-code-guard scan . --format sarif -o results.sarif --fail-on high
- name: Upload SARIF to GitHub Security
uses: github/codeql-action/upload-sarif@v2
if: always()
with:
sarif_file: results.sarif
Pre-commit Hook
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: ai-code-guard
name: AI Code Guard Security Scan
entry: ai-code-guard check
language: python
types: [python]
pass_filenames: false
GitLab CI
security-scan:
image: python:3.11
script:
- pip install ai-code-guard
- ai-code-guard scan . --format json -o gl-sast-report.json
artifacts:
reports:
sast: gl-sast-report.json
๐ Rule Reference
| Rule ID | Category | Severity | Description |
|---|---|---|---|
| SEC001-015 | Secrets | CRITICAL/HIGH | API keys (OpenAI, AWS, GitHub, Stripe, etc.) |
| SEC020-022 | Secrets | CRITICAL | Private keys (RSA, SSH, PGP) |
| SEC030-031 | Secrets | CRITICAL | Database credentials |
| SEC040 | Secrets | MEDIUM | JWT tokens |
| SEC050 | Secrets | MEDIUM | AI placeholder secrets |
| SEC099 | Secrets | MEDIUM | High-entropy strings |
| INJ001 | Injection | CRITICAL | SQL injection |
| INJ002 | Injection | CRITICAL | Command injection |
| INJ003 | Injection | CRITICAL | Code execution (eval/exec) |
| DES001 | Deserialization | CRITICAL | Unsafe YAML |
| DES002 | Deserialization | CRITICAL | Unsafe pickle |
| SSRF001 | SSRF | HIGH | Server-side request forgery |
| PRI001-005 | Prompt Injection | HIGH | Direct prompt injection |
| PRI006 | Prompt Injection | MEDIUM | User input in prompts |
| PRI010-011 | Prompt Injection | MEDIUM | Indirect injection |
| DEP001 | Dependencies | VARIES | Known suspicious packages |
| DEP002 | Dependencies | HIGH | Typosquatting detection |
| DEP003 | Dependencies | HIGH | Dependency confusion |
๐ฌ Technical Details
AST-Based Analysis
Unlike regex-based scanners, AI Code Guard Pro parses Python code into an Abstract Syntax Tree, enabling:
- Taint tracking: Follow user input through variable assignments
- Context awareness: Understand function calls and their arguments
- Reduced false positives: Skip patterns in comments and strings
Entropy-Based Secret Detection
Uses Shannon entropy to distinguish real secrets from placeholders:
# High entropy (likely real secret) - DETECTED
api_key = "sk-proj-aB3xK9mL2pQrStUvWxYz..."
# Low entropy (placeholder) - IGNORED
api_key = "your-api-key-here"
LLM Security Focus
Specifically targets vulnerabilities in AI/LLM applications:
- Detects prompt injection in OpenAI, Anthropic, and LangChain code
- Identifies indirect injection risks in RAG pipelines
- Flags unsafe patterns in agent/tool implementations
๐ค Contributing
Contributions welcome! See CONTRIBUTING.md for guidelines.
Adding Detection Patterns
# ai_code_guard_pro/analyzers/my_analyzer.py
from ai_code_guard_pro.models import Finding, Severity, Category
class MyAnalyzer:
def analyze(self) -> list[Finding]:
findings = []
# Your detection logic
return findings
๐ References
๐ License
MIT License - see LICENSE for details.
Built for the AI era by security engineers who use AI coding assistants daily ๐ก๏ธ
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ai_code_guard-1.0.0.tar.gz.
File metadata
- Download URL: ai_code_guard-1.0.0.tar.gz
- Upload date:
- Size: 31.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fea2508e3ff8eb7a3eacdd313725cf6067f5f532b115db415832807732e36b38
|
|
| MD5 |
fce5516d68014fe3c2a8cd668c3ecc4a
|
|
| BLAKE2b-256 |
bd8e9858030de3753730918c0ffaba018485cd2891765cf0d909057e275b47fc
|
File details
Details for the file ai_code_guard-1.0.0-py3-none-any.whl.
File metadata
- Download URL: ai_code_guard-1.0.0-py3-none-any.whl
- Upload date:
- Size: 35.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4fc1175bec933667d773d3c15f65510945bc21ca5c39e024fa1fab9895186780
|
|
| MD5 |
c30712292b4696d1caeee3bb7862c480
|
|
| BLAKE2b-256 |
34ce7d15f212f5a37d3b4c1633731409cf8b0aa3740f93d1a604f44b9a1948eb
|