Security Research Project

Sec-Context

AI Code Security Anti-Patterns distilled from 150+ sources to help LLMs generate safer code. Use as context for AI assistants or deploy as a standalone security review agent.

150+
Sources Analyzed
25+
Anti-Patterns
40+
CVEs Documented
165K
Tokens of Reference

Anti-Pattern Examples

Real security vulnerabilities AI commonly generates, with secure alternatives.

!

Hardcoded Secrets

CWE-798
// PSEUDOCODE

// BAD: Hardcoded API keys
CONSTANT API_KEY = "sk-abcd1234efgh5678"
CONSTANT DB_PASSWORD = "super_secret_password"

FUNCTION call_api(endpoint):
    headers = {"Authorization": "Bearer " + API_KEY}
    RETURN http.get(endpoint, headers)

// GOOD: Environment variables
FUNCTION call_api(endpoint):
    api_key = environment.get("API_KEY")
    IF api_key IS NULL:
        THROW Error("API_KEY required")
    headers = {"Authorization": "Bearer " + api_key}
    RETURN http.get(endpoint, headers)
!

SQL Injection

CWE-89
// PSEUDOCODE

// BAD: String concatenation
FUNCTION get_user(user_id):
    query = "SELECT * FROM users WHERE id = " + user_id
    RETURN database.execute(query)

// Attacker input: "1 OR 1=1 --"
// Result: Returns ALL users!

// GOOD: Parameterized queries
FUNCTION get_user(user_id):
    query = "SELECT * FROM users WHERE id = ?"
    RETURN database.execute(query, [user_id])
!

Cross-Site Scripting (XSS)

CWE-79
// PSEUDOCODE

// BAD: Direct HTML injection (86% AI failure rate!)
FUNCTION display_comment(comment):
    element.innerHTML = comment

// Attacker input: "<script>steal(cookies)</script>"

// GOOD: Encode output for context
FUNCTION display_comment(comment):
    element.textContent = comment
    // Or use HTML encoding function
    element.innerHTML = html_encode(comment)
!

Command Injection

CWE-78
// PSEUDOCODE

// BAD: Shell command with user input
FUNCTION ping_host(hostname):
    command = "ping -c 4 " + hostname
    RETURN shell.execute(command)

// Attacker input: "google.com; rm -rf /"

// GOOD: Use argument arrays, avoid shell
FUNCTION ping_host(hostname):
    IF NOT is_valid_hostname(hostname):
        THROW Error("Invalid hostname")
    RETURN process.spawn("ping", ["-c", "4", hostname])
!

Weak Password Storage

CWE-327
// PSEUDOCODE

// BAD: Plaintext or weak hashing
FUNCTION save_password(password):
    database.insert({password: password})  // Plaintext!
    database.insert({password: md5(password)})  // Weak!
    database.insert({password: sha256(password)})  // No salt!

// GOOD: bcrypt/argon2 with proper cost
FUNCTION save_password(password):
    hashed = bcrypt.hash(password, cost=12)
    database.insert({password_hash: hashed})

The Security Problem is Real

AI-generated code has significantly higher vulnerability rates than human-written code.

86%
XSS Failure Rate in AI Code
Veracode 2025
2.74x
More Likely to Have XSS vs Human Code
CodeRabbit Study
81%
of Orgs Ship Vulnerable AI Code
Checkmarx Report
72%
Java AI Code Has Vulnerabilities
Veracode 2025
5-21%
Package Hallucination Rate
USENIX Study
75.8%
Devs Wrongly Trust AI Security
Snyk Survey

Top 10 Anti-Patterns

Ranked by Priority Score = (Frequency x 2) + (Severity x 2) + Detectability

Rank
Anti-Pattern
Score
Key Statistic
#1
Dependency Risks (Slopsquatting)
24
5-21% of AI packages don't exist
#2
XSS Vulnerabilities
23
86% failure rate in AI code
#3
Hardcoded Secrets
23
Scraped within minutes of exposure
#4
SQL Injection
22
Thousands of instances in training data
#5
Authentication Failures
22
75.8% false confidence in AI auth
#6
Missing Input Validation
21
Root cause of all injection attacks
#7
Command Injection
21
CVE-2025-53773 real-world RCE
#8
Missing Rate Limiting
20
Very high frequency, easy to detect
#9
Excessive Data Exposure
20
APIs return full objects
#10
Unrestricted File Upload
20
Critical severity, enables RCE

Recommended Architecture

Deploy Sec-Context as a security review agent between AI generation and production.

AI Code Gen

Copilot, Claude, GPT

Sec-Context Agent

Security Review

Secure Code

Production-ready

Research Sources

Synthesized from 150+ individual sources across 6 primary categories.

🗃

CVE Databases

NVD, MITRE CWE, Wiz - 40+ CVEs including IDEsaster collection

📚

Academic Research

Stanford, ACM, arXiv, IEEE, USENIX - Empirical vulnerability studies

📝

Security Blogs

Dark Reading, Veracode, Snyk, Checkmarx, OWASP

💬

Developer Forums

HackerNews (17+ threads), Reddit (6 subreddits)

🐦

Social Media

Twitter/X security researchers - Real-time incidents

🐙

GitHub

Security advisories, academic studies, code analysis

The Files

Large files by design - comprehensive security references for AI consumption.

ANTI_PATTERNS_BREADTH.md

~65K tokens

Full coverage of 25+ patterns. Quick reference format with BAD/GOOD examples for each vulnerability type.

ANTI_PATTERNS_DEPTH.md

~100K tokens

Deep-dive on 7 critical patterns. Multiple examples, attack scenarios, edge cases, and complete mitigations.

Download from GitHub