webhook_redact_test.go 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package provisioner
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "errors"
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. "github.com/smallstep/certificates/webhook"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestRedactWebhookURLError(t *testing.T) {
  13. const name = "test-webhook"
  14. // path and query are marked, not shown; the webhook name is included
  15. err := &url.Error{Op: "Post", URL: "https://hooks.example.com/validate?token=secret", Err: errors.New("connection refused")}
  16. got := redactWebhookURLError(err, name)
  17. require.Equal(t, `webhook "test-webhook": Post "https://hooks.example.com/<redacted>?<redacted>": connection refused`, got.Error())
  18. require.NotContains(t, got.Error(), "token=secret")
  19. require.NotContains(t, got.Error(), "/validate")
  20. // userinfo is marked too
  21. withUser := &url.Error{Op: "Post", URL: "https://user:token@hooks.example.com/validate", Err: errors.New("connection refused")}
  22. got = redactWebhookURLError(withUser, name)
  23. require.Equal(t, `webhook "test-webhook": Post "https://<redacted>@hooks.example.com/<redacted>": connection refused`, got.Error())
  24. require.NotContains(t, got.Error(), "user:token")
  25. // a bare host with a root path renders without markers
  26. bare := &url.Error{Op: "Post", URL: "https://hooks.example.com/", Err: errors.New("connection refused")}
  27. require.Equal(t, `webhook "test-webhook": Post "https://hooks.example.com": connection refused`, redactWebhookURLError(bare, name).Error())
  28. // deadline errors keep working through the wrapper
  29. deadline := &url.Error{Op: "Post", URL: "https://hooks.example.com/x?token=secret", Err: context.DeadlineExceeded}
  30. require.ErrorIs(t, redactWebhookURLError(deadline, name), context.DeadlineExceeded)
  31. // fail closed when the URL cannot be parsed: no raw URL, name and cause kept
  32. bad := &url.Error{Op: "Post", URL: "https://hooks.example.com/val%zz?token=secret", Err: errors.New("connection refused")}
  33. got = redactWebhookURLError(bad, name)
  34. require.Equal(t, `webhook "test-webhook": Post "<redacted>": connection refused`, got.Error())
  35. require.NotContains(t, got.Error(), "token=secret")
  36. require.ErrorIs(t, got, bad.Err)
  37. // non-URL errors pass through unchanged, without a name prefix
  38. plain := errors.New("boom")
  39. require.Equal(t, plain, redactWebhookURLError(plain, name))
  40. }
  41. func TestRedactURLString(t *testing.T) {
  42. // a URL template with actions still parses and redacts
  43. require.Equal(t,
  44. `https://hooks.example.com/<redacted>?<redacted>`,
  45. redactURLString(`https://hooks.example.com/{{ .Webhooks.ScepChallenge.ID }}/validate?token=x`))
  46. // unparseable input fails closed
  47. require.Equal(t, "<redacted>", redactURLString("https://hooks.example.com/val%zz?token=x\n"))
  48. }
  49. func TestWebhookDoWithContextRedactsURL(t *testing.T) {
  50. w := &Webhook{
  51. Name: "test-webhook",
  52. URL: "http://127.0.0.1:1/validate?token=secret",
  53. Secret: base64.StdEncoding.EncodeToString([]byte("secret")),
  54. }
  55. _, err := w.DoWithContext(context.Background(), &http.Client{}, nil, &webhook.RequestBody{}, nil)
  56. require.Error(t, err)
  57. require.NotContains(t, err.Error(), "token=secret")
  58. require.NotContains(t, err.Error(), "/validate")
  59. require.Contains(t, err.Error(), `webhook "test-webhook": Post "http://127.0.0.1:1/<redacted>?<redacted>"`)
  60. require.Contains(t, err.Error(), "connection refused")
  61. }
  62. func TestWebhookDoWithContextRedactsNewRequestError(t *testing.T) {
  63. // A control character in the templated URL makes NewRequestWithContext
  64. // fail; its error carries the raw expanded URL and must be redacted too.
  65. w := &Webhook{
  66. Name: "test-webhook",
  67. URL: "http://127.0.0.1:1/validate?token=secret\n",
  68. Secret: base64.StdEncoding.EncodeToString([]byte("secret")),
  69. }
  70. _, err := w.DoWithContext(context.Background(), &http.Client{}, nil, &webhook.RequestBody{}, nil)
  71. require.Error(t, err)
  72. require.NotContains(t, err.Error(), "token=secret")
  73. require.Contains(t, err.Error(), `webhook "test-webhook"`)
  74. require.Contains(t, err.Error(), "<redacted>")
  75. }