|
1 | 1 | --- |
2 | 2 | agent: "application-security-architect" |
3 | 3 | name: threat-model |
4 | | -description: "Threat model the system using the 4Q framework and produce actionable artifacts." |
| 4 | +description: "Threat model the system using Shostack’s 4Q framework and produce actionable artifacts with repo-grounded diagrams validated via Mermaid Chart tools." |
5 | 5 | --- |
6 | 6 |
|
7 | | -# Prompt: 4Q Threat Model |
| 7 | +# Prompt: 4Q Threat Model (DFDs + Supporting Diagrams, Tool-Validated Mermaid) |
8 | 8 |
|
9 | 9 | ## Mission & Scope |
10 | 10 |
|
11 | | -**Goal:** Embed Adam Shostack’s **Four-Question** threat modeling into daily dev flow using VS Code + GitHub. The agent infers design from code, collaborates with the developer, and produces durable artifacts (e.g., a threat model markdown report), plus a concise PR-ready summary. |
| 11 | +**Goal:** Embed Adam Shostack’s **Four-Question** threat modeling into daily dev flow using VS Code + GitHub. Infer design from the repository (and/or PR diff), collaborate with the developer, and produce durable artifacts: |
| 12 | + |
| 13 | +1. a repo-grounded threat model Markdown report **with validated Mermaid diagrams** |
| 14 | +2. a concise PR-ready summary (copy/paste) |
12 | 15 |
|
13 | 16 | **4 Questions:** |
14 | 17 |
|
15 | | -1. *What are we working on?* → Infer & confirm scope, dataflows, trust boundaries. |
16 | | -2. *What can go wrong?* → Brainstorm threats (context-specific, STRIDE/OWASP mapped). |
17 | | -3. *What are we going to do about it?* → Check current mitigations, propose mitigation status. |
18 | | -4. *Did we do a good job?* → Define validation evidence to collect and owners. |
| 18 | +1. *What are we working on?* → Infer & confirm scope, assets, data flows, trust boundaries |
| 19 | +2. *What can go wrong?* → Enumerate threats (context-specific), map to STRIDE + OWASP |
| 20 | +3. *What are we going to do about it?* → Identify mitigations + gaps; status w/ evidence |
| 21 | +4. *Did we do a good job?* → Validation plan: evidence to collect + owners |
19 | 22 |
|
20 | 23 | **Where it runs:** |
21 | 24 |
|
22 | | -- **Local:** VS Code Copilot Chat / Agent mode for developers. |
23 | | -- **PR review:** Use the same output format as a PR comment or issue description. |
| 25 | +- **Local:** VS Code Copilot Chat / Agent mode |
| 26 | +- **PR review:** Same output format works as PR comment or issue description |
| 27 | + |
| 28 | +--- |
24 | 29 |
|
25 | 30 | ## ✅ Context / Assumptions |
26 | 31 |
|
27 | 32 | - Threat model the current repository and/or current PR diff (if available). |
28 | | -- Persist the resulting threat model as a Markdown file in the project root named: `Threat Model Review - {{DATE}}.md`. |
29 | | -- Evidence-first: cite file paths and (when possible) line ranges for claims about mitigations. |
30 | | -- If you cannot confirm something from the repo/diff, label it as **ASSUMPTION** or **UNKNOWN** (do not guess). |
31 | | -- Ask 2–4 clarifying questions if scope/dataflows/deployment assumptions are unclear. |
32 | | -- Do not generate code changes unless explicitly requested; focus on analysis and artifacts. |
| 33 | +- Persist output as a Markdown file in project root: |
| 34 | + - `Threat Model Review - {{DATE}}.md` |
| 35 | +- Evidence-first: cite file paths and (when possible) line ranges for claims about design, flows, mitigations, and trust boundaries. |
| 36 | +- If you cannot confirm something from the repo/diff, label it **ASSUMPTION** or **UNKNOWN** (do not guess). |
| 37 | +- Ask **2–4 clarifying questions** if scope, dataflows, deployment, or identities are unclear. |
| 38 | +- Do not generate code changes unless explicitly requested. |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## 🧰 Mermaid Diagram Tooling (Mandatory) |
| 43 | + |
| 44 | +You have access to Mermaid Chart tools: |
| 45 | + |
| 46 | +- `mermaidchart.vscode-mermaid-chart/get_syntax_docs` |
| 47 | +- `mermaidchart.vscode-mermaid-chart/mermaid-diagram-validator` |
| 48 | +- `mermaidchart.vscode-mermaid-chart/mermaid-diagram-preview` |
| 49 | + |
| 50 | +**You MUST use them** to prevent syntax errors. |
| 51 | + |
| 52 | +### Tool-driven diagram workflow (required) |
| 53 | + |
| 54 | +For every Mermaid diagram you include: |
| 55 | + |
| 56 | +1. **Pick the correct diagram type** |
| 57 | + - Before drafting each diagram, consult `get_syntax_docs` for that diagram type (e.g., `flowchart`, `sequenceDiagram`, `C4`, `classDiagram`, `erDiagram`). |
| 58 | +2. **Draft the diagram with minimal syntax** |
| 59 | + - Prefer simpler constructs over fancy styling. |
| 60 | + - Avoid experimental/unsupported directives unless confirmed in syntax docs. |
| 61 | +3. **Validate** |
| 62 | + - Run `mermaid-diagram-validator` on each diagram block. |
| 63 | + - If validation fails, fix and re-validate until it passes. |
| 64 | +4. **Preview sanity check (optional but recommended)** |
| 65 | + - Use `mermaid-diagram-preview` for the final versions of the DFD Level 0 and Level 1 diagrams. |
| 66 | + |
| 67 | +### Mermaid reliability rules (to avoid common breakage) |
| 68 | + |
| 69 | +- Always start with a valid diagram header: `flowchart LR`, `sequenceDiagram`, `classDiagram`, `erDiagram`, etc. |
| 70 | +- Don’t mix diagram grammars (e.g., don’t use `participant` in `flowchart`). |
| 71 | +- Avoid parentheses/brackets in node IDs; put complex text in node *labels*. |
| 72 | + - Good: `API[Process: Web API]` |
| 73 | + - Avoid: `API(Process: Web API)` |
| 74 | +- Quote edge labels if they contain special characters: |
| 75 | + - `A -->|"JWT (RS256)"| B` |
| 76 | +- Use unique node IDs and keep them alphanumeric/underscore (e.g., `svc_orders`, `db_main`). |
| 77 | +- Keep subgraph titles simple; avoid `:` if it breaks parsing. |
| 78 | +- Prefer `flowchart`-based DFDs for compatibility; use C4 only if syntax docs confirm availability in your Mermaid environment. |
| 79 | + |
| 80 | +**Gating requirement:** |
| 81 | +> Do not output any Mermaid diagram unless it has passed the Mermaid validator. |
| 82 | +
|
| 83 | +--- |
| 84 | + |
| 85 | +## 🔒 Diagram Requirements (Mermaid) |
| 86 | + |
| 87 | +**You MUST include diagrams** unless Mermaid rendering is not supported. Use Mermaid code blocks. |
| 88 | + |
| 89 | +### Required diagram set |
| 90 | + |
| 91 | +1. **DFD Level 0 (Context)** — entire system + external entities + trust boundaries |
| 92 | +2. **DFD Level 1 (Container / Major Subsystems)** — major processes, datastores, and flows |
| 93 | +3. **Trust Boundary View** — explicitly call out boundary crossings (can be embedded in DFDs if clear) |
| 94 | +4. **Top 2–3 Sequence Diagrams** — highest-risk flows (auth/login, payment, admin action, data export) |
| 95 | + |
| 96 | +### Optional (include when discoverable) |
| 97 | + |
| 98 | +- **Deployment / Runtime Topology** (k8s/compose/serverless/IaC-derived) |
| 99 | +- **Identity & Authorization model diagram** (actors → roles → permissions → enforcement points) |
| 100 | +- **Data classification map** (PII/PHI/secrets/payment data) tied to datastores and flows |
| 101 | + |
| 102 | +### Diagram evidence rules |
| 103 | + |
| 104 | +- Every diagram must include a short **Evidence** list: |
| 105 | + - file path(s) + relevant symbol(s) (and line ranges when possible) |
| 106 | +- If you cannot infer an element, label it **UNKNOWN** in the diagram and explain what evidence is missing. |
| 107 | + |
| 108 | +--- |
33 | 109 |
|
34 | 110 | ## 🔍 Procedure (4Q) |
35 | 111 |
|
36 | | -1) **Q1 — What are we working on?** |
37 | | - - Summarize scope, assets, key dataflows, and trust boundaries. |
38 | | -2) **Q2 — What can go wrong?** |
39 | | - - Enumerate threats specific to the flows; map each to STRIDE + OWASP tag. |
40 | | -3) **Q3 — What are we going to do about it?** |
41 | | - - Identify mitigations as **PRESENT / ABSENT / UNKNOWN**, with evidence when present. |
42 | | -4) **Q4 — Did we do a good job?** |
43 | | - - Define a validation plan (no code): scenarios + evidence to collect + owners. |
| 112 | +### 0) Triage & Inventory (fast, evidence-based) |
| 113 | + |
| 114 | +- Identify entry points, deployables, and primary data stores: |
| 115 | + - manifests (`package.json`, `pom.xml`, `.csproj`, `pyproject.toml`) |
| 116 | + - runtime configs (`docker-compose`, `k8s`, `serverless`, Terraform) |
| 117 | + - auth config and secrets patterns |
| 118 | +- Produce a short inventory list with evidence links. |
| 119 | + |
| 120 | +### 1) **Q1 — What are we working on?** |
| 121 | + |
| 122 | +Deliver: |
| 123 | + |
| 124 | +- System purpose (from README/docs where possible) |
| 125 | +- Components / containers / deployables |
| 126 | +- Key assets (data + systems) |
| 127 | +- **Key dataflows** (ranked by sensitivity and exposure) |
| 128 | +- Trust boundaries (internet/app/network/cloud/3rd party/admin) |
| 129 | +- **Diagrams (DFD L0 + L1 + trust boundaries)** — tool-validated |
| 130 | + |
| 131 | +### 2) **Q2 — What can go wrong?** |
| 132 | + |
| 133 | +For each key flow in the DFD: |
| 134 | + |
| 135 | +- Enumerate threats specific to that flow |
| 136 | +- Map to: |
| 137 | + - **STRIDE** category |
| 138 | + - **OWASP** tag (Top 10 / ASVS control area / API Top 10 — whichever best fits) |
| 139 | +- Include a short “Attack narrative” for the top risks (2–5 sentences) |
| 140 | + |
| 141 | +Also include: |
44 | 142 |
|
45 | | -## 📦 Output Format |
| 143 | +- Abuse cases for privileged/admin pathways |
| 144 | +- Supply chain threats if dependency/build pipeline evidence exists |
46 | 145 |
|
47 | | -Return the threat model as GitHub-flavored Markdown in chat (PR-comment ready) with the structure below. If your environment supports writing files, also write it to `./Threat Model Review - {{DATE}}.md` (project root): |
| 146 | +### 3) **Q3 — What are we going to do about it?** |
48 | 147 |
|
49 | | -### Scope |
| 148 | +For each threat: |
50 | 149 |
|
51 | | -- Components: |
| 150 | +- Identify mitigations as **PRESENT / ABSENT / UNKNOWN** |
| 151 | +- Provide evidence when PRESENT: |
| 152 | + - exact file path + symbol + line range (when possible) |
| 153 | +- If ABSENT/UNKNOWN: |
| 154 | + - propose remediation options |
| 155 | + - note expected effort (S/M/L) and blast-radius |
| 156 | + |
| 157 | +### 4) **Q4 — Did we do a good job?** |
| 158 | + |
| 159 | +Create a validation plan (no code changes) that includes: |
| 160 | + |
| 161 | +- 3–6 scenarios (prioritize highest-risk flows) |
| 162 | +- Evidence to collect (logs, config proof, test results, screenshots, policy outputs) |
| 163 | +- Owners (team/person/role) |
| 164 | + |
| 165 | +Include a final quality review checklist: |
| 166 | + |
| 167 | +- Coverage: do DFD flows map to threats/mitigations? |
| 168 | +- Boundary crossings: are they analyzed? |
| 169 | +- Unknowns: are they actionable questions with owners? |
| 170 | +- Mermaid diagrams: did all pass validator? |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +## 📦 Output Format (GitHub-Flavored Markdown) |
| 175 | + |
| 176 | +Return the threat model as PR-comment-ready Markdown in chat. |
| 177 | + |
| 178 | +If the environment supports writing files, also write: `./Threat Model Review - {{DATE}}.md` |
| 179 | + |
| 180 | +### 0. Executive summary |
| 181 | + |
| 182 | +- 5–10 bullets: top risks, what’s solid, what’s unknown, next actions |
| 183 | + |
| 184 | +### 1. Scope |
| 185 | + |
| 186 | +- In-scope components/containers: |
| 187 | +- Out-of-scope: |
52 | 188 | - Trust boundaries: |
53 | | -- Key dataflows: |
| 189 | +- Key assets (with sensitivity: Public/Internal/Confidential/Restricted): |
| 190 | + |
| 191 | +### 2. Assumptions & Unknowns |
| 192 | + |
| 193 | +- **ASSUMPTION:** … |
| 194 | +- **UNKNOWN:** … (include “Who can confirm” + question) |
| 195 | + |
| 196 | +### 3. Architecture & Data Flows (with tool-validated diagrams) |
| 197 | + |
| 198 | +#### 3.1 DFD Level 0 (Context) |
| 199 | + |
| 200 | +```mermaid |
| 201 | +flowchart LR |
| 202 | + %% (diagram content validated via Mermaid Chart tools) |
| 203 | +``` |
| 204 | + |
| 205 | +**Evidence** |
| 206 | + |
| 207 | +- `path/to/file` (symbol: …, lines …) |
| 208 | + |
| 209 | +#### 3.2 DFD Level 1 (Subsystems / Containers) |
| 210 | + |
| 211 | +```mermaid |
| 212 | +flowchart LR |
| 213 | + %% (diagram content validated via Mermaid Chart tools) |
| 214 | +``` |
| 215 | + |
| 216 | +**Evidence** |
| 217 | + |
| 218 | +- … |
| 219 | + |
| 220 | +#### 3.3 Supporting diagrams (as applicable) |
| 221 | + |
| 222 | +- Trust boundary view (if not already clear) |
| 223 | +- Deployment topology (if discoverable) |
| 224 | +- Identity/authorization model (if discoverable) |
| 225 | + |
| 226 | +### 4. Key Flows (ranked) |
| 227 | + |
| 228 | +For each flow: |
| 229 | + |
| 230 | +- Description |
| 231 | +- Data elements involved (classify) |
| 232 | +- Entry points and enforcement points |
| 233 | +- Evidence links |
| 234 | + |
| 235 | +### 5. Threats |
| 236 | + |
| 237 | +Table: |
| 238 | + |
| 239 | +`ID | Flow | Summary | STRIDE | OWASP | Likelihood (L/M/H) | Impact (L/M/H) | Status (Open/Mitigated/Unknown) | Rationale` |
| 240 | + |
| 241 | +### 6. Mitigations |
| 242 | + |
| 243 | +Table: |
54 | 244 |
|
55 | | -### Assumptions |
| 245 | +`Threat ID | Mitigation | Status (PRESENT/ABSENT/UNKNOWN) | Location/Evidence | Notes/Open questions` |
56 | 246 |
|
57 | | -- (bullets; include owner/questions where possible) |
| 247 | +### 7. High-risk interaction sequences (top 2–3, tool-validated) |
58 | 248 |
|
59 | | -### Threats |
| 249 | +Provide sequence diagrams for the riskiest flows: |
60 | 250 |
|
61 | | -Table: `ID | Summary | STRIDE | OWASP | Likelihood (L/M/H) | Impact (L/M/H) | Status | Rationale` |
| 251 | +```mermaid |
| 252 | +sequenceDiagram |
| 253 | + %% (diagram content validated via Mermaid Chart tools) |
| 254 | +``` |
62 | 255 |
|
63 | | -### Mitigations |
| 256 | +**Evidence** |
64 | 257 |
|
65 | | -Table: `Threat ID | Mitigation | Status (PRESENT/ABSENT/UNKNOWN) | Location/Evidence | Notes/Open questions` |
| 258 | +- … |
66 | 259 |
|
67 | | -### Validation plan (no code) |
| 260 | +### 8. Validation plan (no code) |
68 | 261 |
|
69 | | -Provide **3 scenarios**: |
| 262 | +Provide **3–6 scenarios**: |
70 | 263 |
|
71 | 264 | - Intent |
72 | 265 | - Preconditions |
73 | 266 | - Steps |
74 | 267 | - Expected result |
75 | 268 | - Evidence to collect |
| 269 | +- Owner |
76 | 270 |
|
77 | | -### Owners |
| 271 | +### 9. Owners |
78 | 272 |
|
79 | | -- Who confirms assumptions |
80 | | -- Who drives mitigations |
| 273 | +- Who confirms assumptions: |
| 274 | +- Who drives mitigations: |
| 275 | +- Who validates fixes: |
81 | 276 |
|
82 | | -### Open questions |
| 277 | +### 10. Open questions |
83 | 278 |
|
84 | | -- Items needing confirmation |
| 279 | +- Bullets; each includes an owner and where to look in the repo |
85 | 280 |
|
86 | | -## ✅ Quality checks |
| 281 | +### ✅ Quality checks |
87 | 282 |
|
88 | | -- Every **PRESENT** mitigation includes a concrete code/config location when possible (path + line range). |
89 | | -- **UNKNOWN** is used when evidence is insufficient and includes a follow-up question. |
90 | | -- Threats are specific to described flows (avoid generic lists). |
91 | | -- Evidence vs assumptions are clearly separated and labeled. |
| 283 | +- Every **PRESENT** mitigation includes concrete code/config location (path + lines when possible). |
| 284 | +- **UNKNOWN** includes a follow-up question + owner. |
| 285 | +- Threats are tied to DFD flows (no generic dump). |
| 286 | +- Diagrams match actual repo components and are evidence-linked. |
| 287 | +- Evidence vs. inference is clearly labeled. |
| 288 | +- **All Mermaid diagrams were validated using `mermaid-diagram-validator`.** |
0 commit comments