Skip to content

Commit e151c8b

Browse files
authored
Merge pull request #26 from mongodb-js/compass-6197-redact-password-without-string
fix(redact): handle empty username when redacting password COMPASS-6197
2 parents c85ff2f + 4c12322 commit e151c8b

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/redact.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function redactConnectionString(
5959
const R = replacementString; // alias for conciseness
6060
const replacements: ((uri: string) => string)[] = [
6161
// Username and password
62-
uri => uri.replace(redactUsernames ? /(\/\/)(.*)(@)/g : /(\/\/[^@]+:)(.*)(@)/g, `$1${R}$3`),
62+
uri => uri.replace(redactUsernames ? /(\/\/)(.*)(@)/g : /(\/\/[^@]*:)(.*)(@)/g, `$1${R}$3`),
6363
// AWS IAM Session Token as part of query parameter
6464
uri => uri.replace(/(AWS_SESSION_TOKEN(:|%3A))([^,&]+)/gi, `$1${R}`),
6565
// tlsCertificateKeyFilePassword query parameter

test/redact.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ describe('redact credentials', () => {
4343
expect(redactConnectionString(`${protocol}://admin:[email protected]/admin?proxyUsername=foo&proxyPassword=bar`, { replacementString: '****' }))
4444
.to.equal(`${protocol}://****@cats-data-sets-e08dy.mongodb.net/admin?proxyUsername=****&proxyPassword=****`);
4545
});
46+
47+
it('redacts credentials when username is empty', () => {
48+
expect(
49+
redactConnectionString(`${protocol}://:[email protected]/`)
50+
).to.equal(`${protocol}://<credentials>@mongodb.net/`);
51+
});
4652
});
4753

4854
context('when url contains no credentials', () => {

0 commit comments

Comments
 (0)