Skip to content

Commit c48f438

Browse files
authored
Add Code Flows to finding's description for SARIF test results (DefectDojo#6719)
* Adjusted description of SARIF findings to include Codeflow as well * Fixed some bugs * Added code snipppets, handled case if no column is provided and adjusted unit tests * Removed unneeded import * Adjusted flake8 findings
1 parent 48d6588 commit c48f438

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

dojo/tools/sarif/parser.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,28 @@ def get_snippet(result):
192192
return snippet
193193

194194

195+
def get_codeFlowsDescription(codeFlows):
196+
for codeFlow in codeFlows:
197+
if 'threadFlows' not in codeFlow:
198+
continue
199+
for threadFlow in codeFlow['threadFlows']:
200+
if 'locations' not in threadFlow:
201+
continue
202+
203+
description = '**Code flow:**\n'
204+
for location in threadFlow['locations']:
205+
physicalLocation = location['location']['physicalLocation']
206+
region = physicalLocation['region']
207+
description += '\t' + physicalLocation['artifactLocation']['uri'] + ':' + str(region['startLine'])
208+
if 'startColumn' in region:
209+
description += ':' + str(region['startColumn'])
210+
if 'snippet' in region:
211+
description += '\t-\t' + region['snippet']['text']
212+
description += '\n'
213+
214+
return description
215+
216+
195217
def get_description(result, rule):
196218
description = ''
197219
message = ''
@@ -213,6 +235,9 @@ def get_description(result, rule):
213235
if fullDescription != message and fullDescription != shortDescription:
214236
description += '**Rule full description:** {}\n'.format(fullDescription)
215237

238+
if 'codeFlows' in result:
239+
description += get_codeFlowsDescription(result['codeFlows'])
240+
216241
if description.endswith('\n'):
217242
description = description[:-1]
218243

unittests/tools/test_sarif_parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ def test_example2_report(self):
4242
```add_core(ptr, offset, val);
4343
return;```
4444
**Rule short description:** A variable was used without being initialized.
45-
**Rule full description:** A variable was used without being initialized. This can result in runtime errors such as null reference exceptions."""
45+
**Rule full description:** A variable was used without being initialized. This can result in runtime errors such as null reference exceptions.
46+
**Code flow:**
47+
\tcollections/list.h:15\t-\tint *ptr;
48+
\tcollections/list.h:15\t-\toffset = (y + z) * q + 1;
49+
\tcollections/list.h:25\t-\tadd_core(ptr, offset, val)"""
4650
self.assertEqual(description, item.description)
4751
self.assertEqual(datetime.datetime(2016, 7, 16, 14, 19, 1, tzinfo=datetime.timezone.utc), item.date)
4852
for finding in findings:

0 commit comments

Comments
 (0)