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(engine): add new QueryID pattern #7313

Merged
merged 17 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
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
2 changes: 1 addition & 1 deletion e2e/fixtures/schemas/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
},
"query_id": {
"type": "string",
"pattern": "^[a-f0-9]{8}-[a-f0-9]{4}-4{1}[a-f0-9]{3}-[89ab]{1}[a-f0-9]{3}-[a-f0-9]{12}$"
"pattern": "^(?<uuid>[a-f0-9]{8}-[a-f0-9]{4}-4{1}[a-f0-9]{3}-[89ab]{1}[a-f0-9]{3}-[a-f0-9]{12})|(?<cxone>(t:|p:|a:)(\\d{1,20}))$"
},
"query_url": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package testcases

// E2E-CLI-098
// should perform the scan successfully and return exit code 50
// this test sample contains a different query_id
// that is not a UUID, but contains a prefix ('t:', 'p:', or 'a:') + uint64
func init() { //nolint
testSample := TestCase{
Name: "should perform a valid scan and return one HIGH result [E2E-CLI-098]",
Args: args{
Args: []cmdArgs{
[]string{"scan", "-o", "/path/e2e/output",
"--output-name", "E2E_CLI_098_RESULT",
"-q", "\"/path/test/fixtures/new_queryid_validation\"",
"-p", "\"/path/test/fixtures/new_queryid_validation/Dockerfile\"",
// QueryID 'a:123' does not exist, however, since the first one does, it should perform the scan successfully
"-i", "t:8820143918834007824,a:123",
},
},
ExpectedResult: []ResultsValidation{
{
ResultsFile: "E2E_CLI_098_RESULT",
ResultsFormats: []string{"json"},
},
},
},
WantStatus: []int{50},
}

Tests = append(Tests, testSample)
}
11 changes: 9 additions & 2 deletions internal/console/flags/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ var flagValidationFuncs = flagValidationFuncsMap{
}

func isQueryID(id string) bool {
re := regexp.MustCompile(`^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$`)
return re.MatchString(id)
uuidRegex := regexp.MustCompile(`^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$`)
isQueryID := uuidRegex.MatchString(id)
if !isQueryID {
// (t:|p:|a:) matches strings starting with 't:', 'p:', or 'a:'
// (\d{1,20}) ensures the numeric part has 1 to 20 digits (uint64 validation)
cxoneRegex := regexp.MustCompile(`^(t:|p:|a:)(\d{1,20})$`)
isQueryID = cxoneRegex.MatchString(id)
}
return isQueryID
}

func convertSliceToDummyMap(slice []string) map[string]string {
Expand Down
20 changes: 20 additions & 0 deletions internal/console/flags/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ func TestFlags_isQueryID(t *testing.T) {
id: "test",
expected: false,
},
{
name: "for prefix 't:' should return that query id is valid",
id: "t:12345678901234567890",
expected: true,
},
{
name: "for prefix 'p:' should return that query id is valid",
id: "p:8820143918834007824",
expected: true,
},
{
name: "for prefix 'a:' should return that query id is valid",
id: "a:8820143918834007824",
expected: true,
},
{
name: "should return that query id is invalid because uint exceeds 20 length",
id: "t:123456789012345678901",
expected: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/new_queryid_validation/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM alpine:2.6
USER guest
RUN npm install
12 changes: 12 additions & 0 deletions test/fixtures/new_queryid_validation/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "t:8820143918834007824",
"queryName": "Last User Is 'root'",
"severity": "HIGH",
"category": "Best Practices",
"descriptionText": "Leaving the last user as root can cause security risks. Change to another user after running the commands the need privileges",
"descriptionUrl": "https://docs.docker.com/engine/reference/builder/#user",
"platform": "Dockerfile",
"descriptionID": "f445bd25",
"cwe": "250",
"oldSeverity": "MEDIUM"
}
19 changes: 19 additions & 0 deletions test/fixtures/new_queryid_validation/query.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package Cx

import data.generic.dockerfile as dockerLib

CxPolicy[result] {
resource := input.document[i].command[name]
dockerLib.check_multi_stage(name, input.document[i].command)

userCmd := [x | resource[j].Cmd == "user"; x := resource[j]]
userCmd[minus(count(userCmd), 1)].Value[0] == "guest"

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("FROM={{%s}}.{{%s}}", [name, userCmd[minus(count(userCmd), 1)].Original]),
"issueType": "IncorrectValue",
"keyExpectedValue": "Last User shouldn't be guest",
"keyActualValue": "Last User is guest",
}
}
Loading