From 3843a5a53507688de57762408004f17c2e2d1d3b Mon Sep 17 00:00:00 2001 From: Leonid Date: Thu, 15 Sep 2022 17:59:54 +0300 Subject: [PATCH] Add merkle root index validation --- zp-relayer/endpoints.ts | 8 ++++++++ zp-relayer/validation/validation.ts | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/zp-relayer/endpoints.ts b/zp-relayer/endpoints.ts index 6236027..93783cb 100644 --- a/zp-relayer/endpoints.ts +++ b/zp-relayer/endpoints.ts @@ -9,6 +9,7 @@ import { checkGetLimits, checkGetTransactions, checkGetTransactionsV2, + checkMerkleRootErrors, checkSendTransactionErrors, checkSendTransactionsErrors, } from './validation/validation' @@ -73,6 +74,13 @@ async function sendTransaction(req: Request, res: Response, next: NextFunction) } async function merkleRoot(req: Request, res: Response, next: NextFunction) { + const errors = checkMerkleRootErrors(req.params) + if (errors) { + logger.info('Request errors: %o', errors) + res.status(400).json({ errors }) + return + } + const index = req.params.index const root = await pool.getContractMerkleRoot(index) res.json(root) diff --git a/zp-relayer/validation/validation.ts b/zp-relayer/validation/validation.ts index 251fdd9..bbae2aa 100644 --- a/zp-relayer/validation/validation.ts +++ b/zp-relayer/validation/validation.ts @@ -134,6 +134,18 @@ const AjvGetLimitsSchema: JSONSchemaType<{ required: [], } +const AjvMerkleRootSchema: JSONSchemaType<{ + index: string | number +}> = { + type: 'object', + properties: { + index: { + type: 'integer', + }, + }, + required: ['index'], +} + function checkErrors(schema: JSONSchemaType) { const validate = ajv.compile(schema) return (data: any) => { @@ -147,6 +159,7 @@ function checkErrors(schema: JSONSchemaType) { } } +export const checkMerkleRootErrors = checkErrors(AjvMerkleRootSchema) export const checkSendTransactionErrors = checkErrors(AjvSendTransactionSchema) export const checkSendTransactionsErrors = checkErrors(AjvSendTransactionsSchema) export const checkGetTransactions = checkErrors(AjvGetTransactionsSchema)