|
| 1 | +--- |
| 2 | +name: commit-security-scan |
| 3 | +description: Analyze code changes for security vulnerabilities using LLM reasoning and threat model patterns. Use for PR reviews, pre-commit checks, or branch comparisons. |
| 4 | +version: 1.0.0 |
| 5 | +tags: [security, scanning, vulnerability-detection, ci-cd] |
| 6 | +--- |
| 7 | + |
| 8 | +# Commit Security Scan |
| 9 | + |
| 10 | +Analyze code changes (commits, PRs, diffs) using LLM-powered reasoning to detect security vulnerabilities. This skill reads code directly and applies patterns from the repository's threat model to identify issues across all STRIDE categories. |
| 11 | + |
| 12 | +## When to Use This Skill |
| 13 | + |
| 14 | +- **PR review** - Automated security scan on pull requests |
| 15 | +- **Pre-commit check** - Scan staged changes before committing |
| 16 | +- **Branch comparison** - Review security of feature branch changes |
| 17 | +- **Code review assistance** - Help reviewers spot security issues |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +This skill requires: |
| 22 | + |
| 23 | +1. **Threat model** - `.factory/threat-model.md` must exist |
| 24 | +2. **Security config** - `.factory/security-config.json` for severity thresholds |
| 25 | + |
| 26 | +**IMPORTANT: If these files don't exist, you MUST generate them first before proceeding with the security scan.** |
| 27 | + |
| 28 | +To generate the prerequisites: |
| 29 | + |
| 30 | +1. Tell the user: "The threat model doesn't exist yet. I'll generate it first before scanning." |
| 31 | +2. Run the `threat-model-generation` skill to create both files |
| 32 | +3. Once complete, continue with the security scan |
| 33 | + |
| 34 | +Do NOT ask the user to run the skill manually - just do it automatically as part of this workflow. |
| 35 | + |
| 36 | +## Inputs |
| 37 | + |
| 38 | +The skill determines what to scan from the user's request: |
| 39 | + |
| 40 | +| Scan Type | How to Specify | Example | |
| 41 | +| ----------------- | -------------------------- | -------------------------------------------- | |
| 42 | +| PR | "Scan PR #123" | `Scan PR #456 for security vulnerabilities` | |
| 43 | +| Commit range | "Scan commits X..Y" | `Scan commits abc123..def456` | |
| 44 | +| Single commit | "Scan commit X" | `Scan commit abc123` | |
| 45 | +| Staged changes | "Scan staged changes" | `Scan my staged changes for security issues` | |
| 46 | +| Uncommitted | "Scan uncommitted changes" | `Scan working directory changes` | |
| 47 | +| Branch comparison | "Scan from X to Y" | `Scan changes from main to feature-branch` | |
| 48 | +| Last N commits | "Scan last N commits" | `Scan the last 3 commits` | |
| 49 | + |
| 50 | +If no scope is specified, prompt the user for clarification. |
| 51 | + |
| 52 | +## Instructions |
| 53 | + |
| 54 | +Follow these steps in order: |
| 55 | + |
| 56 | +### Step 1: Verify Prerequisites (Auto-Generate if Missing) |
| 57 | + |
| 58 | +Try to read these files: |
| 59 | + |
| 60 | +- `.factory/threat-model.md` |
| 61 | +- `.factory/security-config.json` |
| 62 | + |
| 63 | +**If either file is missing or cannot be read:** |
| 64 | + |
| 65 | +1. Inform the user: "The security threat model doesn't exist yet. I'll generate it first - this may take a minute." |
| 66 | +2. Invoke the `threat-model-generation` skill to analyze the repository and create both files |
| 67 | +3. Once generation completes, continue with Step 2 |
| 68 | + |
| 69 | +This ensures the security scan always has the threat model context it needs for accurate analysis. |
| 70 | + |
| 71 | +### Step 2: Get Changed Files |
| 72 | + |
| 73 | +Based on the user's request, get the list of changed files and their diffs using git: |
| 74 | + |
| 75 | +- For PRs: use `gh pr diff` |
| 76 | +- For commits/ranges: use `git diff` or `git show` |
| 77 | +- For staged changes: use `git diff --cached` |
| 78 | + |
| 79 | +Read the full content of each changed file for context. |
| 80 | + |
| 81 | +### Step 3: Load Threat Model |
| 82 | + |
| 83 | +Read `.factory/threat-model.md` and `.factory/security-config.json` to understand: |
| 84 | + |
| 85 | +- The system's architecture and trust boundaries |
| 86 | +- Known vulnerability patterns for this codebase |
| 87 | +- Severity thresholds for findings |
| 88 | + |
| 89 | +### Step 4: Analyze for Vulnerabilities |
| 90 | + |
| 91 | +For each changed file, systematically check for STRIDE threats: |
| 92 | + |
| 93 | +#### S - Spoofing Identity |
| 94 | + |
| 95 | +- Missing or weak authentication checks |
| 96 | +- Session handling vulnerabilities |
| 97 | +- Token/credential exposure in code |
| 98 | +- Insecure cookie settings |
| 99 | + |
| 100 | +#### T - Tampering with Data |
| 101 | + |
| 102 | +- **SQL Injection**: String concatenation/interpolation in SQL queries |
| 103 | +- **Command Injection**: User input in shell commands, `eval()`, `exec()` |
| 104 | +- **XSS**: Unescaped user input in HTML/templates |
| 105 | +- **Mass Assignment**: Blind assignment from request to model |
| 106 | +- **Path Traversal**: User input in file paths without validation |
| 107 | + |
| 108 | +#### R - Repudiation |
| 109 | + |
| 110 | +- Missing audit logging for sensitive operations |
| 111 | +- Insufficient error logging |
| 112 | +- Log injection vulnerabilities |
| 113 | + |
| 114 | +#### I - Information Disclosure |
| 115 | + |
| 116 | +- **IDOR**: Direct object access without ownership verification |
| 117 | +- Verbose error messages exposing internals |
| 118 | +- Hardcoded secrets, API keys, credentials |
| 119 | +- Sensitive data in logs or responses |
| 120 | +- Debug endpoints exposed |
| 121 | + |
| 122 | +#### D - Denial of Service |
| 123 | + |
| 124 | +- Missing rate limiting on endpoints |
| 125 | +- Unbounded resource consumption (file uploads, queries) |
| 126 | +- Algorithmic complexity attacks (regex, sorting) |
| 127 | +- Missing pagination on list endpoints |
| 128 | + |
| 129 | +#### E - Elevation of Privilege |
| 130 | + |
| 131 | +- Missing authorization checks on endpoints |
| 132 | +- Role/permission bypass opportunities |
| 133 | +- Privilege escalation through parameter manipulation |
| 134 | + |
| 135 | +### Step 5: Assess Each Finding |
| 136 | + |
| 137 | +For each potential vulnerability: |
| 138 | + |
| 139 | +1. **Trace data flow**: Follow user input from source to sink |
| 140 | + |
| 141 | + - Where does the input come from? (request params, body, headers, files) |
| 142 | + - Does it pass through validation/sanitization? |
| 143 | + - Where does it end up? (database, shell, response, file system) |
| 144 | + |
| 145 | +2. **Check for existing mitigations**: |
| 146 | + |
| 147 | + - Is there validation elsewhere in the codebase? |
| 148 | + - Are there middleware/decorators that protect this code? |
| 149 | + - Does the framework provide automatic protection? |
| 150 | + |
| 151 | +3. **Determine severity**: |
| 152 | + |
| 153 | + - **CRITICAL**: Remote code execution, auth bypass, data breach |
| 154 | + - **HIGH**: SQL injection, XSS, IDOR, privilege escalation |
| 155 | + - **MEDIUM**: Information disclosure, missing security headers |
| 156 | + - **LOW**: Best practice violations, minor issues |
| 157 | + |
| 158 | +4. **Assess confidence**: |
| 159 | + - **HIGH**: Clear vulnerable pattern, direct data flow, no mitigations |
| 160 | + - **MEDIUM**: Possible vulnerability, some uncertainty about context |
| 161 | + - **LOW**: Suspicious pattern, likely has mitigations we can't see |
| 162 | + |
| 163 | +### Step 6: Generate Report |
| 164 | + |
| 165 | +Create `security-findings.json` with this structure: |
| 166 | + |
| 167 | +```json |
| 168 | +{ |
| 169 | + "scan_id": "scan-YYYY-MM-DD-XXX", |
| 170 | + "scan_date": "<ISO 8601 timestamp>", |
| 171 | + "scan_type": "pr|commit|range|staged|working", |
| 172 | + "commit_range": "<base>..<head>", |
| 173 | + "pr_number": null, |
| 174 | + "threat_model_version": "<from security-config.json>", |
| 175 | + "findings": [ |
| 176 | + { |
| 177 | + "id": "VULN-001", |
| 178 | + "severity": "HIGH", |
| 179 | + "stride_category": "Tampering", |
| 180 | + "vulnerability_type": "SQL Injection", |
| 181 | + "cwe": "CWE-89", |
| 182 | + "file": "src/api/users.py", |
| 183 | + "line_range": "45-49", |
| 184 | + "code_context": "<vulnerable code snippet>", |
| 185 | + "analysis": "<explanation of why this is vulnerable>", |
| 186 | + "exploit_scenario": "<how an attacker could exploit this>", |
| 187 | + "threat_model_reference": "Section 5.2 - SQL Injection", |
| 188 | + "existing_mitigations": [], |
| 189 | + "recommended_fix": "<how to fix the vulnerability>", |
| 190 | + "confidence": "HIGH", |
| 191 | + "reasoning": "<why this confidence level>" |
| 192 | + } |
| 193 | + ], |
| 194 | + "summary": { |
| 195 | + "total_findings": 0, |
| 196 | + "by_severity": { "CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0 }, |
| 197 | + "by_stride": { |
| 198 | + "Spoofing": 0, |
| 199 | + "Tampering": 0, |
| 200 | + "Repudiation": 0, |
| 201 | + "InfoDisclosure": 0, |
| 202 | + "DoS": 0, |
| 203 | + "ElevationOfPrivilege": 0 |
| 204 | + }, |
| 205 | + "files_analyzed": 0 |
| 206 | + } |
| 207 | +} |
| 208 | +``` |
| 209 | + |
| 210 | +### Step 7: Report Results |
| 211 | + |
| 212 | +1. Save findings to `security-findings.json` |
| 213 | +2. Report summary to user (findings count by severity, triggered thresholds) |
| 214 | +3. Check severity thresholds from `security-config.json` and note if any are triggered |
| 215 | + |
| 216 | +## CWE Reference |
| 217 | + |
| 218 | +Common CWE mappings for findings: |
| 219 | + |
| 220 | +| Vulnerability Type | CWE | |
| 221 | +| ------------------------ | ------- | |
| 222 | +| SQL Injection | CWE-89 | |
| 223 | +| Command Injection | CWE-78 | |
| 224 | +| XSS (Reflected) | CWE-79 | |
| 225 | +| XSS (Stored) | CWE-79 | |
| 226 | +| Path Traversal | CWE-22 | |
| 227 | +| IDOR | CWE-639 | |
| 228 | +| Missing Authentication | CWE-306 | |
| 229 | +| Missing Authorization | CWE-862 | |
| 230 | +| Hardcoded Credentials | CWE-798 | |
| 231 | +| Sensitive Data Exposure | CWE-200 | |
| 232 | +| Mass Assignment | CWE-915 | |
| 233 | +| Open Redirect | CWE-601 | |
| 234 | +| SSRF | CWE-918 | |
| 235 | +| XXE | CWE-611 | |
| 236 | +| Insecure Deserialization | CWE-502 | |
| 237 | + |
| 238 | +## Example Invocations |
| 239 | + |
| 240 | +**Scan a PR:** |
| 241 | + |
| 242 | +``` |
| 243 | +Scan PR #123 for security vulnerabilities |
| 244 | +``` |
| 245 | + |
| 246 | +**Scan staged changes before committing:** |
| 247 | + |
| 248 | +``` |
| 249 | +Scan my staged changes for security issues |
| 250 | +``` |
| 251 | + |
| 252 | +**Scan a feature branch:** |
| 253 | + |
| 254 | +``` |
| 255 | +Scan changes from main to feature/user-auth for vulnerabilities |
| 256 | +``` |
| 257 | + |
| 258 | +**Scan recent commits:** |
| 259 | + |
| 260 | +``` |
| 261 | +Scan the last 5 commits for security issues |
| 262 | +``` |
| 263 | + |
| 264 | +## References |
| 265 | + |
| 266 | +- Analysis examples: `analysis-examples.md` (in this skill directory) |
| 267 | +- Threat model: `.factory/threat-model.md` |
| 268 | +- Security config: `.factory/security-config.json` |
| 269 | +- [OWASP Top 10](https://owasp.org/www-project-top-ten/) |
| 270 | +- [CWE Top 25](https://cwe.mitre.org/top25/) |
0 commit comments