A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(MessagePinService): do not assert message history access (#3536)
Jiralite3cb4907766da
diff --git a/fluxer_api/src/api/channel/services/interaction/MessagePinService.ts b/fluxer_api/src/api/channel/services/interaction/MessagePinService.tsindex 086860308..3944293da 100644--- a/fluxer_api/src/api/channel/services/interaction/MessagePinService.ts+++ b/fluxer_api/src/api/channel/services/interaction/MessagePinService.ts@@ -41,25 +41,6 @@ export class MessagePinService {private readonly guildAuditLogService: GuildAuditLogService,) {}- private async assertMessageHistoryAccess({- authChannel,- messageId,- }: {- authChannel: AuthenticatedChannel;- messageId: MessageID;- }): Promise<void> {- if (!authChannel.guild) {- return;- }- if (await authChannel.hasPermission(Permissions.READ_MESSAGE_HISTORY)) {- return;- }- const cutoff = authChannel.guild.message_history_cutoff;- if (!cutoff || snowflakeToDate(messageId).getTime() < new Date(cutoff).getTime()) {- throw new UnknownMessageError();- }- }-async getChannelPins({authChannel,userId,@@ -161,7 +142,6 @@ export class MessagePinService {}ensureTextChannel(channel);assertThreadInteractionAllowed(authChannel, 'pin');- await this.assertMessageHistoryAccess({authChannel, messageId});const message = await this.channelRepository.messages.getMessage(channel.id, messageId);if (!message) throw new UnknownMessageError();this.validateMessagePinnable(message);@@ -214,7 +194,6 @@ export class MessagePinService {}ensureTextChannel(channel);assertThreadInteractionAllowed(authChannel, 'pin');- await this.assertMessageHistoryAccess({authChannel, messageId});const message = await this.channelRepository.messages.getMessage(channel.id, messageId);if (!message) throw new UnknownMessageError();this.validateMessagePinnable(message);diff --git a/fluxer_api/src/api/channel/tests/MessagePinPermissions.test.ts b/fluxer_api/src/api/channel/tests/MessagePinPermissions.test.tsnew file mode 100644index 000000000..c4b478d96--- /dev/null+++ b/fluxer_api/src/api/channel/tests/MessagePinPermissions.test.ts@@ -0,0 +1,72 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {+ createPermissionOverwrite,+ pinMessage,+ sendChannelMessage,+ setupTestGuildWithMembers,+} from '@app/api/channel/tests/ChannelTestUtils';+import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';+import {HTTP_STATUS} from '@app/api/test/TestConstants';+import {createBuilder} from '@app/api/test/TestRequestBuilder';+import {Permissions} from '@fluxer/constants/src/ChannelConstants';+import type {ChannelPinsResponse} from '@fluxer/schema/src/domains/message/MessageResponseSchemas';+import {afterAll, beforeAll, beforeEach, describe, expect, it} from 'vitest';++describe('Message pin permissions', () => {+ let harness: ApiTestHarness;++ beforeAll(async () => {+ harness = await createApiTestHarness();+ });++ beforeEach(async () => {+ await harness.reset();+ });++ afterAll(async () => {+ await harness?.shutdown();+ });++ async function listPinnedMessageIds(token: string, channelId: string): Promise<Array<string>> {+ const pins = await createBuilder<ChannelPinsResponse>(harness, token)+ .get(`/channels/${channelId}/messages/pins`)+ .execute();+ return pins.items.map((item) => item.message.id);+ }++ it('allows pinning and unpinning an own message without Read Message History', async () => {+ const {owner, members, systemChannel} = await setupTestGuildWithMembers(harness, 1);+ const member = members[0]!;+ await createPermissionOverwrite(harness, owner.token, systemChannel.id, member.userId, {+ type: 1,+ allow: (Permissions.VIEW_CHANNEL | Permissions.SEND_MESSAGES | Permissions.PIN_MESSAGES).toString(),+ deny: Permissions.READ_MESSAGE_HISTORY.toString(),+ });+ const message = await sendChannelMessage(harness, member.token, systemChannel.id, 'pin me');++ await pinMessage(harness, member.token, systemChannel.id, message.id);+ expect(await listPinnedMessageIds(owner.token, systemChannel.id)).toEqual([message.id]);++ await createBuilder(harness, member.token)+ .delete(`/channels/${systemChannel.id}/pins/${message.id}`)+ .expect(HTTP_STATUS.NO_CONTENT)+ .execute();+ expect(await listPinnedMessageIds(owner.token, systemChannel.id)).toEqual([]);+ });++ it('allows pinning source history without Read Message History', async () => {+ const {owner, members, systemChannel} = await setupTestGuildWithMembers(harness, 1);+ const member = members[0]!;+ const message = await sendChannelMessage(harness, owner.token, systemChannel.id, 'hidden history');+ await createPermissionOverwrite(harness, owner.token, systemChannel.id, member.userId, {+ type: 1,+ allow: (Permissions.VIEW_CHANNEL | Permissions.PIN_MESSAGES).toString(),+ deny: Permissions.READ_MESSAGE_HISTORY.toString(),+ });++ await pinMessage(harness, member.token, systemChannel.id, message.id);++ expect(await listPinnedMessageIds(owner.token, systemChannel.id)).toEqual([message.id]);+ });+});diff --git a/fluxer_docs/src/content/docs/http-api/messages.mdx b/fluxer_docs/src/content/docs/http-api/messages.mdxindex 6897f21ae..41b601c78 100644--- a/fluxer_docs/src/content/docs/http-api/messages.mdx+++ b/fluxer_docs/src/content/docs/http-api/messages.mdx@@ -1872,7 +1872,6 @@ Pins a `DEFAULT` or `REPLY` message. Returns 204 with an empty body on success.### Limitations- A guild caller requires [PIN_MESSAGES](/http-api/permissions/) and channel access.-- A guild caller without [READ_MESSAGE_HISTORY](/http-api/permissions/) can pin only a message on or after the guild's message history cutoff.- A one-to-one direct message caller must satisfy the recipient's direct message policy, and one who may not send fails with 400 `CANNOT_SEND_MESSAGES_TO_USER`.- A one-to-one direct message pin is refused with 403 `NEW_CONVERSATIONS_LIMITED` when the caller is under a [new conversation limit](/http-api/users/private-channels/#new-conversation-limit), the recipient is not a friend or a bot, and the recipient has not written in that direct message.- A group direct message and the personal notes channel apply no send policy.@@ -1899,7 +1898,7 @@ Pins a `DEFAULT` or `REPLY` message. Returns 204 with an empty body on success.| 403 | [error response](/http-api/#error-response) | Caller lacks `VIEW_CHANNEL` or `PIN_MESSAGES` and the request returns `MISSING_PERMISSIONS`, or message sending is temporarily disabled for the guild and the request returns `FEATURE_TEMPORARILY_DISABLED` || 403 | [error response](/http-api/#error-response) | The caller is under a new conversation limit in a direct message the recipient has not written in, and the request returns `NEW_CONVERSATIONS_LIMITED` || 403 | [error response](/http-api/#error-response) | Caller is timed out in a thread and the request returns `COMMUNICATION_DISABLED` |-| 404 | [error response](/http-api/#error-response) | Channel or message does not exist, or the message is outside the caller's message history cutoff |+| 404 | [error response](/http-api/#error-response) | Channel or message does not exist |### Side effects@@ -1944,7 +1943,7 @@ Unpins a message. Returns 204 with an empty body on success. Emits [Message Upda| 400 | [error response](/http-api/#error-response) | Thread is locked and the caller is not a thread moderator, returning `THREAD_LOCKED` || 403 | [error response](/http-api/#error-response) | Caller lacks `VIEW_CHANNEL` or `PIN_MESSAGES` and the request returns `MISSING_PERMISSIONS`, or message sending is temporarily disabled for the guild and the request returns `FEATURE_TEMPORARILY_DISABLED` || 403 | [error response](/http-api/#error-response) | Caller is timed out in a thread and the request returns `COMMUNICATION_DISABLED` |-| 404 | [error response](/http-api/#error-response) | Channel or message does not exist, or the message is outside the caller's message history cutoff |+| 404 | [error response](/http-api/#error-response) | Channel or message does not exist |### Side effects