Skip to content

Commit b80f38a

Browse files
authored
fix(redact): multi-line redactors strip empty lines (#1742)
1 parent dca4e67 commit b80f38a

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

pkg/redact/multi_line.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader {
7676
if err != nil {
7777
return
7878
}
79-
line1, line2, err = getNextTwoLines(reader, line2)
79+
line1, line2, err = getNextTwoLines(reader, &line2)
8080
flushLastLine = true
8181
continue
8282
}
@@ -89,7 +89,7 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader {
8989
if err != nil {
9090
return
9191
}
92-
line1, line2, err = getNextTwoLines(reader, line2)
92+
line1, line2, err = getNextTwoLines(reader, &line2)
9393
flushLastLine = true
9494
continue
9595
}
@@ -127,7 +127,7 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader {
127127
return out
128128
}
129129

130-
func getNextTwoLines(reader *bufio.Reader, curLine2 []byte) (line1 []byte, line2 []byte, err error) {
130+
func getNextTwoLines(reader *bufio.Reader, curLine2 *[]byte) (line1 []byte, line2 []byte, err error) {
131131
line2 = []byte{}
132132

133133
if curLine2 == nil {
@@ -140,7 +140,7 @@ func getNextTwoLines(reader *bufio.Reader, curLine2 []byte) (line1 []byte, line2
140140
return
141141
}
142142

143-
line1 = curLine2
143+
line1 = *curLine2
144144
line2, err = readLine(reader)
145145
if err != nil {
146146
return

pkg/redact/multi_line_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ func Test_NewMultiLineRedactor(t *testing.T) {
9494
"key": "***HIDDEN***"
9595
`,
9696
},
97+
{
98+
name: "Multiple newlines with no match",
99+
selector: LineRedactor{
100+
regex: `(?i)"name": *"[^\"]*SECRET_?ACCESS_?KEY[^\"]*"`,
101+
scan: `secret_?access_?key`,
102+
},
103+
redactor: `(?i)("value": *")(?P<mask>.*[^\"]*)(")`,
104+
inputString: "no match\n\n no match \n\n",
105+
wantString: "no match\n\n no match \n\n",
106+
},
97107
}
98108
for _, tt := range tests {
99109
t.Run(tt.name, func(t *testing.T) {

pkg/redact/single_line_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,16 @@ func TestNewSingleLineRedactor(t *testing.T) {
308308
},
309309
},
310310
},
311+
{
312+
name: "Multiple newlines with no match",
313+
re: `abcd`,
314+
inputString: "no match\n\n no match \n\n",
315+
wantString: "no match\n\n no match \n\n",
316+
wantRedactions: RedactionList{
317+
ByRedactor: map[string][]Redaction{},
318+
ByFile: map[string][]Redaction{},
319+
},
320+
},
311321
}
312322
for _, tt := range tests {
313323
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)