Skip to content

Commit 5b57a4d

Browse files
Ensure loggableDSN scrubs passwords in different ways
The previous implementation of the function was only scrubbing those passwords that were part of a basic http authentication method, which means it expected passwords to be provided as part of the URL. However, the password may also be specified as a parameter, or when using key-values, it may be specified as a password=value string. This commit ensures those passwords will also not be retained, but removed. Signed-off-by: Feike Steenbergen <[email protected]>
1 parent 1b492a6 commit 5b57a4d

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

Diff for: cmd/postgres_exporter/postgres_exporter_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,58 @@ func (s *FunctionalSuite) TestParseUserQueries(c *C) {
421421
}
422422
}
423423
}
424+
425+
func (s *FunctionalSuite) TestLoggableDSN(c *C) {
426+
type TestCase struct {
427+
input string
428+
expected string
429+
}
430+
431+
cases := []TestCase{
432+
{
433+
input: "host=host.example.com user=postgres port=5432 password=s3cr3t",
434+
expected: "host=host.example.com user=postgres port=5432 password=PASSWORD_REMOVED",
435+
},
436+
437+
{
438+
input: "host=host.example.com user=postgres port=5432 password=\"s3cr 3t\"",
439+
expected: "host=host.example.com user=postgres port=5432 password=PASSWORD_REMOVED",
440+
},
441+
442+
{
443+
input: "password=abcde host=host.example.com user=postgres port=5432",
444+
expected: "password=PASSWORD_REMOVED host=host.example.com user=postgres port=5432",
445+
},
446+
447+
{
448+
input: "password=abcde host=host.example.com user=postgres port=5432 password=\"s3cr 3t\"",
449+
expected: "password=PASSWORD_REMOVED host=host.example.com user=postgres port=5432 password=PASSWORD_REMOVED",
450+
},
451+
452+
{
453+
input: "postgresql://host.example.com:5432/tsdb?user=postgres",
454+
expected: "postgresql://host.example.com:5432/tsdb?user=postgres",
455+
},
456+
457+
{
458+
input: "postgresql://user:[email protected]:5432/tsdb?user=postgres",
459+
expected: "postgresql://user:[email protected]:5432/tsdb?user=postgres",
460+
},
461+
462+
{
463+
input: "postgresql://host.example.com:5432/tsdb?user=postgres&password=s3cr3t",
464+
expected: "postgresql://host.example.com:5432/tsdb?password=PASSWORD_REMOVED&user=postgres",
465+
},
466+
467+
{
468+
input: "host=host.example.com user=postgres port=5432",
469+
expected: "host=host.example.com user=postgres port=5432",
470+
},
471+
}
472+
473+
for _, cs := range cases {
474+
loggable := loggableDSN(cs.input)
475+
c.Assert(loggable, Equals, cs.expected)
476+
}
477+
478+
}

Diff for: cmd/postgres_exporter/util.go

+15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"fmt"
1818
"math"
1919
"net/url"
20+
"regexp"
2021
"strconv"
2122
"strings"
2223
"time"
@@ -212,10 +213,24 @@ func loggableDSN(dsn string) string {
212213
if err != nil {
213214
return "could not parse DATA_SOURCE_NAME"
214215
}
216+
217+
// If the DSN is not a URL it is expected to be in the `key1=value1 key2=value2` format
218+
if pDSN.Scheme == "" {
219+
re := regexp.MustCompile(`(\s?password=([^"\s]+|"[^"]+"))`)
220+
stripped := re.ReplaceAllString(dsn, " password=PASSWORD_REMOVED")
221+
return strings.TrimSpace(stripped)
222+
}
223+
215224
// Blank user info if not nil
216225
if pDSN.User != nil {
217226
pDSN.User = url.UserPassword(pDSN.User.Username(), "PASSWORD_REMOVED")
218227
}
219228

229+
// If the password is contained in a URL parameter, we should remove it there
230+
if q, err := url.ParseQuery(pDSN.RawQuery); err == nil && q.Has("password") {
231+
q.Set("password", "PASSWORD_REMOVED")
232+
pDSN.RawQuery = q.Encode()
233+
}
234+
220235
return pDSN.String()
221236
}

0 commit comments

Comments
 (0)