Skip to content

Commit

Permalink
Updates to filtering.
Browse files Browse the repository at this point in the history
  • Loading branch information
arb committed Apr 13, 2015
1 parent 0fcfad8 commit caa91eb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -45,7 +45,7 @@ set `options` to an object with the following optional settings:
- "remove" - `delete`s the value
- a valid regular express string. Only supports a single group. Ex: `"(\\d{4})$"` will replace the last four digits with "X"s. Take extra care when creating this string. You will need to make sure that the resultant RegExp object is what you need.

`filter` can be used to remove potentially sensitive information (credit card numbers, social security numbers, etc.) from the log payloads before they are sent out to reporters. This setting only impacts `response` events and only if payloads are included via `requestPayload` and `responsePayload`. `filter` is intended to impact the reporting of ALL downstream reporters. If you want filtering in only one, you will need to create a customized reporter.
`filter` can be used to remove potentially sensitive information (credit card numbers, social security numbers, etc.) from the log payloads before they are sent out to reporters. This setting only impacts `response` events and only if payloads are included via `requestPayload` and `responsePayload`. `filter` is intended to impact the reporting of ALL downstream reporters. If you want filtering in only one, you will need to create a customized reporter. The filtering is done recursively so if you want to "censor" `ccn`, anywhere `ccn` appears in request or response bodies will be "censor"ed. Currently, you can only filter leaf nodes; nothing with children.

## Reporter Interface

Expand Down
46 changes: 23 additions & 23 deletions lib/utils.js
@@ -1,5 +1,6 @@
// Load modules
var Hoek = require('hoek');
var Traverse = require('traverse');

// Declare internals

Expand Down Expand Up @@ -76,35 +77,34 @@ exports.GreatResponse = function (request, options, filterRules) {
var req = request.raw.req;
var res = request.raw.res;

var replacer = function (match, group1) {
var replacer = function (match, group) {

return (new Array(group1.length + 1).join('X'));
return (new Array(group.length + 1).join('X'));
};

var applyFilter = function (data) {

for (var key in data) {
if (typeof data[key] === 'object') {
return applyFilter(data[key]);
}

// there is a filer for this key, so we are going to update the data
if (filterRules[key]) {
var filter = filterRules[key].toLowerCase();

if (filter === 'censor') {
data[key] = ('' + data[key]).replace(/./gi, 'X');
}
else if (filter === 'remove') {
delete data[key];
}
// Means this is a string that needs to be turned into a RegEx
else {
var regex = new RegExp(filter);
data[key] = ('' + data[key]).replace(regex, replacer);
Traverse(data).forEach(function (value) {

if (this.isLeaf) {
if (filterRules[this.key] || filterRules[this.parent.key]) {

var filter = (filterRules[this.key] || filterRules[this.parent.key]).toLowerCase();

if (filter === 'censor') {
this.update(('' + value).replace(/./g, 'X'));
}
else if (filter === 'remove') {
this.delete();
}
// Means this is a string that needs to be turned into a RegEx
else {
var regex = new RegExp(filter);
this.update(('' + value).replace(regex, replacer));
}
}
}
}
});
};

this.event = 'response';
Expand Down Expand Up @@ -139,7 +139,7 @@ exports.GreatResponse = function (request, options, filterRules) {
}

if (Object.keys(filterRules).length) {

applyFilter(this.requestPayload);
applyFilter(this.responsePayload);
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -16,9 +16,10 @@
"node": ">=0.10.x"
},
"dependencies": {
"items": "1.x.x",
"hoek": "2.x.x",
"items": "1.x.x",
"joi": "5.x.x",
"traverse": "0.6.6",
"wreck": "5.4.x"
},
"peerDependencies": {
Expand Down
33 changes: 25 additions & 8 deletions test/monitor.js
Expand Up @@ -488,13 +488,22 @@ describe('good', function () {
reply({
first: 'John',
last: 'Smith',
ssn: 'ABCDEFG',
ccn: '9999999999',
line: 'foo',
userId: 555645465,
address: {
street: '123 Main Street',
line: ['123 Main street', 'Apt 200', 'Suite 100'],
bar: {
line: '123',
extra: 123456
},
city: 'Pittsburgh',
last: 'Jones'
last: 'Jones',
foo: [{
email: 'adam@hapijs.com',
baz: 'another string',
line: 'another string'
}]
}
});
}
Expand All @@ -508,14 +517,13 @@ describe('good', function () {
requestPayload: true,
responsePayload: true,
filter: {
ssn: 'remove',
last: 'censor',
password: 'censor',
email: 'remove',
ccn: '(\\d{4})$',
userId: '(645)',
street: 'censor',
city: '(\\w?)'
city: '(\\w?)',
line: 'censor'
}
}
};
Expand Down Expand Up @@ -547,10 +555,19 @@ describe('good', function () {
last: 'XXXXX',
ccn: '999999XXXX',
userId: '555XXX465',
line: 'XXX',
address: {
street: 'XXXXXXXXXXXXXXX',
line: ['XXXXXXXXXXXXXXX', 'XXXXXXX', 'XXXXXXXXX'],
bar: {
line: 'XXX',
extra: 123456
},
city: 'Xittsburgh',
last: 'XXXXX'
last: 'XXXXX',
foo: [{
baz: 'another string',
line: 'XXXXXXXXXXXXXX'
}]
}
});
done();
Expand Down

0 comments on commit caa91eb

Please sign in to comment.