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 JSON-RPC response validation #2862

Merged
merged 6 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 4 additions & 8 deletions packages/web3-providers/src/providers/Web3EthereumProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* @date 2018
*/

import JsonRpcResponseValidator from '../validators/JsonRpcResponseValidator';
import AbstractSocketProvider from '../../lib/providers/AbstractSocketProvider';

export default class Web3EthereumProvider extends AbstractSocketProvider {
Expand Down Expand Up @@ -129,14 +128,11 @@ export default class Web3EthereumProvider extends AbstractSocketProvider {
* @returns {Promise<Object>}
*/
async send(method, parameters) {
const response = await this.connection.send(method, parameters);
const validationResult = JsonRpcResponseValidator.validate(response);

if (validationResult instanceof Error) {
throw validationResult;
try {
return await this.connection.send(method, parameters);
} catch (error) {
throw new Error(`Node error: ${error.message}`);
}

return response;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Web3EthereumProvider from '../../../src/providers/Web3EthereumProvider';
import JsonRpcResponseValidator from '../../../src/validators/JsonRpcResponseValidator';
import AbstractMethod from '../../__mocks__/AbstractMethod';
import AbstractWeb3Module from '../../__mocks__/AbstractWeb3Module';
import AbstractSocketProvider from '../../../lib/providers/AbstractSocketProvider';
Expand Down Expand Up @@ -175,10 +174,6 @@ describe('Web3EthereumProviderTest', () => {
});

it('calls send and returns a resolved promise with the response', async () => {
JsonRpcResponseValidator.validate = jest.fn(() => {
return true;
});

socketMock.send = jest.fn((method, parameters) => {
expect(method).toEqual('method');

Expand All @@ -190,26 +185,18 @@ describe('Web3EthereumProviderTest', () => {
const response = await ethereumProvider.send('method', []);

expect(response).toEqual(true);

expect(JsonRpcResponseValidator.validate).toHaveBeenCalled();
});

it('calls send and returns a rejected promise because of a invalid response', async () => {
JsonRpcResponseValidator.validate = jest.fn(() => {
return new Error('invalid');
});

it('calls send and returns a rejected promise because of an error response', async () => {
socketMock.send = jest.fn((method, parameters) => {
expect(method).toEqual('method');

expect(parameters).toEqual([]);

return Promise.resolve(false);
return Promise.reject(new Error('invalid'));
});

await expect(ethereumProvider.send('method', [])).rejects.toThrow('invalid');

expect(JsonRpcResponseValidator.validate).toHaveBeenCalled();
await expect(ethereumProvider.send('method', [])).rejects.toThrow('Node error: invalid');
});

it('calls sendBatch and returns a resolved promise with the response', async () => {
Expand Down