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>
main
Andrew Lytvynov 2 months ago committed by GitHub
parent 8736fbb754
commit ae95d8d222
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      cmd/tailscale/cli/cli.go
  2. 18
      cmd/tailscale/cli/cli_test.go

@ -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)
}
}

Loading…
Cancel
Save