From e04b09b53536f765462c53a5abd2e4f9f4c9ccf4 Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Wed, 5 Jun 2024 10:51:04 -0500 Subject: [PATCH] Check for invalid group membership change messages --- packages/mls-client/src/Client.ts | 21 +++++++++++++++++---- packages/mls-client/src/DecodedMessage.ts | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/mls-client/src/Client.ts b/packages/mls-client/src/Client.ts index a786664b..321dc87a 100644 --- a/packages/mls-client/src/Client.ts +++ b/packages/mls-client/src/Client.ts @@ -2,8 +2,9 @@ import { join } from 'node:path' import process from 'node:process' import { createClient, + NapiGroupMessageKind, type NapiClient, - type NapiEncodedContent, + type NapiMessage, } from '@xmtp/mls-client-bindings-node' import { TextCodec, @@ -11,7 +12,10 @@ import { type ContentTypeId, type EncodedContent, } from '@xmtp/xmtp-js' -import { GroupUpdatedCodec } from '@/codecs/GroupUpdatedCodec' +import { + ContentTypeGroupUpdated, + GroupUpdatedCodec, +} from '@/codecs/GroupUpdatedCodec' import { Conversations } from '@/Conversations' export const ApiUrls = { @@ -168,11 +172,20 @@ export class Client { return encoded } - decodeContent(content: NapiEncodedContent, contentType: ContentTypeId) { + decodeContent(message: NapiMessage, contentType: ContentTypeId) { const codec = this.codecFor(contentType) if (!codec) { throw new Error(`no codec for ${contentType.toString()}`) } - return codec.decode(content as EncodedContent, this) + + // throw an error if there's an invalid group membership change message + if ( + contentType.sameAs(ContentTypeGroupUpdated) && + message.kind !== NapiGroupMessageKind.MembershipChange + ) { + throw new Error('Error decoding group membership change') + } + + return codec.decode(message.content as EncodedContent, this) } } diff --git a/packages/mls-client/src/DecodedMessage.ts b/packages/mls-client/src/DecodedMessage.ts index bac7546f..cc8ee338 100644 --- a/packages/mls-client/src/DecodedMessage.ts +++ b/packages/mls-client/src/DecodedMessage.ts @@ -60,6 +60,6 @@ export class DecodedMessage { this.parameters = message.content.parameters this.fallback = message.content.fallback this.compression = message.content.compression - this.content = this.#client.decodeContent(message.content, this.contentType) + this.content = this.#client.decodeContent(message, this.contentType) } }