- 
                Notifications
    
You must be signed in to change notification settings  - Fork 61
 
Add Support for GitLab SAST #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 11 commits
2699668
              8e5395d
              2d6273e
              663d0e1
              bd9d5b0
              b89219a
              99bd120
              a3dc848
              91f7518
              17466f7
              ab1a3b3
              3311792
              923d9c6
              5986a0c
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package org.owasp.benchmarkutils.score.parsers; | ||
| 
     | 
||
| import org.json.JSONArray; | ||
| import org.json.JSONObject; | ||
| import org.owasp.benchmarkutils.score.*; | ||
| 
     | 
||
| public class GitLabSastReader extends Reader { | ||
| @Override | ||
| public boolean canRead(ResultFile resultFile) { | ||
| return resultFile.isJson() | ||
| && resultFile.json().has("scan") | ||
| && resultFile | ||
| .json() | ||
| .getJSONObject("scan") | ||
| .getJSONObject("analyzer") | ||
| .getJSONObject("vendor") | ||
| .getString("name") | ||
| .equalsIgnoreCase("GitLab"); | ||
| } | ||
| 
     | 
||
| @Override | ||
| public TestSuiteResults parse(ResultFile resultFile) throws Exception { | ||
| TestSuiteResults tr = new TestSuiteResults("GitLab-SAST", true, TestSuiteResults.ToolType.SAST); | ||
| 
     | 
||
| JSONArray vulnerabilities = resultFile.json().getJSONArray("vulnerabilities"); | ||
| 
     | 
||
| for (int vulnerability = 0; vulnerability < vulnerabilities.length(); vulnerability++) { | ||
| TestCaseResult tcr = parseGitLabSastFindings(vulnerabilities.getJSONObject(vulnerability)); | ||
| if (tcr != null) { | ||
| tr.put(tcr); | ||
| } | ||
| } | ||
| return tr; | ||
| } | ||
| 
     | 
||
| private TestCaseResult parseGitLabSastFindings(JSONObject vulnerability) { | ||
| 
     | 
||
| try { | ||
| String className = vulnerability.getJSONObject("location").getString("file"); | ||
| className = (className.substring(className.lastIndexOf('/') + 1)).split("\\.")[0]; | ||
| 
     | 
||
| if (className.startsWith(BenchmarkScore.TESTCASENAME)) { | ||
                
       | 
||
| TestCaseResult tcr = new TestCaseResult(); | ||
| 
     | 
||
| JSONArray identifiers = vulnerability.getJSONArray("identifiers"); | ||
| 
     | 
||
| int cwe = identifiers.getJSONObject(1).getInt("value"); | ||
| cwe = translate(cwe); | ||
| 
     | 
||
| String category = identifiers.getJSONObject(2).getString("name"); | ||
| category = category.split("-")[1].strip(); | ||
| 
     | 
||
| String evidence = vulnerability.getString("cve"); | ||
| 
     | 
||
| tcr.setCWE(cwe); | ||
| tcr.setCategory(category); | ||
| tcr.setEvidence(evidence); | ||
| tcr.setConfidence(0); | ||
| tcr.setNumber(testNumber(className)); | ||
| 
     | 
||
| return tcr; | ||
| } | ||
| } catch (Exception ex) { | ||
| ex.printStackTrace(); | ||
| } | ||
| 
     | 
||
| return null; | ||
| } | ||
| 
     | 
||
| private int translate(int cwe) { | ||
| 
     | 
||
| switch (cwe) { | ||
| case 22: | ||
| return CweNumber.PATH_TRAVERSAL; | ||
| case 79: | ||
| return CweNumber.XSS; | ||
| case 89: | ||
| return CweNumber.SQL_INJECTION; | ||
| case 90: | ||
| return CweNumber.LDAP_INJECTION; | ||
| case 113: | ||
| return CweNumber.HTTP_RESPONSE_SPLITTING; | ||
| case 185: | ||
| return CweNumber.COMMAND_INJECTION; | ||
| case 326: | ||
| case 327: | ||
| case 328: | ||
| return CweNumber.WEAK_CRYPTO_ALGO; | ||
| case 338: | ||
| return CweNumber.WEAK_RANDOM; | ||
| case 614: | ||
| return CweNumber.INSECURE_COOKIE; | ||
| case 643: | ||
| return CweNumber.XPATH_INJECTION; | ||
| case 1004: | ||
| return CweNumber.COOKIE_WITHOUT_HTTPONLY; | ||
| case 259: | ||
| case 306: | ||
| break; | ||
| default: | ||
| System.out.println( | ||
| "INFO: Found following CWE in GitLab SAST results which we haven't seen before: " | ||
| + cwe); | ||
| } | ||
| 
     | 
||
| return cwe; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package org.owasp.benchmarkutils.score.parsers; | ||
                
      
                  0xgoto marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| import org.json.JSONArray; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.owasp.benchmarkutils.score.*; | ||
| 
     | 
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| 
     | 
||
| class GitLabSastReaderTest extends ReaderTestBase { | ||
| 
     | 
||
| private ResultFile resultFile; | ||
| 
     | 
||
| @BeforeEach | ||
| void setUp() { | ||
| resultFile = TestHelper.resultFileOf("testfiles/Benchmark_GitLab_SAST.json"); | ||
| BenchmarkScore.TESTCASENAME = "BenchmarkTest"; | ||
| } | ||
| 
     | 
||
| @Test | ||
| public void onlyGitLabSastReaderReportsCanReadAsTrue() { | ||
| assertOnlyMatcherClassIs(this.resultFile, GitLabSastReader.class); | ||
| } | ||
| 
     | 
||
| @Test | ||
| void readerHandlesGivenResultFile() throws Exception { | ||
| GitLabSastReader reader = new GitLabSastReader(); | ||
| TestSuiteResults result = reader.parse(resultFile); | ||
| 
     | 
||
| assertEquals(TestSuiteResults.ToolType.SAST, result.getToolType()); | ||
| assertTrue(result.isCommercial()); | ||
| assertEquals("GitLab-SAST", result.getToolName()); | ||
| 
     | 
||
| assertEquals(5, result.getTotalResults()); | ||
| 
     | 
||
| assertEquals(CweNumber.WEAK_CRYPTO_ALGO, result.get(1).get(0).getCWE()); | ||
| assertEquals(CweNumber.PATH_TRAVERSAL, result.get(5).get(0).getCWE()); | ||
| } | ||
| 
     | 
||
| @Test | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you test the test here? What does this test do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually before implementing a logic in the main java file, I was trying it out in the test file. Only when It passes, i add the same logic there. This is an unnecessary test, I'll remove it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please let me know, if I need to remove this test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend to remove it, since it's basically just testing the testfile, not the reader.  | 
||
| void isAbleToExtractDataToCreateTestCaseResults() { | ||
| JSONArray vulnerabilities = resultFile.json().getJSONArray("vulnerabilities"); | ||
| String path = vulnerabilities.getJSONObject(1).getJSONObject("location").getString("file"); | ||
| 
     | 
||
| assertEquals("src/main/java/org/owasp/benchmark/testcode/BenchmarkTest00001.java", path); | ||
| 
     | 
||
| String className = (path.substring(path.lastIndexOf('/') + 1)).split("\\.")[0]; | ||
| assertTrue(className.startsWith(BenchmarkScore.TESTCASENAME)); | ||
| 
     | 
||
| JSONArray identifiers = vulnerabilities.getJSONObject(1).getJSONArray("identifiers"); | ||
| int cwe = identifiers.getJSONObject(1).getInt("value"); | ||
| assertEquals(327, cwe); | ||
| 
     | 
||
| String category = identifiers.getJSONObject(2).getString("name"); | ||
| category = category.split("-")[1].strip(); | ||
| assertEquals("Cryptographic Failures", category); | ||
| 
     | 
||
| String evidence = vulnerabilities.getJSONObject(1).getString("cve"); | ||
| assertEquals("semgrep_id:find_sec_bugs.CIPHER_INTEGRITY-1:71:71", evidence); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.