Skip to content

Commit 8b965e2

Browse files
committed
Add some CodeSonar mappings and add suppress warnings for missing
locations attributes.
1 parent 0663d8c commit 8b965e2

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.json.JSONArray;
2525
import org.json.JSONException;
2626
import org.json.JSONObject;
27+
import org.owasp.benchmarkutils.score.CweNumber;
2728

2829
public class CodeSonarReader extends SarifReader {
2930

@@ -68,6 +69,31 @@ public Map<String, Integer> customRuleCweMappings(JSONObject tool) {
6869
}
6970
}
7071

72+
// CodeSonar has some non-security rules that don't map to CWEs. So we manaully add those as
73+
// DONTCARES
74+
mappings.put("Avoid zero-length array allocations (C#)", CweNumber.DONTCARE);
75+
mappings.put("Do not initialize unnecessarily (C#)", CweNumber.DONTCARE);
76+
mappings.put("Do not raise reserved exception types (C#)", CweNumber.DONTCARE);
77+
mappings.put("Do not use 'WaitAll' with a single task (C#)", CweNumber.DONTCARE);
78+
mappings.put("Identifiers should not contain underscores (C#)", CweNumber.DONTCARE);
79+
mappings.put("Mark members as static (C#)", CweNumber.DONTCARE);
80+
mappings.put("Seal internal types (C#)", CweNumber.DONTCARE); // Improves performance
81+
mappings.put(
82+
"Specify IFormatProvider (C#)", CweNumber.DONTCARE); // Localization Issue (fonts)
83+
mappings.put("Use ordinal string comparison (C#)", CweNumber.DONTCARE);
84+
mappings.put("Use XmlReader for XPathDocument constructor (C#)", CweNumber.DONTCARE);
85+
86+
// CodeSonar has numerous security rules that map to ROSLYN.SECURITY rules. For example,
87+
// they have a rule: "ROSLYN.SECURITY.CA5350 : Do Not Use Weak Cryptographic Algorithms
88+
// (C#)" that is described at:
89+
// https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca5350
90+
// Unfortunately, they don't seem to be mapped to CWEs, so we have to map them manually.
91+
mappings.put("Do Not Use Broken Cryptographic Algorithms (C#)", CweNumber.WEAK_CRYPTO_ALGO);
92+
mappings.put("Do Not Use Weak Cryptographic Algorithms (C#)", CweNumber.WEAK_CRYPTO_ALGO);
93+
mappings.put("Insecure DTD processing in XML (C#)", CweNumber.XXE);
94+
mappings.put(
95+
"Rethrow to preserve stack details (C#)",
96+
392); // CWE-392: Missing Report of Error Condition
7197
return mappings;
7298
}
7399
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,16 @@ private static String resultUri(JSONObject result) {
282282
.getJSONObject("artifactLocation")
283283
.getString("uri");
284284
} catch (Exception e) {
285-
System.err.println(
286-
"WARNING: "
287-
+ e.getMessage()
288-
+ " for rule: "
289-
+ result.getString("ruleId")
290-
+ " with message: \""
291-
+ result.getJSONObject("message").getString("text")
292-
+ "\". Skipping this finding.");
285+
if (!e.getMessage().startsWith("JSONObject[\"locations\"] not found")) {
286+
System.err.println(
287+
"WARNING: "
288+
+ e.getMessage()
289+
+ " for rule: "
290+
+ result.getString("ruleId")
291+
+ " with message: \""
292+
+ result.getJSONObject("message").getString("text")
293+
+ "\". Skipping this finding.");
294+
}
293295
return "NoResultURIFound";
294296
}
295297
}

0 commit comments

Comments
 (0)