Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix response log obfuscation #64

Merged
merged 2 commits into from
Oct 25, 2018
Merged
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
18 changes: 10 additions & 8 deletions src/logging/request-obfuscator.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ function obfuscateResponse(request, instance) {
return;
}

if (!get(request, 'response.body')) {
if (!request.body) {
return;
}

if (get(request, `response.headers['content-type']`) === 'application/octet-stream') {
request.response.body = '******';
if (get(request, `headers['content-type']`) === 'application/octet-stream') {
request.body = '******';

return;
}
Expand All @@ -49,17 +49,19 @@ function obfuscateResponse(request, instance) {
return;
}

request.body = JSON.parse(request.body);
Copy link
Owner

Choose a reason for hiding this comment

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

@pedrobranco shouldn't we be keeping the same string as before?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Owner

Choose a reason for hiding this comment

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

I suggest passing the parsed body as an argument instead of mutating request.body here. I would then stringify the resulting (mutated) body object at the end of the method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh sure.


const requestBody = JSON.parse(instance.body);

if (isArray(request.response.body)) {
if (isArray(request.body)) {
const methodsById = mapKeys(requestBody, method => method.id);

request.response.body = map(request.response.body, request => obfuscateResponseBody(request, methodsById[request.id].method));

return;
request.body = map(request.body, request => obfuscateResponseBody(request, methodsById[request.id].method));
} else {
request.body = obfuscateResponseBody(request.body, requestBody.method);
}

request.response.body = obfuscateResponseBody(request.response.body, requestBody.method);
request.body = JSON.stringify(request.body);
}

/**
Expand Down
41 changes: 17 additions & 24 deletions test/logging/request-obfuscator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,46 +149,39 @@ describe('RequestObfuscator', () => {
request.body.should.eql('[{"id":"1485369469422","method":"walletpassphrase","params":["******"]},{"id":"1485369469423","method":"walletpassphrase","params":["******"]}]');
});

it('should obfuscate the private key from `response.body` when `method` is `dumpprivkey`', () => {
const request = { response: { body: { id: '1485369469422-0', result: 'foobiz' } }, type: 'response' };
it('should obfuscate the private key from `body` when `method` is `dumpprivkey`', () => {
const request = { body: '{"id":"1485369469422-0","result":"foobiz"}', type: 'response' };

obfuscate(request, { body: '{"id":"1485369469422","method":"dumpprivkey","params":["foobar"]}' });

request.response.body.should.eql({ id: '1485369469422-0', result: '******' });
JSON.parse(request.body).should.eql({ id: '1485369469422-0', result: '******' });
});

it('should obfuscate the `response.body` when `headers.content-type` is `application/octet-stream`', () => {
const request = { response: { body: new Buffer('foobar'), headers: { 'content-type': 'application/octet-stream' } }, type: 'response' };
it('should obfuscate the `body` when `headers.content-type` is `application/octet-stream`', () => {
const request = { body: new Buffer('foobar'), headers: { 'content-type': 'application/octet-stream' }, type: 'response' };

obfuscate(request, { uri: 'foobar.bin' });

request.response.should.eql({ body: '******', headers: { 'content-type': 'application/octet-stream' } });
request.should.eql({ body: '******', headers: { 'content-type': 'application/octet-stream' }, type: 'response' });
});

it('should obfuscate the `request.response.body` of a batch request', () => {
it('should obfuscate the `request.body` of a batch request', () => {
const request = {
response: {
body: [
{ id: '1485369469422-0', result: 'foobar' },
{ id: '1485369469422-2', result: 'foobiz' },
{ id: '1485369469422-1', result: 'foo' }
]
},
body: JSON.stringify([
{ id: '1485369469422-0', result: 'foobar' },
{ id: '1485369469422-2', result: 'foobiz' },
{ id: '1485369469422-1', result: 'foo' }
]),
type: 'response'
};

obfuscate(request, { body: '[{"id":"1485369469422-0","method":"dumpprivkey","params":["foobar"]},{"id":"1485369469422-2","method":"getnewaddress","params":["foobiz"]},{"id":"1485369469422-1","method":"dumpprivkey","params":["foobiz"]}]' });

request.should.eql({
response: {
body: [
{ id: '1485369469422-0', result: '******' },
{ id: '1485369469422-2', result: 'foobiz' },
{ id: '1485369469422-1', result: '******' }
]
},
type: 'response'
});
JSON.parse(request.body).should.eql([
{ id: '1485369469422-0', result: '******' },
{ id: '1485369469422-2', result: 'foobiz' },
{ id: '1485369469422-1', result: '******' }
]);
});
});
});