Skip to content

Repository files navigation

code-validator

License: MITPython 3.9+OfflineAI Code

What is code-validator? A static security scanner purpose-built for code produced by AI assistants (Claude, ChatGPT, GitHub Copilot, Cursor). Detects hardcoded credentials, CORS misconfigurations, SQL injection patterns, and dependency CVEs — in under 1 second per file, fully offline, with zero external API calls.

Why this exists: AI assistants frequently emit code that looks correct but contains subtle security defects — hardcoded API keys in examples, allow_origins=["*"] with allow_credentials=True, string-concatenated SQL. code-validator is the lightweight CI gate that catches these before they reach main.

🎯 Use caseBlock insecure AI-generated code at PR time
Speed<1s per file, --git-diff mode scans only changed files
🔒 Privacy100% offline. No code leaves your machine. Only dependency: pydantic
🧪 Detection rules20 rules across security, quality, and dependency layers (SEC001–SEC013 excluding SEC007, QUAL001–QUAL002, DEP001–DEP006)
📦 Installpip install -r requirements.txt — done

Features

Security Scanning

  • Hardcoded credentials: API keys (OpenAI, Anthropic, Google, GitHub tokens), passwords, database URLs, Django/Flask SECRET_KEY, AWS access keys, and embedded PEM private keys
  • Dangerous CORS configurations: wildcard origins, and wildcard origins combined with allow_credentials=True
  • SQL injection patterns: f-string interpolation, + concatenation, str.format(), and % operator
  • Command injection: os.system / os.popen / subprocess(..., shell=True)
  • Unsafe deserialization: pickle / marshal / shelve / yaml.load without a safe loader
  • Dynamic code execution: eval / exec (ast.literal_eval is excluded)

Code Quality Checks

  • Lines exceeding the configured maximum length (default: 120 characters, set via quality_rules.max_line_length)
  • Unused import detection via Python's ast, gated by a textual usage check to avoid false positives. __init__.py, from __future__ import, wildcard imports, and # noqa lines are exempt
  • Function complexity: not implemented (stub reserved for a cyclomatic complexity tool)

Dependency Auditing

  • Python: delegates to pip-audit when available
  • Node.js: delegates to npm audit when available
  • Fails loudly when the audit cannot run. A resolution error, timeout, or crash is reported as HIGH (DEP005 / DEP006) rather than silently passing, so "0 findings" never means "nothing was inspected"

Suppression

Intentionally vulnerable code (test fixtures, documented exceptions) can be exempted inline:

API_KEY="sk-..."# code-validator: ignoreAPI_KEY="sk-..."# code-validator: ignore[SEC001]# code-validator: ignore-file[SEC004] # whole file, selected rules# code-validator: ignore-file # whole file, all rules

File-level markers exist because some rules (CORS) are reported against the file rather than a single line.

Reporting

  • HTML: human-readable browser report with color-coded severity cards
  • JSON: machine-readable output for CI/CD integration
  • Console: summary printed to stdout with per-severity counts

Git Integration

  • --git-diff mode: scans only files changed since HEAD, keeping CI runs fast

Tech Stack

ComponentDetail
LanguagePython 3.9+
Core dependencypydantic == 2.13.4
Optionalpip-audit >= 2.6.0 (Python dep auditing)
CIGitHub Actions

No external API calls. Runs fully offline.


Setup

# Clone or copy the repository
git clone https://github.com/TTMK7777/code-validator.git
cd code-validator
# Install dependencies (only pydantic is required)
pip install -r requirements.txt
# Optional: enable Python dependency auditing
pip install pip-audit

Python 3.9 or later is required.


Usage

Scan a directory

python validator.py --path /path/to/project

Scan only files changed in the latest commit (recommended for CI)

python validator.py --git-diff

Scan a specific commit range

python validator.py --git-diff --from HEAD~3 --to HEAD

Generate an HTML report

python validator.py --path . --output report.html --format html

Generate a JSON report

python validator.py --path . --output report.json --format json

Use a custom configuration file

python validator.py --path . --config config/validator_config.json

CLI reference

usage: validator.py [-h] [--path PATH] [--git-diff] [--output OUTPUT]
[--format {html,json}] [--config CONFIG]
optional arguments:
--path PATH Project path to scan (default: current directory)
--git-diff Scan only files changed since HEAD
--output OUTPUT Output file path for the report
--format Report format: html | json (default: html)
--config CONFIG Path to a custom JSON configuration file

Exit codes:0 = no critical/high issues found, 1 = at least one critical or high issue detected (useful for blocking CI pipelines).


Configuration

Edit config/validator_config.json to customize behavior:

