Fix Code Vulnerability
Overview
This skill provides a systematic approach for identifying, analyzing, and fixing security vulnerabilities in codebases. It covers common vulnerability types (injection attacks, input validation issues, etc.) and provides verification strategies to ensure fixes are complete and correct.
Workflow
Phase 1: Initial Reconnaissance
Start by understanding the scope and nature of the vulnerability:
Run existing tests first - Execute the test suite to identify any failing security-related tests. Failing tests often directly indicate what vulnerability needs to be fixed and what behavior is expected.
Read failing tests immediately - When security tests fail, read them first before exploring the broader codebase. Tests reveal:
- The exact vulnerability type (CWE number, attack vector)
- Expected defensive behavior
- Specific inputs that should be blocked
- The API or function under test
Identify the vulnerability type - Classify the vulnerability:
- CWE-89: SQL Injection
- CWE-78: OS Command Injection
- CWE-79: Cross-Site Scripting (XSS)
- CWE-93: CRLF Injection (HTTP Response Splitting)
- CWE-22: Path Traversal
- CWE-94: Code Injection
- CWE-611: XML External Entity (XXE)
Phase 2: Code Analysis
Trace the vulnerable code path:
Follow the data flow - Trace from user input to the vulnerable operation:
- Entry points (API endpoints, form handlers, file readers)
- Data transformation functions
- Output points (database queries, file operations, HTTP responses)
Identify the fix location - Prefer fixing at centralized helper functions rather than multiple call sites:
- Look for utility functions that process the vulnerable data
- Fixing a shared helper covers all callers automatically
- Avoid scattered fixes that may miss edge cases
Read code in larger chunks - When analyzing related functions, read them together rather than making many small reads. This provides better context for understanding the code flow.
Phase 3: Implementing the Fix
Apply the appropriate defensive measure:
Input validation - For injection vulnerabilities:
- Validate against dangerous characters (e.g.,
\n, \r, \0 for CRLF)
- Use allowlists when possible (define what IS allowed vs. what is NOT)
- Raise clear error messages that identify the issue
Output encoding - For XSS and similar:
- Encode output appropriate to the context (HTML, URL, JavaScript)
- Use framework-provided encoding functions
Parameterization - For SQL/command injection:
- Use parameterized queries or prepared statements
- Avoid string concatenation with user input
Error handling - Ensure the fix:
- Raises appropriate exceptions with descriptive messages
- Does not leak sensitive information in error messages
- Fails securely (deny by default)
Phase 4: Verification
Confirm the fix is complete:
Run the full test suite - All tests should pass, including:
- The originally failing security tests
- Existing functionality tests (ensure no regressions)
Verify edge cases are covered - Check that the fix handles:
- All variations of the attack (e.g., all control characters, not just
\n)
- Both direct and indirect attack paths
- All methods that could trigger the vulnerability
Create a vulnerability report - Document:
- Vulnerability type and CWE identifier
- Affected code locations
- Fix description and rationale
- Test coverage confirmation
Common Pitfalls
Avoid These Mistakes
Incomplete character coverage - When blocking dangerous characters, ensure all variants are covered (e.g., both \r and \n for CRLF, not just one).
Fixing at wrong level - Don't patch individual call sites when a centralized fix is available. Find the common helper function.
Missing indirect paths - A function may be called through multiple code paths. Verify the fix covers all entry points.
Skipping test verification - Always run tests after applying fixes. Visual inspection is insufficient.
Overly broad fixes - Don't break legitimate functionality. Understand what valid inputs look like before blocking patterns.
Attack Scenarios to Consider
When fixing vulnerabilities, understand the attack:
- CRLF Injection (CWE-93): Attacker injects
\r\n to split HTTP headers, enabling response splitting attacks
- SQL Injection (CWE-89): Attacker escapes string context to execute arbitrary SQL
- Command Injection (CWE-78): Attacker uses shell metacharacters to execute system commands
- Path Traversal (CWE-22): Attacker uses
../ sequences to access files outside intended directory
Process Efficiency Tips
Start with tests - Running tests first immediately reveals which security checks are failing and what behavior is expected.
Read tests before code - Security tests describe the vulnerability and expected fix more clearly than searching the codebase.
Fix centralized functions - Identify and fix shared helper functions to cover all code paths with minimal changes.
Verify once, comprehensively - Run the full test suite rather than individual tests to catch any regressions.
Document the vulnerability - Create a report that explains the vulnerability type, fix applied, and verification performed.
1---2name: fix-code-vulnerability3description: Guidance for identifying and fixing security vulnerabilities in code. This skill should be used when asked to fix security issues, address CVEs or CWEs, remediate vulnerabilities like injection attacks (SQL, command, CRLF, XSS), or when working with failing security-related tests.4---5
6# Fix Code Vulnerability
7
8## Overview
9
10This skill provides a systematic approach for identifying, analyzing, and fixing security vulnerabilities in codebases. It covers common vulnerability types (injection attacks, input validation issues, etc.) and provides verification strategies to ensure fixes are complete and correct.
11
12## Workflow
13
14### Phase 1: Initial Reconnaissance
15
16Start by understanding the scope and nature of the vulnerability:
17
181. **Run existing tests first** - Execute the test suite to identify any failing security-related tests. Failing tests often directly indicate what vulnerability needs to be fixed and what behavior is expected.
19
202. **Read failing tests immediately** - When security tests fail, read them first before exploring the broader codebase. Tests reveal:
21 - The exact vulnerability type (CWE number, attack vector)
22 - Expected defensive behavior
23 - Specific inputs that should be blocked
24 - The API or function under test
25
263. **Identify the vulnerability type** - Classify the vulnerability:
27 - CWE-89: SQL Injection
28 - CWE-78: OS Command Injection
29 - CWE-79: Cross-Site Scripting (XSS)
30 - CWE-93: CRLF Injection (HTTP Response Splitting)
31 - CWE-22: Path Traversal
32 - CWE-94: Code Injection
33 - CWE-611: XML External Entity (XXE)
34
35### Phase 2: Code Analysis
36
37Trace the vulnerable code path:
38
391. **Follow the data flow** - Trace from user input to the vulnerable operation:
40 - Entry points (API endpoints, form handlers, file readers)
41 - Data transformation functions
42 - Output points (database queries, file operations, HTTP responses)
43
442. **Identify the fix location** - Prefer fixing at centralized helper functions rather than multiple call sites:
45 - Look for utility functions that process the vulnerable data
46 - Fixing a shared helper covers all callers automatically
47 - Avoid scattered fixes that may miss edge cases
48
493. **Read code in larger chunks** - When analyzing related functions, read them together rather than making many small reads. This provides better context for understanding the code flow.
50
51### Phase 3: Implementing the Fix
52
53Apply the appropriate defensive measure:
54
551. **Input validation** - For injection vulnerabilities:
56 - Validate against dangerous characters (e.g., `\n`, `\r`, `\0` for CRLF)
57 - Use allowlists when possible (define what IS allowed vs. what is NOT)
58 - Raise clear error messages that identify the issue
59
602. **Output encoding** - For XSS and similar:
61 - Encode output appropriate to the context (HTML, URL, JavaScript)
62 - Use framework-provided encoding functions
63
643. **Parameterization** - For SQL/command injection:
65 - Use parameterized queries or prepared statements
66 - Avoid string concatenation with user input
67
684. **Error handling** - Ensure the fix:
69 - Raises appropriate exceptions with descriptive messages
70 - Does not leak sensitive information in error messages
71 - Fails securely (deny by default)
72
73### Phase 4: Verification
74
75Confirm the fix is complete:
76
771. **Run the full test suite** - All tests should pass, including:
78 - The originally failing security tests
79 - Existing functionality tests (ensure no regressions)
80
812. **Verify edge cases are covered** - Check that the fix handles:
82 - All variations of the attack (e.g., all control characters, not just `\n`)
83 - Both direct and indirect attack paths
84 - All methods that could trigger the vulnerability
85
863. **Create a vulnerability report** - Document:
87 - Vulnerability type and CWE identifier
88 - Affected code locations
89 - Fix description and rationale
90 - Test coverage confirmation
91
92## Common Pitfalls
93
94### Avoid These Mistakes
95
961. **Incomplete character coverage** - When blocking dangerous characters, ensure all variants are covered (e.g., both `\r` and `\n` for CRLF, not just one).
97
982. **Fixing at wrong level** - Don't patch individual call sites when a centralized fix is available. Find the common helper function.
99
1003. **Missing indirect paths** - A function may be called through multiple code paths. Verify the fix covers all entry points.
101
1024. **Skipping test verification** - Always run tests after applying fixes. Visual inspection is insufficient.
103
1045. **Overly broad fixes** - Don't break legitimate functionality. Understand what valid inputs look like before blocking patterns.
105
106### Attack Scenarios to Consider
107
108When fixing vulnerabilities, understand the attack:
109
110- **CRLF Injection (CWE-93)**: Attacker injects `\r\n` to split HTTP headers, enabling response splitting attacks
111- **SQL Injection (CWE-89)**: Attacker escapes string context to execute arbitrary SQL
112- **Command Injection (CWE-78)**: Attacker uses shell metacharacters to execute system commands
113- **Path Traversal (CWE-22)**: Attacker uses `../` sequences to access files outside intended directory
114
115## Process Efficiency Tips
116
1171. **Start with tests** - Running tests first immediately reveals which security checks are failing and what behavior is expected.
118
1192. **Read tests before code** - Security tests describe the vulnerability and expected fix more clearly than searching the codebase.
120
1213. **Fix centralized functions** - Identify and fix shared helper functions to cover all code paths with minimal changes.
122
1234. **Verify once, comprehensively** - Run the full test suite rather than individual tests to catch any regressions.
124
1255. **Document the vulnerability** - Create a report that explains the vulnerability type, fix applied, and verification performed.