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 obfuscator with named parameters #62

Merged
merged 1 commit into from
Feb 13, 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
8 changes: 5 additions & 3 deletions src/logging/request-obfuscator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Module dependencies.
*/

import { defaults, get, has, isArray, isEmpty, isString, map, mapKeys } from 'lodash';
import { assign, defaults, get, has, isArray, isEmpty, isPlainObject, isString, map, mapKeys } from 'lodash';
import methods from '../methods';

/**
Expand Down Expand Up @@ -73,9 +73,11 @@ function obfuscateRequestBody(body) {
return body;
}

body.params = method(body.params);
if (isPlainObject(body.params)) {
return assign(body, { params: method.named(body.params) });
}

return body;
return assign(body, { params: method.default(body.params) });
}

/**
Expand Down
40 changes: 33 additions & 7 deletions src/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export default {
dumpWallet: { version: '>=0.9.0' },
encryptWallet: {
obfuscate: {
request: params => set([...params], '[0]', '******')
request: {
default: params => set([...params], '[0]', '******'),
named: params => set(params, 'passphrase', '******')
}
},
version: '>=0.1.0'
},
Expand Down Expand Up @@ -92,13 +95,19 @@ export default {
importAddress: { version: '>=0.10.0' },
importMulti: {
obfuscate: {
request: params => set(params, '[0]', map(params[0], request => set(request, 'keys', map(request.keys, () => '******'))))
request: {
default: params => set(params, '[0]', map(params[0], request => set(request, 'keys', map(request.keys, () => '******')))),
named: params => set(params, 'requests', map(params.requests, request => set(request, 'keys', map(request.keys, () => '******'))))
}
},
version: '>=0.14.0'
},
importPrivKey: {
obfuscate: {
request: () => ['******']
request: {
default: () => ['******'],
named: params => set(params, 'privkey', '******')
}
},
version: '>=0.6.0'
},
Expand Down Expand Up @@ -135,13 +144,19 @@ export default {
signMessage: { version: '>=0.5.0' },
signMessageWithPrivKey: {
obfuscate: {
request: params => set([...params], '[0]', '******')
request: {
default: params => set([...params], '[0]', '******'),
named: params => set(params, 'privkey', '******')
}
},
version: '>=0.13.0'
},
signRawTransaction: {
obfuscate: {
request: params => set([...params], '[2]', map(params[2], () => '******'))
request: {
default: params => set([...params], '[2]', map(params[2], () => '******')),
named: params => set(params, 'privkeys', map(params.privkeys || [], () => '******'))
}
},
version: '>=0.7.0'
},
Expand All @@ -155,9 +170,20 @@ export default {
walletLock: { version: '>=0.1.0' },
walletPassphrase: {
obfuscate: {
request: params => set([...params], '[0]', '******')
request: {
default: params => set([...params], '[0]', '******'),
named: params => set(params, 'passphrase', '******')
}
},
version: '>=0.1.0'
},
walletPassphraseChange: { version: '>=0.1.0' }
walletPassphraseChange: {
obfuscate: {
request: {
default: params => set(set([...params], '[0]', '******'), '[1]', '******'),
named: params => set(set(params, 'oldpassphrase', '******'), 'newpassphrase', '******')
}
},
version: '>=0.1.0'
}
};
66 changes: 66 additions & 0 deletions test/logging/request-obfuscator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ describe('RequestObfuscator', () => {
request.body.should.eql('{"id":"1485369469422","method":"importprivkey","params":["******"]}');
});

it('should obfuscate the private key from `request.body` when `method` is `importprivkey` and RPC is called with named parameters', () => {
const request = { body: '{"id":"1485369469422","method":"importprivkey","params":{"privkey":"foobar"}}', type: 'request' };

obfuscate(request);

request.body.should.eql('{"id":"1485369469422","method":"importprivkey","params":{"privkey":"******"}}');
});

it('should obfuscate the private key from `request.body` when `method` is `signmessagewithprivkey`', () => {
const request = { body: '{"id":"1485369469422","method":"signmessagewithprivkey","params":["foobar", "foobiz"]}', type: 'request' };

Expand All @@ -51,6 +59,14 @@ describe('RequestObfuscator', () => {
request.body.should.eql('{"id":"1485369469422","method":"signmessagewithprivkey","params":["******","foobiz"]}');
});

it('should obfuscate the private key from `request.body` when `method` is `signmessagewithprivkey` and RPC is called with named parameters', () => {
const request = { body: '{"id":"1485369469422","method":"signmessagewithprivkey","params":{"privkey":"foobar","message":"foobiz"}}', type: 'request' };

obfuscate(request);

request.body.should.eql('{"id":"1485369469422","method":"signmessagewithprivkey","params":{"privkey":"******","message":"foobiz"}}');
});

it('should obfuscate all private keys from `request.body` when `method` is `signrawtransaction`', () => {
const request = { body: '{"id":"1485369469422","method":"signrawtransaction","params":["foo","bar",["biz", "boz"]]}', type: 'request' };

Expand All @@ -59,6 +75,24 @@ describe('RequestObfuscator', () => {
request.body.should.eql('{"id":"1485369469422","method":"signrawtransaction","params":["foo","bar",["******","******"]]}');
});

it('should obfuscate all private keys from `request.body` when `method` is `signrawtransaction` and RPC is called with named parameters', () => {
const request = { body: `{"id":"1485369469422","method":"signrawtransaction","params":${JSON.stringify({
hexstring: 'foo',
prevtxs: [],
privkeys: ['foo', 'bar'],
sighashtype: 'bar'
})}}`, type: 'request' };

obfuscate(request);

request.body.should.eql(`{"id":"1485369469422","method":"signrawtransaction","params":${JSON.stringify({
hexstring: 'foo',
prevtxs: [],
privkeys: ['******', '******'],
sighashtype: 'bar'
})}}`);
});

it('should obfuscate the passphrase from `request.body` when `method` is `encryptwallet`', () => {
const request = { body: '{"id":"1485369469422","method":"encryptwallet","params":["foobar"]}', type: 'request' };

Expand All @@ -67,6 +101,14 @@ describe('RequestObfuscator', () => {
request.body.should.eql('{"id":"1485369469422","method":"encryptwallet","params":["******"]}');
});

it('should obfuscate the passphrase from `request.body` when `method` is `encryptwallet` and RPC is called with named parameters', () => {
const request = { body: '{"id":"1485369469422","method":"encryptwallet","params":{"passphrase":"foobar"}}', type: 'request' };

obfuscate(request);

request.body.should.eql('{"id":"1485369469422","method":"encryptwallet","params":{"passphrase":"******"}}');
});

it('should obfuscate the passphrase from `request.body` when `method` is `walletpassphrase`', () => {
const request = { body: '{"id":"1485369469422","method":"walletpassphrase","params":["foobar"]}', type: 'request' };

Expand All @@ -75,6 +117,30 @@ describe('RequestObfuscator', () => {
request.body.should.eql('{"id":"1485369469422","method":"walletpassphrase","params":["******"]}');
});

it('should obfuscate the passphrase from `request.body` when `method` is `walletpassphrase` and RPC is called with named parameters', () => {
const request = { body: '{"id":"1485369469422","method":"walletpassphrase","params":{"passphrase":"foobar"}}', type: 'request' };

obfuscate(request);

request.body.should.eql('{"id":"1485369469422","method":"walletpassphrase","params":{"passphrase":"******"}}');
});

it('should obfuscate the oldpassphrase and newpassphrase from `request.body` when `method` is `walletpassphrasechange`', () => {
const request = { body: '{"id":"1485369469422","method":"walletpassphrasechange","params":["foobar", "foobiz"]}', type: 'request' };

obfuscate(request);

request.body.should.eql('{"id":"1485369469422","method":"walletpassphrasechange","params":["******","******"]}');
});

it('should obfuscate the oldpassphrase and newpassphrase from `request.body` when `method` is `walletpassphrasechange` and RPC is called with named parameters', () => {
const request = { body: '{"id":"1485369469422","method":"walletpassphrasechange","params":{"oldpassphrase":"foobar","newpassphrase":"foobar"}}', type: 'request' };

obfuscate(request);

request.body.should.eql('{"id":"1485369469422","method":"walletpassphrasechange","params":{"oldpassphrase":"******","newpassphrase":"******"}}');
});

it('should obfuscate the `request.body` of a batch request', () => {
const request = { body: '[{"id":"1485369469422","method":"walletpassphrase","params":["foobar"]},{"id":"1485369469423","method":"walletpassphrase","params":["foobar"]}]', type: 'request' };

Expand Down