Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion ee/packages/federation-matrix/src/api/_matrix/invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions ee/packages/federation-matrix/src/api/_matrix/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion ee/packages/federation-matrix/src/api/_matrix/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion ee/packages/federation-matrix/src/api/_matrix/send-join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 11 additions & 4 deletions packages/http-router/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -187,13 +188,20 @@ export class Router<
const [middlewares, action] = splitArray<MiddlewareHandler, TActionCallback>(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<Method>] as (
path: string,
...handlers: Array<MiddlewareHandler | ((c: Context) => Promise<ResponseSchema<TypedOptions>>)>
) => InnerRouter
)(path, ...middlewares, async (c: Context) => {
)(path, bodyParserMiddleware, ...middlewares, async (c: Context) => {
const { req, res } = c;

let queryParams: Record<string, any>;
Expand Down Expand Up @@ -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,
Expand Down
Loading