Skip to content

Commit

Permalink
Merge pull request Checkmarx#6285 from Checkmarx/bug/kics/755
Browse files Browse the repository at this point in the history
fix(method): Added regex to calculate Levenshtein distance correctly
  • Loading branch information
cx-henriqueAlvelos authored May 3, 2023
2 parents c5aa128 + 10c887a commit 9260c42
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions assets/queries/openAPI/general/pattern_undefined/query.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Cx

import data.generic.openapi as openapi_lib
import data.generic.common as common_lib

CxPolicy[result] {
doc := input.document[i]
Expand All @@ -15,6 +16,7 @@ CxPolicy[result] {
result := {
"documentId": doc.id,
"searchKey": sprintf("%s.type", [openapi_lib.concat_path(path)]),
"searchLine": common_lib.build_search_line(path, ["type"]),
"issueType": "MissingAttribute",
"keyExpectedValue": "'pattern' should be defined",
"keyActualValue": "'pattern' is undefined",
Expand All @@ -34,6 +36,7 @@ CxPolicy[result] {
result := {
"documentId": doc.id,
"searchKey": sprintf("%s.type", [openapi_lib.concat_path(path)]),
"searchLine": common_lib.build_search_line(path, ["type"]),
"issueType": "MissingAttribute",
"keyExpectedValue": "'pattern' should be defined",
"keyActualValue": "'pattern' is undefined",
Expand Down
3 changes: 3 additions & 0 deletions pkg/detector/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ func (d *DefaultDetectLineResponse) DetectCurrentLine(str1, str2 string, recurse
}

func checkLine(str1, str2 string, distances map[int]int, line string, i int) map[int]int {
// Tackle initial spaces that were inducing wrong results
regex := regexp.MustCompile(`^\s+`)
line = regex.ReplaceAllString(line, "")
if str1 != "" && str2 != "" && strings.Contains(line, str1) {
restLine := line[strings.Index(line, str1)+len(str1):]
if strings.Contains(restLine, str2) {
Expand Down
66 changes: 66 additions & 0 deletions pkg/detector/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,69 @@ func TestDefaultDetectLineResponse_checkResolvedFile(t *testing.T) {
})
}
}

func TestDetectCurrentLine(t *testing.T) {
type fields struct {
defaultDetectLineResponse *DefaultDetectLineResponse
}

type args struct {
lines []string
str1 string
str2 string
}

type want struct {
defaultDetectLineResponse *DefaultDetectLineResponse
}

tests := []struct {
name string
fields fields
args args
want want
}{
{
name: "test_checkLines",
args: args{
lines: []string{
" \"type\": \"string\"",
" \"type\": \"array\"",
},
str1: "\"type\"",
str2: "",
},
fields: fields{
&DefaultDetectLineResponse{
CurrentLine: 0,
IsBreak: false,
FoundAtLeastOne: false,
ResolvedFile: "",
ResolvedFiles: map[string]model.ResolvedFileSplit{},
},
},
want: want{
&DefaultDetectLineResponse{
CurrentLine: 0,
IsBreak: false,
FoundAtLeastOne: false,
ResolvedFile: "",
ResolvedFiles: map[string]model.ResolvedFileSplit{},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := tt.fields.defaultDetectLineResponse

d, _ = d.DetectCurrentLine(tt.args.str1, tt.args.str2, 0, tt.args.lines)

if d.CurrentLine != tt.want.defaultDetectLineResponse.CurrentLine {
t.Errorf("DetectCurrentLine() = %v, want %v", d.CurrentLine, tt.want.defaultDetectLineResponse.CurrentLine)
}
})
}

}

0 comments on commit 9260c42

Please sign in to comment.