Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly discard malformed messages during batch calls #149

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sources/XMTP/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ public class Conversations {
for batch in batches {
messages += try await client.apiClient.batchQuery(request: batch)
.responses.flatMap { (res) in
try res.envelopes.compactMap { (envelope) in
res.envelopes.compactMap { (envelope) in
let conversation = conversationsByTopic[envelope.contentTopic]
if conversation == nil {
print("discarding message, unknown conversation \(envelope)")
return nil
}
let msg = try conversation?.decode(envelope)
if msg == nil {
do {
return try conversation?.decode(envelope)
} catch {
print("discarding message, unable to decode \(envelope)")
return nil
}
return msg
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions Tests/XMTPTests/ConversationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,35 @@ class ConversationTests: XCTestCase {
XCTAssertEqual(bobConversation.topic, messages[2].topic)
}

func testProperlyDiscardBadBatchMessages() async throws {

guard case let .v2(bobConversation) = try await aliceClient.conversations
.newConversation(with: bob.address) else {
XCTFail("did not get a v2 conversation for bob")
return
}

try await bobConversation.send(content: "Hello")

// Now we send some garbage and expect it to be properly ignored.
try await bobClient.apiClient.publish(envelopes: [
Envelope(
topic: bobConversation.topic,
timestamp: Date(),
message: Data([1, 2, 3]) // garbage, malformed message
)
])

try await bobConversation.send(content: "Goodbye")

let messages = try await aliceClient.conversations.listBatchMessages(
topics: [bobConversation.topic : nil]
)
XCTAssertEqual(2, messages.count)
XCTAssertEqual("Goodbye", try messages[0].content())
XCTAssertEqual("Hello", try messages[1].content())
}

func testImportV1ConversationFromJS() async throws {
let jsExportJSONData = Data("""
{
Expand Down
2 changes: 1 addition & 1 deletion XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "XMTP"
spec.version = "0.4.9-alpha0"
spec.version = "0.4.10-alpha0"
spec.summary = "XMTP SDK Cocoapod"

# This description is used to generate tags and improve search results.
Expand Down