Security of AI Generated Code
Security of AI Generated Code
Presentation
The Effect of Context on
the Security of AI
Generated Code
Table of contents
01
Background
02
Hypothesis
03
Literature
Review
04
Experimental
05
Analysis
06
Q&A
Approach
Motivation
Vulnerability
Decreased caution Prompt variation
types
AI assistance increased 29.8% of Copilot snippets Minor variations in prompts
security vulnerabilities by contained vulnerabilities could significantly impact
20% across 38 CWE categories whether Copilot generated
secure or vulnerable code
Security Attacks & Offensive Uses
➔ AI code tools vulnerable to jailbreaking, prompt injection, and
adversarial inputs
➔ INSEC framework: simple comment-based attacks succeeded in
generating insecure code
➔ Debug mode enabled lowered security controls (Elgedawy et al.,
2024)
➔ Neural Machine Translation models improved with contextual
information for exploit generation
➔ Copilot outperformed CodeWhisperer in security-relevant code
generation
Benchmarking Studies
➔ SecurityEval dataset: 68% of InCoder and 74% of Copilot code
contained vulnerabilities
➔ Automated tools detected significantly fewer vulnerabilities than
manual inspection
➔ CodeLMSec benchmark: LLMs can generate secure code, but
effectiveness depends on context inclusion
➔ Wang et al. (2024): Security context dramatically improved
performance (Claude 3 Opus: 13.83 → 39.89)
➔ DeVAIC tool: 54% of AI-generated Python code contained
vulnerabilities
04
Approach
Process
Step 1 Step 6
Narrow down AI code Compare performance
assistance tools across tools
Step 3 Step 4
Generate prompts for Test code generation
each context level across tool
Context Levels
File level
Function level Project level
Includes configuration,
Minimal description and dependencies, and
function signature implicit security
Includes preceding code requirements
and relevant comments
Testing & Prompt Design
➔ Security-relevant coding tasks based on CWE categories
➔ Common vulnerability patterns:
◆ Weak randomization (CWE-330)
◆ OS command injection (CWE-78)
◆ SQL injection (CWE-89)
◆ Hard-coded credentials (CWE-798)
◆ Improper input validation (CWE-20)
➔ Natural language prompts or code comments
➔ No explicit security instructions to mimic real-world usage
Vulnerability Detection and
Categorization
Classificatio
SAST Manual review Incidence
n
SonarQube Validate findings Leverage CWE Tracking multiple
CodeQL and identify logical taxonomy vulnerabilities per
DeVAIC for Python issues snippet
Analysis Metrics
% of vulnerable Vulnerability density
Distribution of CWE
code snippets per (vulnerabilities per
categories
tool and context snippet/100 LOC)
level
Statistical
Benchmark against
Severity metrics comparisons
prior studies
(0-3 scale based on (z-test/chi-square)
(30-40% baseline
tool ratings) between tools and
for function-level)
contexts
Comparing tools
Vulnerability Cross-tool
types vulnerability
transfer
Significance