fix: verify geohash signatures off main thread

This commit is contained in:
a1denvalu3 2026-07-30 19:30:20 +02:00
parent 57a7d3d955
commit 7e22480732
2 changed files with 18 additions and 3 deletions

View File

@ -5,8 +5,11 @@ import android.util.Log
import com.bitchat.android.model.BitchatMessage import com.bitchat.android.model.BitchatMessage
import com.bitchat.android.ui.ChatState import com.bitchat.android.ui.ChatState
import com.bitchat.android.ui.MessageManager import com.bitchat.android.ui.MessageManager
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.Date import java.util.Date
/** /**
@ -21,7 +24,8 @@ class GeohashMessageHandler(
private val messageManager: MessageManager, private val messageManager: MessageManager,
private val repo: GeohashRepository, private val repo: GeohashRepository,
private val scope: CoroutineScope, private val scope: CoroutineScope,
private val dataManager: com.bitchat.android.ui.DataManager private val dataManager: com.bitchat.android.ui.DataManager,
private val signatureVerificationDispatcher: CoroutineDispatcher = Dispatchers.Default
) { ) {
companion object { private const val TAG = "GeohashMessageHandler" } companion object { private const val TAG = "GeohashMessageHandler" }
@ -47,7 +51,10 @@ class GeohashMessageHandler(
if (event.kind != NostrKind.EPHEMERAL_EVENT && event.kind != NostrKind.GEOHASH_PRESENCE) return@launch if (event.kind != NostrKind.EPHEMERAL_EVENT && event.kind != NostrKind.GEOHASH_PRESENCE) return@launch
val tagGeo = event.tags.firstOrNull { it.size >= 2 && it[0] == "g" }?.getOrNull(1) val tagGeo = event.tags.firstOrNull { it.size >= 2 && it[0] == "g" }?.getOrNull(1)
if (tagGeo == null || !tagGeo.equals(subscribedGeohash, true)) return@launch if (tagGeo == null || !tagGeo.equals(subscribedGeohash, true)) return@launch
if (!event.isValidSignature()) { val hasValidSignature = withContext(signatureVerificationDispatcher) {
event.isValidSignature()
}
if (!hasValidSignature) {
Log.w(TAG, "Rejecting geohash event ${event.id.take(8)}... with invalid signature") Log.w(TAG, "Rejecting geohash event ${event.id.take(8)}... with invalid signature")
return@launch return@launch
} }

View File

@ -42,7 +42,15 @@ class GeohashMessageHandlerSignatureTest {
dataManager = DataManager(context = application) dataManager = DataManager(context = application)
messageManager = MessageManager(state = chatState) messageManager = MessageManager(state = chatState)
repo = GeohashRepository(application, chatState, dataManager) repo = GeohashRepository(application, chatState, dataManager)
handler = GeohashMessageHandler(application, chatState, messageManager, repo, testScope, dataManager) handler = GeohashMessageHandler(
application,
chatState,
messageManager,
repo,
testScope,
dataManager,
testDispatcher
)
} }
private fun buildSignedEvent(identity: NostrIdentity, content: String): NostrEvent { private fun buildSignedEvent(identity: NostrIdentity, content: String): NostrEvent {