From b6d7542023283ed4c8034c0fb0d728a6bf5687f1 Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Mon, 27 Sep 2021 17:08:56 +0200 Subject: [PATCH] Do not marshal secrets in URL's Signed-off-by: Julien Pivotto --- config/http_config.go | 16 +++++++++++++++- config/http_config_test.go | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/config/http_config.go b/config/http_config.go index 6c0c033d..f47892dc 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -110,11 +110,25 @@ func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error { // MarshalYAML implements the yaml.Marshaler interface for URLs. func (u URL) MarshalYAML() (interface{}, error) { if u.URL != nil { - return u.String(), nil + return u.Redacted(), nil } return nil, nil } +// Redacted returns the URL but replaces any password with "xxxxx". +func (u URL) Redacted() string { + if u.URL == nil { + return "" + } + + ru := *u.URL + if _, ok := ru.User.Password(); ok { + // We can not use secretToken because it would be escaped. + ru.User = url.UserPassword(ru.User.Username(), "xxxxx") + } + return ru.String() +} + // UnmarshalJSON implements the json.Marshaler interface for URL. func (u *URL) UnmarshalJSON(data []byte) error { var s string diff --git a/config/http_config_test.go b/config/http_config_test.go index 5ec32297..689bbde2 100644 --- a/config/http_config_test.go +++ b/config/http_config_test.go @@ -1418,3 +1418,19 @@ func TestUnmarshalURL(t *testing.T) { t.Fatalf("URL not properly unmarshaled in YAML, got '%s'", u.String()) } } + +func TestMarshalURLWithSecret(t *testing.T) { + var u URL + err := yaml.Unmarshal([]byte("http://foo:bar@example.com"), &u) + if err != nil { + t.Fatal(err) + } + + b, err := yaml.Marshal(u) + if err != nil { + t.Fatal(err) + } + if strings.TrimSpace(string(b)) != "http://foo:xxxxx@example.com" { + t.Fatalf("URL not properly marshaled in YAML, got '%s'", string(b)) + } +}