From 65952c72f90d8e6887490b15dc8b3e4abea71ec8 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Fri, 27 Feb 2026 17:41:11 -0300 Subject: [PATCH 1/2] parse request body before route action --- packages/http-router/src/Router.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/http-router/src/Router.ts b/packages/http-router/src/Router.ts index f6fd314dce599..189b62b261cfe 100644 --- a/packages/http-router/src/Router.ts +++ b/packages/http-router/src/Router.ts @@ -4,6 +4,7 @@ import type { AnySchema } from 'ajv'; import express from 'express'; import type { Context, HonoRequest, MiddlewareHandler } from 'hono'; import { Hono } from 'hono'; +import { createMiddleware } from 'hono/factory'; import type { StatusCode } from 'hono/utils/http-status'; import type { ResponseSchema, TypedOptions } from './definition'; @@ -187,13 +188,20 @@ export class Router< const [middlewares, action] = splitArray(actions); const convertedAction = this.convertActionToHandler(action); + const bodyParserMiddleware = createMiddleware(async (c: Context, next) => { + const bodyParams = await this.parseBodyParams({ request: c.req }); + c.set('bodyParams', bodyParams); + + return next(); + }); + const path = `/${subpath}`.replace('//', '/'); ( this.innerRouter[method.toLowerCase() as Lowercase] as ( path: string, ...handlers: Array Promise>)> ) => InnerRouter - )(path, ...middlewares, async (c: Context) => { + )(path, bodyParserMiddleware, ...middlewares, async (c: Context) => { const { req, res } = c; let queryParams: Record; @@ -228,12 +236,11 @@ export class Router< } } - const bodyParams = await this.parseBodyParams({ request: req }); - c.set('bodyParams', bodyParams); + const bodyParams = c.get('bodyParams') || {}; if (options.body) { const validatorFn = options.body; - if (typeof options.body === 'function' && !validatorFn((req as any).bodyParams || bodyParams)) { + if (typeof options.body === 'function' && !validatorFn(bodyParams)) { logger.warn({ msg: 'Request body validation failed - route spec does not match request payload', method: req.method, From c4d29f5904154481350a631fe936d782176e4518 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Fri, 27 Feb 2026 17:47:10 -0300 Subject: [PATCH 2/2] use parsed bodyParam from req --- ee/packages/federation-matrix/src/api/_matrix/invite.ts | 2 +- ee/packages/federation-matrix/src/api/_matrix/profiles.ts | 4 ++-- ee/packages/federation-matrix/src/api/_matrix/rooms.ts | 2 +- ee/packages/federation-matrix/src/api/_matrix/send-join.ts | 2 +- ee/packages/federation-matrix/src/api/_matrix/send-leave.ts | 2 +- ee/packages/federation-matrix/src/api/_matrix/transactions.ts | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ee/packages/federation-matrix/src/api/_matrix/invite.ts b/ee/packages/federation-matrix/src/api/_matrix/invite.ts index d27dbfc0cf57a..f88855d10f9be 100644 --- a/ee/packages/federation-matrix/src/api/_matrix/invite.ts +++ b/ee/packages/federation-matrix/src/api/_matrix/invite.ts @@ -146,7 +146,7 @@ export const getMatrixInviteRoutes = () => { isAuthenticatedMiddleware(), async (c) => { const { roomId, eventId } = c.req.param(); - const { event, room_version: roomVersion, invite_room_state: strippedStateEvents } = await c.req.json(); + const { event, room_version: roomVersion, invite_room_state: strippedStateEvents } = c.get('bodyParams'); const userToCheck = event.state_key as string; diff --git a/ee/packages/federation-matrix/src/api/_matrix/profiles.ts b/ee/packages/federation-matrix/src/api/_matrix/profiles.ts index e3b5560a1918d..0981b23091e31 100644 --- a/ee/packages/federation-matrix/src/api/_matrix/profiles.ts +++ b/ee/packages/federation-matrix/src/api/_matrix/profiles.ts @@ -396,7 +396,7 @@ export const getMatrixProfilesRoutes = () => { license: ['federation'], }, async (c) => { - const body = await c.req.json(); + const body = c.get('bodyParams'); const response = await federationSDK.queryKeys(body.device_keys); @@ -473,7 +473,7 @@ export const getMatrixProfilesRoutes = () => { canAccessResourceMiddleware('room'), async (c) => { const { roomId } = c.req.param(); - const body = await c.req.json(); + const body = c.get('bodyParams'); const response = await federationSDK.getMissingEvents( roomIdSchema.parse(roomId), diff --git a/ee/packages/federation-matrix/src/api/_matrix/rooms.ts b/ee/packages/federation-matrix/src/api/_matrix/rooms.ts index e8fedb038931f..4026caa12c29f 100644 --- a/ee/packages/federation-matrix/src/api/_matrix/rooms.ts +++ b/ee/packages/federation-matrix/src/api/_matrix/rooms.ts @@ -168,7 +168,7 @@ export const getMatrixRoomsRoutes = () => { license: ['federation'], }, async (c) => { - const body = await c.req.json(); + const body = c.get('bodyParams'); const defaultObj = { join_rule: 'public', diff --git a/ee/packages/federation-matrix/src/api/_matrix/send-join.ts b/ee/packages/federation-matrix/src/api/_matrix/send-join.ts index 4e65194498cf7..2971f67a06eb9 100644 --- a/ee/packages/federation-matrix/src/api/_matrix/send-join.ts +++ b/ee/packages/federation-matrix/src/api/_matrix/send-join.ts @@ -239,7 +239,7 @@ export const getMatrixSendJoinRoutes = () => { canAccessResourceMiddleware('room'), async (c) => { const { roomId, stateKey } = c.req.param(); - const body = await c.req.json(); + const body = c.get('bodyParams'); const response = await federationSDK.sendJoin(roomId, stateKey as EventID, body); diff --git a/ee/packages/federation-matrix/src/api/_matrix/send-leave.ts b/ee/packages/federation-matrix/src/api/_matrix/send-leave.ts index 7d12b743ed139..76716db08a7b4 100644 --- a/ee/packages/federation-matrix/src/api/_matrix/send-leave.ts +++ b/ee/packages/federation-matrix/src/api/_matrix/send-leave.ts @@ -76,7 +76,7 @@ export const getMatrixSendLeaveRoutes = () => { isAuthenticatedMiddleware(), async (c) => { const { roomId, eventId } = c.req.param(); - const body = await c.req.json(); + const body = c.get('bodyParams'); try { await federationSDK.sendLeave(roomId, eventId, body); return { diff --git a/ee/packages/federation-matrix/src/api/_matrix/transactions.ts b/ee/packages/federation-matrix/src/api/_matrix/transactions.ts index c101ffa10270f..61af3f5343882 100644 --- a/ee/packages/federation-matrix/src/api/_matrix/transactions.ts +++ b/ee/packages/federation-matrix/src/api/_matrix/transactions.ts @@ -333,7 +333,7 @@ export const getMatrixTransactionsRoutes = () => { license: ['federation'], }, async (c) => { - const body = await c.req.json(); + const body = c.get('bodyParams'); try { await federationSDK.processIncomingTransaction(body);