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:
Andrew Lytvynov
2026-02-02 15:38:40 -08:00
committed by GitHub
parent 8736fbb754
commit ae95d8d222
2 changed files with 21 additions and 3 deletions
+3 -3
View File
@@ -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
}
+18
View File
@@ -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)
}
}