From 7c69f3657b5db4f6168af3d2b6e6796b7bb568d7 Mon Sep 17 00:00:00 2001 From: Alexander Weaver Date: Tue, 18 Jun 2024 16:02:33 -0500 Subject: [PATCH] Notifications: Redact URL from errors (#85687) * Remove url logs and redact * Reinclude redacted URL --- pkg/services/notifications/webhook.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/pkg/services/notifications/webhook.go b/pkg/services/notifications/webhook.go index 6ff7df5efe8..7fbaf59eaad 100644 --- a/pkg/services/notifications/webhook.go +++ b/pkg/services/notifications/webhook.go @@ -4,10 +4,12 @@ import ( "bytes" "context" "crypto/tls" + "errors" "fmt" "io" "net" "net/http" + "net/url" "time" "github.com/grafana/grafana/pkg/util" @@ -62,6 +64,11 @@ func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook * if err != nil { return err } + url, err := url.Parse(webhook.Url) + if err != nil { + // Should not be possible - NewRequestWithContext should also err if the URL is bad. + return err + } if webhook.ContentType == "" { webhook.ContentType = "application/json" @@ -80,7 +87,7 @@ func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook * resp, err := netClient.Do(request) if err != nil { - return err + return redactURL(err) } defer func() { if err := resp.Body.Close(); err != nil { @@ -96,16 +103,25 @@ func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook * if webhook.Validation != nil { err := webhook.Validation(body, resp.StatusCode) if err != nil { - ns.log.Debug("Webhook failed validation", "url", webhook.Url, "statuscode", resp.Status, "body", string(body)) + ns.log.Debug("Webhook failed validation", "url", url.Redacted(), "statuscode", resp.Status, "body", string(body), "error", err) return fmt.Errorf("webhook failed validation: %w", err) } } if resp.StatusCode/100 == 2 { - ns.log.Debug("Webhook succeeded", "url", webhook.Url, "statuscode", resp.Status) + ns.log.Debug("Webhook succeeded", "url", url.Redacted(), "statuscode", resp.Status) return nil } - ns.log.Debug("Webhook failed", "url", webhook.Url, "statuscode", resp.Status, "body", string(body)) + ns.log.Debug("Webhook failed", "url", url.Redacted(), "statuscode", resp.Status, "body", string(body)) return fmt.Errorf("webhook response status %v", resp.Status) } + +func redactURL(err error) error { + var e *url.Error + if !errors.As(err, &e) { + return err + } + e.URL = "" + return e +}