@@ -591,3 +591,65 @@ func TestNotifierGetSchema(t *testing.T) {
591591 require .Contains (t , schema , `CREATE OR REPLACE TRIGGER "trigger_public.test_table"` )
592592 require .Contains (t , schema , `AFTER INSERT ON public.test_table` )
593593}
594+
595+ func TestRedactDataSource (t * testing.T ) {
596+ tests := []struct {
597+ name string
598+ input string
599+ expected string
600+ }{
601+ // #nosec
602+ {
603+ name : "classic URL" ,
604+ input : "postgres://alice:s3cr3t@localhost:5432/mydb" ,
605+ expected : "postgres://alice:xxxxx@localhost:5432/mydb" ,
606+ },
607+ // #nosec
608+ {
609+ name : "postgresql scheme with password" ,
610+ input : "postgresql://bob:hunter2@db.example.com/prod" ,
611+ expected : "postgresql://bob:xxxxx@db.example.com/prod" ,
612+ },
613+ {
614+ name : "URL without password" ,
615+ input : "postgres://alice@localhost:5432/mydb" ,
616+ expected : "postgres://alice@localhost:5432/mydb" ,
617+ },
618+ {
619+ name : "URL without user info" ,
620+ input : "postgres://localhost:5432/mydb" ,
621+ expected : "postgres://localhost:5432/mydb" ,
622+ },
623+ {
624+ name : "DSN key=value with quoted password" ,
625+ input : "host=localhost user=alice password='s3cr3t' dbname=mydb" ,
626+ expected : "host=localhost user=alice password=xxxxx dbname=mydb" ,
627+ },
628+ {
629+ name : "DSN key=value with unquoted password" ,
630+ input : "host=localhost user=alice password=s3cr3t dbname=mydb" ,
631+ expected : "host=localhost user=alice password=xxxxx dbname=mydb" ,
632+ },
633+ {
634+ name : "DSN key=value without password" ,
635+ input : "host=localhost user=alice dbname=mydb" ,
636+ expected : "host=localhost user=alice dbname=mydb" ,
637+ },
638+ {
639+ name : "empty string" ,
640+ input : "" ,
641+ expected : "" ,
642+ },
643+ {
644+ name : "DSN with quoted password containing spaces" ,
645+ input : "host=localhost password='my secret pass' dbname=mydb" ,
646+ expected : "host=localhost password=xxxxx dbname=mydb" ,
647+ },
648+ }
649+
650+ for _ , tc := range tests {
651+ t .Run (tc .name , func (t * testing.T ) {
652+ require .Equal (t , tc .expected , redactDataSource (tc .input ))
653+ })
654+ }
655+ }
0 commit comments