Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions config/common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (

const SecretToken = "<secret>"

var errUnsupportedScheme = errors.New("unsupported scheme")

var SecretTokenJSON string

func init() {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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
}
26 changes: 26 additions & 0 deletions config/common/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
64 changes: 64 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
}
Loading