Skip to content
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

feat(cwe): add cwe into sarif report and KICS CLI results #6845

Merged
merged 58 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
4eaedbe
feat(cwe): add cwe into sarif report and KICS CLI results
ArturRibeiro-CX Jan 4, 2024
4cf3afd
feat(cwe): add cwe into sarif report and KICS CLI results
ArturRibeiro-CX Jan 4, 2024
90bb750
feat(cwe): add cwe into sarif report and KICS CLI results
ArturRibeiro-CX Jan 4, 2024
df81fab
feat(cwe): add cwe into sarif report and KICS CLI results
ArturRibeiro-CX Jan 5, 2024
9636be8
feat(cwe): add cwe into sarif report and KICS CLI results
ArturRibeiro-CX Jan 5, 2024
1722b58
Merge branch 'master' into cwe
gabriel-cx Jan 5, 2024
20cd3c4
added e2e test for cwe field in sarif report format
ArturRibeiro-CX Jan 5, 2024
060b459
Merge branch 'master' of https://github.com/Checkmarx/kics into cwe
ArturRibeiro-CX Jan 5, 2024
4fb60c3
Merge branch 'cwe' of https://github.com/Checkmarx/kics into cwe
ArturRibeiro-CX Jan 5, 2024
33a15ed
added e2e test for cwe field in sarif report format
ArturRibeiro-CX Jan 5, 2024
18f9dc9
fix cyclonedx commit on wrong branch
ArturRibeiro-CX Jan 8, 2024
4698101
fix cyclonedx commit on wrong branch
ArturRibeiro-CX Jan 8, 2024
aeec710
fix cyclonedx commit on wrong branch and remove comments
ArturRibeiro-CX Jan 8, 2024
4a6ffda
Merge branch 'master' into cwe
gabriel-cx Jan 9, 2024
ca788e3
remove cwe from sarifMessage
ArturRibeiro-CX Jan 9, 2024
b15da83
changes to addition of cwe into sarif report and KICS CLI results
ArturRibeiro-CX Jan 19, 2024
cfa3df8
fixing potential file inclusion via variable error by cleaning paths
ArturRibeiro-CX Jan 19, 2024
6aad350
linting issues fixed
ArturRibeiro-CX Jan 19, 2024
5d26283
fixing linting issues
ArturRibeiro-CX Jan 19, 2024
1fd0d35
Merge branch 'master' of https://github.com/Checkmarx/kics into cwe
ArturRibeiro-CX Jan 19, 2024
3ebc66c
fix linting e2e and naming
ArturRibeiro-CX Jan 19, 2024
842f2e4
fix linting problems
ArturRibeiro-CX Jan 19, 2024
a516a11
fixing linting
ArturRibeiro-CX Jan 19, 2024
b548e42
fixing linting problem sarif.go
ArturRibeiro-CX Jan 19, 2024
8cd6e4b
change to result-sarif.json to accept the new sarif format
ArturRibeiro-CX Jan 19, 2024
30c5e55
changes to result-sarif-required.json to accept new sarif format
ArturRibeiro-CX Jan 19, 2024
e797987
e2e for sarif report format
ArturRibeiro-CX Jan 19, 2024
30ae8bf
change bool to boolean type in result-sarif.json
ArturRibeiro-CX Jan 19, 2024
f4c5c04
addition of fullDescription text field as empty string to fix e2e error
ArturRibeiro-CX Jan 19, 2024
c854f72
fixing fullDescription text required issue on e2e
ArturRibeiro-CX Jan 19, 2024
9a827a1
Merge branch 'master' of https://github.com/Checkmarx/kics into cwe
ArturRibeiro-CX Jan 19, 2024
cfa1383
fullDescription equal to shortDescription in taxonomies when empty an…
ArturRibeiro-CX Jan 19, 2024
f23a0f9
last fix e2e result sarif files
ArturRibeiro-CX Jan 19, 2024
dfe8739
add path to uri locations in E2E_CLI_080 and E2E_CLI_070
ArturRibeiro-CX Jan 22, 2024
a256ac0
fix paths E2E_CLI 069 and 070
ArturRibeiro-CX Jan 22, 2024
cedc806
add new files to Dockerfile and error verification to sarif.go
ArturRibeiro-CX Jan 22, 2024
49af1ae
fix addition of new files to Dockerfile
ArturRibeiro-CX Jan 22, 2024
8770998
relationship target changed from array to object
ArturRibeiro-CX Jan 23, 2024
7786f26
fix to index being required in relationships as CWE does not require …
ArturRibeiro-CX Jan 23, 2024
1cd8a84
fix to name and guid not being required and lenght of this parameters
ArturRibeiro-CX Jan 23, 2024
ffebe5b
fix id lenght when parameters are empty in relationships
ArturRibeiro-CX Jan 23, 2024
8ba89af
changes to sarif report cwe field empty or not and according files
ArturRibeiro-CX Jan 24, 2024
655febf
change to name being required on toolComponent and not target in rela…
ArturRibeiro-CX Jan 24, 2024
9c48780
add test to printer with CWE field
ArturRibeiro-CX Jan 24, 2024
0e4a197
Merge branch 'master' into cwe
ArturRibeiro-CX Jan 24, 2024
9da0e25
add test case for vulnerability builder test with CWE field
ArturRibeiro-CX Jan 24, 2024
6138693
add test to summary_test
ArturRibeiro-CX Jan 25, 2024
86ef7e0
changes to summary_test to have CWE complete
ArturRibeiro-CX Jan 25, 2024
f5ca9b6
add tests for taxonomies and taxa fields in sarif report
ArturRibeiro-CX Jan 25, 2024
8f40e37
add one more test to taxonomies definition with no CWE field
ArturRibeiro-CX Jan 25, 2024
5634af0
added test for reading the csv file with CWE info correctly
ArturRibeiro-CX Jan 25, 2024
85b338f
add new cwe_csv to all dockerfiles in docker directory
ArturRibeiro-CX Jan 26, 2024
646f5a5
test Dockerfile change by coppying all directory
ArturRibeiro-CX Jan 29, 2024
82c3962
changes to all dockerfile necessary to copy cwe_csv directory without…
ArturRibeiro-CX Jan 29, 2024
8670aaa
fix typo replacing Run with RUN on Dockerfile in kics root
ArturRibeiro-CX Jan 29, 2024
30846f7
remove '/' from COPY statement in Dockerfiles to maintain the same logic
ArturRibeiro-CX Jan 29, 2024
6c4c407
Merge branch 'master' into cwe
asofsilva Feb 5, 2024
1fafda4
Merge branch 'master' into cwe
gabriel-cx Feb 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
488 changes: 488 additions & 0 deletions e2e/fixtures/E2E_CLI_077_RESULT

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions e2e/testcases/e2e-cli-077_cwe_sarif_result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package testcases

