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

[WIP] #248 Handle malformed QR code #293

Closed
Closed
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
26 changes: 22 additions & 4 deletions Sources/WalletConnectSign/Engine/Controller/PairEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,31 @@ actor PairEngine {
throw WalletConnectError.pairingAlreadyExist
}
var pairing = WCPairing(uri: uri)
try await networkingInteractor.subscribe(topic: pairing.topic)
let symKey = try! SymmetricKey(hex: uri.symKey) // FIXME: Malformed QR code from external source can crash the SDK
try! kms.setSymmetricKey(symKey, for: pairing.topic)

try await subscribe(to: uri.topic)
try await setSymmetricKey(uri.symKey, topic: pairing.topic)

pairing.activate()
pairingStore.setPairing(pairing)
}


private func setSymmetricKey(_ uriSymKey: String, topic: String) async throws {
do {
let symKey = try SymmetricKey(hex: uriSymKey)
try kms.setSymmetricKey(symKey, for: topic)
} catch {
throw WalletConnectError.malformedPairingURI
}
}

private func subscribe(to topic: String) async throws {
do {
try await networkingInteractor.subscribe(topic: topic)
} catch {
throw WalletConnectError.noPairingMatchingTopic(topic)
}
}

func hasPairing(for topic: String) -> Bool {
return pairingStore.hasPairing(forTopic: topic)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/WalletConnectSign/WalletConnectError.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
enum WalletConnectError: Error {

enum WalletConnectError: Error, Equatable {
case pairingProposalFailed
case malformedPairingURI
case noPairingMatchingTopic(String)
Expand Down
15 changes: 14 additions & 1 deletion Tests/WalletConnectSignTests/PairEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final class PairEngineTests: XCTestCase {
networkingInteractor = nil
storageMock = nil
cryptoMock = nil
topicGenerator = nil
proposalPayloadsStore = nil
engine = nil
}

Expand All @@ -44,7 +46,9 @@ final class PairEngineTests: XCTestCase {
if i == 1 {
await XCTAssertNoThrowAsync(try await engine.pair(uri))
} else {
await XCTAssertThrowsErrorAsync(try await engine.pair(uri))
await XCTAssertThrowsErrorAsync(try await engine.pair(uri)) {
XCTAssertEqual($0 as! WalletConnectError, WalletConnectError.pairingAlreadyExist)
}
}
}
}
Expand All @@ -57,4 +61,13 @@ final class PairEngineTests: XCTestCase {
XCTAssert(cryptoMock.hasSymmetricKey(for: topic), "Responder must store the symmetric key matching the pairing topic")
XCTAssert(storageMock.hasPairing(forTopic: topic), "The engine must store a pairing")
}

func testPairMalformedQRCode() async {
let uri = WalletConnectURI(topic: topicGenerator.getTopic(),
symKey: "0653ca620c7b4990392e1c53c4a51c14a2840cd20f0f1524cf435b17b6fe988c",
relay: RelayProtocolOptions(protocol: "waku", data: nil))
await XCTAssertThrowsErrorAsync(try await engine.pair(uri)) {
XCTAssertEqual($0 as! WalletConnectError, WalletConnectError.malformedPairingURI)
}
}
}