Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 29fddd0

Browse files
Merge pull request #197 from secrethub/fix/mask
Fix bug MaskedWriter is not flushed before timeout
2 parents 9b68bbc + 76ab8f1 commit 29fddd0

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

internals/cli/masker/writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ func (mw *MaskedWriter) process() {
145145
mw.flushBuffer()
146146
}
147147
case p := <-mw.incomingBytesCh:
148-
matchInProgress := false
149148
for _, b := range p {
149+
matchInProgress := false
150150
mw.buf = append(mw.buf, maskByte{byte: b})
151151

152152
for _, matcher := range mw.matchers {

internals/cli/masker/writer_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/secrethub/secrethub-go/pkg/randchar"
1212
)
1313

14+
var maskString = "<redacted by SecretHub>"
15+
1416
func TestMatcher(t *testing.T) {
1517
tests := []struct {
1618
matchString string
@@ -120,8 +122,6 @@ func TestMatcher(t *testing.T) {
120122
}
121123

122124
func TestNewMaskedWriter(t *testing.T) {
123-
maskString := "<redacted by SecretHub>"
124-
125125
timeout10s := time.Second * 10
126126
timeout1us := time.Microsecond * 1
127127
timeout0 := time.Second * 0
@@ -271,6 +271,35 @@ func TestNewMaskedWriter(t *testing.T) {
271271
}
272272
}
273273

274+
func TestNewMaskedWriter_OnlyFlushingOnTimeoutBug(t *testing.T) {
275+
// There was a bug in MaskedWriter where it was only flushed on a timeout when a secret was found in the middle
276+
// of write. This test assures this bug is not present by writing a secret in the middle of a Write and
277+
// checking whether Flush() returns before the timeout of the MaskedWriter.
278+
279+
var buf bytes.Buffer
280+
281+
maskStrings := [][]byte{[]byte("foo")}
282+
283+
w := NewMaskedWriter(&buf, maskStrings, maskString, time.Second*10)
284+
285+
go w.Run()
286+
_, err := w.Write([]byte("teststring foo more text"))
287+
assert.OK(t, err)
288+
289+
done := make(chan struct{})
290+
go func() {
291+
err := w.Flush()
292+
assert.OK(t, err)
293+
done <- struct{}{}
294+
}()
295+
296+
select {
297+
case <-time.After(5 * time.Second):
298+
t.Error("MaskedWriter was not flushed before timeout")
299+
case <-done:
300+
}
301+
}
302+
274303
type errWriter struct {
275304
err error
276305
}

0 commit comments

Comments
 (0)