Skip to content

Commit

Permalink
Fix: Hide config.SecretURL when the URL is incorrect. (prometheus#3887)
Browse files Browse the repository at this point in the history
* fix: Hide config.SecretURL when the URL is incorrect.

    Updated the config.go to redact the URL.
    Added test cases to check URL stays hidden.
Signed-off-by: Kapil Ramwani(kanishkramwani6@gmail.com)

---------

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
Co-authored-by: Simon Pasquier <spasquie@redhat.com>
  • Loading branch information
2 people authored and TheMeier committed Sep 29, 2024
1 parent 90dc5e7 commit 0cd874a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ func (s *SecretURL) UnmarshalJSON(data []byte) error {
s.URL = &url.URL{}
return nil
}
return json.Unmarshal(data, (*URL)(s))
// Redact the secret URL in case of errors
if err := json.Unmarshal(data, (*URL)(s)); err != nil {
return errors.New(strings.ReplaceAll(err.Error(), string(data), "[REDACTED]"))
}

return nil
}

// Load parses the YAML input s into a Config.
Expand Down
9 changes: 9 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,15 @@ func TestUnmarshalSecretURL(t *testing.T) {
require.Equal(t, "http://example.com/se%20cret", u.String(), "SecretURL not properly unmarshaled in YAML.")
}

func TestHideSecretURL(t *testing.T) {
b := []byte(`"://wrongurl/"`)
var u SecretURL

err := json.Unmarshal(b, &u)
require.Error(t, err)
require.NotContains(t, err.Error(), "wrongurl")
}

func TestMarshalURL(t *testing.T) {
for name, tc := range map[string]struct {
input *URL
Expand Down

0 comments on commit 0cd874a

Please sign in to comment.