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

[SES-2162] - Remove wrapping of config message #1517

Merged
merged 6 commits into from
Jun 27, 2024
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
1 change: 0 additions & 1 deletion app/src/main/java/org/thoughtcrime/securesms/AppContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import kotlinx.coroutines.asExecutor
import nl.komponents.kovenant.Kovenant
import nl.komponents.kovenant.jvm.asDispatcher
import org.session.libsignal.utilities.Log
import org.session.libsignal.utilities.ThreadUtils
import java.util.concurrent.Executors

object AppContext {
Expand Down
2 changes: 1 addition & 1 deletion libsession-util/libsession-util
Submodule libsession-util updated 57 files
+65 −36 .drone.jsonnet
+1 −0 .gitignore
+3 −0 .gitmodules
+17 −20 cmake/StaticBuild.cmake
+13 −0 external/CMakeLists.txt
+1 −1 external/nlohmann-json
+1 −0 external/protobuf
+116 −0 include/session/blinding.h
+91 −3 include/session/blinding.hpp
+5 −0 include/session/config/groups/keys.h
+5 −4 include/session/config/groups/keys.hpp
+61 −0 include/session/curve25519.h
+35 −0 include/session/curve25519.hpp
+100 −0 include/session/ed25519.h
+55 −0 include/session/ed25519.hpp
+36 −0 include/session/hash.h
+22 −0 include/session/hash.hpp
+176 −0 include/session/onionreq/builder.h
+82 −0 include/session/onionreq/builder.hpp
+9 −25 include/session/onionreq/hop_encryption.hpp
+2 −2 include/session/onionreq/parser.hpp
+61 −0 include/session/onionreq/response_parser.h
+31 −0 include/session/onionreq/response_parser.hpp
+24 −0 include/session/random.h
+18 −0 include/session/random.hpp
+219 −0 include/session/session_encrypt.h
+137 −5 include/session/session_encrypt.hpp
+10 −0 include/session/util.hpp
+9 −5 include/session/xed25519.h
+2 −32 proto/CMakeLists.txt
+7 −1 src/CMakeLists.txt
+381 −26 src/blinding.cpp
+13 −1 src/config/base.cpp
+3 −1 src/config/contacts.cpp
+2 −2 src/config/groups/info.cpp
+5 −5 src/config/groups/keys.cpp
+3 −0 src/config/user_profile.cpp
+87 −0 src/curve25519.cpp
+148 −0 src/ed25519.cpp
+65 −0 src/hash.cpp
+275 −0 src/onionreq/builder.cpp
+24 −25 src/onionreq/hop_encryption.cpp
+3 −2 src/onionreq/parser.cpp
+113 −0 src/onionreq/response_parser.cpp
+29 −0 src/random.cpp
+588 −15 src/session_encrypt.cpp
+11 −10 src/xed25519.cpp
+4 −0 tests/CMakeLists.txt
+150 −0 tests/test_blinding.cpp
+51 −0 tests/test_curve25519.cpp
+88 −0 tests/test_ed25519.cpp
+17 −10 tests/test_group_keys.cpp
+50 −0 tests/test_hash.cpp
+3 −3 tests/test_onionreq.cpp
+16 −0 tests/test_random.cpp
+308 −1 tests/test_session_encrypt.cpp
+6 −12 tests/test_xed25519.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ data class ConfigurationSyncJob(val destination: Destination): Job {
SharedConfigurationMessage(config.protoKindFor(), data, seqNo) to config
}.map { (message, config) ->
// return a list of batch request objects
val snodeMessage = MessageSender.buildWrappedMessageToSnode(destination, message, true)
val snodeMessage = MessageSender.buildConfigMessageToSnode(destination.destinationPublicKey(), message)
val authenticated = SnodeAPI.buildAuthenticatedStoreBatchInfo(
destination.destinationPublicKey(),
config.configNamespace(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ object MessageSender {
}
}

fun buildConfigMessageToSnode(destinationPubKey: String, message: SharedConfigurationMessage): SnodeMessage {
return SnodeMessage(
destinationPubKey,
Base64.encodeBytes(message.data),
ttl = message.ttl,
SnodeAPI.nowWithOffset
)
}

// One-on-One Chats & Closed Groups
@Throws(Exception::class)
fun buildWrappedMessageToSnode(destination: Destination, message: Message, isSyncMessage: Boolean): SnodeMessage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.session.libsession.snode.RawResponse
import org.session.libsession.snode.SnodeAPI
import org.session.libsession.snode.SnodeModule
import org.session.libsession.utilities.ConfigFactoryProtocol
import org.session.libsignal.utilities.Base64
import org.session.libsignal.utilities.Log
import org.session.libsignal.utilities.Namespace
import org.session.libsignal.utilities.Snode
Expand Down Expand Up @@ -126,37 +127,26 @@ class Poller(private val configFactory: ConfigFactoryProtocol, debounceTimer: Ti
private fun processConfig(snode: Snode, rawMessages: RawResponse, namespace: Int, forConfigObject: ConfigBase?) {
if (forConfigObject == null) return

val messages = SnodeAPI.parseRawMessagesResponse(
rawMessages,
snode,
userPublicKey,
namespace,
updateLatestHash = true,
updateStoredHashes = true,
)
val messages = rawMessages["messages"] as? List<*>
val processed = if (!messages.isNullOrEmpty()) {
SnodeAPI.updateLastMessageHashValueIfPossible(snode, userPublicKey, messages, namespace)
SnodeAPI.removeDuplicates(userPublicKey, messages, namespace, true).mapNotNull { messageBody ->
val rawMessageAsJSON = messageBody as? Map<*, *> ?: return@mapNotNull null
val hashValue = rawMessageAsJSON["hash"] as? String ?: return@mapNotNull null
val b64EncodedBody = rawMessageAsJSON["data"] as? String ?: return@mapNotNull null
val timestamp = rawMessageAsJSON["t"] as? Long ?: SnodeAPI.nowWithOffset
val body = Base64.decode(b64EncodedBody)
Triple(body, hashValue, timestamp)
}
} else emptyList()

if (messages.isEmpty()) {
// no new messages to process
return
}
if (processed.isEmpty()) return

var latestMessageTimestamp: Long? = null
messages.forEach { (envelope, hash) ->
processed.forEach { (body, hash, timestamp) ->
try {
val (message, _) = MessageReceiver.parse(data = envelope.toByteArray(),
// assume no groups in personal poller messages
openGroupServerID = null, currentClosedGroups = emptySet()
)
// sanity checks
if (message !is SharedConfigurationMessage) {
Log.w("Loki", "shared config message handled in configs wasn't SharedConfigurationMessage but was ${message.javaClass.simpleName}")
return@forEach
}
val merged = forConfigObject.merge(hash!! to message.data).firstOrNull { it == hash }
if (merged != null) {
// We successfully merged the hash, we can now update the timestamp
latestMessageTimestamp = if ((message.sentTimestamp ?: 0L) > (latestMessageTimestamp ?: 0L)) { message.sentTimestamp } else { latestMessageTimestamp }
}
forConfigObject.merge(hash to body)
latestMessageTimestamp = if (timestamp > (latestMessageTimestamp ?: 0L)) { timestamp } else { latestMessageTimestamp }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
latestMessageTimestamp = if (timestamp > (latestMessageTimestamp ?: 0L)) { timestamp } else { latestMessageTimestamp }
latestMessageTimestamp = max(timestamp, latestMessageTimestamp ?: 0L)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the behaviour as it would never return a null latestMessageTimestamp. That nullability is used as a check further down and would be set to 0L in this case and pass instead of being caught by that if.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes here are coming from the groups branch and they will be reverted before the groups are merged back in, I'm not sure if we want to spend too much energy on these

} catch (e: Exception) {
Log.e("Loki", e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ object SnodeAPI {
}
}

private fun updateLastMessageHashValueIfPossible(snode: Snode, publicKey: String, rawMessages: List<*>, namespace: Int) {
fun updateLastMessageHashValueIfPossible(snode: Snode, publicKey: String, rawMessages: List<*>, namespace: Int) {
val lastMessageAsJSON = rawMessages.lastOrNull() as? Map<*, *>
val hashValue = lastMessageAsJSON?.get("hash") as? String
if (hashValue != null) {
Expand All @@ -839,7 +839,7 @@ object SnodeAPI {
}
}

private fun removeDuplicates(publicKey: String, rawMessages: List<*>, namespace: Int, updateStoredHashes: Boolean): List<*> {
fun removeDuplicates(publicKey: String, rawMessages: List<*>, namespace: Int, updateStoredHashes: Boolean): List<*> {
val originalMessageHashValues = database.getReceivedMessageHashValues(publicKey, namespace)?.toMutableSet() ?: mutableSetOf()
val receivedMessageHashValues = originalMessageHashValues.toMutableSet()
val result = rawMessages.filter { rawMessage ->
Expand Down