From 23e95a2fc905dd51081954cd2f9574b260f92d6c Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Mon, 9 May 2022 16:52:28 -0300 Subject: [PATCH 1/6] Improve query and body params validation --- apps/meteor/app/api/server/v1/channels.ts | 142 ++++++++++------------ apps/meteor/app/api/server/v1/teams.ts | 72 +++++------ 2 files changed, 96 insertions(+), 118 deletions(-) diff --git a/apps/meteor/app/api/server/v1/channels.ts b/apps/meteor/app/api/server/v1/channels.ts index 8d4c21ef3512a..e1ab4da8e8d93 100644 --- a/apps/meteor/app/api/server/v1/channels.ts +++ b/apps/meteor/app/api/server/v1/channels.ts @@ -62,13 +62,12 @@ function findChannelByIdOrName({ API.v1.addRoute( 'channels.addAll', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsAddAllProps, + }, { post() { - if (!isChannelsAddAllProps(this.bodyParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsAddAllProps.errors?.map((error: any) => error.message).join('\n ')); - } - const { activeUsersOnly, ...params } = this.bodyParams; const findResult = findChannelByIdOrName({ params, userId: this.userId }); @@ -83,13 +82,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.archive', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsArchiveProps, + }, { post() { - if (!isChannelsArchiveProps(this.bodyParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsArchiveProps.errors?.map((error: any) => error.message).join('\n ')); - } - const findResult = findChannelByIdOrName({ params: this.bodyParams }); Meteor.call('archiveRoom', findResult._id); @@ -101,13 +99,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.unarchive', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsUnarchiveProps, + }, { post() { - if (!isChannelsUnarchiveProps(this.bodyParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsUnarchiveProps.errors?.map((error: any) => error.message).join('\n ')); - } - const findResult = findChannelByIdOrName({ params: this.bodyParams, checkedArchived: false, @@ -126,12 +123,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.history', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsHistoryProps, + }, { get() { - if (!isChannelsHistoryProps(this.queryParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsHistoryProps.errors?.map((error: any) => error.message).join('\n ')); - } const { roomId, unreads, oldest, latest, showThreadMessages, inclusive } = this.queryParams; const findResult = findChannelByIdOrName({ params: { roomId }, @@ -162,12 +159,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.roles', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsRolesProps, + }, { get() { - if (!isChannelsRolesProps(this.queryParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsRolesProps.errors?.map((error: any) => error.message).join('\n ')); - } const findResult = findChannelByIdOrName({ params: this.queryParams }); const roles = Meteor.call('getRoomRoles', findResult._id); @@ -181,13 +178,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.join', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsJoinProps, + }, { post() { - if (!isChannelsJoinProps(this.bodyParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsJoinProps.errors?.map((error: any) => error.message).join('\n ')); - } - const { roomId, joinCode } = this.bodyParams; const findResult = findChannelByIdOrName({ params: { roomId } }); @@ -202,12 +198,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.kick', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsKickProps, + }, { post() { - if (!isChannelsKickProps(this.bodyParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsKickProps.errors?.map((error: any) => error.message).join('\n ')); - } const { roomId /* userId */ } = this.bodyParams; const findResult = findChannelByIdOrName({ params: { roomId } }); @@ -224,13 +220,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.leave', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsLeaveProps, + }, { post() { - if (!isChannelsLeaveProps(this.bodyParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsLeaveProps.errors?.map((error: any) => error.message).join('\n ')); - } - const { roomId } = this.bodyParams; const findResult = findChannelByIdOrName({ params: { roomId } }); @@ -247,12 +242,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.messages', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsMessagesProps, + }, { get() { - if (!isChannelsMessagesProps(this.queryParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsMessagesProps.errors?.map((error: any) => error.message).join('\n ')); - } const { roomId } = this.queryParams; const findResult = findChannelByIdOrName({ params: { roomId }, @@ -296,13 +291,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.open', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsOpenProps, + }, { post() { - if (!isChannelsOpenProps(this.bodyParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsOpenProps.errors?.map((error: any) => error.message).join('\n ')); - } - const { roomId } = this.bodyParams; const findResult = findChannelByIdOrName({ @@ -329,13 +323,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.setReadOnly', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsSetReadOnlyProps, + }, { post() { - if (!isChannelsSetReadOnlyProps(this.bodyParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsSetReadOnlyProps.errors?.map((error: any) => error.message).join('\n ')); - } - const { roomId } = this.bodyParams; const findResult = findChannelByIdOrName({ params: { roomId } }); @@ -355,15 +348,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.setAnnouncement', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsSetAnnouncementProps, + }, { post() { - if (!isChannelsSetAnnouncementProps(this.bodyParams)) { - throw new Meteor.Error( - 'error-invalid-params', - isChannelsSetAnnouncementProps.errors?.map((error: any) => error.message).join('\n '), - ); - } const { roomId, announcement } = this.bodyParams; const findResult = findChannelByIdOrName({ params: { roomId } }); @@ -379,15 +369,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.getAllUserMentionsByChannel', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsGetAllUserMentionsByChannelProps, + }, { get() { - if (!isChannelsGetAllUserMentionsByChannelProps(this.queryParams)) { - throw new Meteor.Error( - 'error-invalid-params', - isChannelsGetAllUserMentionsByChannelProps.errors?.map((error: any) => error.message).join('\n '), - ); - } const { roomId } = this.queryParams; const { offset, count } = this.getPaginationItems(); const { sort } = this.parseJsonQuery(); @@ -422,12 +409,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.moderators', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsModeratorsProps, + }, { get() { - if (!isChannelsModeratorsProps(this.queryParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsModeratorsProps.errors?.map((error: any) => error.message).join('\n ')); - } const { roomId } = this.queryParams; const findResult = findChannelByIdOrName({ params: { roomId } }); @@ -447,12 +434,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.delete', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsDeleteProps, + }, { post() { - if (!isChannelsDeleteProps(this.bodyParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsDeleteProps.errors?.map((error: any) => error.message).join('\n ')); - } const room = findChannelByIdOrName({ params: this.bodyParams, checkedArchived: false, @@ -467,13 +454,12 @@ API.v1.addRoute( API.v1.addRoute( 'channels.convertToTeam', - { authRequired: true }, + { + authRequired: true, + validateParams: isChannelsConvertToTeamProps, + }, { async post() { - if (!isChannelsConvertToTeamProps(this.bodyParams)) { - throw new Meteor.Error('error-invalid-params', isChannelsConvertToTeamProps.errors?.map((error: any) => error.message).join('\n ')); - } - if (!hasAllPermission(this.userId, ['create-team', 'edit-room'])) { return API.v1.unauthorized(); } diff --git a/apps/meteor/app/api/server/v1/teams.ts b/apps/meteor/app/api/server/v1/teams.ts index 112cbe2f263d5..a3da449d0b717 100644 --- a/apps/meteor/app/api/server/v1/teams.ts +++ b/apps/meteor/app/api/server/v1/teams.ts @@ -115,13 +115,12 @@ const getTeamByIdOrName = async (params: { teamId: string } | { teamName: string API.v1.addRoute( 'teams.convertToChannel', - { authRequired: true }, + { + authRequired: true, + validateParams: isTeamsConvertToChannelProps, + }, { async post() { - if (!isTeamsConvertToChannelProps(this.bodyParams)) { - return API.v1.failure('invalid-body-params', isTeamsConvertToChannelProps.errors?.map((e) => e.message).join('\n ')); - } - const { roomsToRemove = [] } = this.bodyParams; const team = await getTeamByIdOrName(this.bodyParams); @@ -197,13 +196,12 @@ API.v1.addRoute( API.v1.addRoute( 'teams.removeRoom', - { authRequired: true }, + { + authRequired: true, + validateParams: isTeamsRemoveRoomProps, + }, { async post() { - if (!isTeamsRemoveRoomProps(this.bodyParams)) { - return API.v1.failure('body-params-invalid', isTeamsRemoveRoomProps.errors?.map((error) => error.message).join('\n ')); - } - const team = await getTeamByIdOrName(this.bodyParams); if (!team) { return API.v1.failure('team-does-not-exist'); @@ -431,13 +429,12 @@ API.v1.addRoute( API.v1.addRoute( 'teams.addMembers', - { authRequired: true }, + { + authRequired: true, + validateParams: isTeamsAddMembersProps, + }, { async post() { - if (!isTeamsAddMembersProps(this.bodyParams)) { - return API.v1.failure('invalid-params'); - } - const { bodyParams } = this; const { members } = bodyParams; @@ -459,13 +456,12 @@ API.v1.addRoute( API.v1.addRoute( 'teams.updateMember', - { authRequired: true }, + { + authRequired: true, + validateParams: isTeamsUpdateMemberProps, + }, { async post() { - if (!isTeamsUpdateMemberProps(this.bodyParams)) { - return API.v1.failure('invalid-params', isTeamsUpdateMemberProps.errors?.map((e) => e.message).join('\n ')); - } - const { bodyParams } = this; const { member } = bodyParams; @@ -487,13 +483,12 @@ API.v1.addRoute( API.v1.addRoute( 'teams.removeMember', - { authRequired: true }, + { + authRequired: true, + validateParams: isTeamsRemoveMemberProps, + }, { async post() { - if (!isTeamsRemoveMemberProps(this.bodyParams)) { - return API.v1.failure('invalid-params', isTeamsRemoveMemberProps.errors?.map((e) => e.message).join('\n ')); - } - const { bodyParams } = this; const { userId, rooms } = bodyParams; @@ -533,13 +528,12 @@ API.v1.addRoute( API.v1.addRoute( 'teams.leave', - { authRequired: true }, + { + authRequired: true, + validateParams: isTeamsLeaveProps, + }, { async post() { - if (!isTeamsLeaveProps(this.bodyParams)) { - return API.v1.failure('invalid-params', isTeamsLeaveProps.errors?.map((e) => e.message).join('\n ')); - } - const { rooms = [] } = this.bodyParams; const team = await getTeamByIdOrName(this.bodyParams); @@ -592,15 +586,14 @@ API.v1.addRoute( API.v1.addRoute( 'teams.delete', - { authRequired: true }, + { + authRequired: true, + validateParams: isTeamsDeleteProps, + }, { async post() { const { roomsToRemove = [] } = this.bodyParams; - if (!isTeamsDeleteProps(this.bodyParams)) { - return API.v1.failure('invalid-params', isTeamsDeleteProps.errors?.map((e) => e.message).join('\n ')); - } - const team = await getTeamByIdOrName(this.bodyParams); if (!team) { return API.v1.failure('team-does-not-exist'); @@ -659,13 +652,12 @@ API.v1.addRoute( API.v1.addRoute( 'teams.update', - { authRequired: true }, + { + authRequired: true, + validateParams: isTeamsUpdateProps, + }, { async post() { - if (!isTeamsUpdateProps(this.bodyParams)) { - return API.v1.failure('invalid-params', isTeamsUpdateProps.errors?.map((e) => e.message).join('\n ')); - } - const { data } = this.bodyParams; const team = await getTeamByIdOrName(this.bodyParams); From 09b34dcf9839867a7b53db5186a31b70be4085a1 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Mon, 9 May 2022 17:46:13 -0300 Subject: [PATCH 2/6] fix --- apps/meteor/app/api/server/api.js | 2 +- apps/meteor/app/api/server/v1/teams.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/meteor/app/api/server/api.js b/apps/meteor/app/api/server/api.js index 7259e14cca5b4..23ee45ff105af 100644 --- a/apps/meteor/app/api/server/api.js +++ b/apps/meteor/app/api/server/api.js @@ -397,7 +397,7 @@ 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)) { + 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))) { diff --git a/apps/meteor/app/api/server/v1/teams.ts b/apps/meteor/app/api/server/v1/teams.ts index a3da449d0b717..157f1ac6d8943 100644 --- a/apps/meteor/app/api/server/v1/teams.ts +++ b/apps/meteor/app/api/server/v1/teams.ts @@ -1,4 +1,4 @@ -import { FilterQuery } from 'mongodb'; +import type { FilterQuery } from 'mongodb'; import { Meteor } from 'meteor/meteor'; import { Match, check } from 'meteor/check'; import { escapeRegExp } from '@rocket.chat/string-helpers'; From d8144e7228139df3f96f8535c7d24b4219099d72 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Tue, 10 May 2022 14:45:04 -0300 Subject: [PATCH 3/6] fix --- apps/meteor/app/api/server/api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/app/api/server/api.js b/apps/meteor/app/api/server/api.js index 23ee45ff105af..a6ce49e22cb5b 100644 --- a/apps/meteor/app/api/server/api.js +++ b/apps/meteor/app/api/server/api.js @@ -398,7 +398,7 @@ export class APIClass extends Restivus { 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 ')); + return API.v1.failure('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', { From e9385047eb34062a11fe9b89fcf81a91667bc329 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Tue, 10 May 2022 15:28:01 -0300 Subject: [PATCH 4/6] .. --- apps/meteor/app/api/server/api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/app/api/server/api.js b/apps/meteor/app/api/server/api.js index a6ce49e22cb5b..b7493eb23602c 100644 --- a/apps/meteor/app/api/server/api.js +++ b/apps/meteor/app/api/server/api.js @@ -398,7 +398,7 @@ export class APIClass extends Restivus { api.enforceRateLimit(objectForRateLimitMatch, this.request, this.response, this.userId); if (_options.validateParams && !_options.validateParams(this.request.method === 'GET' ? this.queryParams : this.bodyParams)) { - return API.v1.failure('error-invalid-params', _options.validateParams.errors?.map((error) => error.message).join('\n ')); + return API.v1.failure('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', { From f9f4a0ab08b2d3c3def55a12502232d074c3cf02 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Tue, 10 May 2022 17:06:03 -0300 Subject: [PATCH 5/6] throw new Meteor.Error --- apps/meteor/app/api/server/api.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/meteor/app/api/server/api.js b/apps/meteor/app/api/server/api.js index b7493eb23602c..ea76ccba786ef 100644 --- a/apps/meteor/app/api/server/api.js +++ b/apps/meteor/app/api/server/api.js @@ -398,7 +398,11 @@ export class APIClass extends Restivus { api.enforceRateLimit(objectForRateLimitMatch, this.request, this.response, this.userId); if (_options.validateParams && !_options.validateParams(this.request.method === 'GET' ? this.queryParams : this.bodyParams)) { - return API.v1.failure('invalid-params', _options.validateParams.errors?.map((error) => error.message).join('\n ')); + throw new Meteor.Error( + 'invalid-params', + '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', { From 98bccd2c8c1172374ab2b99df0c83646ed145877 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Tue, 10 May 2022 21:34:15 -0300 Subject: [PATCH 6/6] .. --- apps/meteor/app/api/server/api.js | 6 +----- apps/meteor/tests/end-to-end/api/25-teams.js | 4 ++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/apps/meteor/app/api/server/api.js b/apps/meteor/app/api/server/api.js index ea76ccba786ef..e92d061ae338e 100644 --- a/apps/meteor/app/api/server/api.js +++ b/apps/meteor/app/api/server/api.js @@ -398,11 +398,7 @@ export class APIClass extends Restivus { 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( - 'invalid-params', - 'invalid-params', - _options.validateParams.errors?.map((error) => error.message).join('\n '), - ); + throw new Meteor.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', { diff --git a/apps/meteor/tests/end-to-end/api/25-teams.js b/apps/meteor/tests/end-to-end/api/25-teams.js index 83129073c1851..e549e761c6c8d 100644 --- a/apps/meteor/tests/end-to-end/api/25-teams.js +++ b/apps/meteor/tests/end-to-end/api/25-teams.js @@ -633,7 +633,7 @@ describe('[Teams]', () => { .expect((res) => { expect(res.body).to.have.property('success', false); expect(res.body).to.have.property('error'); - expect(res.body.error).to.be.equal('invalid-params'); + expect(res.body.errorType).to.be.equal('invalid-params'); }) .then(() => done()) .catch(done); @@ -773,7 +773,7 @@ describe('[Teams]', () => { .expect((res) => { expect(res.body).to.have.property('success', false); expect(res.body).to.have.property('error'); - expect(res.body.error).to.be.equal('invalid-params'); + expect(res.body.errorType).to.be.equal('invalid-params'); }) .then(() => done()) .catch(done);