Skip to content

Commit 6684110

Browse files
fix(redact): use fast and safe path and email regex MONGOSH-1392 (#401)
* fix: use fast and safe path and email regex MONGOSH-1392 * refactor: clean up * test: update test name * fix: handle export/home
1 parent 65a4bf6 commit 6684110

File tree

2 files changed

+33
-37
lines changed

2 files changed

+33
-37
lines changed

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

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,44 +85,43 @@ describe('mongodb-redact', function () {
8585
expect(redact(PRIVATE_KEY)).to.equal('<private key>');
8686
});
8787

88-
it('should redact OS X resource paths', function () {
89-
const res = redact(
90-
'/Applications/MongoDB%20Compass.app/Contents/Resources/app/index.html'
88+
it('should redact OS X user paths', function () {
89+
let res = redact(
90+
'/Users/foo/Applications/MongoDB%20Compass.app/Contents/Resources/app/index.html'
91+
);
92+
expect(res).to.equal(
93+
'/Users/<user>/Applications/MongoDB%20Compass.app/Contents/Resources/app/index.html'
9194
);
92-
expect(res).to.equal('/<path>/index.html');
95+
res = redact('/Users/JohnDoe/Documents/letter.pages');
96+
expect(res).to.equal(res, '/Users/<user>/Documents/letter.pages');
97+
res = redact('file:///Users/JohnDoe/Documents/letter.pages');
98+
expect(res).to.equal(res, 'file:///Users/<user>/Documents/letter.pages');
9399
});
94100

95-
it('should redact Windows resource paths using forward slash', function () {
96-
const res = redact(
101+
it('should redact Windows user paths using backward slash', function () {
102+
let res = redact(
97103
'C:\\Users\\foo\\AppData\\Local\\MongoDBCompass\\app-1.0.1\\resources\\app\\index.js'
98104
);
99-
expect(res).to.equal('\\<path>\\index.js');
105+
expect(res).to.equal(res, 'C:\\Users\\<user>\\index.js');
106+
res = redact('c:\\Users\\JohnDoe\\test');
107+
expect(res).to.equal(res, 'c:\\Users\\<user>\\test');
108+
res = redact('C:\\Documents and Settings\\JohnDoe\\test');
109+
expect(res).to.equal(res, 'C:\\Documents and Settings\\<user>\\test');
100110
});
101111

102-
it('should redact Windows resource paths using backward slash', function () {
112+
it('should redact Windows user paths using forward slash', function () {
103113
const res = redact(
104114
'C:/Users/foo/AppData/Local/MongoDBCompass/app-1.0.1/resources/app/index.js'
105115
);
106-
expect(res).to.equal('/<path>/index.js');
116+
expect(res).to.equal(
117+
res,
118+
'C:/Users/<user>/AppData/Local/MongoDBCompass/app-1.0.1/resources/app/index.js'
119+
);
107120
});
108121

109-
it('should redact Linux resource paths', function () {
122+
it('should redact Linux user paths', function () {
110123
const res = redact('/usr/foo/myapps/resources/app/index.html');
111-
expect(res).to.equal('/<path>/index.html');
112-
});
113-
114-
it('should redact general Windows user paths', function () {
115-
let res = redact('c:\\Users\\JohnDoe\\test');
116-
expect(res).to.equal('c:\\Users\\<user>\\test');
117-
res = redact('C:\\Documents and Settings\\JohnDoe\\test');
118-
expect(res).to.equal('C:\\Documents and Settings\\<user>\\test');
119-
});
120-
121-
it('should redact general OS X user paths', function () {
122-
let res = redact('/Users/JohnDoe/Documents/letter.pages');
123-
expect(res).to.equal('/Users/<user>/Documents/letter.pages');
124-
res = redact('file:///Users/JohnDoe/Documents/letter.pages');
125-
expect(res).to.equal('file:///Users/<user>/Documents/letter.pages');
124+
expect(res).to.equal(res, '/usr/<user>/myapps/resources/app/index.html');
126125
});
127126

128127
it('should redact URLs', function () {

packages/mongodb-redact/src/regexes.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,20 @@ export const regexes = [
1111
'<private key>',
1212
],
1313

14-
// Electron app resources specific directories
15-
[/(file:\/\/)?\S+\/Contents\/Resources\/app\//gm, '$1/<path>/'],
16-
[/(file:\/\/)?([a-zA-Z]:)?\\\S+\\resources\\app\\/gm, '$1\\<path>\\'],
17-
[/(file:\/\/)?([a-zA-Z]:)?\/\S+\/resources\/app\//gm, '$1/<path>/'],
18-
19-
// Generic user directories
20-
[/\/(Users?)\/[^/]*\//gm, '/$1/<user>/'],
14+
// User directories
15+
[
16+
/(file:\/\/|\/)(Users|user|users|user|usr|u01|var\/users|home|export\/home|Documents and Settings|Profiles)\/[^/]*\//gm,
17+
'$1$2/<user>/',
18+
],
2119
[
22-
/\/(usr|home|user|users|u01|var\/users|export\/home)\/[^/]*\//gm,
23-
'/$1/<user>/',
20+
/(file:\/\/|\\)(Users|user|users|user|usr|u01|var\\users|home|export\\home|Documents and Settings|Profiles)\\[^/]*\\/gm,
21+
'$1$2\\<user>\\',
2422
],
25-
[/\\(Users|Documents and Settings|Profiles)\\[^/\\]*\\/gm, '\\$1\\<user>\\'],
2623

2724
// Email addresses
2825
[
29-
/(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/gim,
30-
'<email>',
26+
/(^|[ \t\r\n\v\f])([a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]{1,64}@[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?){1,500})/gim,
27+
'$1<email>',
3128
],
3229

3330
// IP addresses

0 commit comments

Comments
 (0)