Skip to content

Commit

Permalink
fix(crypto): setting error variable in case of getting invalid Base-6…
Browse files Browse the repository at this point in the history
…4 input
  • Loading branch information
jguz-pubnub committed Nov 23, 2023
1 parent 22031d1 commit c0f9076
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
16 changes: 15 additions & 1 deletion Sources/PubNub/Networking/Routers/HistoryRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,24 @@ struct MessageHistoryResponseDecoder: ResponseDecoder {
messageType: message.messageType,
error: error
)
PubNub.log.error("History message failed to decrypt due to \(error)")
PubNub.log.warn("History message failed to decrypt due to \(error)")
}
} else {
messages[index] = MessageHistoryMessagePayload(
message: message.message,
timetoken: message.timetoken,
meta: message.meta,
uuid: message.uuid,
messageType: message.messageType,
error: PubNubError(
.decryptionFailure,
additional: ["Cannot decrypt message due to invalid Base-64 input"]
)
)
}
}


return messages
}

Expand Down
12 changes: 7 additions & 5 deletions Sources/PubNub/Networking/Routers/SubscribeRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,25 @@ struct SubscribeDecoder: ResponseDecoder {
}

func decrypt(_ cryptoModule: CryptoModule, message: SubscribeMessagePayload) -> SubscribeMessagePayload {
// Convert base64 string into Data
var message = message
// Convert Base64 string into Data
if let messageData = message.payload.dataOptional {
// If a message fails we just return the original and move on
switch cryptoModule.decryptedString(from: messageData) {
case .success(let decodedString):
// Create mutable copy of payload
var message = message
message.payload = AnyJSON(reverse: decodedString)
return message
case .failure(let error):
PubNub.log.error("Subscribe message failed to decrypt due to \(error)")
var message = message
PubNub.log.warn("Subscribe message failed to decrypt due to \(error)")
message.error = error
return message
}
}

message.error = PubNubError(
.decryptionFailure,
additional: ["Cannot decrypt message due to invalid Base-64 input"]
)
return message
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ extension SubscribeRouterTests {
listener.didReceiveMessage = { [weak self, pubNubWithMockedSession] message in
XCTAssertEqual(message.channel, self?.testChannel)
XCTAssertEqual(message.payload.stringOptional, "Test Message")
XCTAssertNil(message.error)
XCTAssertTrue(message.error?.reason == .decryptionFailure)

pubNubWithMockedSession.unsubscribeAll()
messageExpect.fulfill()
Expand Down

0 comments on commit c0f9076

Please sign in to comment.