cmd/tailscale: fix sanitizeOutput and add a test (#18589)
Follow up from https://github.com/tailscale/tailscale/pull/18563 which I totally botched. Updates #18562 Signed-off-by: Andrew Lytvynov <awly@tailscale.com>
This commit is contained in:
@@ -581,11 +581,11 @@ type sanitizeWriter struct {
|
||||
w io.Writer
|
||||
}
|
||||
|
||||
var reTskey = regexp.MustCompile(`tskey-\w+`)
|
||||
var rxTskey = regexp.MustCompile(`tskey-[\w-]+`)
|
||||
|
||||
func (w sanitizeWriter) Write(buf []byte) (int, error) {
|
||||
sanitized := reTskey.ReplaceAll(buf, []byte("tskey-REDACTED"))
|
||||
diff := len(buf) - len(sanitized)
|
||||
sanitized := rxTskey.ReplaceAll(buf, []byte("tskey-REDACTED"))
|
||||
diff := len(sanitized) - len(buf)
|
||||
n, err := w.w.Write(sanitized)
|
||||
return n - diff, err
|
||||
}
|
||||
|
||||
@@ -1799,3 +1799,21 @@ func TestDepsNoCapture(t *testing.T) {
|
||||
}.Check(t)
|
||||
|
||||
}
|
||||
|
||||
func TestSanitizeWriter(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
w := sanitizeOutput(buf)
|
||||
|
||||
in := []byte(`my auth key is tskey-auth-abc123-def456, what's yours?`)
|
||||
want := []byte(`my auth key is tskey-REDACTED, what's yours?`)
|
||||
n, err := w.Write(in)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != len(in) {
|
||||
t.Errorf("unexpected write length %d, want %d", n, len(in))
|
||||
}
|
||||
if got := buf.Bytes(); !bytes.Equal(got, want) {
|
||||
t.Errorf("unexpected sanitized content\ngot: %q\nwant: %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user