Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not marshal secrets in URL's #328

Merged
merged 1 commit into from Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion config/http_config.go
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions config/http_config_test.go
Expand Up @@ -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))
}
}