2121import java .nio .file .Files ;
2222import java .nio .file .Paths ;
2323import org .json .JSONArray ;
24- import org .json .JSONException ;
2524import org .json .JSONObject ;
2625import org .owasp .benchmark .score .BenchmarkScore ;
2726import org .owasp .benchmark .score .TestCaseResult ;
@@ -39,42 +38,41 @@ public TestSuiteResults parse(File f) throws Exception {
3938 tr .setTime (f );
4039
4140 String content = new String (Files .readAllBytes (Paths .get (f .getPath ())));
42-
4341 JSONObject obj = new JSONObject (content );
44- // int version = obj.getInt( "formatVersion" );
45- JSONArray arr ;
4642
47- boolean hotSpotIssue = true ;
43+ parseIssues (tr , obj );
44+ parseHotspots (tr , obj );
4845
49- // Figure out if there are quality issues or security hotspots in the JSON file
50- // Each has a different JSON format.
51- try {
52- arr = obj .getJSONArray ("issues" );
53- hotSpotIssue = false ;
54- } catch (JSONException e ) {
55- try {
56- arr = obj .getJSONArray ("hotspots" );
57- } catch (JSONException e2 ) {
58- System .out .println (
59- "ERROR: Couldn't find 'issues' or 'hotspots' element in SonarQube JSON results."
60- + " Maybe not SonarQube results file?" );
61- return null ;
62- }
46+ return tr ;
47+ }
48+
49+ private void parseHotspots (TestSuiteResults tr , JSONObject obj ) {
50+ parseResults (tr , obj , true );
51+ }
52+
53+ private void parseIssues (TestSuiteResults tr , JSONObject obj ) {
54+ parseResults (tr , obj , false );
55+ }
56+
57+ private void parseResults (TestSuiteResults tr , JSONObject obj , boolean isHotspots ) {
58+ String key = isHotspots ? "hotspots" : "issues" ;
59+
60+ if (!obj .has (key )) {
61+ return ;
6362 }
6463
64+ JSONArray arr = obj .getJSONArray (key );
6565 int numIssues = arr .length ();
66- for (int i = 0 ; i < numIssues ; i ++) {
6766
67+ for (int i = 0 ; i < numIssues ; i ++) {
6868 TestCaseResult tcr =
69- (hotSpotIssue
69+ (isHotspots
7070 ? parseSonarQubeHotSpotIssue (arr .getJSONObject (i ))
7171 : parseSonarQubeQualityIssue (arr .getJSONObject (i )));
7272 if (tcr != null ) {
7373 tr .put (tcr );
7474 }
7575 }
76-
77- return tr ;
7876 }
7977
8078 /**
@@ -197,9 +195,12 @@ public static int securityCategoryCWELookup(String secCat, String message) {
197195 .equals (message )
198196 || "Ensure that string concatenation is required and safe for this SQL query."
199197 .equals (message )
198+ || "Make sure using a dynamically formatted SQL query is safe here." .equals (message )
200199 || "Make sure creating this cookie without the \" secure\" flag is safe here."
201200 .equals (message )
202201 || "Make sure that hashing data is safe here." .equals (message )
202+ || "Make sure this weak hash algorithm is not used in a sensitive context here."
203+ .equals (message )
203204 || "Make sure creating this cookie without the \" HttpOnly\" flag is safe."
204205 .equals (message ))) {
205206 System .out .println (
0 commit comments