Skip to content

Commit

Permalink
Move redactPasswordsInMeta into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
rachaelshaw committed Sep 30, 2019
1 parent bad0701 commit 8ff4bdb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
59 changes: 23 additions & 36 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

var _ = require('@sailshq/lodash');
var async = require('async');
var redactPasswords = require('./private/redact-passwords');
var Helpers = require('../helpers');

module.exports = (function sailsPostgresql() {
Expand Down Expand Up @@ -62,7 +63,7 @@ module.exports = (function sailsPostgresql() {
}).execSync();
} catch (e) {
setImmediate(function done() {
return cb(redactPasswordsInMeta(e));
return cb(redactPasswords(e));
});
return;
}
Expand Down Expand Up @@ -95,14 +96,14 @@ module.exports = (function sailsPostgresql() {
modelDefinitions: modelDefinitions
}).switch({
error: function error(err) {
return next(redactPasswordsInMeta(err));
return next(redactPasswords(err));
},
success: function success() {
return next();
}
});
}, function asyncCb(err) {
cb(redactPasswordsInMeta(err));
cb(redactPasswords(err));
});
},

Expand Down Expand Up @@ -130,12 +131,12 @@ module.exports = (function sailsPostgresql() {
query: query
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
notUnique: function error(errInfo) {
var e = new Error(errInfo.message);
e.footprint = errInfo.footprint;
return cb(redactPasswordsInMeta(e));
return cb(redactPasswords(e));
},
success: function success(report) {
var record = report && report.record || undefined;
Expand All @@ -158,12 +159,12 @@ module.exports = (function sailsPostgresql() {
query: query
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
notUnique: function error(errInfo) {
var e = new Error(errInfo.message);
e.footprint = errInfo.footprint;
return cb(redactPasswordsInMeta(e));
return cb(redactPasswords(e));
},
success: function success(report) {
var records = report && report.records || undefined;
Expand All @@ -186,7 +187,7 @@ module.exports = (function sailsPostgresql() {
query: query
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
success: function success(report) {
return cb(undefined, report.records);
Expand All @@ -208,12 +209,12 @@ module.exports = (function sailsPostgresql() {
query: query
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
notUnique: function error(errInfo) {
var e = new Error(errInfo.message);
e.footprint = errInfo.footprint;
return cb(redactPasswordsInMeta(e));
return cb(redactPasswords(e));
},
success: function success(report) {
if (report) {
Expand All @@ -239,7 +240,7 @@ module.exports = (function sailsPostgresql() {
query: query
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
success: function success(report) {
if (report) {
Expand All @@ -265,7 +266,7 @@ module.exports = (function sailsPostgresql() {
query: query
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
success: function success(report) {
return cb(undefined, report);
Expand All @@ -287,7 +288,7 @@ module.exports = (function sailsPostgresql() {
query: query
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
success: function success(report) {
return cb(undefined, report);
Expand All @@ -309,7 +310,7 @@ module.exports = (function sailsPostgresql() {
query: query
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
success: function success(report) {
return cb(undefined, report);
Expand All @@ -331,7 +332,7 @@ module.exports = (function sailsPostgresql() {
query: query
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
success: function success(report) {
return cb(undefined, report);
Expand Down Expand Up @@ -364,7 +365,7 @@ module.exports = (function sailsPostgresql() {
meta: meta
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
success: function success(report) {
// Waterline expects the result to be undefined if the table doesn't
Expand Down Expand Up @@ -393,7 +394,7 @@ module.exports = (function sailsPostgresql() {
meta: meta
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
success: function success() {
return cb();
Expand All @@ -414,7 +415,7 @@ module.exports = (function sailsPostgresql() {
meta: meta
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
success: function success() {
return cb();
Expand All @@ -435,10 +436,10 @@ module.exports = (function sailsPostgresql() {
meta: meta
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
badConnection: function badConnection(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
success: function success() {
return cb();
Expand All @@ -460,7 +461,7 @@ module.exports = (function sailsPostgresql() {
meta: meta
}).switch({
error: function error(err) {
return cb(redactPasswordsInMeta(err));
return cb(redactPasswords(err));
},
success: function success() {
return cb();
Expand All @@ -473,18 +474,4 @@ module.exports = (function sailsPostgresql() {
return adapter;
})();

var REDACT_REPLACEMENT = '$1:****@';
var REDACT_REGEX_SINGLE = /^(postgres:\/\/[^:\s]*):[^@\s]*@/;
var REDACT_REGEX_MULTI = /(postgres:\/\/[^:\s]*):[^@\s]*@/g;

function redactPasswordsInMeta(err) {
if(err) {
if(err.meta && err.meta.url) {
err.meta.url = err.meta.url.replace(REDACT_REGEX_SINGLE, REDACT_REPLACEMENT);
}
if(err.message) {
err.message = err.message.replace(REDACT_REGEX_MULTI, REDACT_REPLACEMENT);
}
}
return err;
}

31 changes: 31 additions & 0 deletions lib/private/redact-passwords.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ██████╗ ███████╗██████╗ █████╗ ██████╗████████╗
// ██╔══██╗██╔════╝██╔══██╗██╔══██╗██╔════╝╚══██╔══╝
// ██████╔╝█████╗ ██║ ██║███████║██║ ██║
// ██╔══██╗██╔══╝ ██║ ██║██╔══██║██║ ██║
// ██║ ██║███████╗██████╔╝██║ ██║╚██████╗ ██║
// ╚═╝ ╚═╝╚══════╝╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
//
// ██████╗ █████╗ ███████╗███████╗██╗ ██╗ ██████╗ ██████╗ ██████╗ ███████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║ ██║██╔═══██╗██╔══██╗██╔══██╗██╔════╝
// ██████╔╝███████║███████╗███████╗██║ █╗ ██║██║ ██║██████╔╝██║ ██║███████╗
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║███╗██║██║ ██║██╔══██╗██║ ██║╚════██║
// ██║ ██║ ██║███████║███████║╚███╔███╔╝╚██████╔╝██║ ██║██████╔╝███████║
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝ ╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝ ╚══════╝
//
// Remove database passwords from the error instance.

module.exports = function redactPasswords(err) {
var REDACT_REPLACEMENT = '$1:****@';
var REDACT_REGEX_SINGLE = /^(postgres:\/\/[^:\s]*):[^@\s]*@/;
var REDACT_REGEX_MULTI = /(postgres:\/\/[^:\s]*):[^@\s]*@/g;

if(err) {
if(err.meta && typeof err.meta === 'object' && err.meta.url && typeof err.meta.url === 'string') {
err.meta.url = err.meta.url.replace(REDACT_REGEX_SINGLE, REDACT_REPLACEMENT);
}
if(err.message && typeof err.message === 'string') {
err.message = err.message.replace(REDACT_REGEX_MULTI, REDACT_REPLACEMENT);
}
}
return err;
}

0 comments on commit 8ff4bdb

Please sign in to comment.