Skip to content

Commit

Permalink
Check for invalid group membership change messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Jun 5, 2024
1 parent e35ed5d commit e04b09b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions packages/mls-client/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ 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,
type ContentCodec,
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 = {
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion packages/mls-client/src/DecodedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit e04b09b

Please sign in to comment.