diff --git a/apps/meteor/app/api/server/api.js b/apps/meteor/app/api/server/api.js index 7259e14cca5b4..e92d061ae338e 100644 --- a/apps/meteor/app/api/server/api.js +++ b/apps/meteor/app/api/server/api.js @@ -397,8 +397,8 @@ 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 (_options.validateParams && !_options.validateParams(this.request.method === 'GET' ? this.queryParams : this.bodyParams)) { + 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/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..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'; @@ -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); 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);