Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Chore add validation option to rest endpoints #25443

Merged
merged 1 commit into from
May 9, 2022
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
20 changes: 16 additions & 4 deletions apps/meteor/app/api/server/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
UrlParams,
} from '@rocket.chat/rest-typings';
import type { IUser, IMethodConnection } from '@rocket.chat/core-typings';
import type { ValidateFunction } from 'ajv';

import { ITwoFactorOptions } from '../../2fa/server/code';

Expand Down Expand Up @@ -54,7 +55,7 @@ export type NonEnterpriseTwoFactorOptions = {
twoFactorOptions: ITwoFactorOptions;
};

type Options =
type Options = (
| {
permissionsRequired?: string[];
authRequired?: boolean;
Expand All @@ -64,7 +65,10 @@ type Options =
authRequired: true;
twoFactorRequired: true;
twoFactorOptions?: ITwoFactorOptions;
};
}
) & {
validateParams?: ValidateFunction;
};

type Request = {
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
Expand All @@ -80,9 +84,17 @@ type PartialThis = {
type ActionThis<TMethod extends Method, TPathPattern extends PathPattern, TOptions> = {
urlParams: UrlParams<TPathPattern>;
// TODO make it unsafe
readonly queryParams: TMethod extends 'GET' ? Partial<OperationParams<TMethod, TPathPattern>> : Record<string, string>;
readonly queryParams: TMethod extends 'GET'
? TOptions extends { validateParams: ValidateFunction<infer T> }
? T
: Partial<OperationParams<TMethod, TPathPattern>>
: Record<string, string>;
// TODO make it unsafe
readonly bodyParams: TMethod extends 'GET' ? Record<string, unknown> : Partial<OperationParams<TMethod, TPathPattern>>;
readonly bodyParams: TMethod extends 'GET'
? Record<string, unknown>
: TOptions extends { validateParams: ValidateFunction<infer T> }
? T
: Partial<OperationParams<TMethod, TPathPattern>>;
readonly request: Request;
requestParams(): OperationParams<TMethod, TPathPattern>;
getLoggedInUser(): IUser | undefined;
Expand Down
3 changes: 3 additions & 0 deletions apps/meteor/app/api/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ export class APIClass extends Restivus {
try {
api.enforceRateLimit(objectForRateLimitMatch, this.request, this.response, this.userId);

if (_options.validateParams && _options.validateParams(this.request.method === 'GET' ? this.queryParams : this.bodyParams)) {
throw new Meteor.Error('error-invalid-params', _options.validateParams.errors?.map((error) => error.message).join('\n '));
}
if (shouldVerifyPermissions && (!this.userId || !hasAllPermission(this.userId, _options.permissionsRequired))) {
throw new Meteor.Error('error-unauthorized', 'User does not have the permissions required for this action', {
permissions: _options.permissionsRequired,
Expand Down