From c973de8676b42feeb0170e331c4484cdaec097a9 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:42:51 +0000 Subject: [PATCH] agent: accept confirmations only with the client version that the agent version implies (#2053) * agent: accept confirmations only with the client version that the agent version implies * tests: connections with apps of earlier versions * agent: send confirmation with the client version agreed when the join is retried --- src/Simplex/Messaging/Agent.hs | 4 +- src/Simplex/Messaging/Agent/Protocol.hs | 7 + tests/AgentTests/FunctionalAPITests.hs | 176 ++++++++++++++++++++++++ 3 files changed, 186 insertions(+), 1 deletion(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 0e3a1bce9..3febe2e97 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -1541,7 +1541,8 @@ startJoinInvitation c userId connId sq_ enableNtfs cReqUri pqSupport = Left e -> do nonBlockingWriteTBQueue (subQ c) ("", connId, AEvt SAEConn (ERR $ INTERNAL $ "no snd ratchet " <> show e)) runExceptT $ snd <$> createRatchet_ db g connId maxSupported pqSupport e2eRcvParams - pure ((cData, sq), (Just e2eSndParams, Nothing)) + let Compatible SMPQueueInfo {clientVersion} = qInfo + pure ((cData, (sq :: SndQueue) {smpClientVersion = clientVersion}), (Just e2eSndParams, Nothing)) _ -> do let Compatible SMPQueueInfo {queueAddress = SMPQueueAddress {smpServer, senderId}} = qInfo invLink_ <- withStore' c $ \db -> getInvShortLinkKeys db smpServer senderId @@ -3931,6 +3932,7 @@ processSMPTransmissions c@AgentClient {subQ, smpServersStats} (tSess@(userId, sr let compatible = (agentVersion `isCompatible` smpAgentVRange || agentVersion <= agreedAgentVersion) && (phVer `isCompatible` smpClientVRange || phVer <= agreedClientVerion) + && phVer >= min agreedClientVerion (minSMPClientVersion agentVersion) unless compatible $ throwE $ AGENT A_VERSION smpConfirmation :: SMP.MsgId -> Connection c -> Maybe C.APublicAuthKey -> C.PublicKeyX25519 -> Maybe (CR.SndE2ERatchetParams 'C.X448) -> ByteString -> VersionSMPC -> VersionSMPA -> AM () diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index 7625092d1..e7cf26969 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -45,6 +45,7 @@ module Simplex.Messaging.Agent.Protocol rpcAddressSMPAgentVersion, currentSMPAgentVersion, supportedSMPAgentVRange, + minSMPClientVersion, e2eEncConnInfoLength, e2eEncAgentMsgLength, @@ -333,6 +334,12 @@ currentSMPAgentVersion = VersionSMPA 8 supportedSMPAgentVRange :: VersionRangeSMPA supportedSMPAgentVRange = mkVersionRange minSupportedSMPAgentVersion currentSMPAgentVersion +minSMPClientVersion :: VersionSMPA -> VersionSMPC +minSMPClientVersion v + | v >= rpcAddressSMPAgentVersion = shortLinksSMPClientVersion + | v >= ratchetOnConfSMPAgentVersion = sndAuthKeySMPClientVersion + | otherwise = initialSMPClientVersion + -- it is shorter to allow all handshake headers, -- including E2E (double-ratchet) parameters and -- signing key of the sender for the server diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 2e1cac849..dc2cfab64 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -84,6 +84,7 @@ import Data.Text.Encoding (decodeLatin1) import qualified Data.Text.IO as T import Data.Time.Clock (diffUTCTime, getCurrentTime) import Data.Time.Clock.System (SystemTime (..), getSystemTime) +import Data.Tuple (swap) import Data.Type.Equality (testEquality, (:~:) (Refl)) import Data.Word (Word16) import GHC.Stack (withFrozenCallStack) @@ -344,6 +345,8 @@ functionalAPITests ps = do withSmpServer ps testEnablePQEncryption it "should refuse joining already used invitation and keep connection" $ withSmpServer ps testJoinUsedInvitation + it "should refuse confirmation with client version below the version of its agent" $ + withSmpServer ps testConfClientVersionBelowAgentVersion describe "Duplex connection - delivery stress test" $ do describe "one way (50)" $ testMatrix2Stress ps $ runAgentClientStressTestOneWay 50 xdescribe "one way (1000)" $ testMatrix2Stress ps $ runAgentClientStressTestOneWay 1000 @@ -627,6 +630,15 @@ functionalAPITests ps = do describe "Delivery receipts" $ do it "should send and receive delivery receipt" $ withSmpServer ps testDeliveryReceipts it "send delivery receipts concurrently with messages" $ testDeliveryReceiptsConcurrent ps + describe "Connections with apps of earlier versions" $ do + describe "app with agent versions 2-6, client versions 1-3, e2e versions 2-3" $ + appVersionTests ps 6 (appVersionsCfg (2, 6) (1, 3) (2, 3)) + describe "app with agent versions 2-7, client versions 1-3, e2e versions 2-3" $ + appVersionTests ps 7 (appVersionsCfg (2, 7) (1, 3) (2, 3)) + describe "app with agent versions 2-7, client versions 1-4, e2e versions 2-3" $ + appVersionTests ps 7 (appVersionsCfg (2, 7) (1, 4) (2, 3)) + describe "app with agent versions 6-8, client versions 1-4, e2e version 3" $ + appVersionTests ps 8 (appVersionsCfg (6, 8) (1, 4) (3, 3)) functionalAPITimingTests :: (ASrvTransport, AStoreType) -> Spec functionalAPITimingTests ps = do @@ -941,6 +953,14 @@ testJoinUsedInvitation = liftIO $ r `shouldBe` Left (CONN DUPLICATE "") exchangeGreetings alice bobId bob aliceId +testConfClientVersionBelowAgentVersion :: (HasCallStack, HasTestEnv) => IO () +testConfClientVersionBelowAgentVersion = + withAgentClientsCfg2 agentCfg agentCfg {smpClientVRange = mkVersionRange 1 2} $ \alice bob -> runRight_ $ do + (bobId, CCLink qInfo Nothing) <- A.createConnection alice NRMInteractive 1 True True SCMInvitation Nothing Nothing (IKLinkPQ PQSupportOn) False SMSubscribe + (aliceId, _) <- A.prepareConnectionToJoin bob 1 True qInfo PQSupportOn + _ <- A.joinConnection bob NRMInteractive 1 aliceId True qInfo "bob's connInfo" PQSupportOn SMSubscribe + get alice =##> \case ("", c, ERR (AGENT A_VERSION)) -> c == bobId; _ -> False + testEnablePQEncryption :: (HasCallStack, HasTestEnv) => IO () testEnablePQEncryption = withAgentClients2 $ \ca cb -> runRight_ $ do @@ -4514,6 +4534,162 @@ testDeliveryReceiptsConcurrent (t, msType) = Just r -> pure r _ -> error "timeout" +appVersionsCfg :: HasTestEnv => (Word16, Word16) -> (Word16, Word16) -> (Word16, Word16) -> AgentConfig +appVersionsCfg (a1, a2) (c1, c2) (e1, e2) = + agentCfg + { smpAgentVRange = mkVersionRange a1 a2, + smpClientVRange = mkVersionRange c1 c2, + e2eEncryptVRange = mkVersionRange e1 e2 + } + +appVersionTests :: (ASrvTransport, AStoreType) -> Word16 -> (HasTestEnv => AgentConfig) -> Spec +appVersionTests ps agreedVersion appCfg' = do + it "connects via invitation link of the app" $ + testAppConnection ps agreedVersion appCfg' connectViaInvitation noAction + it "connects via invitation link of the current agent" $ + testAppConnection ps agreedVersion appCfg' (\app current -> swap <$> connectViaInvitation current app) noAction + it "connects via contact address of the app" $ + testAppConnection ps agreedVersion appCfg' (connectViaAddress (usesAddressRatchetKeys appCfg') id) noAction + it "connects via contact address of the current agent" $ + testAppConnection ps agreedVersion appCfg' (\app current -> swap <$> connectViaAddress False id current app) noAction + it "connects via contact address with ratchet keys of the current agent" $ + testAppConnection ps agreedVersion appCfg' (\app current -> swap <$> connectViaAddress True (addressAsReadBy appCfg') current app) noAction + it "switches the receiving queue of the app" $ + testAppConnection ps agreedVersion appCfg' connectViaInvitation (switchQueue agreedVersion) + it "switches the receiving queue of the current agent" $ + testAppConnection ps agreedVersion appCfg' connectViaInvitation $ \app currentId current appId -> + switchQueue agreedVersion current appId app currentId + it "switches queues of a connection made before both apps are upgraded" $ + testSwitchAfterUpgrade ps appCfg' agentCfg 8 + it "switches queues of a connection made before one app is upgraded" $ + testSwitchAfterUpgrade ps appCfg' appCfg' agreedVersion + it "connects when the app is upgraded during the join" $ + testJoinCompletedAfterUpgrade ps appCfg' + +testAppConnection :: + (HasCallStack, HasTestEnv) => + (ASrvTransport, AStoreType) -> + Word16 -> + AgentConfig -> + (AgentClient -> AgentClient -> ExceptT AgentErrorType IO (ConnId, ConnId)) -> + (AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO ()) -> + IO () +testAppConnection ps agreedVersion appCfg' connect continue = + withSmpServer ps $ withAgentClientsCfg2 appCfg' agentCfg $ \app current -> runRight_ $ do + (appId, currentId) <- connect app current + checkVersion app currentId agreedVersion + checkVersion current appId agreedVersion + exchangeMessages app currentId current appId + continue app currentId current appId + +noAction :: AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO () +noAction _ _ _ _ = pure () + +testSwitchAfterUpgrade :: (HasCallStack, HasTestEnv) => (ASrvTransport, AStoreType) -> AgentConfig -> AgentConfig -> Word16 -> IO () +testSwitchAfterUpgrade ps appCfg' aCfgAfter agreedVersion = + withSmpServer ps $ do + (aId, bId) <- withAgentClientsCfg2 appCfg' appCfg' $ \a b -> runRight $ do + ids@(aId, bId) <- connectViaInvitation a b + exchangeMessages a bId b aId + pure ids + withAgentClientsCfg2 aCfgAfter agentCfg $ \a b -> runRight_ $ do + subscribeConnection a bId + subscribeConnection b aId + exchangeMessages a bId b aId + checkVersion a bId agreedVersion + checkVersion b aId agreedVersion + switchQueue agreedVersion a bId b aId + switchQueue agreedVersion b aId a bId + +testJoinCompletedAfterUpgrade :: (HasCallStack, HasTestEnv) => (ASrvTransport, AStoreType) -> AgentConfig -> IO () +testJoinCompletedAfterUpgrade ps appCfg' = + withAgent 1 agentCfg initAgentServers testDB $ \alice -> do + (bobId, aliceId, cReq) <- withAgent 2 appCfg' initAgentServers testDB2 $ \bob -> do + (bobId, cReq) <- withSmpServerStoreLogOn ps testPort $ \_ -> runRight $ createConnection alice 1 True SCMInvitation Nothing SMSubscribe + ("", "", DOWN _ _) <- nGet alice + (aliceId, _) <- runRight $ A.prepareConnectionToJoin bob 1 True cReq PQSupportOn + Left (BROKER _ (NETWORK _)) <- runExceptT $ A.joinConnection bob NRMInteractive 1 aliceId True cReq "bob's connInfo" PQSupportOn SMSubscribe + pure (bobId, aliceId, cReq) + withAgent 2 agentCfg initAgentServers testDB2 $ \bob -> + withSmpServerStoreLogOn ps testPort $ \_ -> runRight_ $ do + ("", "", UP _ _) <- nGet alice + void $ A.joinConnection bob NRMInteractive 1 aliceId True cReq "bob's connInfo" PQSupportOn SMSubscribe + confId <- getConfirmation alice bobId "bob's connInfo" + allowConnection alice bobId confId "alice's connInfo" + get alice ##> ("", bobId, CON) + get bob ##> ("", aliceId, INFO "alice's connInfo") + get bob ##> ("", aliceId, CON) + exchangeMessages alice bobId bob aliceId + +connectViaInvitation :: HasCallStack => AgentClient -> AgentClient -> ExceptT AgentErrorType IO (ConnId, ConnId) +connectViaInvitation alice bob = do + (bobId, cReq) <- createConnection alice 1 True SCMInvitation Nothing SMSubscribe + (aliceId, _) <- joinConnection bob 1 True cReq "bob's connInfo" SMSubscribe + confId <- getConfirmation alice bobId "bob's connInfo" + allowConnection alice bobId confId "alice's connInfo" + get alice ##> ("", bobId, CON) + get bob ##> ("", aliceId, INFO "alice's connInfo") + get bob ##> ("", aliceId, CON) + pure (aliceId, bobId) + +connectViaAddress :: + HasCallStack => + UseRatchetKeys -> + (ConnectionRequestUri 'CMContact -> ConnectionRequestUri 'CMContact) -> + AgentClient -> + AgentClient -> + ExceptT AgentErrorType IO (ConnId, ConnId) +connectViaAddress useDR readAddress alice bob = do + (_, CCLink cReq Nothing) <- A.createConnection alice NRMInteractive 1 True True SCMContact Nothing Nothing IKPQOn useDR SMSubscribe + (aliceId, _) <- joinConnection bob 1 True (readAddress cReq) "bob's connInfo" SMSubscribe + invId <- + get alice >>= \case + ("", _, REQ invId _ "bob's connInfo") -> pure invId + r -> error $ "unexpected event: " <> show r + (bobId, _) <- A.prepareConnectionToAccept alice 1 True invId PQSupportOn + void $ acceptContact alice 1 bobId True invId "alice's connInfo" PQSupportOn SMSubscribe + confId <- getConfirmation bob aliceId "alice's connInfo" + allowConnection bob aliceId confId "bob's connInfo" + get alice ##> ("", bobId, INFO "bob's connInfo") + get alice ##> ("", bobId, CON) + get bob ##> ("", aliceId, CON) + pure (aliceId, bobId) + +usesAddressRatchetKeys :: AgentConfig -> Bool +usesAddressRatchetKeys AgentConfig {smpAgentVRange} = maxVersion smpAgentVRange >= rpcAddressSMPAgentVersion + +addressAsReadBy :: AgentConfig -> ConnectionRequestUri 'CMContact -> ConnectionRequestUri 'CMContact +addressAsReadBy cfg cReq@(CRContactUri crData _) + | usesAddressRatchetKeys cfg = cReq + | otherwise = CRContactUri crData Nothing + +getConfirmation :: HasCallStack => AgentClient -> ConnId -> ConnInfo -> ExceptT AgentErrorType IO ConfirmationId +getConfirmation c connId connInfo = + get c >>= \case + ("", connId', CONF confId _ connInfo') | connId' == connId && connInfo' == connInfo -> pure confId + r -> error $ "unexpected event: " <> show r + +switchQueue :: HasCallStack => Word16 -> AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO () +switchQueue agreedVersion a bId b aId = do + void $ switchConnectionAsync a "" bId + if VersionSMPA agreedVersion >= rpcAddressSMPAgentVersion + then fastSwitchComplete a bId b aId + else switchComplete a bId b aId + exchangeMessages a bId b aId + +exchangeMessages :: HasCallStack => AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO () +exchangeMessages alice bobId bob aliceId = do + deliverMessage alice bobId bob aliceId "hello" + deliverMessage bob aliceId alice bobId "hello too" + +deliverMessage :: HasCallStack => AgentClient -> ConnId -> AgentClient -> ConnId -> MsgBody -> ExceptT AgentErrorType IO () +deliverMessage a bId b aId msg = do + msgId <- sendMessage a bId SMP.noMsgFlags msg + get a ##> ("", bId, SENT msgId) + get b >>= \case + ("", aId', Msg' rcvMsgId _ msg') | aId' == aId && msg' == msg -> ackMessage b aId rcvMsgId Nothing + r -> error $ "unexpected event: " <> show r + testTwoUsers :: (HasCallStack, HasTestEnv) => IO () testTwoUsers = withAgentClients2 $ \a b -> do let nc = netCfg initAgentServers