mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-11-22 19:58:39 +00:00
xftp: limit chunk size in received file descriptions (#2001)
This commit is contained in:
1 parent
9c4a814fda
commit
32f61fd104
4 files changed
+17
-12
No files matched your search
@@ -226,12 +226,14 @@ validateFileDescription :: FileDescription p -> Either String (ValidFileDescript
|
||||
validateFileDescription fd@FileDescription {size, chunks}
|
||||
| chunkNos /= [1 .. length chunks] = Left "chunk numbers are not sequential"
|
||||
| chunksSize chunks /= unFileSize size = Left "chunks total size is different than file size"
|
||||
| any incorrectChunkSize chunks = Left "incorrect chunk size"
|
||||
| any (> 16) replicaCounts = Left "too many chunk replicas"
|
||||
| sum replicaCounts > 131072 = Left "too many file replicas"
|
||||
| otherwise = Right $ ValidFD fd
|
||||
where
|
||||
chunkNos = map (\FileChunk {chunkNo} -> chunkNo) chunks
|
||||
chunksSize = foldl' (\(s :: Int64) FileChunk {chunkSize} -> s + fromIntegral (unFileSize chunkSize)) 0
|
||||
incorrectChunkSize FileChunk {chunkSize} = unFileSize chunkSize `notElem` serverChunkSizes
|
||||
replicaCounts = map (\FileChunk {replicas} -> length replicas) chunks
|
||||
|
||||
sharedDescriptionHash :: FileDescription p -> ByteString
|
||||
|
||||
@@ -10,7 +10,7 @@ import Control.Exception (bracket_)
|
||||
import qualified Data.ByteString.Char8 as B
|
||||
import Data.Either (isRight)
|
||||
import qualified Data.Yaml as Y
|
||||
import Simplex.FileTransfer.Chunks (chunkSize3)
|
||||
import Simplex.FileTransfer.Chunks (chunkSize3, serverChunkSizes)
|
||||
import Simplex.FileTransfer.Client (prepareChunkSizes)
|
||||
import Simplex.FileTransfer.Description
|
||||
import Simplex.FileTransfer.Protocol
|
||||
@@ -29,7 +29,7 @@ fileDescriptionTests = do
|
||||
it "serialize YAML file description" testSerializeYAMLFileDescription
|
||||
it "parse file description" testParseFileDescription
|
||||
it "reject file description with YAML aliases" testRejectYAMLAliases
|
||||
it "limit replicas per chunk and per file" testFileDescriptionLimits
|
||||
it "limit chunk sizes and replicas per chunk and per file" testFileDescriptionLimits
|
||||
it "serialize file description" testSerializeFileDescription
|
||||
describe "file description URIs" $ do
|
||||
it "round trip file description URI" testFileDescriptionURI
|
||||
@@ -51,7 +51,7 @@ fileDesc :: FileDescription 'FRecipient
|
||||
fileDesc =
|
||||
FileDescription
|
||||
{ party = SFRecipient,
|
||||
size = FileSize $ mb 26,
|
||||
size = FileSize $ mb 13,
|
||||
digest = FileDigest "abc",
|
||||
key = testSbKey,
|
||||
nonce = testCbNonce,
|
||||
@@ -87,7 +87,7 @@ fileDesc =
|
||||
FileChunk
|
||||
{ chunkNo = 4,
|
||||
digest = chunkDigest,
|
||||
chunkSize = FileSize $ mb 2,
|
||||
chunkSize = FileSize $ mb 1,
|
||||
replicas =
|
||||
[ FileChunkReplica {server = "xftp://abc=@example2.com", replicaId, replicaKey},
|
||||
FileChunkReplica {server = "xftp://abc=@example3.com", replicaId, replicaKey}
|
||||
@@ -97,7 +97,7 @@ fileDesc =
|
||||
redirect = Nothing
|
||||
}
|
||||
where
|
||||
defaultChunkSize = FileSize $ mb 8
|
||||
defaultChunkSize = FileSize $ mb 4
|
||||
replicaId = ChunkReplicaId $ EntityId "abc"
|
||||
replicaKey = C.APrivateAuthKey C.SEd25519 "MC4CAQAwBQYDK2VwBCIEIDfEfevydXXfKajz3sRkcQ7RPvfWUPoq6pu1TYHV1DEe"
|
||||
chunkDigest = FileDigest "ghi"
|
||||
@@ -106,8 +106,8 @@ yamlFileDesc :: YAMLFileDescription
|
||||
yamlFileDesc =
|
||||
YAMLFileDescription
|
||||
{ party = FRecipient,
|
||||
size = "26mb",
|
||||
chunkSize = "8mb",
|
||||
size = "13mb",
|
||||
chunkSize = "4mb",
|
||||
digest = FileDigest "abc",
|
||||
key = testSbKey,
|
||||
nonce = testCbNonce,
|
||||
@@ -123,7 +123,7 @@ yamlFileDesc =
|
||||
{ server = "xftp://abc=@example2.com",
|
||||
chunks =
|
||||
[ "2:YWJj:MC4CAQAwBQYDK2VwBCIEIDfEfevydXXfKajz3sRkcQ7RPvfWUPoq6pu1TYHV1DEe:Z2hp",
|
||||
"4:YWJj:MC4CAQAwBQYDK2VwBCIEIDfEfevydXXfKajz3sRkcQ7RPvfWUPoq6pu1TYHV1DEe:Z2hp:2mb"
|
||||
"4:YWJj:MC4CAQAwBQYDK2VwBCIEIDfEfevydXXfKajz3sRkcQ7RPvfWUPoq6pu1TYHV1DEe:Z2hp:1mb"
|
||||
]
|
||||
},
|
||||
YAMLServerReplicas
|
||||
@@ -181,6 +181,9 @@ testFileDescriptionLimits = do
|
||||
validateFileDescription (descriptionOf split64GB 4) `shouldSatisfy` isRight
|
||||
validateFileDescription (descriptionOf (replicate 8192 chunkSize3) 16) `shouldSatisfy` isRight
|
||||
validateFileDescription (descriptionOf (replicate 8193 chunkSize3) 16) `shouldBe` Left "too many file replicas"
|
||||
validateFileDescription (descriptionOf serverChunkSizes 1) `shouldSatisfy` isRight
|
||||
validateFileDescription (descriptionOf [chunkSize3, mb 2] 1) `shouldBe` Left "incorrect chunk size"
|
||||
validateFileDescription (descriptionOf [chunkSize3, mb 8] 1) `shouldBe` Left "incorrect chunk size"
|
||||
where
|
||||
FileDescription {chunks = fixtureChunks} = fileDesc
|
||||
fixtureReplicas = concatMap (\FileChunk {replicas} -> replicas) fixtureChunks
|
||||
|
||||
@@ -2137,7 +2137,7 @@ tsDescriptionTests = describe "protocol/description" $ do
|
||||
<> "fd.redirect === null ? 'null' : 'redirect'"
|
||||
<> "].join(',');"
|
||||
<> jsOut "new TextEncoder().encode(r)"
|
||||
tsResult `shouldBe` "recipient,27262976,8388608,4,2,2097152,null"
|
||||
tsResult `shouldBe` "recipient,13631488,4194304,4,2,1048576,null"
|
||||
|
||||
it "encode with redirect round-trips" $ do
|
||||
tsResult <-
|
||||
|
||||
Vendored
+3
-3
@@ -1,4 +1,4 @@
|
||||
chunkSize: 8mb
|
||||
chunkSize: 4mb
|
||||
digest: YWJj
|
||||
key: 00n8p1tJq5E-SGnHcYTOrS4A9I07gTA_WFD6MTFFFOY=
|
||||
nonce: dPSF-wrQpDiK_K6sYv0BDBZ9S4dg-jmu
|
||||
@@ -10,7 +10,7 @@ replicas:
|
||||
server: xftp://abc=@example1.com
|
||||
- chunks:
|
||||
- 2:YWJj:MC4CAQAwBQYDK2VwBCIEIDfEfevydXXfKajz3sRkcQ7RPvfWUPoq6pu1TYHV1DEe:Z2hp
|
||||
- 4:YWJj:MC4CAQAwBQYDK2VwBCIEIDfEfevydXXfKajz3sRkcQ7RPvfWUPoq6pu1TYHV1DEe:Z2hp:2mb
|
||||
- 4:YWJj:MC4CAQAwBQYDK2VwBCIEIDfEfevydXXfKajz3sRkcQ7RPvfWUPoq6pu1TYHV1DEe:Z2hp:1mb
|
||||
server: xftp://abc=@example2.com
|
||||
- chunks:
|
||||
- 1:YWJj:MC4CAQAwBQYDK2VwBCIEIDfEfevydXXfKajz3sRkcQ7RPvfWUPoq6pu1TYHV1DEe
|
||||
@@ -20,4 +20,4 @@ replicas:
|
||||
- 2:YWJj:MC4CAQAwBQYDK2VwBCIEIDfEfevydXXfKajz3sRkcQ7RPvfWUPoq6pu1TYHV1DEe
|
||||
- 3:YWJj:MC4CAQAwBQYDK2VwBCIEIDfEfevydXXfKajz3sRkcQ7RPvfWUPoq6pu1TYHV1DEe
|
||||
server: xftp://abc=@example4.com
|
||||
size: 26mb
|
||||
size: 13mb
|
||||
Reference in new issue
Block a user