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

Use null terminator for masking inline ignore #111

Merged
merged 3 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all 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: 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