Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions packages/relay/src/lib/validators/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// SPDX-License-Identifier: Apache-2.0

import { predefined } from '../errors/JsonRpcError';
import { IMethodValidation } from '../types/validation';
import { validateParam } from './utils';

export function validateParams(params: any[], indexes: IMethodValidation) {
if (params.length > Object.keys(indexes).length) {
throw predefined.INVALID_REQUEST;
}

for (const index of Object.keys(indexes)) {
const validation = indexes[Number(index)];
const param = params[Number(index)];
Expand Down
81 changes: 81 additions & 0 deletions packages/server/tests/acceptance/rpc_batch3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2192,4 +2192,85 @@
);
});
});

describe('Validate length of the rpc parameters array', async function () {
const testClient = Axios.create({
baseURL: 'http://localhost:' + ConfigService.get('E2E_SERVER_PORT'),
responseType: 'json' as const,
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
timeout: 30 * 1000,
});

const generateTest = (method, params) => {
it(method, async () => {
try {
await testClient.post('/', {
id: '2',
jsonrpc: '2.0',
method,
params,
});

Assertions.expectedError();
} catch (e: any) {
const res = e.response;
expect(res.status).to.equal(400);

Check warning on line 2220 in packages/server/tests/acceptance/rpc_batch3.spec.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/server/tests/acceptance/rpc_batch3.spec.ts#L2220

Method expect.to.equal has 52 lines of code (limit is 50)
Assertions.jsonRpcError(res.data.error, predefined.INVALID_REQUEST);
}
});
};

const TEST_SUITES = {
eth_getBalance: ['0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0x140d78a', null],
eth_getCode: ['0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0x140d78a', null],
eth_getBlockByHash: ['0x4cc9a77780cf0e6d0dc75373bf00e3437db450ede45cb51b5da936fb46342c99', false, null],
eth_getBlockByNumber: ['0x4cc9a7', false, null],
eth_getBlockTransactionCountByHash: ['0x4cc9a77780cf0e6d0dc75373bf00e3437db450ede45cb51b5da936fb46342c99', null],
eth_getBlockTransactionCountByNumber: ['0x4cc9a779', null],
eth_getTransactionByBlockHashAndIndex: [
'0x4cc9a77780cf0e6d0dc75373bf00e3437db450ede45cb51b5da936fb46342c99',
'0x1',
null,
],
eth_getTransactionByBlockNumberAndIndex: ['0x4cc9a77', '0x1', null],
eth_getTransactionCount: ['0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0x13455', null],
eth_sendRawTransaction: [
'0xf86a018203e882520894f17f52151ebef6c7334fad080c5704d77216b732896c6b935b8bbd400000801ba093129415f03b4794fd1512e79ee7f097e4271f66721020f8407aac92179893a5a0451b875d89721ec98be55201092980b0a87bb1c48507fccb86da713596b2a09e',
null,
],
eth_call: [
{
to: '0x6b175474e89094c44da98b954eedeac495271d0f',
data: '0x70a082310000000000000000000000006E0d01A76C3Cf4288372a29124A26D4353EE51BE',
},
'latest',
null,
],
eth_getTransactionByHash: ['0x4cc9a77780cf0e6d0dc75373bf00e3437db450ede45cb51b5da936fb46342c99', null],
eth_getTransactionReceipt: ['0x4cc9a77780cf0e6d0dc75373bf00e3437db450ede45cb51b5da936fb46342c99', null],
eth_getLogs: [
{
address: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
},
null,
],
eth_getBlockReceipts: ['0x5661236', null],
eth_newFilter: [
{
address: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
},
null,
],
eth_getFilterLogs: ['0xdf2a59ba81f4f052230c9992443cb801', null],
eth_getFilterChanges: ['0xdf2a59ba81f4f052230c9992443cb801', null],
eth_uninstallFilter: ['0xdf2a59ba81f4f052230c9992443cb801', null],
};

for (const [method, params] of Object.entries(TEST_SUITES)) {
generateTest(method, params);
}
});
});
4 changes: 2 additions & 2 deletions packages/ws-server/tests/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class WsTestHelper {
} catch (error: any) {
const errorToCheck = error.info || error;
expect(errorToCheck.error).to.exist;
expect(errorToCheck.error.code).to.be.oneOf([-32000, -32602, -32603]);
expect(errorToCheck.error.code).to.be.oneOf([-32000, -32600, -32602, -32603]);
}
}

Expand Down Expand Up @@ -55,7 +55,7 @@ export class WsTestHelper {
const response = await WsTestHelper.sendRequestToStandardWebSocket(method, params);
WsTestHelper.assertJsonRpcObject(response);
expect(response.error).to.exist;
expect(response.error.code).to.be.oneOf([-32000, -32602, -32603]);
expect(response.error.code).to.be.oneOf([-32000, -32600, -32602, -32603]);
}

static assertJsonRpcObject(obj: any) {
Expand Down
Loading