{
"exclude_patterns": [
"**/node_modules/**",
"**/venv/**",
"**/__pycache__/**",
"**/.git/**"
],
"file_extensions": [".py", ".js", ".ts", ".tsx", ".json", ".yaml", ".yml"],
"security_rules": {
"check_credentials": true,
"check_cors": true,
"check_sql_injection": true,
"check_dangerous_calls": true
},
"quality_rules": {
"max_line_length": 120,
"check_unused_imports": true,
"check_complex_functions": true
},
"dependency_rules": {
"check_python": true,
"check_node": true
}
}
KeyEffect
exclude_patternsGlob patterns excluded from scanning (matched against both the project-relative and absolute path)
file_extensionsExtensions collected when scanning a directory
security_rules.check_credentialsSEC001–SEC003, SEC008–SEC010
security_rules.check_corsSEC004–SEC005
security_rules.check_sql_injectionSEC006
security_rules.check_dangerous_callsSEC011–SEC013
quality_rules.max_line_lengthQUAL001 threshold
quality_rules.check_unused_importsQUAL002
quality_rules.check_complex_functionsReserved; the check is a stub and emits nothing
dependency_rules.check_pythonpip-audit delegation (DEP001, DEP002, DEP005)
dependency_rules.check_nodenpm audit delegation (DEP003, DEP004, DEP006)

A config file is only applied when passed explicitly via --config. Without it, the defaults above are used.


CI/CD Integration

GitHub Actions

Add the following workflow to your repository (.github/workflows/code-validation.yml):

name: Code Validationon:
push:
branches: [main, develop]pull_request:
branches: [main, develop]jobs:
validate:
runs-on: ubuntu-lateststeps:
- uses: actions/checkout@v4with:
fetch-depth: 2# required for --git-diff
- name: Set up Pythonuses: actions/setup-python@v5with:
python-version: '3.11'
- name: Install dependenciesrun: | pip install -r requirements.txt pip install pip-audit || echo "pip-audit not available" - name: Run Code Validatorrun: python validator.py --git-diff --output validation-report.json --format json
- name: Upload validation reportuses: actions/upload-artifact@v4if: always()with:
name: validation-reportpath: validation-report.json

The validator exits with code 1 when critical or high severity issues are found, which automatically blocks the CI job.

GitLab CI

code-validation:
image: python:3.11-slimscript:
- pip install -r requirements.txt
- pip install pip-audit || true
- python validator.py --git-diff --output validation-report.json --format jsonartifacts:
paths:
- validation-report.jsonwhen: always

Detection Rules

Rule IDSeverityCategoryDescription
SEC001CriticalSecurityHardcoded API key detected (OpenAI / Anthropic / Google / GitHub)
SEC002CriticalSecurityHardcoded password detected
SEC003CriticalSecurityHardcoded database credentials detected
SEC004CriticalSecurityCORS wildcard origins + credentials enabled
SEC005HighSecurityCORS wildcard origins (production risk)
SEC006HighSecurityPotential SQL injection via f-string, +, .format(), or %
SEC008CriticalSecurityHardcoded SECRET_KEY (Django / Flask session signing)
SEC009CriticalSecurityHardcoded AWS access key or secret access key
SEC010CriticalSecurityPEM private key embedded in source
SEC011HighSecurityCommand injection via shell execution
SEC012HighSecurityUnsafe deserialization (pickle / marshal / yaml.load)
SEC013HighSecurityDynamic code execution (eval / exec)
QUAL001LowQualityLine exceeds maximum length
QUAL002LowQualityUnused import
DEP001InfoDependenciespip-audit not installed
DEP002HighDependenciesPython package with known CVE
DEP003VariableDependenciesNode.js package with known CVE
DEP004InfoDependenciesnpm not installed
DEP005HighDependenciespip-audit could not run — dependency audit did not happen
DEP006HighDependenciesnpm audit could not run — dependency audit did not happen

On DEP005 / DEP006: these are deliberately HIGH, which fails CI. An audit that could not execute is not the same as an audit that found nothing — treating it as INFO (the previous behavior) let unresolvable dependency trees pass as green.


Example Output

============================================================
Validation Summary
============================================================
Project: /home/user/my-project
Files scanned: 42
Execution time: 0.83s
Issues by severity:
Critical : 0
High : 1
Medium : 2
Low : 5
Info : 1
============================================================

How code-validator Compares

ToolTargetSpeedOfflineAI-code focusDependency-free
code-validatorAI-generated code in CI<1s/file✅ Yes✅ Yes (purpose-built)✅ pydantic only
BanditGeneral PythonFast✅ Yes❌ No❌ Multiple deps
SemgrepMulti-language patternsMedium⚠️ Hybrid❌ No❌ Heavy
GitGuardianSecrets in git historySlow (API)❌ No❌ No❌ SaaS
TruffleHogSecrets in git historySlow✅ Yes❌ No❌ Multiple deps

