Skip to content

Commit b6d7542

Browse files
author
Julien Pivotto
committed
Do not marshal secrets in URL's
Signed-off-by: Julien Pivotto <[email protected]>
1 parent 8d1c9f8 commit b6d7542

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Diff for: config/http_config.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,25 @@ func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error {
110110
// MarshalYAML implements the yaml.Marshaler interface for URLs.
111111
func (u URL) MarshalYAML() (interface{}, error) {
112112
if u.URL != nil {
113-
return u.String(), nil
113+
return u.Redacted(), nil
114114
}
115115
return nil, nil
116116
}
117117

118+
// Redacted returns the URL but replaces any password with "xxxxx".
119+
func (u URL) Redacted() string {
120+
if u.URL == nil {
121+
return ""
122+
}
123+
124+
ru := *u.URL
125+
if _, ok := ru.User.Password(); ok {
126+
// We can not use secretToken because it would be escaped.
127+
ru.User = url.UserPassword(ru.User.Username(), "xxxxx")
128+
}
129+
return ru.String()
130+
}
131+
118132
// UnmarshalJSON implements the json.Marshaler interface for URL.
119133
func (u *URL) UnmarshalJSON(data []byte) error {
120134
var s string

Diff for: config/http_config_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -1418,3 +1418,19 @@ func TestUnmarshalURL(t *testing.T) {
14181418
t.Fatalf("URL not properly unmarshaled in YAML, got '%s'", u.String())
14191419
}
14201420
}
1421+
1422+
func TestMarshalURLWithSecret(t *testing.T) {
1423+
var u URL
1424+
err := yaml.Unmarshal([]byte("http://foo:[email protected]"), &u)
1425+
if err != nil {
1426+
t.Fatal(err)
1427+
}
1428+
1429+
b, err := yaml.Marshal(u)
1430+
if err != nil {
1431+
t.Fatal(err)
1432+
}
1433+
if strings.TrimSpace(string(b)) != "http://foo:[email protected]" {
1434+
t.Fatalf("URL not properly marshaled in YAML, got '%s'", string(b))
1435+
}
1436+
}

0 commit comments

Comments
 (0)