Skip to content

Commit f2c9bea

Browse files
committed
chore: redact MongoDB URIs better
1 parent 7ce9d8c commit f2c9bea

File tree

4 files changed

+99
-17
lines changed

4 files changed

+99
-17
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"mochaExplorer.files": "{src,lib}/**/*.spec.ts",
3+
"mochaExplorer.require": [
4+
"../../scripts/import-expansions.js",
5+
"ts-node/register"
6+
],
7+
"mochaExplorer.timeout": 60000,
8+
"mochaExplorer.ui": "bdd",
9+
"mochaExplorer.monkeyPatch": true,
10+
"mochaExplorer.autoload": true,
11+
"testExplorer.codeLens": true,
12+
"testExplorer.gutterDecoration": true,
13+
"testExplorer.onStart": "reset",
14+
"testExplorer.onReload": "reset"
15+
}

packages/mongodb-redact/src/index.spec.ts

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,82 @@ describe('mongodb-redact', function () {
161161
expect(res).to.equal('<url>');
162162
});
163163

164-
it('should redact MongoDB connection URIs', function () {
165-
let res = redact(
166-
'mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test&connectTimeoutMS=300000',
167-
);
168-
expect(res).to.equal('<mongodb uri>');
169-
res = redact('mongodb://localhost,localhost:27018,localhost:27019');
170-
expect(res).to.equal('<mongodb uri>');
164+
describe('MongoDB connection strings', function () {
165+
it('should redact MongoDB connection URIs', function () {
166+
let res = redact(
167+
'mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test&connectTimeoutMS=300000',
168+
);
169+
expect(res).to.equal('<mongodb uri>');
170+
res = redact('mongodb://localhost,localhost:27018,localhost:27019');
171+
expect(res).to.equal('<mongodb uri>');
172+
});
173+
174+
it('should redact MongoDB URIs with credentials', function () {
175+
let res = redact('mongodb://user:password@localhost:27017/admin');
176+
expect(res).to.equal('<mongodb uri>');
177+
res = redact('mongodb://admin:[email protected]/mydb');
178+
expect(res).to.equal('<mongodb uri>');
179+
});
180+
181+
it('should redact MongoDB URIs with special characters in usernames and passwords', function () {
182+
let res = redact('mongodb://user:p%40ss!word@localhost:27017/');
183+
expect(res).to.equal('<mongodb uri>');
184+
res = redact('mongodb://ad!min:te%st#[email protected]:27017/');
185+
expect(res).to.equal('<mongodb uri>');
186+
res = redact('mongodb://!user:my%20pass@localhost/mydb');
187+
expect(res).to.equal('<mongodb uri>');
188+
res = redact(
189+
'mongodb://user:p&ssw!rd#[email protected]:27017/db?authSource=admin',
190+
);
191+
expect(res).to.equal('<mongodb uri>');
192+
});
193+
194+
it('should redact MongoDB SRV URIs', function () {
195+
let res = redact(
196+
'mongodb+srv://user:[email protected]/test',
197+
);
198+
expect(res).to.equal('<mongodb uri>');
199+
res = redact(
200+
'mongodb+srv://admin:[email protected]/mydb?retryWrites=true',
201+
);
202+
expect(res).to.equal('<mongodb uri>');
203+
});
204+
205+
it('should redact MongoDB URIs with query parameters', function () {
206+
let res = redact(
207+
'mongodb://localhost:27017/mydb?ssl=true&replicaSet=rs0',
208+
);
209+
expect(res).to.equal('<mongodb uri>');
210+
res = redact(
211+
'mongodb://user:[email protected]/db?authSource=admin&readPreference=primary',
212+
);
213+
expect(res).to.equal('<mongodb uri>');
214+
});
215+
216+
it('should redact MongoDB URIs with replica sets', function () {
217+
let res = redact(
218+
'mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplSet',
219+
);
220+
expect(res).to.equal('<mongodb uri>');
221+
res = redact('mongodb://user:pass@host1,host2,host3/db?replicaSet=rs0');
222+
expect(res).to.equal('<mongodb uri>');
223+
});
224+
225+
it('should redact MongoDB URIs with IP addresses', function () {
226+
let res = redact('mongodb://192.168.1.100:27017/mydb');
227+
expect(res).to.equal('<mongodb uri>');
228+
res = redact('mongodb://user:[email protected]:27017/admin');
229+
expect(res).to.equal('<mongodb uri>');
230+
});
231+
232+
it('should redact simple MongoDB URIs', function () {
233+
let res = redact('mongodb://localhost');
234+
expect(res).to.equal('<mongodb uri>');
235+
res = redact('mongodb://localhost:27017');
236+
expect(res).to.equal('<mongodb uri>');
237+
res = redact('mongodb://localhost/mydb');
238+
expect(res).to.equal('<mongodb uri>');
239+
});
171240
});
172241

173242
it('should redact general linux/unix user paths', function () {

packages/mongodb-redact/src/regexes.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ export const regexes = [
2727
'$1<email>$6',
2828
],
2929

30+
// MongoDB connection strings (before IP addresses to handle mongodb://IP:port URIs)
31+
[
32+
/(mongodb(?:\+srv)?:\/\/)(www\.)?(?:[^:/@\s]+:[^@\s]*@)?[-a-zA-Z0-9@:%._+~#=,]{2,256}(\.[a-z]{2,6})?\b([-a-zA-Z0-9@:%_+.~#?&/=]*)/gim,
33+
'<mongodb uri>',
34+
],
35+
3036
// IP addresses
3137
[
3238
/((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])/gm,
@@ -39,12 +45,6 @@ export const regexes = [
3945
'<url>',
4046
],
4147

42-
// MongoDB connection strings
43-
[
44-
/(mongodb:\/\/)(www\.)?[-a-zA-Z0-9@:%._+~#=,]{2,256}(\.[a-z]{2,6})?\b([-a-zA-Z0-9@:%_+.~#?&/=]*)/gim,
45-
'<mongodb uri>',
46-
],
47-
4848
// Compass Schema URL fragments
4949
[/#schema\/\w+\.\w+/, '#schema/<namespace>'],
5050
] as const;

packages/mongodb-redact/src/secrets.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,11 @@ describe('dictionary-based secret redaction', function () {
111111

112112
it('redacts a special-character only connection string', function () {
113113
const secret = '!#!!';
114-
const content = 'Connection string: mongodb://user:!#!!@localhost:27017/';
114+
const content = 'Connection string: mongodb://!!!#:!#!!@localhost:27017/';
115115

116116
const redacted = redact(content, [{ value: secret, kind: 'password' }]);
117117

118-
expect(redacted).to.equal(
119-
'Connection string: <mongodb uri><password>@localhost:27017/',
120-
);
118+
expect(redacted).to.equal('Connection string: <mongodb uri>');
121119
});
122120

123121
for (const { char, password } of [

0 commit comments

Comments
 (0)