Skip to content

Commit

Permalink
update regex to blur mongo credentials (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
sredxny authored Jul 26, 2023
1 parent af48e86 commit 1a6d80b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pumps/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ func createDBObject(tableName string) dbObject {
}

func (b *BaseMongoConf) GetBlurredURL() string {
// mongo uri match with regex ^(mongodb:(?:\/{2})?)((\w+?):(\w+?)@|:?@?)(\S+?):(\d+)(\/(\S+?))?(\?replicaSet=(\S+?))?$
// but we need only a segment, so regex explanation: https://regex101.com/r/8Uzwtw/1
regex := `^(mongodb:(?:\/{2})?)((...+?):(...+?)@)`
// mongo uri match with regex ^(mongodb\S*(+srv)*:(?:\/{2})?)((\w+?):(\w+?)@|:?@?)(\S+?):(\d+)(\/(\S+?))?(\?replicaSet=(\S+?))?$
// but we need only a segment, so regex explanation: https://regex101.com/r/C4GQvi/1
regex := `^(mongodb\S*(srv)*:(?:\/{2})?)((...+?):(...+?)@)`
re := regexp.MustCompile(regex)

blurredUrl := re.ReplaceAllString(b.MongoURL, "***:***@")
Expand Down
5 changes: 5 additions & 0 deletions pumps/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,11 @@ func TestGetBlurredURL(t *testing.T) {
givenURL: "mongodb://UserName:Password@sample-cluster-instance.cluster-corlsfccjozr.us-east-1.docdb.amazonaws.com:27017?replicaSet=rs0&ssl_ca_certs=rds-combined-ca-bundle.pem",
expectedBlurredURL: "***:***@sample-cluster-instance.cluster-corlsfccjozr.us-east-1.docdb.amazonaws.com:27017?replicaSet=rs0&ssl_ca_certs=rds-combined-ca-bundle.pem",
},
{
testName: "DNS seed list connection",
givenURL: "mongodb+srv://admin:pass@server.example.com/?connectTimeoutMS=300000",
expectedBlurredURL: "***:***@server.example.com/?connectTimeoutMS=300000",
},
}

for _, tc := range tcs {
Expand Down

0 comments on commit 1a6d80b

Please sign in to comment.