Skip to content

Commit

Permalink
add-secure-paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Grandvuinet committed Jul 3, 2017
1 parent 80ad178 commit 0cd3938
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
26 changes: 24 additions & 2 deletions secure-log-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const SENSITIVE = [
'security_token'
];

// if the path is 'foo.bar.key' then put the following in the map:
// key: 'foo.bar'
const SENSATIVE_PATH = {
user: 'auth'
};

const TOKEN = /token=[^;]*/;
const TOKEN_ENC = /token%3[^&]*/;

Expand All @@ -20,7 +26,8 @@ function sanitize(val) {
return;
} else if (
schemaError(this.key, this.parent && this.parent.node) ||
SENSITIVE.indexOf(this.key.toLowerCase()) !== -1) {
SENSITIVE.indexOf(this.key.toLowerCase()) !== -1 ||
isSensativePath(this.key, this.parent && this.parent.path) ) {
this.update('***');
} else if (TOKEN.test(val)) {
this.update(val.replace(TOKEN, 'token=***'));
Expand All @@ -29,6 +36,21 @@ function sanitize(val) {
}
}

function isSensativePath(key, path) {
if (!SENSATIVE_PATH[key] || !path || path.length < 1) {
return false;
}
const sensativePath = SENSATIVE_PATH[key];
let partialPath;
for (var i = path.length; i >= 0; i--) {
const previousPath = partialPath ? `${partialPath}.` : '';
if (`${previousPath}${path[i]}` === sensativePath) {
return true;
}
}
return false;
}

function schemaError(key, obj) {
return key === 'value' &&
obj &&
Expand All @@ -39,4 +61,4 @@ function schemaError(key, obj) {

module.exports = function(data) {
return traverse(data).map(sanitize);
};
};
70 changes: 68 additions & 2 deletions test/secure-log-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const expect = require('chai').expect;
const secure = require('../secure-log-data');

describe('secure-data.spec.js', function() {

it('should mask all x-credentials values', function() {
expect(secure({
headers: {
Expand Down Expand Up @@ -101,4 +101,70 @@ describe('secure-data.spec.js', function() {
});
});

});
it('should mask all sensative paths', function() {
expect(secure({
auth: {
user: 'secretUserName',
good: 'not secret'
},
authX: {
user: 'not secret',
good: 'not secret'
},
data: {
auth: {
user: 'secretUserName',
good: 'not secret'
}
},
datas: [
{
auth: {
user: 'secretUserName',
good: 'not secret'
}
},
{
data: {
auth: {
user: 'secretUserName',
good: 'not secret'
}
}
}
]
})).to.eql({
auth: {
user: '***',
good: 'not secret'
},
authX: {
user: 'not secret',
good: 'not secret'
},
data: {
auth: {
user: '***',
good: 'not secret'
}
},
datas: [
{
auth: {
user: '***',
good: 'not secret'
}
},
{
data: {
auth: {
user: '***',
good: 'not secret'
}
}
}
]
});
});

});

0 comments on commit 0cd3938

Please sign in to comment.