Skip to content

Commit 658b59d

Browse files
author
Dave Wichers
committed
Add error handling for SarifReader parsing of locations block in a
result because CodeSonar SARIF files sometimes don't include locations blocks in their findings.
1 parent 3a2b00a commit 658b59d

File tree

1 file changed

+20
-5
lines changed
  • plugin/src/main/java/org/owasp/benchmarkutils/score/parsers/sarif

1 file changed

+20
-5
lines changed

plugin/src/main/java/org/owasp/benchmarkutils/score/parsers/sarif/SarifReader.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,26 @@ private TestCaseResult testCaseResultFor(JSONObject result, Map<String, Integer>
264264
}
265265

266266
private static String resultUri(JSONObject result) {
267-
return result.getJSONArray("locations")
268-
.getJSONObject(0)
269-
.getJSONObject("physicalLocation")
270-
.getJSONObject("artifactLocation")
271-
.getString("uri");
267+
// This try/catch was added because CodeSonar SARIF results sometimes don't have locations
268+
// elements. The have fingerprints and partialFingerprints elements which might refer
269+
// back to findings of the same type that do include proper locations elements
270+
try {
271+
return result.getJSONArray("locations")
272+
.getJSONObject(0)
273+
.getJSONObject("physicalLocation")
274+
.getJSONObject("artifactLocation")
275+
.getString("uri");
276+
} catch (Exception e) {
277+
System.err.println(
278+
"WARNING: "
279+
+ e.getMessage()
280+
+ " for rule: "
281+
+ result.getString("ruleId")
282+
+ " with message: \""
283+
+ result.getJSONObject("message").getString("text")
284+
+ "\". Skipping this finding.");
285+
return "NoResultURIFound";
286+
}
272287
}
273288

274289
/**

0 commit comments

Comments
 (0)