From b51ce8073a526c85a411f4b6600a12ee6a103a33 Mon Sep 17 00:00:00 2001 From: vigneshwar Date: Tue, 17 Nov 2026 07:18:16 +0530 Subject: [PATCH] config: do not echo secret URLs in YAML parse errors A secret URL that fails to parse in the YAML config used to produce an error quoting the whole value, including any token in the path. The JSON path already redacts it. Do the same for YAML, for SecretURL and for the non-templated case of SecretTemplateURL, and keep the original error when secrets are marshaled in clear. Signed-off-by: vigneshwar --- config/common/url.go | 35 +++++++++++++++++++-- config/common/url_test.go | 26 ++++++++++++++++ config/config_test.go | 64 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 2 deletions(-) diff --git a/config/common/url.go b/config/common/url.go index 5a5fd10d27..34502877ca 100644 --- a/config/common/url.go +++ b/config/common/url.go @@ -26,6 +26,8 @@ import ( const SecretToken = "" +var errUnsupportedScheme = errors.New("unsupported scheme") + var SecretTokenJSON string func init() { @@ -118,7 +120,13 @@ func (s *SecretURL) UnmarshalYAML(unmarshal func(any) error) error { s.URL = &url.URL{} return nil } - return unmarshal((*URL)(s)) + urlp, err := ParseURL(str) + if err != nil { + _, rerr := redactURLError(err) + return rerr + } + s.URL = urlp.URL + return nil } // MarshalJSON implements the json.Marshaler interface for SecretURL. @@ -204,6 +212,9 @@ func (s *SecretTemplateURL) UnmarshalYAML(unmarshal func(any) error) error { // Only validate as URL if it's not templated if !isTemplated { if _, err := ParseURL(urlStr); err != nil { + if redacted, rerr := redactURLError(err); redacted { + return rerr + } return fmt.Errorf("invalid URL: %w", err) } } @@ -245,10 +256,30 @@ func ParseURL(s string) (*URL, error) { return nil, err } if u.Scheme != "http" && u.Scheme != "https" { - return nil, fmt.Errorf("unsupported scheme %q for URL", u.Scheme) + return nil, fmt.Errorf("%w %q for URL", errUnsupportedScheme, u.Scheme) } if u.Host == "" { return nil, errors.New("missing host for URL") } return &URL{u}, nil } + +// redactURLError returns a version of an error from ParseURL that does not +// repeat any part of the URL, for use with URLs that may contain credentials. +// Errors from net/url quote the input, and the unsupported scheme error +// quotes the part before the first colon, so both are replaced by a fixed +// message. The error is returned unchanged, with false, when secrets are +// marshaled in clear or the error does not quote the input. +func redactURLError(err error) (bool, error) { + if commoncfg.MarshalSecretValue { + return false, err + } + var uerr *url.Error + switch { + case errors.As(err, &uerr): + return true, errors.New("invalid URL (value redacted)") + case errors.Is(err, errUnsupportedScheme): + return true, errors.New("unsupported scheme for URL (value redacted)") + } + return false, err +} diff --git a/config/common/url_test.go b/config/common/url_test.go index 5d78c28d4b..df8285d852 100644 --- a/config/common/url_test.go +++ b/config/common/url_test.go @@ -218,3 +218,29 @@ func TestUnmarshalRelativeURL(t *testing.T) { t.Errorf("Expected an error parsing URL") } } + +func TestHideSecretURLYAML(t *testing.T) { + b := []byte(`"://wrongurl/"`) + var u SecretURL + + err := yaml.Unmarshal(b, &u) + require.Error(t, err) + require.NotContains(t, err.Error(), "wrongurl") + + var tu SecretTemplateURL + err = yaml.Unmarshal(b, &tu) + require.Error(t, err) + require.NotContains(t, err.Error(), "wrongurl") +} + +func TestShowMarshalSecretURLYAML(t *testing.T) { + commoncfg.MarshalSecretValue = true + defer func() { commoncfg.MarshalSecretValue = false }() + + b := []byte(`"://wrongurl/"`) + var u SecretURL + + err := yaml.Unmarshal(b, &u) + require.Error(t, err) + require.Contains(t, err.Error(), "wrongurl") +} diff --git a/config/config_test.go b/config/config_test.go index 28f08982a5..0846cb43cd 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2027,3 +2027,67 @@ receivers: t.Errorf("expected local proxy_url %q, got %q", "http://local-proxy.example.com:8080", got) } } + +// Secret URL fields must not be echoed back in the error returned for a +// value that fails to parse. +func TestLoadInvalidSecretURLDoesNotLeak(t *testing.T) { + const token = "SECRETTOKEN123" + + for name, tc := range map[string]struct { + in string + wantErr string + }{ + "global slack_api_url": { + in: ` +global: + slack_api_url: "http://hooks.example.com/%zz/` + token + `" +route: + receiver: r +receivers: +- name: r +`, + wantErr: "invalid URL", + }, + "slack api_url": { + in: ` +route: + receiver: r +receivers: +- name: r + slack_configs: + - api_url: "http://hooks.example.com:badport/` + token + `" +`, + wantErr: "invalid URL", + }, + "discord webhook_url": { + in: ` +route: + receiver: r +receivers: +- name: r + discord_configs: + - webhook_url: "` + token + `://hooks.example.com/x" +`, + wantErr: "unsupported scheme", + }, + "webhook url": { + in: ` +route: + receiver: r +receivers: +- name: r + webhook_configs: + - url: "http://hooks.example.com/%zz/` + token + `" +`, + wantErr: "invalid URL", + }, + } { + t.Run(name, func(t *testing.T) { + _, err := Load(tc.in) + require.Error(t, err) + require.Contains(t, err.Error(), tc.wantErr) + // Schemes are lower-cased by net/url, so compare without case. + require.NotContains(t, strings.ToLower(err.Error()), strings.ToLower(token)) + }) + } +}