|
| 1 | +package org.owasp.benchmarkutils.score.parsers; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import org.json.JSONArray; |
| 6 | +import org.json.JSONException; |
| 7 | +import org.json.JSONObject; |
| 8 | +import org.owasp.benchmarkutils.score.BenchmarkScore; |
| 9 | +import org.owasp.benchmarkutils.score.CweNumber; |
| 10 | +import org.owasp.benchmarkutils.score.ResultFile; |
| 11 | +import org.owasp.benchmarkutils.score.TestCaseResult; |
| 12 | +import org.owasp.benchmarkutils.score.TestSuiteResults; |
| 13 | + |
| 14 | +public class SnykReader extends Reader { |
| 15 | + |
| 16 | + public static final int INVALID_RULE_ID = -1; |
| 17 | + private static final Map<String, Integer> snykCweMap = |
| 18 | + new HashMap<String, Integer>() { |
| 19 | + { |
| 20 | + put("Xpath", CweNumber.XPATH_INJECTION); |
| 21 | + put("WebCookieWithSecureFalse", CweNumber.INSECURE_COOKIE); |
| 22 | + put("Sqli", CweNumber.SQL_INJECTION); |
| 23 | + put("PT", CweNumber.PATH_TRAVERSAL); |
| 24 | + put("HardcodedPassword", 0); |
| 25 | + put("WebCookieMissesCallToSetHttpOnly", CweNumber.INSECURE_COOKIE); |
| 26 | + put("ServerInformationExposure", 0); |
| 27 | + put("UserControlledFormatString", CweNumber.EXTERNALLY_CONTROLLED_STRING); |
| 28 | + put("SpringCSRF", CweNumber.CSRF); |
| 29 | + put("TrustBoundaryViolation", CweNumber.TRUST_BOUNDARY_VIOLATION); |
| 30 | + put("CommandInjection", CweNumber.COMMAND_INJECTION); |
| 31 | + put("DOMXSS", CweNumber.XSS); |
| 32 | + put("XSS", CweNumber.XSS); |
| 33 | + put("InsecureCipherNoIntegrity", CweNumber.WEAK_CRYPTO_ALGO); |
| 34 | + put("InsecureDefaultAesCipher", CweNumber.WEAK_CRYPTO_ALGO); |
| 35 | + put("HttpResponseSplitting", CweNumber.HTTP_RESPONSE_SPLITTING); |
| 36 | + put("InsecureSecret", CweNumber.WEAK_RANDOM); |
| 37 | + put("LdapInjection", CweNumber.LDAP_INJECTION); |
| 38 | + put("InsecureCipher", CweNumber.WEAK_CRYPTO_ALGO); |
| 39 | + put("InsecureHash", CweNumber.WEAK_HASH_ALGO); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean canRead(ResultFile resultFile) { |
| 45 | + return resultFile.isJson() && isSnyk(resultFile); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public TestSuiteResults parse(ResultFile resultFile) throws Exception { |
| 50 | + TestSuiteResults tr = new TestSuiteResults("Snyk", true, TestSuiteResults.ToolType.SAST); |
| 51 | + |
| 52 | + JSONArray results = |
| 53 | + resultFile.json().getJSONArray("runs").getJSONObject(0).getJSONArray("results"); |
| 54 | + |
| 55 | + for (int result = 0; result < results.length(); result++) { |
| 56 | + TestCaseResult tcr = parseSnykFindings(results.getJSONObject(result)); |
| 57 | + if (tcr != null) { |
| 58 | + tr.put(tcr); |
| 59 | + } |
| 60 | + } |
| 61 | + return tr; |
| 62 | + } |
| 63 | + |
| 64 | + private TestCaseResult parseSnykFindings(JSONObject result) { |
| 65 | + try { |
| 66 | + String className = |
| 67 | + result.getJSONArray("locations") |
| 68 | + .getJSONObject(0) |
| 69 | + .getJSONObject("physicalLocation") |
| 70 | + .getJSONObject("artifactLocation") |
| 71 | + .getString("uri"); |
| 72 | + className = (className.substring(className.lastIndexOf('/') + 1)).split("\\.")[0]; |
| 73 | + if (className.startsWith(BenchmarkScore.TESTCASENAME)) { |
| 74 | + |
| 75 | + TestCaseResult tcr = new TestCaseResult(); |
| 76 | + |
| 77 | + String ruleId = result.getString("ruleId"); |
| 78 | + ruleId = (ruleId.substring(ruleId.lastIndexOf('/') + 1)).split("\\.")[0]; |
| 79 | + |
| 80 | + int cwe = snykCweMap.getOrDefault(ruleId, INVALID_RULE_ID); |
| 81 | + |
| 82 | + if (cwe == INVALID_RULE_ID) { |
| 83 | + System.out.println("CWE # not parseable from: " + ruleId); |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + String evidence = result.getJSONObject("message").getString("text"); |
| 88 | + |
| 89 | + tcr.setCWE(cwe); |
| 90 | + tcr.setCategory(ruleId); |
| 91 | + tcr.setEvidence(evidence); |
| 92 | + tcr.setConfidence(0); |
| 93 | + tcr.setNumber(testNumber(className)); |
| 94 | + |
| 95 | + return tcr; |
| 96 | + } |
| 97 | + } catch (Exception ex) { |
| 98 | + ex.printStackTrace(); |
| 99 | + } |
| 100 | + |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + private Boolean isSnyk(ResultFile resultFile) { |
| 105 | + |
| 106 | + try { |
| 107 | + return resultFile |
| 108 | + .json() |
| 109 | + .getJSONArray("runs") |
| 110 | + .getJSONObject(0) |
| 111 | + .getJSONObject("tool") |
| 112 | + .getJSONObject("driver") |
| 113 | + .getString("name") |
| 114 | + .equalsIgnoreCase("SnykCode"); |
| 115 | + } catch (JSONException e) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments