Skip to content

Commit

Permalink
fix: validate node ids to avoid crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonvdb committed Sep 27, 2024
1 parent 452c098 commit ac06aed
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ PODS:
- React-jsinspector (0.72.4)
- React-logger (0.72.4):
- glog
- react-native-ldk (0.0.151):
- react-native-ldk (0.0.153):
- React
- react-native-randombytes (3.6.1):
- React-Core
Expand Down Expand Up @@ -621,7 +621,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: c7f826e40fa9cab5d37cab6130b1af237332b594
React-jsinspector: aaed4cf551c4a1c98092436518c2d267b13a673f
React-logger: da1ebe05ae06eb6db4b162202faeafac4b435e77
react-native-ldk: a7e71785237dd3d12dc52b4287abd88c865f5262
react-native-ldk: 56632dc56319db257b5f03e71e791530b2590cbb
react-native-randombytes: 421f1c7d48c0af8dbcd471b0324393ebf8fe7846
react-native-tcp-socket: c1b7297619616b4c9caae6889bcb0aba78086989
React-NativeModulesApple: edb5ace14f73f4969df6e7b1f3e41bef0012740f
Expand Down
5 changes: 5 additions & 0 deletions lib/android/src/main/java/com/reactnativeldk/LdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,11 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
fun addPeer(address: String, port: Double, pubKey: String, timeout: Double, promise: Promise) {
peerHandler ?: return handleReject(promise, LdkErrors.init_peer_handler)

if (pubKey.hexa().count() != 33 || NodeId.read(pubKey.hexa()).is_ok.not()) {
LdkEventEmitter.send(EventTypes.native_log, "Failed to add new peer. Invalid pubKey: $pubKey")
return handleReject(promise, LdkErrors.add_peer_fail, Error("Invalid pubKey"))
}

//If peer is already connected don't add again
val currentList = peerManager!!.list_peers().map { it._counterparty_node_id.hexEncodedString() }
if (currentList.contains(pubKey)) {
Expand Down
5 changes: 5 additions & 0 deletions lib/ios/Ldk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,11 @@ class Ldk: NSObject {
func addPeer(_ address: NSString, port: NSInteger, pubKey: NSString, timeout: NSInteger, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
// timeout param not used. Only for android.

guard String(pubKey).hexaBytes.count == 33 && NodeId.read(ser: String(pubKey).hexaBytes).isOk() else {
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Failed to add new peer. Invalid public key: \(pubKey)")
return handleReject(reject, .add_peer_fail, "Invalid peer public key")
}

if backgroundedAt != nil {
// Give it a second maybe it's just restarting
sleep(1)
Expand Down
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@synonymdev/react-native-ldk",
"title": "React Native LDK",
"version": "0.0.152",
"version": "0.0.153",
"description": "React Native wrapper for LDK",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down

0 comments on commit ac06aed

Please sign in to comment.