From 2b77470593ecfd9ef5c83bc7291e9caf610ab00b Mon Sep 17 00:00:00 2001 From: Caitlin Elfring Date: Wed, 21 Jul 2021 19:18:41 -0400 Subject: [PATCH 1/3] Add test for wokeignore inline in the beginning of the line --- pkg/result/lineresult_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/result/lineresult_test.go b/pkg/result/lineresult_test.go index 84e7b7e4..0fe612bc 100644 --- a/pkg/result/lineresult_test.go +++ b/pkg/result/lineresult_test.go @@ -22,6 +22,8 @@ func TestFindResults(t *testing.T) { // inline-ignoring is handled in Parser.generateFileFindings, not FindResults rs = FindResults(&rule.TestRule, "my/file", "this has the term whitelist #wokeignore:rule=whitelist", 1) assert.Len(t, rs, 1) + rs = FindResults(&rule.TestRule, "my/file", "/* wokeignore:rule=whitelist */ this has the term whitelist", 1) + assert.Len(t, rs, 1) } func TestLineResult_MarshalJSON(t *testing.T) { From f0638a8342fbc367d786341b01c80bc42df12e85 Mon Sep 17 00:00:00 2001 From: Caitlin Elfring Date: Wed, 21 Jul 2021 19:20:12 -0400 Subject: [PATCH 2/3] Use a single byte (null terminator) character for masking inline ignore rule --- pkg/rule/rule.go | 4 ++-- pkg/rule/rule_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/rule/rule.go b/pkg/rule/rule.go index 0231a272..5c37d3a8 100644 --- a/pkg/rule/rule.go +++ b/pkg/rule/rule.go @@ -4,7 +4,6 @@ import ( "fmt" "regexp" "strings" - "unicode" "github.com/get-woke/woke/pkg/util" ) @@ -198,7 +197,8 @@ func removeInlineIgnore(line string) string { end := inlineIgnoreMatch[1] for i := start; i < end; i++ { - lineWithoutIgnoreRule[i] = unicode.ReplacementChar + // use null terminator to indicate a masked character + lineWithoutIgnoreRule[i] = rune(0) } return string(lineWithoutIgnoreRule) diff --git a/pkg/rule/rule_test.go b/pkg/rule/rule_test.go index ad7bce88..66a7ceaa 100644 --- a/pkg/rule/rule_test.go +++ b/pkg/rule/rule_test.go @@ -171,7 +171,7 @@ func Test_removeInlineIgnore(t *testing.T) { { desc: "replace wokeignore:rule", line: "wokeignore:rule=master-slave", - expected: "����������������������������", + expected: "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", }, { desc: "not replace wokeignore:rule", From 93b586d5c08cdc7bbdf0a644b070931244cc418e Mon Sep 17 00:00:00 2001 From: Caitlin Elfring Date: Wed, 21 Jul 2021 19:22:01 -0400 Subject: [PATCH 3/3] Rename function to be more clear --- pkg/rule/rule.go | 10 +++++----- pkg/rule/rule_test.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/rule/rule.go b/pkg/rule/rule.go index 5c37d3a8..18482917 100644 --- a/pkg/rule/rule.go +++ b/pkg/rule/rule.go @@ -33,7 +33,7 @@ func (r *Rule) FindMatchIndexes(text string) [][]int { r.SetRegexp() // Remove inline ignores from text to avoid matching against other rules - matches := r.re.FindAllStringSubmatchIndex(removeInlineIgnore(text), -1) + matches := r.re.FindAllStringSubmatchIndex(maskInlineIgnore(text), -1) if matches == nil { return [][]int(nil) } @@ -182,10 +182,10 @@ func escape(ss []string) []string { return ss } -// removeInlineIgnore removes the entire match of the ignoreRuleRegex from the line -// and replaces it with the unicode replacement character so the rule matcher won't -// attempt to find findings within -func removeInlineIgnore(line string) string { +// maskInlineIgnore removes the entire match of the ignoreRuleRegex from the line +// and replaces it with the null terminator (\x00) character so the rule matcher won't +// attempt to find findings within the inline ignore +func maskInlineIgnore(line string) string { inlineIgnoreMatch := ignoreRuleRegex.FindStringIndex(line) if inlineIgnoreMatch == nil || len(inlineIgnoreMatch) < 2 { return line diff --git a/pkg/rule/rule_test.go b/pkg/rule/rule_test.go index 66a7ceaa..a8765a5d 100644 --- a/pkg/rule/rule_test.go +++ b/pkg/rule/rule_test.go @@ -162,7 +162,7 @@ func TestRule_regexString(t *testing.T) { } } -func Test_removeInlineIgnore(t *testing.T) { +func Test_maskInlineIgnore(t *testing.T) { tests := []struct { desc string line string @@ -181,7 +181,7 @@ func Test_removeInlineIgnore(t *testing.T) { } for _, tt := range tests { t.Run(tt.desc, func(t *testing.T) { - assert.Equal(t, tt.expected, removeInlineIgnore(tt.line)) + assert.Equal(t, tt.expected, maskInlineIgnore(tt.line)) }) } }