From e8aff94ca93c60725115ae97bf3cd67812b01ba1 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Sat, 7 Nov 2026 16:17:27 +0000 Subject: [PATCH] lib, ntf server: reject null byte in host and server without key hash (#2027) * core: reject empty server key hash and null byte in host * core: reject notification subscription to a server without key hash * lib: exclude null byte in host with the other excluded characters * tests: name the subscription server address test by the rejected values --- .../Messaging/Notifications/Protocol.hs | 6 +++++- src/Simplex/Messaging/Transport/Client.hs | 2 +- tests/CoreTests/EncodingTests.hs | 2 ++ tests/NtfServerTests.hs | 18 ++++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 44f096f9b..423a048e1 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -384,9 +384,13 @@ data SMPQueueNtf = SMPQueueNtf instance Encoding SMPQueueNtf where smpEncode SMPQueueNtf {smpServer, notifierId} = smpEncode (smpServer, notifierId) smpP = do - smpServer <- updateSMPServerHosts <$> smpP + smpServer <- updateSMPServerHosts <$> (requireKeyHash <$?> smpP) notifierId <- smpP pure SMPQueueNtf {smpServer, notifierId} + where + requireKeyHash srv + | B.null (C.unKeyHash (keyHash srv)) = Left "SMP server without key hash" + | otherwise = Right srv instance StrEncoding SMPQueueNtf where strEncode SMPQueueNtf {smpServer, notifierId} = strEncode smpServer <> "/" <> strEncode notifierId diff --git a/src/Simplex/Messaging/Transport/Client.hs b/src/Simplex/Messaging/Transport/Client.hs index 1f4914e85..1fd81e5b1 100644 --- a/src/Simplex/Messaging/Transport/Client.hs +++ b/src/Simplex/Messaging/Transport/Client.hs @@ -89,7 +89,7 @@ instance StrEncoding TransportHost where [ THIPv4 <$> ((,,,) <$> ipNum <*> ipNum <*> ipNum <*> A.decimal), maybe (Left "bad IPv6") (Right . THIPv6 . fromIPv6w) . readMaybe . B.unpack <$?> ipv6StrP, THOnionHost <$> ((<>) <$> A.takeWhile (\c -> isAsciiLower c || isDigit c) <*> A.string ".onion"), - THDomainName . B.unpack <$> (notOnion <$?> A.takeWhile1 (A.notInClass ":#,;/ \n\r\t[]")) + THDomainName . B.unpack <$> (notOnion <$?> A.takeWhile1 (A.notInClass ":#,;/ \n\r\t[]\0")) ] where ipNum = validIP <$?> (A.decimal <* A.char '.') diff --git a/tests/CoreTests/EncodingTests.hs b/tests/CoreTests/EncodingTests.hs index 5f2169372..4108bef2a 100644 --- a/tests/CoreTests/EncodingTests.hs +++ b/tests/CoreTests/EncodingTests.hs @@ -57,6 +57,8 @@ encodingTests = modifyMaxSuccess (const 1000) $ do shouldNotParse @TransportHost "smp#simplex.im" "endOfInput" shouldNotParse @TransportHost "smp simplex.im" "endOfInput" shouldNotParse @TransportHost "smp\nsimplex.im" "endOfInput" + shouldNotParse @TransportHost "smp\0simplex.im" "endOfInput" + shouldReject @TransportHost "\0" describe "onion hosts" $ do it "should encode / decode onion host" $ THOnionHost "beccx4yfxxbvyhqypaavemqurytl6hozr47wfc7uuecacjqdvwpw2xid.onion" #==# "beccx4yfxxbvyhqypaavemqurytl6hozr47wfc7uuecacjqdvwpw2xid.onion" it "should only allow latin letters and digits" $ do diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs index 951dcd8d1..947142402 100644 --- a/tests/NtfServerTests.hs +++ b/tests/NtfServerTests.hs @@ -78,6 +78,7 @@ ntfServerTests ps@(t, _) = do describe "Notification subscriptions (NEW with ntf creds)" $ testNotificationSubscription ps createNtfQueueNEW describe "Retried notification subscription" $ testRetriedNtfSubscription ps describe "Notification server restart" $ testResubscribeAfterRestart ps + describe "Notification subscription address" $ testSNEWServerAddress ps describe "Notification service" $ do it "resubscribes queues when service ID changes" $ testResubscribeOnServiceIdChange ps @@ -377,6 +378,23 @@ testResubscribeAfterRestart (ATransport t, msType) = 5000000 `timeout` getMockNotification apns (DeviceToken PPApnsTest "abcd") pure () +testSNEWServerAddress :: (ASrvTransport, AStoreType) -> Spec +testSNEWServerAddress (ATransport t, _msType) = + it "should reject a server without key hash or with a null byte in the host" $ do + g <- C.newRandom + (_nPub, nKey) <- atomically $ C.generateAuthKeyPair C.SEd25519 g + withAPNSMockServer $ \apns -> + ntfTest t $ \nh -> do + (tknKey, _dhSecret, tId, regCode) <- registerToken nh apns "abcd" + RespNtf "2" _ NROk <- signSendRecvNtf nh tknKey ("2", tId, TVFY regCode) + let nulHostSrv = SMPServer (THDomainName "\0" :| []) SMP.testPort SMP.testKeyHash + emptyKeyHashSrv = SMPServer SMP.testHost SMP.testPort (C.KeyHash "") + RespNtf "3" NoEntity (NRErr (CMD SYNTAX)) <- + signSendRecvNtf nh tknKey ("3", NoEntity, SNEW $ NewNtfSub tId (SMPQueueNtf nulHostSrv (EntityId "abcd")) nKey) + RespNtf "4" NoEntity (NRErr (CMD SYNTAX)) <- + signSendRecvNtf nh tknKey ("4", NoEntity, SNEW $ NewNtfSub tId (SMPQueueNtf emptyKeyHashSrv (EntityId "abcd")) nKey) + pure () + testResubscribeOnServiceIdChange :: HasTestEnv => (ASrvTransport, AStoreType) -> Expectation testResubscribeOnServiceIdChange ps@(ATransport (t :: TProxy c 'TServer), _) = do g <- C.newRandom