Positioning: code-validator is the only tool in this list specifically tuned for the failure modes of AI-generated code (e.g., the CORS wildcard + allow_credentials=True pattern that LLMs disproportionately emit, or eval on model-produced strings).

Scope honesty: this is a line-oriented pattern scanner, not a dataflow analyzer. It does not perform taint tracking, so it flags shapes known to be risky rather than proving a path from untrusted input to a sink. For deep dataflow analysis, pair it with Semgrep or CodeQL — the point of this tool is a sub-second gate, not a replacement.


FAQ

Q: Why a separate tool for AI-generated code? Can't I just use Bandit or Semgrep?

General-purpose linters were designed for human-written code. AI assistants exhibit specific failure modes — hardcoded example credentials, overly permissive CORS for demos, string-concat SQL because the model "remembered" pre-ORM patterns. code-validator's rule set is tuned for these patterns and weights severity accordingly.

Q: Does code-validator send my code anywhere?

No. The scanner runs fully offline. The only optional network call is pip-audit for CVE lookups, and that contacts only the official PyPA advisory database — never your source code.

Q: How is this different from running Bandit + truffleHog + pip-audit separately?

code-validator unifies them into a single CI step with a coherent severity model and one report format (HTML / JSON). For --git-diff mode, only files changed in the current PR are scanned, keeping CI fast.

Q: Can I customize the detection rules?

Yes — see config/validator_config.json, and pass it with --config. You can disable rule categories, change the line-length threshold, and add exclude patterns. Every key in that file is honored; see the table in Configuration.

Q: How do I exempt intentionally vulnerable code?

Use an inline # code-validator: ignore comment — see Suppression. This is how this repository's own test fixtures pass the gate.

Q: What happens if pip-audit fails to run?

You get a HIGH finding (DEP005), which fails CI. This is deliberate: an audit that could not execute must not be reported as green.

Q: Does it work with Claude Code, Cursor, GitHub Copilot output?

Yes. It scans the resulting source files regardless of which AI assistant generated them. The detection patterns target the output, not the tool.

Q: What Python version do I need?

Python 3.9 or later.

Q: Is there a pre-commit hook?

Use python validator.py --git-diff in a pre-commit hook — exit code 1 blocks the commit when critical/high issues are found.


Author

Built by Taimu Tsuji (辻大夢) — Founder of Tsuji Lab, Applied AI Architect specializing in multi-agent AI coordination and AI-assisted software development at scale.

  • Experience: ~1.4M lines of AI-assisted code shipped across 4 concurrent projects; ~8,000 engineering hours saved per year via Claude Code automation.
  • Expertise: Claude Agent SDK, MCP, multi-agent orchestration, AI security gating.
  • Why I built this: After watching AI-generated PRs land with allow_origins=["*"] more than once, I needed a gate that ran in <1s and didn't ship code off-box. Existing tools were either too heavy, too noisy, or required SaaS.

GitHub: @TTMK7777


Structured Data (Schema.org)

For AI search engines and developer-tooling indexes:

{
"@context": "https://schema.org",
"@type": "SoftwareApplication",
"name": "code-validator",
"alternateName": "AI Code Security Scanner",
"applicationCategory": "DeveloperApplication",
"operatingSystem": "Linux, macOS, Windows",
"description": "Static security scanner for AI-generated code. Detects hardcoded credentials, CORS misconfigurations, SQL injection patterns, and dependency CVEs in under 1 second per file, fully offline.",
"url": "https://github.com/TTMK7777/code-validator",
"license": "https://opensource.org/licenses/MIT",
"programmingLanguage": "Python",
"softwareRequirements": "Python 3.9+",
"author": {
"@type": "Person",
"name": "Taimu Tsuji",
"alternateName": "辻大夢",
"jobTitle": "Founder, Tsuji Lab",
"url": "https://github.com/TTMK7777"
},
"keywords": "AI security, static analysis, code validation, AI-generated code, CI/CD security, secret detection, CORS validation, dependency audit, Claude Code, GitHub Copilot, LLM security"
}

License

MIT License. See LICENSE for details.


Contributing

Issues and PRs welcome. For security-related findings, see SECURITY.md.

About

Static security scanner purpose-built for AI-generated code (Claude Code, GitHub Copilot, ChatGPT, Cursor). Detects hardcoded credentials, CORS misconfigurations, SQL injection, and dependency CVEs in under 1 second per file — fully offline, zero data exfiltration.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages