Skip to content

Commit

Permalink
Use null terminator for masking inline ignore (#111)
Browse files Browse the repository at this point in the history
* Add test for wokeignore inline in the beginning of the line

* Use a single byte (null terminator) character for masking inline ignore rule

* Rename function to be more clear
  • Loading branch information
caitlinelfring authored Jul 22, 2021
1 parent 4c46b2f commit 5f47e75
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 2 additions & 0 deletions pkg/result/lineresult_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions pkg/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"regexp"
"strings"
"unicode"

"github.com/get-woke/woke/pkg/util"
)
Expand Down Expand Up @@ -34,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)
}
Expand Down Expand Up @@ -183,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
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/rule/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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))
})
}
}
Expand Down

0 comments on commit 5f47e75

Please sign in to comment.