Skip to content
Merged
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
1 change: 1 addition & 0 deletions packages/http-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
],
"dependencies": {
"@rocket.chat/core-typings": "workspace:^",
"@rocket.chat/logger": "workspace:^",
"@rocket.chat/rest-typings": "workspace:^",
"ajv": "^8.17.1",
"express": "^4.21.2",
Expand Down
21 changes: 21 additions & 0 deletions packages/http-router/src/Router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Logger } from '@rocket.chat/logger';
import type { Method } from '@rocket.chat/rest-typings';
import type { AnySchema } from 'ajv';
import express from 'express';
Expand All @@ -9,6 +10,8 @@ import qs from 'qs'; // Using qs specifically to keep express compatibility
import type { ResponseSchema, TypedOptions } from './definition';
import { honoAdapterForExpress } from './middlewares/honoAdapterForExpress';

const logger = new Logger('HttpRouter');

type MiddlewareHandlerListAndActionHandler<TOptions extends TypedOptions, TContext = (c: Context) => Promise<ResponseSchema<TOptions>>> = [
...MiddlewareHandler[],
TContext,
Expand Down Expand Up @@ -199,6 +202,12 @@ export class Router<
if (options.query) {
const validatorFn = options.query;
if (typeof options.query === 'function' && !validatorFn(queryParams)) {
logger.warn({
msg: 'Query parameters validation failed - route spec does not match request payload',
method: req.method,
path: req.url,
error: validatorFn.errors?.map((error: any) => error.message).join('\n '),
});
return c.json(
{
success: false,
Expand All @@ -215,6 +224,12 @@ export class Router<
if (options.body) {
const validatorFn = options.body;
if (typeof options.body === 'function' && !validatorFn((req as any).bodyParams || bodyParams)) {
logger.warn({
msg: 'Request body validation failed - route spec does not match request payload',
method: req.method,
path: req.url,
error: validatorFn.errors?.map((error: any) => error.message).join('\n '),
});
return c.json(
{
success: false,
Expand All @@ -240,6 +255,12 @@ export class Router<
throw new Error(`Missing response validator for endpoint ${req.method} - ${req.url} with status code ${statusCode}`);
}
if (responseValidatorFn && !responseValidatorFn(coerceDatesToStrings(body))) {
logger.warn({
msg: 'Response validation failed - response does not match route spec',
method: req.method,
path: req.url,
error: responseValidatorFn.errors?.map((error: any) => error.message).join('\n '),
});
return c.json(
{
success: false,
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7876,6 +7876,7 @@ __metadata:
"@rocket.chat/core-typings": "workspace:^"
"@rocket.chat/eslint-config": "workspace:~"
"@rocket.chat/jest-presets": "workspace:^"
"@rocket.chat/logger": "workspace:^"
"@rocket.chat/rest-typings": "workspace:^"
"@rocket.chat/tsconfig": "workspace:*"
"@types/express": "npm:^4.17.23"
Expand Down
Loading