Skip to content

Commit 9b6fb1b

Browse files
author
Pedro Branco
committed
Fix obfuscator with named parameters
1 parent e760e4d commit 9b6fb1b

File tree

3 files changed

+104
-10
lines changed

3 files changed

+104
-10
lines changed

src/logging/request-obfuscator.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Module dependencies.
44
*/
55

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

99
/**
@@ -73,9 +73,11 @@ function obfuscateRequestBody(body) {
7373
return body;
7474
}
7575

76-
body.params = method(body.params);
76+
if (isPlainObject(body.params)) {
77+
return assign(body, { params: method.named(body.params) });
78+
}
7779

78-
return body;
80+
return assign(body, { params: method.default(body.params) });
7981
}
8082

8183
/**

src/methods.js

+33-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export default {
3535
dumpWallet: { version: '>=0.9.0' },
3636
encryptWallet: {
3737
obfuscate: {
38-
request: params => set([...params], '[0]', '******')
38+
request: {
39+
default: params => set([...params], '[0]', '******'),
40+
named: body => set(body, 'passphrase', '******')
41+
}
3942
},
4043
version: '>=0.1.0'
4144
},
@@ -92,13 +95,19 @@ export default {
9295
importAddress: { version: '>=0.10.0' },
9396
importMulti: {
9497
obfuscate: {
95-
request: params => set(params, '[0]', map(params[0], request => set(request, 'keys', map(request.keys, () => '******'))))
98+
request: {
99+
default: params => set(params, '[0]', map(params[0], request => set(request, 'keys', map(request.keys, () => '******')))),
100+
named: body => set(body, 'requests', map(body.requests, request => set(request, 'keys', map(request.keys, () => '******'))))
101+
}
96102
},
97103
version: '>=0.14.0'
98104
},
99105
importPrivKey: {
100106
obfuscate: {
101-
request: () => ['******']
107+
request: {
108+
default: () => ['******'],
109+
named: body => set(body, 'privkey', '******')
110+
}
102111
},
103112
version: '>=0.6.0'
104113
},
@@ -135,13 +144,19 @@ export default {
135144
signMessage: { version: '>=0.5.0' },
136145
signMessageWithPrivKey: {
137146
obfuscate: {
138-
request: params => set([...params], '[0]', '******')
147+
request: {
148+
default: params => set([...params], '[0]', '******'),
149+
named: body => set(body, 'privkey', '******')
150+
}
139151
},
140152
version: '>=0.13.0'
141153
},
142154
signRawTransaction: {
143155
obfuscate: {
144-
request: params => set([...params], '[2]', map(params[2], () => '******'))
156+
request: {
157+
default: params => set([...params], '[2]', map(params[2], () => '******')),
158+
named: body => set(body, 'privkeys', map(body.privkeys || [], () => '******'))
159+
}
145160
},
146161
version: '>=0.7.0'
147162
},
@@ -155,9 +170,20 @@ export default {
155170
walletLock: { version: '>=0.1.0' },
156171
walletPassphrase: {
157172
obfuscate: {
158-
request: params => set([...params], '[0]', '******')
173+
request: {
174+
default: params => set([...params], '[0]', '******'),
175+
named: body => set(body, 'passphrase', '******')
176+
}
159177
},
160178
version: '>=0.1.0'
161179
},
162-
walletPassphraseChange: { version: '>=0.1.0' }
180+
walletPassphraseChange: {
181+
obfuscate: {
182+
request: {
183+
default: params => set(set([...params], '[0]', '******'), '[1]', '******'),
184+
named: body => set(set(body, 'oldpassphrase', '******'), 'newpassphrase', '******')
185+
}
186+
},
187+
version: '>=0.1.0'
188+
}
163189
};

test/logging/request-obfuscator_test.js

+66
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ describe('RequestObfuscator', () => {
4343
request.body.should.eql('{"id":"1485369469422","method":"importprivkey","params":["******"]}');
4444
});
4545

46+
it('should obfuscate the private key from `request.body` when `method` is `importprivkey` and RPC is called with named parameters', () => {
47+
const request = { body: '{"id":"1485369469422","method":"importprivkey","params":{"privkey":"foobar"}}', type: 'request' };
48+
49+
obfuscate(request);
50+
51+
request.body.should.eql('{"id":"1485369469422","method":"importprivkey","params":{"privkey":"******"}}');
52+
});
53+
4654
it('should obfuscate the private key from `request.body` when `method` is `signmessagewithprivkey`', () => {
4755
const request = { body: '{"id":"1485369469422","method":"signmessagewithprivkey","params":["foobar", "foobiz"]}', type: 'request' };
4856

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

62+
it('should obfuscate the private key from `request.body` when `method` is `signmessagewithprivkey` and RPC is called with named parameters', () => {
63+
const request = { body: '{"id":"1485369469422","method":"signmessagewithprivkey","params":{"privkey":"foobar","message":"foobiz"}}', type: 'request' };
64+
65+
obfuscate(request);
66+
67+
request.body.should.eql('{"id":"1485369469422","method":"signmessagewithprivkey","params":{"privkey":"******","message":"foobiz"}}');
68+
});
69+
5470
it('should obfuscate all private keys from `request.body` when `method` is `signrawtransaction`', () => {
5571
const request = { body: '{"id":"1485369469422","method":"signrawtransaction","params":["foo","bar",["biz", "boz"]]}', type: 'request' };
5672

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

78+
it('should obfuscate all private keys from `request.body` when `method` is `signrawtransaction` and RPC is called with named parameters', () => {
79+
const request = { body: `{"id":"1485369469422","method":"signrawtransaction","params":${JSON.stringify({
80+
hexstring: 'foo',
81+
prevtxs: [],
82+
privkeys: ['foo', 'bar'],
83+
sighashtype: 'bar'
84+
})}}`, type: 'request' };
85+
86+
obfuscate(request);
87+
88+
request.body.should.eql(`{"id":"1485369469422","method":"signrawtransaction","params":${JSON.stringify({
89+
hexstring: 'foo',
90+
prevtxs: [],
91+
privkeys: ['******', '******'],
92+
sighashtype: 'bar'
93+
})}}`);
94+
});
95+
6296
it('should obfuscate the passphrase from `request.body` when `method` is `encryptwallet`', () => {
6397
const request = { body: '{"id":"1485369469422","method":"encryptwallet","params":["foobar"]}', type: 'request' };
6498

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

104+
it('should obfuscate the passphrase from `request.body` when `method` is `encryptwallet` and RPC is called with named parameters', () => {
105+
const request = { body: '{"id":"1485369469422","method":"encryptwallet","params":{"passphrase":"foobar"}}', type: 'request' };
106+
107+
obfuscate(request);
108+
109+
request.body.should.eql('{"id":"1485369469422","method":"encryptwallet","params":{"passphrase":"******"}}');
110+
});
111+
70112
it('should obfuscate the passphrase from `request.body` when `method` is `walletpassphrase`', () => {
71113
const request = { body: '{"id":"1485369469422","method":"walletpassphrase","params":["foobar"]}', type: 'request' };
72114

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

120+
it('should obfuscate the passphrase from `request.body` when `method` is `walletpassphrase` and RPC is called with named parameters', () => {
121+
const request = { body: '{"id":"1485369469422","method":"walletpassphrase","params":{"passphrase":"foobar"}}', type: 'request' };
122+
123+
obfuscate(request);
124+
125+
request.body.should.eql('{"id":"1485369469422","method":"walletpassphrase","params":{"passphrase":"******"}}');
126+
});
127+
128+
it('should obfuscate the oldpassphrase and newpassphrase from `request.body` when `method` is `walletpassphrasechange`', () => {
129+
const request = { body: '{"id":"1485369469422","method":"walletpassphrasechange","params":["foobar", "foobiz"]}', type: 'request' };
130+
131+
obfuscate(request);
132+
133+
request.body.should.eql('{"id":"1485369469422","method":"walletpassphrasechange","params":["******","******"]}');
134+
});
135+
136+
it('should obfuscate the oldpassphrase and newpassphrase from `request.body` when `method` is `walletpassphrasechange` and RPC is called with named parameters', () => {
137+
const request = { body: '{"id":"1485369469422","method":"walletpassphrasechange","params":{"oldpassphrase":"foobar","newpassphrase":"foobar"}}', type: 'request' };
138+
139+
obfuscate(request);
140+
141+
request.body.should.eql('{"id":"1485369469422","method":"walletpassphrasechange","params":{"oldpassphrase":"******","newpassphrase":"******"}}');
142+
});
143+
78144
it('should obfuscate the `request.body` of a batch request', () => {
79145
const request = { body: '[{"id":"1485369469422","method":"walletpassphrase","params":["foobar"]},{"id":"1485369469423","method":"walletpassphrase","params":["foobar"]}]', type: 'request' };
80146

0 commit comments

Comments
 (0)