// E2E-CLI-077 - KICS scan
// should perform a scan saving the reports in sarif format, completing the cwe field when it has values
func init() { //nolint
testSample := TestCase{
Name: "should perform a scan saving the reports in sarif format, completing the cwe field when it has values [E2E-CLI-077]",
Args: args{
Args: []cmdArgs{
[]string{"scan", "-o", "/path/e2e/output",
"--output-name", "E2E_CLI_077_RESULT",
"-p", "\"/path/test/fixtures/test_sarif_cwe_report\"",
"--report-formats", "sarif",
},
},
ExpectedResult: []ResultsValidation{
{
ResultsFile: "E2E_CLI_077_RESULT",
ResultsFormats: []string{"sarif"},
},
},
},
WantStatus: []int{50},
}

Tests = append(Tests, testSample)
}
1 change: 1 addition & 0 deletions pkg/engine/vulnerability_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ var DefaultVulnerabilityBuilder = func(ctx *QueryContext, tracker Tracker,
DescriptionID: getStringFromMap("descriptionID", DefaultQueryDescriptionID, overrideKey, vObj, &logWithFields),
Severity: severity,
Platform: getStringFromMap("platform", "", overrideKey, vObj, &logWithFields),
CWE: getStringFromMap("cwe", "", overrideKey, vObj, &logWithFields),
Line: linesVulne.Line,
VulnLines: linesVulne.VulnLines,
ResourceType: PtrStringToString(mustMapKeyToString(vObj, "resourceType")),
Expand Down
2 changes: 2 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type QueryMetadata struct {
Content string
Metadata map[string]interface{}
Platform string
CWE string
// special field for generic queries
// represents how many queries are aggregated into a single rego file
Aggregation int
Expand All @@ -161,6 +162,7 @@ type Vulnerability struct {
Description string `json:"description"`
DescriptionID string `json:"descriptionID"`
Platform string `db:"platform" json:"platform"`
CWE string `db:"cwe" json:"cwe"`
Severity Severity `json:"severity"`
Line int `json:"line"`
VulnLines *[]CodeLine `json:"vulnLines"`
Expand Down
2 changes: 2 additions & 0 deletions pkg/model/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type QueryResult struct {
QueryURI string `json:"query_url"`
Severity Severity `json:"severity"`
Platform string `json:"platform"`
CWE string `json:"cwe,omitempty"`
CloudProvider string `json:"cloud_provider,omitempty"`
Category string `json:"category"`
Experimental bool `json:"experimental"`
Expand Down Expand Up @@ -200,6 +201,7 @@ func CreateSummary(counters Counters, vulnerabilities []Vulnerability,
Severity: item.Severity,
QueryURI: item.QueryURI,
Platform: item.Platform,
CWE: item.CWE,
Experimental: item.Experimental,
CloudProvider: strings.ToUpper(item.CloudProvider),
Category: item.Category,
Expand Down
4 changes: 4 additions & 0 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func PrintResult(summary *model.Summary, failedQueries map[string]error, printer
}
fmt.Printf("%s %s\n", printer.Bold("Platform:"), summary.Queries[idx].Platform)

if summary.Queries[idx].CWE != "" {
fmt.Printf("%s %s\n", printer.Bold("CWE:"), summary.Queries[idx].CWE)
}

queryCloudProvider := summary.Queries[idx].CloudProvider
if queryCloudProvider != "" {
queryCloudProvider = strings.ToLower(queryCloudProvider) + "/"
Expand Down
1 change: 1 addition & 0 deletions pkg/report/model/cyclonedx.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,6 @@ func BuildCycloneDxReport(summary *model.Summary, filePaths map[string]string) *

bom.Components.Components = append(bom.Components.Components, component)
}

return bom
}
7 changes: 5 additions & 2 deletions pkg/report/model/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,11 @@ func (sr *sarifReport) BuildSarifIssue(issue *model.QueryResult) {
ResultRuleIndex: ruleIndex,
ResultKind: kind,
ResultMessage: sarifMessage{
Text: issue.Files[idx].KeyActualValue,
MessageProperties: sarifProperties{"platform": issue.Platform},
Text: issue.Files[idx].KeyActualValue,
MessageProperties: sarifProperties{
"platform": issue.Platform,
"cwe": issue.CWE,
},
},
ResultLocations: []sarifLocation{
{
Expand Down
8 changes: 4 additions & 4 deletions pkg/report/model/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var sarifTests = []sarifTest{
ResultRuleID: "1",
ResultRuleIndex: 0,
ResultKind: "fail",
ResultMessage: sarifMessage{Text: "test", MessageProperties: sarifProperties{"platform": ""}},
ResultMessage: sarifMessage{Text: "test", MessageProperties: sarifProperties{"platform": "", "cwe": ""}},
ResultLocations: []sarifLocation{
{
PhysicalLocation: sarifPhysicalLocation{
Expand Down Expand Up @@ -195,7 +195,7 @@ var sarifTests = []sarifTest{
ResultKind: "fail",
ResultMessage: sarifMessage{
Text: "test",
MessageProperties: sarifProperties{"platform": ""},
MessageProperties: sarifProperties{"platform": "", "cwe": ""},
},
ResultLocations: []sarifLocation{
{
Expand All @@ -212,7 +212,7 @@ var sarifTests = []sarifTest{
ResultKind: "fail",
ResultMessage: sarifMessage{
Text: "test",
MessageProperties: sarifProperties{"platform": ""},
MessageProperties: sarifProperties{"platform": "", "cwe": ""},
},
ResultLocations: []sarifLocation{
{
Expand All @@ -229,7 +229,7 @@ var sarifTests = []sarifTest{
ResultKind: "informational",
ResultMessage: sarifMessage{
Text: "test",
MessageProperties: sarifProperties{"platform": ""},
MessageProperties: sarifProperties{"platform": "", "cwe": ""},
},
ResultLocations: []sarifLocation{
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "20f14e1a-a899-4e79-9f09-b6a84cd4649b",
"queryName": "Run Block Injection",
"severity": "HIGH",
"category": "Insecure Configurations",
"descriptionText": "GitHub Actions workflows can be triggered by a variety of events. Every workflow trigger is provided with a GitHub context that contains information about the triggering event, such as which user triggered it, the branch name, and other event context details. Some of this event data, like the base repository name, hash value of a changeset, or pull request number, is unlikely to be controlled or used for injection by the user that triggered the event.",
"descriptionUrl": "https://securitylab.github.com/research/github-actions-untrusted-input/",
"platform": "CICD",
"descriptionID": "02044a75",
"cloudProvider": "common",
"cwe": "1234"
}
186 changes: 186 additions & 0 deletions test/fixtures/test_sarif_cwe_report/run_block_injection/query.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package Cx

import data.generic.common as common_lib

CxPolicy[result] {

input.document[i].on["pull_request_target"]
run := input.document[i].jobs[j].steps[k].run

patterns := [
"github.head_ref",
"github.event.pull_request.body",
"github.event.pull_request.head.label",
"github.event.pull_request.head.ref",
"github.event.pull_request.head.repo.default_branch",
"github.event.pull_request.head.repo.description",
"github.event.pull_request.head.repo.homepage",
"github.event.pull_request.title"
]

matched = containsPatterns(run, patterns)

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("run={{%s}}", [run]),
"issueType": "IncorrectValue",
"keyExpectedValue": "Run block does not contain dangerous input controlled by user.",
"keyActualValue": "Run block contains dangerous input controlled by user.",
"searchLine": common_lib.build_search_line(["jobs", j, "steps", k, "run"],[]),
"searchValue": matched[m]
}
}

CxPolicy[result] {

input.document[i].on["issues"]
run := input.document[i].jobs[j].steps[k].run

patterns := [
"github.event.issue.body",
"github.event.issue.title"
]

matched = containsPatterns(run, patterns)

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("run={{%s}}", [run]),
"issueType": "IncorrectValue",
"keyExpectedValue": "Run block does not contain dangerous input controlled by user.",
"keyActualValue": "Run block contains dangerous input controlled by user.",
"searchLine": common_lib.build_search_line(["jobs", j, "steps", k, "run"],[]),
"searchValue": matched[m]
}
}

CxPolicy[result] {

input.document[i].on["issue_comment"]
run := input.document[i].jobs[j].steps[k].run

patterns := [
"github.event.comment.body",
"github.event.issue.body",
"github.event.issue.title"
]

matched = containsPatterns(run, patterns)

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("run={{%s}}", [run]),
"issueType": "IncorrectValue",
"keyExpectedValue": "Run block does not contain dangerous input controlled by user.",
"keyActualValue": "Run block contains dangerous input controlled by user.",
"searchLine": common_lib.build_search_line(["jobs", j, "steps", k, "run"],[]),
"searchValue": matched[m]
}
}

CxPolicy[result] {

input.document[i].on["discussion"]
run := input.document[i].jobs[j].steps[k].run

patterns := [
"github.event.discussion.body",
"github.event.discussion.title"
]

matched = containsPatterns(run, patterns)

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("run={{%s}}", [run]),
"issueType": "IncorrectValue",
"keyExpectedValue": "Run block does not contain dangerous input controlled by user.",
"keyActualValue": "Run block contains dangerous input controlled by user.",
"searchLine": common_lib.build_search_line(["jobs", j, "steps", k, "run"],[]),
"searchValue": matched[m]
}
}

CxPolicy[result] {

input.document[i].on["discussion_comment"]
run := input.document[i].jobs[j].steps[k].run

patterns := [
"github.event.comment.body",
"github.event.discussion.body",
"github.event.discussion.title"
]

matched = containsPatterns(run, patterns)

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("run={{%s}}", [run]),
"issueType": "IncorrectValue",
"keyExpectedValue": "Run block does not contain dangerous input controlled by user.",
"keyActualValue": "Run block contains dangerous input controlled by user.",
"searchLine": common_lib.build_search_line(["jobs", j, "steps", k, "run"],[]),
"searchValue": matched[m]
}
}

CxPolicy[result] {

input.document[i].on["workflow_run"]
run := input.document[i].jobs[j].steps[k].run

patterns := [
"github.event.workflow.path",
"github.event.workflow_run.head_branch",
"github.event.workflow_run.head_commit.author.email",
"github.event.workflow_run.head_commit.author.name",
"github.event.workflow_run.head_commit.message",
"github.event.workflow_run.head_repository.description"
]

matched = containsPatterns(run, patterns)

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("run={{%s}}", [run]),
"issueType": "IncorrectValue",
"keyExpectedValue": "Run block does not contain dangerous input controlled by user.",
"keyActualValue": "Run block contains dangerous input controlled by user.",
"searchLine": common_lib.build_search_line(["jobs", j, "steps", k, "run"],[]),
"searchValue": matched[m]
}
}

CxPolicy[result] {

input.document[i].on["author"]
run := input.document[i].jobs[j].steps[k].run

patterns := [
"github.*.authors.name",
"github.*.authors.email"
]

matched = containsPatterns(run, patterns)

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("run={{%s}}", [run]),
"issueType": "IncorrectValue",
"keyExpectedValue": "Run block does not contain dangerous input controlled by user.",
"keyActualValue": "Run block contains dangerous input controlled by user.",
"searchLine": common_lib.build_search_line(["jobs", j, "steps", k, "run"],[]),
"searchValue": matched[m]
}
}



containsPatterns(str, patterns) = matched {
matched := {pattern |
pattern := patterns[_]
regex.match(pattern, str)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: check-go-coverage

on:
pull_request_target:
branches: [master]

jobs:
coverage:
name: Check Go coverage
runs-on: ubuntu-latest
steps:
- name: Checkout Source
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up Go 1.20.x
uses: actions/setup-go@v4
with:
go-version: 1.20.x
- name: Run test metrics script
id: testcov
run: |
make test-coverage-report | tee test-results
echo "coverage=$(cat test-results | grep "Total coverage: " test-results | cut -d ":" -f 2 | bc)" >> $GITHUB_ENV
- name: Checks if Go coverage is at least 80%
if: env.coverage < 80
run: |
echo "Go coverage is lower than 80%: ${{ env.coverage }}%"
exit 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Issue Workflow

on:
issues:
types:
- opened

jobs:
process_issue:
runs-on: ubuntu-latest
steps:
- name: Greet the New Issue
run: |
# Echo a simple sentence
echo "Hello, a new issue has been opened!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Discussion Workflow

on:
discussion:
types:
- created

jobs:
process_discussion:
runs-on: ubuntu-latest
steps:
- name: Greet the New Discussion
run: |
echo "Hello, a new discussion has been created!"
Loading
Loading