Skip to content

Commit 5a10bfd

Browse files
fix: remove request auth headers from debug logging (#622)
Co-authored-by: Elise Shanholtz <[email protected]>
1 parent d1157f2 commit 5a10bfd

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

lib/base/RequestClient.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ RequestClient.prototype.request = function(opts) {
9191
};
9292

9393
if (opts.logLevel === 'debug') {
94-
logRequest(options)
94+
this.logRequest(options)
9595
}
9696

9797
var _this = this;
@@ -102,7 +102,6 @@ RequestClient.prototype.request = function(opts) {
102102
if (opts.logLevel === 'debug') {
103103
console.log(`response.statusCode: ${response.status}`)
104104
console.log(`response.headers: ${JSON.stringify(response.headers)}`)
105-
console.log(`response.data: ${JSON.stringify(response.data)}`)
106105
}
107106
_this.lastResponse = new Response(response.status, response.data, response.headers);
108107
deferred.resolve({
@@ -117,23 +116,25 @@ RequestClient.prototype.request = function(opts) {
117116
return deferred.promise;
118117
};
119118

120-
function logRequest(options) {
119+
RequestClient.prototype.filterLoggingHeaders = function (headers){
120+
return Object.keys(headers).filter((header) => {
121+
return !'authorization'.includes(header.toLowerCase());
122+
});
123+
}
124+
125+
RequestClient.prototype.logRequest = function (options){
121126
console.log('-- BEGIN Twilio API Request --');
122127
console.log(`${options.method} ${options.url}`);
123128

124-
if (options.data) {
125-
console.log('Form data:');
126-
console.log(options.data);
127-
}
128-
129129
if (options.params) {
130130
console.log('Querystring:');
131131
console.log(options.params);
132132
}
133133

134134
if (options.headers) {
135135
console.log('Headers:');
136-
console.log(options.headers)
136+
const filteredHeaderKeys = this.filterLoggingHeaders(options.headers);
137+
filteredHeaderKeys.forEach((header) => console.log(`${header}: ${options.headers[header]}`));
137138
}
138139

139140
console.log('-- END Twilio API Request --');

spec/unit/base/RequestClient.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ describe('lastResponse and lastRequest defined', function() {
4646
expect(client.lastResponse.body).toEqual('voltron');
4747
});
4848

49+
it('should not include request authorization header in filtered headers', function () {
50+
const filteredHeaderKeys = client.filterLoggingHeaders(client.lastRequest.headers);
51+
expect(filteredHeaderKeys).toEqual(['test-header-key', 'Connection'])
52+
});
53+
4954
});
5055

5156
describe('lastRequest defined, lastResponse undefined', function() {

0 commit comments

Comments
 (0)