Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,23 @@ module.exports = {
* (e.g. "password")
*
* > WARNING:
* > This is a SHALLOW check of request body, querystring, and route path parameters.
* > If recursive is set to false (default) This is a SHALLOW check of request body, querystring, and route path parameters.
* > Deeply nested properties with these names are not redacted.
* > If recursive is set to true, a one level check and reduction is done.
* > For example on put request:
* > { '0': 'api/v1/users/2',
* > user:
* > { username: 'fauser',
* > password: '*REDACTED*',
* > token: '*REDACTED*',
* > }
* > }
*/
dontLogParams: [
'password',
'token'
],
recursive: false,

/**
* When request starts...
Expand Down
10 changes: 5 additions & 5 deletions private/log-request.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function logRequest_middleware(req, res, next) {
path: req.path,
method: req.method,

allParams: redactProtectedKeys(req.allParams(), sails.config.apianalytics.dontLogParams),
allParams: redactProtectedKeys(req.allParams(), sails.config.apianalytics.dontLogParams, sails.config.apianalytics.recursive),

protocol: req.protocol,

Expand All @@ -39,9 +39,9 @@ module.exports = function logRequest_middleware(req, res, next) {
url: req.url,
transport: req.transport,
options: req.options,
queryParams: redactProtectedKeys(req.query, sails.config.apianalytics.dontLogParams),
routeParams: redactProtectedKeys(req.params, sails.config.apianalytics.dontLogParams),
bodyParams: redactProtectedKeys(req.body, sails.config.apianalytics.dontLogParams)
queryParams: redactProtectedKeys(req.query, sails.config.apianalytics.dontLogParams, sails.config.apianalytics.recursive),
routeParams: redactProtectedKeys(req.params, sails.config.apianalytics.dontLogParams, sails.config.apianalytics.recursive),
bodyParams: redactProtectedKeys(req.body, sails.config.apianalytics.dontLogParams, sails.config.apianalytics.recursive)
}

};//</build initial report>
Expand Down Expand Up @@ -95,7 +95,7 @@ module.exports = function logRequest_middleware(req, res, next) {
report.responseHeaders = res._headers;

// Save user session as embedded JSON to keep a permanent record
report.userSession = _.cloneDeep(req.session);
report.userSession = redactProtectedKeys(_.cloneDeep(req.session), sails.config.apianalytics.dontLogParams, sails.config.apianalytics.recursive);

// Call log function
if (_.isFunction(sails.config.apianalytics.onResponse)) {
Expand Down
18 changes: 17 additions & 1 deletion private/redact-protected-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var _ = require('lodash');
* @returns {Dictionary}
*/

module.exports = function redactProtectedKeys(dictionary, blacklist) {
module.exports = function redactProtectedKeys(dictionary, blacklist, recursive) {

if (!_.isObject(dictionary)) {
return dictionary;
Expand All @@ -44,6 +44,9 @@ module.exports = function redactProtectedKeys(dictionary, blacklist) {
throw new Error('Consistency violation: Unexpected bad usage in cleanseDictionary. Expected blacklist to be an array of strings, but got: '+util.inspect(blacklist,{depth:null}));
}//>-•

if(_.isUndefined(recursive)){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, imho if (....) { because that is used elsewhere in this code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dan-omniscience yeah, if you wouldn't mind, that'd be helpful just for consistency
@tarlepp thanks, good catch

recursive = false;
}

// Get deep clone of dictionary.
var cleansedCopy = _.cloneDeep(dictionary);
Expand All @@ -57,5 +60,18 @@ module.exports = function redactProtectedKeys(dictionary, blacklist) {

});//</_.each>

if(recursive){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

// Loop over each top-level property and check if it is an object
_.each(cleansedCopy, function(value, key){
if(_.isObject(cleansedCopy[key])){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

// Loop over each object inner property and redact any that are protected.
_.each(cleansedCopy[key], function(value, PropName){
if(blacklist.indexOf(PropName) >= 0){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

cleansedCopy[key][PropName] = '*REDACTED*';
}
});
}
});//</_.each>
}
return cleansedCopy;
};