redact_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132
  1. package stringutil
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestRedactURLs(t *testing.T) {
  7. cases := map[string]string{
  8. "plain error": "plain error",
  9. `Put "https://b.s3.amazonaws.com/k?X-Amz-Signature=abc&X-Amz-Credential=AKIA": EOF`: `Put "https://b.s3.amazonaws.com/k?REDACTED": EOF`,
  10. "dial https://user:secret@host/path failed": "dial https://REDACTED@host/path failed",
  11. "dial https://user:p@ss@host/path failed": "dial https://REDACTED@host/path failed",
  12. "see https://host/a#frag and s3://b/k?sig=1": "see https://host/a?REDACTED and s3://b/k?REDACTED",
  13. "no query https://host/path/object.gz": "no query https://host/path/object.gz",
  14. "Put bucket.s3.amazonaws.com/k?X-Amz-Signature=abc&x=1": "Put bucket.s3.amazonaws.com/k?X-Amz-Signature=REDACTED&x=1",
  15. `https:\/\/h\/k?sv=2020&sig=SECRET`: `https:\/\/h\/k?sv=2020&sig=REDACTED`,
  16. "AccountName=a;AccountKey=SECRET;EndpointSuffix=core": "AccountName=a;AccountKey=REDACTED;EndpointSuffix=core",
  17. "SharedAccessSignature=SECRET;BlobEndpoint=x": "SharedAccessSignature=REDACTED;BlobEndpoint=x",
  18. "https://h/k?a=1 &sig=SECRET": "https://h/k?REDACTED &sig=REDACTED",
  19. "https://storage.googleapis.com/b/o?X-Goog-Signature=SECRET": "https://storage.googleapis.com/b/o?REDACTED",
  20. }
  21. for in, want := range cases {
  22. got := RedactURLs(in)
  23. if got != want {
  24. t.Errorf("RedactURLs(%q) = %q, want %q", in, got, want)
  25. }
  26. if strings.Contains(got, "SECRET") {
  27. t.Errorf("RedactURLs(%q) leaked a secret: %q", in, got)
  28. }
  29. }
  30. }