Skip to content

Commit bad0701

Browse files
author
alxndrsn
committed
Redact passwords provided in URL when passing errors to callbacks
1 parent accc716 commit bad0701

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

lib/adapter.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module.exports = (function sailsPostgresql() {
6262
}).execSync();
6363
} catch (e) {
6464
setImmediate(function done() {
65-
return cb(e);
65+
return cb(redactPasswordsInMeta(e));
6666
});
6767
return;
6868
}
@@ -95,14 +95,14 @@ module.exports = (function sailsPostgresql() {
9595
modelDefinitions: modelDefinitions
9696
}).switch({
9797
error: function error(err) {
98-
return next(err);
98+
return next(redactPasswordsInMeta(err));
9999
},
100100
success: function success() {
101101
return next();
102102
}
103103
});
104104
}, function asyncCb(err) {
105-
cb(err);
105+
cb(redactPasswordsInMeta(err));
106106
});
107107
},
108108

@@ -130,12 +130,12 @@ module.exports = (function sailsPostgresql() {
130130
query: query
131131
}).switch({
132132
error: function error(err) {
133-
return cb(err);
133+
return cb(redactPasswordsInMeta(err));
134134
},
135135
notUnique: function error(errInfo) {
136136
var e = new Error(errInfo.message);
137137
e.footprint = errInfo.footprint;
138-
return cb(e);
138+
return cb(redactPasswordsInMeta(e));
139139
},
140140
success: function success(report) {
141141
var record = report && report.record || undefined;
@@ -158,12 +158,12 @@ module.exports = (function sailsPostgresql() {
158158
query: query
159159
}).switch({
160160
error: function error(err) {
161-
return cb(err);
161+
return cb(redactPasswordsInMeta(err));
162162
},
163163
notUnique: function error(errInfo) {
164164
var e = new Error(errInfo.message);
165165
e.footprint = errInfo.footprint;
166-
return cb(e);
166+
return cb(redactPasswordsInMeta(e));
167167
},
168168
success: function success(report) {
169169
var records = report && report.records || undefined;
@@ -186,7 +186,7 @@ module.exports = (function sailsPostgresql() {
186186
query: query
187187
}).switch({
188188
error: function error(err) {
189-
return cb(err);
189+
return cb(redactPasswordsInMeta(err));
190190
},
191191
success: function success(report) {
192192
return cb(undefined, report.records);
@@ -208,12 +208,12 @@ module.exports = (function sailsPostgresql() {
208208
query: query
209209
}).switch({
210210
error: function error(err) {
211-
return cb(err);
211+
return cb(redactPasswordsInMeta(err));
212212
},
213213
notUnique: function error(errInfo) {
214214
var e = new Error(errInfo.message);
215215
e.footprint = errInfo.footprint;
216-
return cb(e);
216+
return cb(redactPasswordsInMeta(e));
217217
},
218218
success: function success(report) {
219219
if (report) {
@@ -239,7 +239,7 @@ module.exports = (function sailsPostgresql() {
239239
query: query
240240
}).switch({
241241
error: function error(err) {
242-
return cb(err);
242+
return cb(redactPasswordsInMeta(err));
243243
},
244244
success: function success(report) {
245245
if (report) {
@@ -265,7 +265,7 @@ module.exports = (function sailsPostgresql() {
265265
query: query
266266
}).switch({
267267
error: function error(err) {
268-
return cb(err);
268+
return cb(redactPasswordsInMeta(err));
269269
},
270270
success: function success(report) {
271271
return cb(undefined, report);
@@ -287,7 +287,7 @@ module.exports = (function sailsPostgresql() {
287287
query: query
288288
}).switch({
289289
error: function error(err) {
290-
return cb(err);
290+
return cb(redactPasswordsInMeta(err));
291291
},
292292
success: function success(report) {
293293
return cb(undefined, report);
@@ -309,7 +309,7 @@ module.exports = (function sailsPostgresql() {
309309
query: query
310310
}).switch({
311311
error: function error(err) {
312-
return cb(err);
312+
return cb(redactPasswordsInMeta(err));
313313
},
314314
success: function success(report) {
315315
return cb(undefined, report);
@@ -331,7 +331,7 @@ module.exports = (function sailsPostgresql() {
331331
query: query
332332
}).switch({
333333
error: function error(err) {
334-
return cb(err);
334+
return cb(redactPasswordsInMeta(err));
335335
},
336336
success: function success(report) {
337337
return cb(undefined, report);
@@ -364,7 +364,7 @@ module.exports = (function sailsPostgresql() {
364364
meta: meta
365365
}).switch({
366366
error: function error(err) {
367-
return cb(err);
367+
return cb(redactPasswordsInMeta(err));
368368
},
369369
success: function success(report) {
370370
// Waterline expects the result to be undefined if the table doesn't
@@ -393,7 +393,7 @@ module.exports = (function sailsPostgresql() {
393393
meta: meta
394394
}).switch({
395395
error: function error(err) {
396-
return cb(err);
396+
return cb(redactPasswordsInMeta(err));
397397
},
398398
success: function success() {
399399
return cb();
@@ -414,7 +414,7 @@ module.exports = (function sailsPostgresql() {
414414
meta: meta
415415
}).switch({
416416
error: function error(err) {
417-
return cb(err);
417+
return cb(redactPasswordsInMeta(err));
418418
},
419419
success: function success() {
420420
return cb();
@@ -435,10 +435,10 @@ module.exports = (function sailsPostgresql() {
435435
meta: meta
436436
}).switch({
437437
error: function error(err) {
438-
return cb(err);
438+
return cb(redactPasswordsInMeta(err));
439439
},
440440
badConnection: function badConnection(err) {
441-
return cb(err);
441+
return cb(redactPasswordsInMeta(err));
442442
},
443443
success: function success() {
444444
return cb();
@@ -460,7 +460,7 @@ module.exports = (function sailsPostgresql() {
460460
meta: meta
461461
}).switch({
462462
error: function error(err) {
463-
return cb(err);
463+
return cb(redactPasswordsInMeta(err));
464464
},
465465
success: function success() {
466466
return cb();
@@ -472,3 +472,19 @@ module.exports = (function sailsPostgresql() {
472472

473473
return adapter;
474474
})();
475+
476+
var REDACT_REPLACEMENT = '$1:****@';
477+
var REDACT_REGEX_SINGLE = /^(postgres:\/\/[^:\s]*):[^@\s]*@/;
478+
var REDACT_REGEX_MULTI = /(postgres:\/\/[^:\s]*):[^@\s]*@/g;
479+
480+
function redactPasswordsInMeta(err) {
481+
if(err) {
482+
if(err.meta && err.meta.url) {
483+
err.meta.url = err.meta.url.replace(REDACT_REGEX_SINGLE, REDACT_REPLACEMENT);
484+
}
485+
if(err.message) {
486+
err.message = err.message.replace(REDACT_REGEX_MULTI, REDACT_REPLACEMENT);
487+
}
488+
}
489+
return err;
490+
}

0 commit comments

Comments
 (0)