diff --git a/src/Simplex/FileTransfer/Description.hs b/src/Simplex/FileTransfer/Description.hs index bfc1896fd..db2423a33 100644 --- a/src/Simplex/FileTransfer/Description.hs +++ b/src/Simplex/FileTransfer/Description.hs @@ -280,13 +280,13 @@ fileDescriptionURI :: ValidFileDescription 'FRecipient -> FileDescriptionURI fileDescriptionURI vfd = FileDescriptionURI SSSimplex vfd mempty instance StrEncoding FileDescriptionURI where - strEncode FileDescriptionURI {scheme, description, clientData} = mconcat [strEncode scheme, "/file", "#/?", queryStr] + strEncode FileDescriptionURI {scheme, description, clientData} = mconcat [strEncode scheme, "file", "#/?", queryStr] where queryStr = strEncode $ QSP QEscape qs qs = ("desc", strEncode description) : maybe [] (\cd -> [("data", encodeUtf8 cd)]) clientData strP = do scheme <- strP - _ <- "/file" <* optional (A.char '/') <* "#/?" + _ <- "file" <* optional (A.char '/') <* "#/?" query <- strP description <- queryParam "desc" query let clientData = safeDecodeUtf8 <$> queryParamStr "data" query diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index 1d28db006..2c1dccca3 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -1234,7 +1234,7 @@ instance ConnectionModeI m => StrEncoding (ConnectionRequestUri m) where where crEncode :: ByteString -> ConnReqUriData -> (Maybe (RcvE2ERatchetParamsUri 'C.X448), Maybe RatchetKeyId) -> ByteString crEncode crMode ConnReqUriData {crScheme, crAgentVRange, crSmpQueues, crClientData} (e2eParams, rk) = - strEncode crScheme <> "/" <> crMode <> "#/?" <> queryStr + strEncode crScheme <> crMode <> "#/?" <> queryStr where queryStr = strEncode . QSP QEscape $ @@ -1289,7 +1289,7 @@ instance StrEncoding AConnectionRequestUri where connReqUriP :: Maybe ServiceScheme -> Parser AConnectionRequestUri connReqUriP overrideScheme = do crScheme <- (`fromMaybe` overrideScheme) <$> strP -- always parse, but use the passed one if any - crMode <- A.char '/' *> crModeP <* optional (A.char '/') <* "#/?" + crMode <- crModeP <* optional (A.char '/') <* "#/?" query <- strP aVRange <- queryParam "v" query crSmpQueues <- queryParamParser queuesP "smp" query @@ -1737,11 +1737,11 @@ instance ConnectionModeI m => StrEncoding (ConnShortLink m) where CSLContact sch ct srv (LinkKey k) -> slEncode sch srv (toLower $ ctTypeChar ct) "" k where slEncode sch (SMPServer (h :| hs) port (C.KeyHash kh)) linkType lnkId k = - B.concat [authority, "/", B.singleton linkType, "#", lnkIdStr, B64.encodeUnpadded k, queryStr] + B.concat [strEncode baseUri, B.singleton linkType, "#", lnkIdStr, B64.encodeUnpadded k, queryStr] where - (authority, paramHosts) = case sch of - SLSSimplex -> ("simplex:", h : hs) - SLSServer -> ("https://" <> strEncode h, hs) + (baseUri, paramHosts) = case sch of + SLSSimplex -> (SSSimplex, h : hs) + SLSServer -> (SSAppServer (SrvLoc h ""), hs) lnkIdStr = if B.null lnkId then "" else B64.encodeUnpadded lnkId <> "/" queryStr = if B.null query then "" else "?" <> query query = @@ -1756,7 +1756,7 @@ instance StrEncoding AConnShortLink where strEncode (ACSL _ l) = strEncode l {-# INLINE strEncode #-} strP = do - (sch, h_) <- authorityP <* A.char '/' + (sch, h_) <- baseUriP ct_ <- contactTypeP <* optional (A.char '/') <* A.char '#' case ct_ of Nothing -> do @@ -1769,10 +1769,11 @@ instance StrEncoding AConnShortLink where srv <- serverQueryP h_ pure $ ACSL SCMContact $ CSLContact sch ct srv (LinkKey k) where - authorityP = - "simplex:" $> (SLSSimplex, Nothing) - <|> "https://" *> ((SLSServer,) . Just <$> strP) - <|> fail "bad short link scheme" + baseUriP = + strP >>= \case + SSSimplex -> pure (SLSSimplex, Nothing) + SSAppServer (SrvLoc h "") -> pure (SLSServer, Just h) + SSAppServer _ -> fail "bad short link scheme" contactTypeP = do Just <$> (A.anyChar >>= ctTypeP . toUpper) <|> A.char 'i' $> Nothing diff --git a/src/Simplex/Messaging/ServiceScheme.hs b/src/Simplex/Messaging/ServiceScheme.hs index 904ab971e..8a7af7b69 100644 --- a/src/Simplex/Messaging/ServiceScheme.hs +++ b/src/Simplex/Messaging/ServiceScheme.hs @@ -7,7 +7,7 @@ module Simplex.Messaging.ServiceScheme simplexChat, ) where -import Control.Applicative ((<|>)) +import Control.Applicative (optional, (<|>)) import qualified Data.Attoparsec.ByteString.Char8 as A import qualified Data.ByteString.Char8 as B import Data.Functor (($>)) @@ -20,11 +20,11 @@ data ServiceScheme = SSSimplex | SSAppServer SrvLoc instance StrEncoding ServiceScheme where strEncode = \case - SSSimplex -> "simplex:" - SSAppServer srv -> "https://" <> strEncode srv + SSSimplex -> "simplex:/" + SSAppServer srv -> "https://" <> strEncode srv <> "/" strP = - "simplex:" $> SSSimplex - <|> "https://" *> (SSAppServer <$> strP) + "simplex:" *> optional (A.char '/') $> SSSimplex + <|> "https://" *> (SSAppServer <$> strP) <* A.char '/' data SrvLoc = SrvLoc TransportHost ServiceName deriving (Eq, Ord, Show) diff --git a/src/Simplex/Messaging/SimplexName.hs b/src/Simplex/Messaging/SimplexName.hs index 690781196..5eb7b84dd 100644 --- a/src/Simplex/Messaging/SimplexName.hs +++ b/src/Simplex/Messaging/SimplexName.hs @@ -36,6 +36,7 @@ import Simplex.Messaging.Agent.Store.DB (Binary (..), FromField (..), ToField (. import Simplex.Messaging.Encoding (Encoding (..)) import Simplex.Messaging.Encoding.String import Simplex.Messaging.Parsers (defaultJSON, dropPrefix, enumJSON) +import Simplex.Messaging.ServiceScheme (ServiceScheme (..)) import Simplex.Messaging.Util (eitherToMaybe, safeDecodeUtf8, (<$?>)) data SimplexNameInfo = SimplexNameInfo @@ -110,8 +111,12 @@ boundedNonSpace = do instance StrEncoding SimplexNameInfo where strEncode SimplexNameInfo {nameType, nameDomain} = strEncode nameType <> strEncode nameDomain - strP = optional "simplex:/name" *> ((strP >>= infoP) <|> infoP NTPublicGroup) + strP = optional nameSchemeP *> (strP >>= infoP) where + nameSchemeP = + strP >>= \case + SSSimplex -> "name" + SSAppServer _ -> fail "bad name scheme" infoP NTPublicGroup = SimplexNameInfo NTPublicGroup <$> (strP <|> bareName) infoP NTContact = SimplexNameInfo NTContact <$> strP bareName = parseBare . safeDecodeUtf8 <$?> boundedNonSpace diff --git a/tests/AgentTests/ConnectionRequestTests.hs b/tests/AgentTests/ConnectionRequestTests.hs index 40ca12cf9..98b1a7aa3 100644 --- a/tests/AgentTests/ConnectionRequestTests.hs +++ b/tests/AgentTests/ConnectionRequestTests.hs @@ -327,6 +327,17 @@ connectionRequestTests = CSLContact SLSSimplex CCTContact shortSrv (LinkKey "0123456789abcdef0123456789abcdef") #==# "simplex:/a#MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY?h=smp.simplex.im" CSLInvitation SLSSimplex srv (EntityId "0123456789abcdef01234567") (LinkKey "0123456789abcdef0123456789abcdef") #==# "simplex:/i#MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3/MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY?h=smp.simplex.im%2Cjjbyvoemxysm7qxap7m5d5m35jzv5qq6gnlv7s4rsn7tdwwmuqciwpid.onion&p=5223&c=1234-w" CSLInvitation SLSSimplex shortSrv (EntityId "0123456789abcdef01234567") (LinkKey "0123456789abcdef0123456789abcdef") #==# "simplex:/i#MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3/MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY?h=smp.simplex.im" + it "should parse simplex: links without the slash after the scheme" $ do + let channel = CSLContact SLSSimplex CCTChannel shortSrv (LinkKey "0123456789abcdef0123456789abcdef") + name = SimplexNameInfo NTPublicGroup (SimplexDomain TLDSimplex "alice" []) + contactAddress #== ("simplex:contact#/?v=6-8&smp=" <> url queueStr) + connectionRequest #== ("simplex:invitation#/?v=6-8&smp=" <> url queueStr <> "&e2e=" <> testE2ERatchetParamsStrUri) + channel #==# "simplex:/c#MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY?h=smp.simplex.im" + channel #== "simplex:c#MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY?h=smp.simplex.im" + name #== "simplex:/name#alice.simplex" + name #== "simplex:name#alice.simplex" + (strDecode "https://simplex.chat/name#alice.simplex" :: Either String SimplexNameInfo) `shouldSatisfy` isLeft + (strDecode "simplex:namealice" :: Either String SimplexNameInfo) `shouldSatisfy` isLeft it "should shorten / restore short links" $ do let contact = CSLContact SLSServer CCTContact shortenShortLink [srv] (contact srv (LinkKey "0123456789abcdef0123456789abcdef")) diff --git a/tests/CoreTests/EncodingTests.hs b/tests/CoreTests/EncodingTests.hs index 4108bef2a..2e489bc30 100644 --- a/tests/CoreTests/EncodingTests.hs +++ b/tests/CoreTests/EncodingTests.hs @@ -82,12 +82,12 @@ encodingTests = modifyMaxSuccess (const 1000) $ do shouldReject @TransportHost "[smp.simplex.im]" describe "Encoding service locations" $ do it "should parse bracketed IPv6 host with port" $ do - strDecode @ServiceScheme "https://[2001:db8::1]:8443" + strDecode @ServiceScheme "https://[2001:db8::1]:8443/" `shouldBe` Right (SSAppServer $ SrvLoc "2001:db8::1" "8443") strEncode (SSAppServer $ SrvLoc "2001:db8::1" "8443") - `shouldBe` "https://[2001:db8::1]:8443" + `shouldBe` "https://[2001:db8::1]:8443/" it "should reject bracketed non-IPv6 host" $ - shouldReject @ServiceScheme "https://[simplex.chat]:8443" + shouldReject @ServiceScheme "https://[simplex.chat]:8443/" describe "Encoding protocol servers" $ do it "should parse bracketed IPv6 server host with port" $ case strDecode @XFTPServer "xftp://1234-w==@[2001:db8::1]:443" of diff --git a/tests/FileDescriptionTests.hs b/tests/FileDescriptionTests.hs index 21c62c476..10cfa3020 100644 --- a/tests/FileDescriptionTests.hs +++ b/tests/FileDescriptionTests.hs @@ -47,6 +47,7 @@ fileDescriptionTests = do describe "file description URIs" $ do it "round trip file description URI" testFileDescriptionURI it "round trip file description URI with extra JSON" testFileDescriptionURIExtras + it "parse file description URI without the slash after the scheme" testFileDescriptionURIWithoutSlash fileDescPath :: FilePath fileDescPath = "tests/fixtures/file_description.yaml" @@ -250,6 +251,14 @@ testFileDescriptionURIExtras = do let descr = FileDescriptionURI SSSimplex vfd $ Just "{\"something\":\"extra\",\"more\":true}" strDecode (strEncode descr) `shouldBe` Right descr +testFileDescriptionURIWithoutSlash :: IO () +testFileDescriptionURIWithoutSlash = do + vfd <- either fail pure $ validateFileDescription fileDesc + let descr = FileDescriptionURI SSSimplex vfd mempty + (prefix, rest) = B.splitAt 9 $ strEncode descr + prefix `shouldBe` "simplex:/" + strDecode ("simplex:" <> rest) `shouldBe` Right descr + testHashChunks :: HasTestEnv => IO () testHashChunks = do let sizes = [0, 65535, 65536, 100000, 200000]