diff --git a/packages/ws-server/src/controllers/index.ts b/packages/ws-server/src/controllers/index.ts index a563f39c24..a2d2a7ae26 100644 --- a/packages/ws-server/src/controllers/index.ts +++ b/packages/ws-server/src/controllers/index.ts @@ -18,7 +18,6 @@ * */ -import { resolveParams } from '../utils/utils'; import { WS_CONSTANTS } from '../utils/constants'; import WsMetricRegistry from '../metrics/wsMetricRegistry'; import ConnectionLimiter from '../metrics/connectionLimiter'; @@ -27,6 +26,8 @@ import { Validator } from '@hashgraph/json-rpc-server/dist/validator'; import { handleEthSubsribe, handleEthUnsubscribe } from './eth_subscribe'; import { MirrorNodeClient } from '@hashgraph/json-rpc-relay/dist/lib/clients'; import jsonResp from '@hashgraph/json-rpc-server/dist/koaJsonRpc/lib/RpcResponse'; +import { resolveParams, validateJsonRpcRequest, verifySupportedMethod } from '../utils/utils'; +import { InvalidRequest, MethodNotFound } from '@hashgraph/json-rpc-server/dist/koaJsonRpc/lib/RpcError'; /** * Handles sending requests to a Relay by calling a specified method with given parameters. @@ -102,6 +103,17 @@ export const getRequestResult = async ( wsMetricRegistry.getCounter('methodsCounter').labels(method).inc(); wsMetricRegistry.getCounter('methodsCounterByIp').labels(ctx.request.ip, method).inc(); + // validate request's jsonrpc object + if (!validateJsonRpcRequest(request, logger, requestIdPrefix, connectionIdPrefix)) { + return jsonResp(request.id || null, new InvalidRequest(), undefined); + } + + // verify supported method + if (!verifySupportedMethod(request.method)) { + logger.warn(`${connectionIdPrefix} ${requestIdPrefix}: Method not supported: ${request.method}`); + return jsonResp(request.id || null, new MethodNotFound(request.method), undefined); + } + // Validate request's params try { const methodValidations = Validator.METHODS[method]; @@ -110,7 +122,7 @@ export const getRequestResult = async ( Validator.validateParams(params, methodValidations); } } catch (error) { - logger.error( + logger.warn( error, `${connectionIdPrefix} ${requestIdPrefix} Error in parameter validation. Method: ${method}, params: ${JSON.stringify( params, @@ -152,7 +164,7 @@ export const getRequestResult = async ( response = await handleSendingRequestsToRelay({ ...sharedParams }); } } catch (error) { - logger.error( + logger.warn( error, `${connectionIdPrefix} ${requestIdPrefix} Encountered error on connectionID: ${ ctx.websocket.id diff --git a/packages/ws-server/src/utils/utils.ts b/packages/ws-server/src/utils/utils.ts index 50c3c39b89..d40d132440 100644 --- a/packages/ws-server/src/utils/utils.ts +++ b/packages/ws-server/src/utils/utils.ts @@ -112,7 +112,7 @@ export const validateJsonRpcRequest = ( !hasOwnProperty(request, 'id') ) { logger.warn( - `${connectionIdPrefix} ${requestIdPrefix} Invalid request, body.jsonrpc: ${request.jsonrpc}, body[method]: ${request.method}, body[id]: ${request.id}, ctx.request.method: ${request.method}`, + `${connectionIdPrefix} ${requestIdPrefix} Invalid request, request.jsonrpc: ${request.jsonrpc}, request.method: ${request.method}, request.id: ${request.id}, request.method: ${request.method}`, ); return false; } else { @@ -148,7 +148,7 @@ export const getMultipleAddressesEnabled = (): boolean => { * @returns {boolean} A boolean indicating whether WebSocket batch requests are enabled. */ export const getWsBatchRequestsEnabled = (): boolean => { - return process.env.WS_BATCH_REQUESTS_ENABLED === 'true' || true; + return process.env.WS_BATCH_REQUESTS_ENABLED ? process.env.WS_BATCH_REQUESTS_ENABLED === 'true' : true; }; /** diff --git a/packages/ws-server/src/webSocketServer.ts b/packages/ws-server/src/webSocketServer.ts index d590482d19..ed70f7c48c 100644 --- a/packages/ws-server/src/webSocketServer.ts +++ b/packages/ws-server/src/webSocketServer.ts @@ -34,15 +34,7 @@ dotenv.config({ path: path.resolve(__dirname, '../../../.env') }); import KoaJsonRpc from '@hashgraph/json-rpc-server/dist/koaJsonRpc'; import jsonResp from '@hashgraph/json-rpc-server/dist/koaJsonRpc/lib/RpcResponse'; import { type Relay, RelayImpl, predefined, JsonRpcError } from '@hashgraph/json-rpc-relay'; -import { InvalidRequest, MethodNotFound } from '@hashgraph/json-rpc-server/dist/koaJsonRpc/lib/RpcError'; -import { - sendToClient, - handleConnectionClose, - verifySupportedMethod, - validateJsonRpcRequest, - getBatchRequestsMaxSize, - getWsBatchRequestsEnabled, -} from './utils/utils'; +import { sendToClient, handleConnectionClose, getBatchRequestsMaxSize, getWsBatchRequestsEnabled } from './utils/utils'; const mainLogger = pino({ name: 'hedera-json-rpc-relay', @@ -120,18 +112,6 @@ app.ws.use(async (ctx) => { return; } - // validate request's jsonrpc object - if (!validateJsonRpcRequest(request, logger, requestIdPrefix, connectionIdPrefix)) { - ctx.websocket.send(JSON.stringify(jsonResp(request.id || null, new InvalidRequest(), undefined))); - return; - } - - // verify supported method - if (!verifySupportedMethod(request.method)) { - ctx.websocket.send(JSON.stringify(jsonResp(request.id || null, new MethodNotFound(request.method), undefined))); - logger.warn(`${connectionIdPrefix} ${requestIdPrefix}: Method not found: ${request.method}`); - return; - } // check if request is a batch request (array) or a signle request (JSON) if (Array.isArray(request)) { logger.trace(`${connectionIdPrefix} ${requestIdPrefix}: Receive batch request=${JSON.stringify(request)}`);