-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into dependabot/npm_and_yarn/husky-3.0.4
- Loading branch information
Showing
7 changed files
with
162 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
var util = require('util'); | ||
|
||
/** | ||
* @module errors | ||
*/ | ||
var errors = (module.exports = {}); | ||
|
||
/** | ||
* Given a response request error, sanitize sensitive data. | ||
* | ||
* @method sanitizeErrorRequestData | ||
* @memberOf module:errors | ||
*/ | ||
errors.sanitizeErrorRequestData = function(error) { | ||
if (!error.response || !error.response.request || !error.response.request._data) { | ||
return error; | ||
} | ||
|
||
Object.keys(error.response.request._data).forEach(function(key) { | ||
if (key.toLowerCase().match('password|secret')) { | ||
error.response.request._data[key] = '[SANITIZED]'; | ||
} | ||
}); | ||
|
||
return error; | ||
}; | ||
|
||
/** | ||
* Given an Api Error, modify the original error and sanitize | ||
* sensitive information using sanitizeErrorRequestData | ||
* | ||
* @method SanitizedError | ||
* @memberOf module:errors | ||
*/ | ||
var SanitizedError = function(name, message, status, requestInfo, originalError) { | ||
this.name = name || this.constructor.name || this.constructor.prototype.name || ''; | ||
this.message = message || ''; | ||
this.statusCode = status || (originalError && originalError.code); | ||
this.requestInfo = Object.assign({}, requestInfo); | ||
this.originalError = errors.sanitizeErrorRequestData(originalError); | ||
|
||
Error.captureStackTrace(this, this.constructor); | ||
}; | ||
|
||
util.inherits(SanitizedError, Error); | ||
|
||
errors.SanitizedError = SanitizedError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
var expect = require('chai').expect; | ||
|
||
var errors = require('../src/errors'); | ||
|
||
describe('Errors', function() { | ||
describe('sanitizeErrorRequestData', function() { | ||
describe('when passed in error is missing request data', function() { | ||
var error = { response: { request: {} } }; | ||
var sanitizedError = errors.sanitizeErrorRequestData(error); | ||
|
||
it('should return error', function() { | ||
expect(sanitizedError).to.equal(error); | ||
}); | ||
}); | ||
|
||
describe('when passed in error has request data', function() { | ||
const error = { | ||
response: { | ||
request: { | ||
_data: { | ||
DATA_SECRET: 'secret', | ||
USER_PASSWORD: 'password', | ||
USER_NAME: 'username' | ||
} | ||
} | ||
} | ||
}; | ||
const sanitizedError = errors.sanitizeErrorRequestData(error); | ||
const sanitizedData = sanitizedError.response.request._data; | ||
|
||
it('should return [SANITIZED] for DATA_SECRET', function() { | ||
expect(sanitizedData.DATA_SECRET).to.equal('[SANITIZED]'); | ||
}); | ||
it('should return [SANITIZED] for DATA_SECRET', function() { | ||
expect(sanitizedData.DATA_SECRET).to.equal('[SANITIZED]'); | ||
}); | ||
it('should return original value for USER_NAME', function() { | ||
expect(sanitizedData.USER_NAME).to.equal(sanitizedData.USER_NAME); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('SanitizedError', function() { | ||
var name = 'ErrorName'; | ||
var message = 'message'; | ||
var status = 500; | ||
var requestInfo = { keyA: 'a', keyB: 'b' }; | ||
var originalError = { response: { request: { _data: { secret: 'secretpassword' } } } }; | ||
var sanitizedError = new errors.SanitizedError( | ||
name, | ||
message, | ||
status, | ||
requestInfo, | ||
originalError | ||
); | ||
|
||
it('should be an instance of the builtin Error', function() { | ||
expect(sanitizedError).to.be.an.instanceof(Error); | ||
}); | ||
|
||
it('should be an instance of its class', function() { | ||
expect(sanitizedError).to.be.an.instanceof(errors.SanitizedError); | ||
}); | ||
|
||
it('should have a name', function() { | ||
expect(sanitizedError.name).to.eql(name); | ||
}); | ||
|
||
it('should have a message', function() { | ||
expect(sanitizedError.message).to.eql(message); | ||
}); | ||
|
||
it('should have a statusCode', function() { | ||
expect(sanitizedError.statusCode).to.eql(status); | ||
}); | ||
|
||
it('should have request info', function() { | ||
expect(sanitizedError.requestInfo).to.deep.eql(requestInfo); | ||
}); | ||
|
||
it('should have the original error', function() { | ||
expect(sanitizedError.originalError).to.eql(originalError); | ||
}); | ||
|
||
it('should sanitize the original error sensitive information', function() { | ||
expect(sanitizedError.originalError.response.request._data.secret).to.eql('[SANITIZED]'); | ||
}); | ||
|
||
it('should have a stack with the message and location the error was created', function() { | ||
expect(sanitizedError.stack).to.exist; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters