Skip to content

Commit 0e2e45a

Browse files
authored
Merge pull request #283 from alxndrsn/redact-passwords-in-error-meta
Redact passwords provided in URL when passing errors to callbacks
2 parents accc716 + 8ff4bdb commit 0e2e45a

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

lib/adapter.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
var _ = require('@sailshq/lodash');
1818
var async = require('async');
19+
var redactPasswords = require('./private/redact-passwords');
1920
var Helpers = require('../helpers');
2021

2122
module.exports = (function sailsPostgresql() {
@@ -62,7 +63,7 @@ module.exports = (function sailsPostgresql() {
6263
}).execSync();
6364
} catch (e) {
6465
setImmediate(function done() {
65-
return cb(e);
66+
return cb(redactPasswords(e));
6667
});
6768
return;
6869
}
@@ -95,14 +96,14 @@ module.exports = (function sailsPostgresql() {
9596
modelDefinitions: modelDefinitions
9697
}).switch({
9798
error: function error(err) {
98-
return next(err);
99+
return next(redactPasswords(err));
99100
},
100101
success: function success() {
101102
return next();
102103
}
103104
});
104105
}, function asyncCb(err) {
105-
cb(err);
106+
cb(redactPasswords(err));
106107
});
107108
},
108109

@@ -130,12 +131,12 @@ module.exports = (function sailsPostgresql() {
130131
query: query
131132
}).switch({
132133
error: function error(err) {
133-
return cb(err);
134+
return cb(redactPasswords(err));
134135
},
135136
notUnique: function error(errInfo) {
136137
var e = new Error(errInfo.message);
137138
e.footprint = errInfo.footprint;
138-
return cb(e);
139+
return cb(redactPasswords(e));
139140
},
140141
success: function success(report) {
141142
var record = report && report.record || undefined;
@@ -158,12 +159,12 @@ module.exports = (function sailsPostgresql() {
158159
query: query
159160
}).switch({
160161
error: function error(err) {
161-
return cb(err);
162+
return cb(redactPasswords(err));
162163
},
163164
notUnique: function error(errInfo) {
164165
var e = new Error(errInfo.message);
165166
e.footprint = errInfo.footprint;
166-
return cb(e);
167+
return cb(redactPasswords(e));
167168
},
168169
success: function success(report) {
169170
var records = report && report.records || undefined;
@@ -186,7 +187,7 @@ module.exports = (function sailsPostgresql() {
186187
query: query
187188
}).switch({
188189
error: function error(err) {
189-
return cb(err);
190+
return cb(redactPasswords(err));
190191
},
191192
success: function success(report) {
192193
return cb(undefined, report.records);
@@ -208,12 +209,12 @@ module.exports = (function sailsPostgresql() {
208209
query: query
209210
}).switch({
210211
error: function error(err) {
211-
return cb(err);
212+
return cb(redactPasswords(err));
212213
},
213214
notUnique: function error(errInfo) {
214215
var e = new Error(errInfo.message);
215216
e.footprint = errInfo.footprint;
216-
return cb(e);
217+
return cb(redactPasswords(e));
217218
},
218219
success: function success(report) {
219220
if (report) {
@@ -239,7 +240,7 @@ module.exports = (function sailsPostgresql() {
239240
query: query
240241
}).switch({
241242
error: function error(err) {
242-
return cb(err);
243+
return cb(redactPasswords(err));
243244
},
244245
success: function success(report) {
245246
if (report) {
@@ -265,7 +266,7 @@ module.exports = (function sailsPostgresql() {
265266
query: query
266267
}).switch({
267268
error: function error(err) {
268-
return cb(err);
269+
return cb(redactPasswords(err));
269270
},
270271
success: function success(report) {
271272
return cb(undefined, report);
@@ -287,7 +288,7 @@ module.exports = (function sailsPostgresql() {
287288
query: query
288289
}).switch({
289290
error: function error(err) {
290-
return cb(err);
291+
return cb(redactPasswords(err));
291292
},
292293
success: function success(report) {
293294
return cb(undefined, report);
@@ -309,7 +310,7 @@ module.exports = (function sailsPostgresql() {
309310
query: query
310311
}).switch({
311312
error: function error(err) {
312-
return cb(err);
313+
return cb(redactPasswords(err));
313314
},
314315
success: function success(report) {
315316
return cb(undefined, report);
@@ -331,7 +332,7 @@ module.exports = (function sailsPostgresql() {
331332
query: query
332333
}).switch({
333334
error: function error(err) {
334-
return cb(err);
335+
return cb(redactPasswords(err));
335336
},
336337
success: function success(report) {
337338
return cb(undefined, report);
@@ -364,7 +365,7 @@ module.exports = (function sailsPostgresql() {
364365
meta: meta
365366
}).switch({
366367
error: function error(err) {
367-
return cb(err);
368+
return cb(redactPasswords(err));
368369
},
369370
success: function success(report) {
370371
// Waterline expects the result to be undefined if the table doesn't
@@ -393,7 +394,7 @@ module.exports = (function sailsPostgresql() {
393394
meta: meta
394395
}).switch({
395396
error: function error(err) {
396-
return cb(err);
397+
return cb(redactPasswords(err));
397398
},
398399
success: function success() {
399400
return cb();
@@ -414,7 +415,7 @@ module.exports = (function sailsPostgresql() {
414415
meta: meta
415416
}).switch({
416417
error: function error(err) {
417-
return cb(err);
418+
return cb(redactPasswords(err));
418419
},
419420
success: function success() {
420421
return cb();
@@ -435,10 +436,10 @@ module.exports = (function sailsPostgresql() {
435436
meta: meta
436437
}).switch({
437438
error: function error(err) {
438-
return cb(err);
439+
return cb(redactPasswords(err));
439440
},
440441
badConnection: function badConnection(err) {
441-
return cb(err);
442+
return cb(redactPasswords(err));
442443
},
443444
success: function success() {
444445
return cb();
@@ -460,7 +461,7 @@ module.exports = (function sailsPostgresql() {
460461
meta: meta
461462
}).switch({
462463
error: function error(err) {
463-
return cb(err);
464+
return cb(redactPasswords(err));
464465
},
465466
success: function success() {
466467
return cb();
@@ -472,3 +473,5 @@ module.exports = (function sailsPostgresql() {
472473

473474
return adapter;
474475
})();
476+
477+

lib/private/redact-passwords.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// ██████╗ ███████╗██████╗ █████╗ ██████╗████████╗
2+
// ██╔══██╗██╔════╝██╔══██╗██╔══██╗██╔════╝╚══██╔══╝
3+
// ██████╔╝█████╗ ██║ ██║███████║██║ ██║
4+
// ██╔══██╗██╔══╝ ██║ ██║██╔══██║██║ ██║
5+
// ██║ ██║███████╗██████╔╝██║ ██║╚██████╗ ██║
6+
// ╚═╝ ╚═╝╚══════╝╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
7+
//
8+
// ██████╗ █████╗ ███████╗███████╗██╗ ██╗ ██████╗ ██████╗ ██████╗ ███████╗
9+
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║ ██║██╔═══██╗██╔══██╗██╔══██╗██╔════╝
10+
// ██████╔╝███████║███████╗███████╗██║ █╗ ██║██║ ██║██████╔╝██║ ██║███████╗
11+
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║███╗██║██║ ██║██╔══██╗██║ ██║╚════██║
12+
// ██║ ██║ ██║███████║███████║╚███╔███╔╝╚██████╔╝██║ ██║██████╔╝███████║
13+
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝ ╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝ ╚══════╝
14+
//
15+
// Remove database passwords from the error instance.
16+
17+
module.exports = function redactPasswords(err) {
18+
var REDACT_REPLACEMENT = '$1:****@';
19+
var REDACT_REGEX_SINGLE = /^(postgres:\/\/[^:\s]*):[^@\s]*@/;
20+
var REDACT_REGEX_MULTI = /(postgres:\/\/[^:\s]*):[^@\s]*@/g;
21+
22+
if(err) {
23+
if(err.meta && typeof err.meta === 'object' && err.meta.url && typeof err.meta.url === 'string') {
24+
err.meta.url = err.meta.url.replace(REDACT_REGEX_SINGLE, REDACT_REPLACEMENT);
25+
}
26+
if(err.message && typeof err.message === 'string') {
27+
err.message = err.message.replace(REDACT_REGEX_MULTI, REDACT_REPLACEMENT);
28+
}
29+
}
30+
return err;
31+
}

0 commit comments

Comments
 (0)