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
This commit is contained in:
Evgeny @ SimpleX Chat authored and GitHub committed 2026-11-07 16:17:27 +00:00
1 parent 5f914a040e
commit e8aff94ca9
4 files changed
+26 -2

No files matched your search

@@ -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
+1 -1
View File
@@ -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 '.')
+2
View File
@@ -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
+18
View File
@@ -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