Skip to content

Commit

Permalink
chore: add validation for even calldata length
Browse files Browse the repository at this point in the history
Signed-off-by: nikolay <[email protected]>
  • Loading branch information
natanasow committed Oct 11, 2024
1 parent 1c65943 commit f5042f5
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/server/src/validator/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
export const BASE_HEX_REGEX = '^0[xX][a-fA-F0-9]';
export const ERROR_CODE = -32602;
export const DEFAULT_HEX_ERROR = 'Expected 0x prefixed hexadecimal value';
export const EVEN_HEX_ERROR = `${DEFAULT_HEX_ERROR} with even length`;
export const HASH_ERROR = 'Expected 0x prefixed string representing the hash (32 bytes)';
export const ADDRESS_ERROR = 'Expected 0x prefixed string representing the address (20 bytes)';
export const BLOCK_NUMBER_ERROR =
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/validator/objectTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const OBJECTS_VALIDATIONS: { [key: string]: IObjectSchema } = {
nullable: false,
},
data: {
type: 'hex',
type: 'hexEvenLength',
nullable: true,
},
type: {
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/validator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export const TYPES: { [key: string]: ITypeValidation } = {
test: (param: string) => new RegExp(Constants.BASE_HEX_REGEX + '*$').test(param),
error: Constants.DEFAULT_HEX_ERROR,
},
hexEvenLength: {
test: (param: string) => new RegExp(Constants.BASE_HEX_REGEX + '*$').test(param) && !(param.length % 2),
error: Constants.EVEN_HEX_ERROR,
},
hex64: {
test: (param: string) => new RegExp(Constants.BASE_HEX_REGEX + '{1,64}$').test(param),
error: Constants.HASH_ERROR,
Expand Down
4 changes: 2 additions & 2 deletions packages/server/tests/integration/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ describe('RPC Server', function () {
BaseTest.invalidParamError(
error.response,
Validator.ERROR_CODE,
`Invalid parameter 'data' for TransactionObject: ${Validator.DEFAULT_HEX_ERROR}, value: 123`,
`Invalid parameter 'data' for TransactionObject: ${Validator.EVEN_HEX_ERROR}, value: 123`,
);
}
});
Expand Down Expand Up @@ -1503,7 +1503,7 @@ describe('RPC Server', function () {
BaseTest.invalidParamError(
error.response,
Validator.ERROR_CODE,
`Invalid parameter 'data' for TransactionObject: ${Validator.DEFAULT_HEX_ERROR}, value: 123`,
`Invalid parameter 'data' for TransactionObject: ${Validator.EVEN_HEX_ERROR}, value: 123`,
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/server/tests/integration/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ describe('Validator', async () => {
expectInvalidObject('value', Validator.DEFAULT_HEX_ERROR, object, '123456'),
);
expect(() => Validator.validateParams([{ data: '123456' }], validation)).to.throw(
expectInvalidObject('data', Validator.DEFAULT_HEX_ERROR, object, '123456'),
expectInvalidObject('data', Validator.EVEN_HEX_ERROR, object, '123456'),
);
});
});
Expand Down

0 comments on commit f5042f5

Please sign in to comment.