-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathexample.sarif
More file actions
354 lines (354 loc) · 19 KB
/
Copy pathexample.sarif
File metadata and controls
354 lines (354 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
{
"$schema": "https://www.schemastore.org/schemas/json/sarif-2.1.0.json",
"version": "2.1.0",
"runs": [
{
"tool": {
"driver": {
"name": "AI Security Analyzer",
"organization": "Contoso",
"semanticVersion": "1.2.0",
"informationUri": "https://contoso.com/ai-security-analyzer",
"rules": [
{
"id": "CWE-78",
"name": "CommandInjectionApiHandler",
"shortDescription": {
"text": "API handler parameter flows to a command execution sink without sanitization"
},
"fullDescription": {
"text": "Detects cases where user-supplied input from an API handler (HTTP request parameter, tool call argument, etc.) flows to a command execution function such as subprocess.run(), child_process.exec(), or os.system() without validation or sanitization. This enables arbitrary command injection by any caller who can reach the endpoint."
},
"helpUri": "https://cwe.mitre.org/data/definitions/78.html",
"defaultConfiguration": {
"level": "error"
},
"properties": {
"tags": ["security", "injection", "CWE-78"]
}
},
{
"id": "CWE-20",
"name": "MissingInputValidation",
"shortDescription": {
"text": "External input accepted without validation or type checking"
},
"fullDescription": {
"text": "Detects cases where external input (API parameters, file contents, environment variables) is used in security-sensitive operations without prior validation, type checking, or allowlist enforcement."
},
"helpUri": "https://cwe.mitre.org/data/definitions/20.html",
"properties": {
"tags": ["security", "input-validation", "CWE-20"]
}
}
]
},
"extensions": [
{
"name": "GPT-4o",
"version": "2024-11-20"
},
{
"name": "function-auditor",
"semanticVersion": "2.1.0",
"shortDescription": {
"text": "Autonomous agent that audits function-level security properties"
}
}
]
},
"automationDetails": {
"id": "ai-security-analyzer/contoso/my-project/my-service/",
"guid": "a3f2e917-4b1c-4d2e-8f3a-1234567890ab",
"correlationGuid": "b4e3f028-5c2d-4e3f-9a4b-2345678901bc",
"description": {
"text": "AI Security Analyzer v1.2.0 · GPT-4o 2024-11-20 · Unauthenticated API servers concern"
}
},
"properties": {
"ai/origin": "generated",
"ai/handoff": "## For remediation\nAuth pattern throughout is Flask `@login_required` (see src/auth.py:18). Formatter is `black --line-length 100`. Tests for `src/` live under `tests/`; run with `pytest -q`.\n\n## Ruled out\n`/health` and `/metrics` endpoints checked — no user-controlled sinks."
},
"versionControlProvenance": [
{
"repositoryUri": "https://dev.azure.com/contoso/my-project/_git/my-service",
"revisionId": "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
"branch": "main",
"mappedTo": {
"uriBaseId": "SRCROOT"
}
}
],
"artifacts": [
{
"location": {
"uri": "src/handler.py",
"uriBaseId": "SRCROOT"
},
"roles": ["analysisTarget", "resultFile"],
"mimeType": "text/x-python",
"sourceLanguage": "python",
"hashes": {
"sha-256": "3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4"
},
"contents": {
"text": "import subprocess\nfrom flask import Flask, request, jsonify, abort\n\napp = Flask(__name__)\n\n# ... lines 5-36 omitted for brevity ...\n\ndef execute_job(request):\n command = request.json['command'] # Line 38: untrusted input\n timeout = request.json.get('timeout', 300) # Line 39: no upper bound\n # Line 40-41: processing\n result = subprocess.run(command, shell=True, capture_output=True, timeout=timeout) # Line 42: injection sink\n return jsonify({'output': result.stdout.decode()})\n\n# ... remainder of file ...\n"
}
},
{
"location": {
"uri": "src/routes/api.ts",
"uriBaseId": "SRCROOT"
},
"roles": ["resultFile"],
"mimeType": "text/typescript",
"sourceLanguage": "typescript",
"hashes": {
"sha-256": "1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b"
}
}
],
"results": [
{
"ruleId": "CWE-78/api-handler",
"ruleIndex": 0,
"kind": "fail",
"level": "error",
"rank": 92.5,
"message": {
"text": "The 'command' parameter flows unsanitized from the 'execute_job' API handler to subprocess.run(shell=True), enabling arbitrary command execution by an unauthenticated caller.",
"markdown": "## Command Injection via API Handler Parameter\n\n**Rule:** `CWE-78/api-handler` · **Severity:** Error · **Rank:** 92.5\n\n### What's Wrong\n\nThe `command` parameter supplied by the API caller at `src/handler.py:38` is assigned directly from `request.json['command']` and passed to `subprocess.run()` with `shell=True` at line 42. There is no validation, sanitization, or allowlisting. Any network-reachable client can execute arbitrary OS commands on the server.\n\n### Exploit Chain\n\n1. Attacker sends POST to `/execute` with crafted JSON body\n2. `request.json['command']` returns attacker-controlled string\n3. String passed verbatim to `subprocess.run(command, shell=True)`\n4. Shell interprets metacharacters (`;`, `|`, `$()`) enabling command chaining\n5. Output returned in HTTP response body — full read access to server\n\n### Evidence\n```python\n# src/handler.py:38-43\ndef execute_job(request):\n command = request.json['command'] # untrusted input\n timeout = request.json.get('timeout', 300)\n result = subprocess.run(command, shell=True, capture_output=True, timeout=timeout) # injection sink\n return jsonify({'output': result.stdout.decode()})\n```\n\n### Reproduction Steps\n\n**Prerequisites:**\n- Python 3.8+, Flask installed (`pip install flask`)\n- Network access to port 8080\n\n**Steps:**\n1. Start the server: `python src/server.py`\n2. Run: `curl -X POST http://localhost:8080/execute -H 'Content-Type: application/json' -d '{\"command\": \"id; cat /etc/passwd\"}'`\n3. Observe: server executes `id` and `cat /etc/passwd`, returns output in response body.\n\n**Vulnerable behavior:** Injected commands execute; response contains `/etc/passwd` contents.\n**Fixed behavior:** Request returns 400 Bad Request with `Invalid command`.\n\n### Fix Suggestions\n\n**Approach 1 (recommended):** Allowlist valid commands and disable `shell=True`.\n```python\nALLOWED_COMMANDS = {'status', 'health', 'version'}\ncommand = request.json.get('command', '')\nif command not in ALLOWED_COMMANDS:\n abort(400, 'Invalid command')\nresult = subprocess.run(['/usr/bin/mytool', command], shell=False, capture_output=True)\n```\n\n**Approach 2:** Add authentication middleware to restrict endpoint access (defense in depth, does not fix injection).\n\n### Bypass Concerns\n\n- If allowlist is regex-based rather than exact-match, attacker may craft strings that pass the regex but contain shell metacharacters\n- If `shell=True` is retained with an allowlist, shell expansion may still occur on allowed values\n- If fix only covers this endpoint, other handlers accepting `command`-like parameters remain vulnerable\n\n### Mitigating Factors\n\n- ❌ No authentication middleware found in call chain\n- ❌ Server binds to `0.0.0.0` (externally reachable)\n- ❌ No network-layer restriction found in deployment config\n- ❌ No input validation or allowlist applied to `command`\n\n### Context\n\n- **Language:** Python 3 / Flask\n- **Auth framework:** None detected on this endpoint\n- **Deployment:** Standalone Flask dev server, no WSGI wrapper detected\n\n### References\n\n- [CWE-78: OS Command Injection](https://cwe.mitre.org/data/definitions/78.html)\n- [OWASP Command Injection](https://owasp.org/www-community/attacks/Command_Injection)"
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": "src/handler.py",
"uriBaseId": "SRCROOT",
"index": 0
},
"region": {
"startLine": 42,
"startColumn": 5,
"endLine": 42,
"endColumn": 86,
"snippet": {
"text": " result = subprocess.run(command, shell=True, capture_output=True, timeout=timeout)"
}
},
"contextRegion": {
"startLine": 37,
"endLine": 46,
"snippet": {
"text": "def execute_job(request):\n command = request.json['command'] # untrusted input\n timeout = request.json.get('timeout', 300)\n # processing\n result = subprocess.run(command, shell=True, capture_output=True, timeout=timeout) # injection sink\n return jsonify({'output': result.stdout.decode()})\n\n@app.route('/health')\ndef health():\n return jsonify({'status': 'ok'})"
}
}
},
"logicalLocations": [
{
"name": "execute_job",
"fullyQualifiedName": "handler::execute_job",
"kind": "function"
}
]
}
],
"codeFlows": [
{
"message": { "text": "Unsanitized parameter flows from API handler input to subprocess.run()" },
"threadFlows": [
{
"locations": [
{
"location": {
"physicalLocation": {
"artifactLocation": { "uri": "src/handler.py", "uriBaseId": "SRCROOT" },
"region": {
"startLine": 38,
"startColumn": 15,
"snippet": { "text": " command = request.json['command']" }
}
},
"message": { "text": "User input received: request.json['command']" }
}
},
{
"location": {
"physicalLocation": {
"artifactLocation": { "uri": "src/handler.py", "uriBaseId": "SRCROOT" },
"region": {
"startLine": 42,
"startColumn": 5,
"snippet": { "text": " result = subprocess.run(command, shell=True, capture_output=True, timeout=timeout)" }
}
},
"message": { "text": "Unsanitized value passed to subprocess.run(command, shell=True)" }
}
}
]
}
]
}
],
"fixes": [
{
"description": { "text": "Allowlist valid commands and disable shell=True. Direct remediation of the injection vector." },
"artifactChanges": [
{
"artifactLocation": { "uri": "src/handler.py", "uriBaseId": "SRCROOT" },
"replacements": [
{
"deletedRegion": { "startLine": 38, "endLine": 43 },
"insertedContent": {
"text": " ALLOWED_COMMANDS = {'status', 'health', 'version'}\n command = request.json.get('command', '')\n if command not in ALLOWED_COMMANDS:\n abort(400, 'Invalid command')\n timeout = min(int(request.json.get('timeout', 300)), 600)\n result = subprocess.run(['/usr/bin/mytool', command], shell=False, capture_output=True, timeout=timeout)\n"
}
}
]
}
]
},
{
"description": { "text": "Alternative: Add authentication middleware to the route. Does not fix the injection itself but restricts access to authorized callers." },
"artifactChanges": [
{
"artifactLocation": { "uri": "src/routes/api.ts", "uriBaseId": "SRCROOT" },
"replacements": [
{
"deletedRegion": { "startLine": 15, "endLine": 15 },
"insertedContent": {
"text": "router.post('/execute', requireAuth, executeJobHandler);\n"
}
}
]
}
]
}
],
"relatedLocations": [
{
"id": 0,
"message": { "text": "API route registration — no auth middleware in chain" },
"physicalLocation": {
"artifactLocation": { "uri": "src/routes/api.ts", "uriBaseId": "SRCROOT", "index": 1 },
"region": { "startLine": 15 }
}
}
],
"webRequest": {
"protocol": "HTTP",
"version": "1.1",
"target": "/execute",
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"body": {
"text": "{\"command\": \"id; cat /etc/passwd\"}"
}
},
"webResponse": {
"protocol": "HTTP",
"version": "1.1",
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
"text": "{\"output\": \"uid=1000(appuser) gid=1000(appuser)\\nroot:x:0:0:root:/root:/bin/bash\\n...\"}"
}
},
"properties": {
"ai/exploitability": "demonstrated",
"ai/attackerPosition": "unauthenticated-network",
"ai/evidence": [
{
"strength": "demonstrated",
"scope": "service",
"backing": [
"sarif:/runs/0/results/0/codeFlows/0",
"sarif:/runs/0/results/0/webRequest",
"sarif:/runs/0/results/0/webResponse"
],
"note": "End-to-end: crafted POST to /execute returns injected command output"
}
],
"ai/handoff": "## For remediation\nAn allowlist already exists at `config/commands.yaml` but isn't wired up — Approach 1 in `fixes[]` should consult it rather than inlining `ALLOWED_COMMANDS`.\n\n## Open questions (human)\nIs `/execute` reachable from the public load balancer, or internal-mesh only?"
}
},
{
"ruleId": "CWE-20/timeout-unbounded",
"ruleIndex": 1,
"kind": "fail",
"rank": 65.0,
"message": {
"text": "The 'timeout' parameter in the 'execute_job' handler is accepted as a raw integer from user input without bounds checking, allowing denial-of-service via extremely large values.",
"markdown": "## Missing Input Validation on Timeout Parameter\n\n**Rule:** `CWE-20/timeout-unbounded` · **Rank:** 65.0\n\nThe `timeout` parameter at `src/handler.py:39` is read from the request body and passed directly to `subprocess.run(timeout=...)` without bounds validation.\n\n### Evidence\n```python\n# src/handler.py:39\ntimeout = request.json.get('timeout', 300)\nresult = subprocess.run(command, shell=True, timeout=timeout) # No upper bound\n```\n\n### Mitigating Factors Checked\n- ✅ Default value of 300 exists (mitigates absent parameter)\n- ❌ No upper bound enforced (caller can specify timeout=999999)\n- ❌ No rate limiting on the endpoint\n\n### Recommended Fix\nEnforce bounds on the timeout value:\n```python\nMAX_TIMEOUT = 600 # 10 minutes\ntimeout = min(int(request.json.get('timeout', 300)), MAX_TIMEOUT)\nif timeout <= 0:\n abort(400, 'Timeout must be positive')\n```\n\n### References\n- [CWE-20: Improper Input Validation](https://cwe.mitre.org/data/definitions/20.html)"
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": "src/handler.py",
"uriBaseId": "SRCROOT",
"index": 0
},
"region": {
"startLine": 39,
"startColumn": 15,
"endLine": 39,
"endColumn": 55,
"snippet": {
"text": " timeout = request.json.get('timeout', 300)"
}
},
"contextRegion": {
"startLine": 37,
"endLine": 43,
"snippet": {
"text": "def execute_job(request):\n command = request.json['command']\n timeout = request.json.get('timeout', 300)\n # processing\n result = subprocess.run(command, shell=True, capture_output=True, timeout=timeout)\n return jsonify({'output': result.stdout.decode()})"
}
}
},
"logicalLocations": [
{
"name": "execute_job",
"fullyQualifiedName": "handler::execute_job",
"kind": "function"
}
]
}
],
"fixes": [
{
"description": { "text": "Add upper-bound validation and positive-integer check on the timeout parameter" },
"artifactChanges": [
{
"artifactLocation": { "uri": "src/handler.py", "uriBaseId": "SRCROOT" },
"replacements": [
{
"deletedRegion": { "startLine": 39, "endLine": 39 },
"insertedContent": {
"text": " MAX_TIMEOUT = 600\n timeout = min(int(request.json.get('timeout', 300)), MAX_TIMEOUT)\n if timeout <= 0:\n abort(400, 'Timeout must be positive')\n"
}
}
]
}
]
}
],
"properties": {
"ai/exploitability": "theoretical",
"ai/attackerPosition": "unauthenticated-network",
"ai/evidence": [
{
"strength": "theoretical",
"scope": "service",
"note": "Static reasoning only — no upper bound on caller-supplied timeout; not exercised dynamically"
}
]
}
}
]
}
]
}