mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-11-22 17:48:24 +00:00
xftp: limit received files to the size of the sent file (#2067)
* tests: decrypt chunk files into the original file * xftp: limit received files to the size of the sent file
This commit is contained in:
1 parent
99d84f7350
commit
239290ca98
2 files changed
+33
-6
No files matched your search
@@ -77,7 +77,7 @@ decryptChunks encSize (chPath : chPaths) key nonce getDestFile = case reverse ch
|
||||
destFile@(CryptoFile path _) <- withExceptT FTCEFileIOError $ getDestFile fileName
|
||||
authOk <- CF.withFile destFile WriteMode $ \h -> liftIO $ do
|
||||
CF.hPut h ch'
|
||||
state' <- foldM (decryptChunk h) state $ reverse chPaths'
|
||||
state' <- foldM (decryptChunk h expectedLen) state $ reverse chPaths'
|
||||
decryptLastChunk h state' expectedLen
|
||||
unless authOk $ do
|
||||
removeFile path
|
||||
@@ -91,11 +91,11 @@ decryptChunks encSize (chPath : chPaths) key nonce getDestFile = case reverse ch
|
||||
(!expectedLen, ch2) <- liftEitherWith FTCECryptoError $ LC.splitLen ch1
|
||||
let len1 = LB.length ch2
|
||||
pure ((sb', len1), expectedLen, ch2)
|
||||
decryptChunk h (!sb, !len) chPth = do
|
||||
decryptChunk h expectedLen (!sb, !len) chPth = do
|
||||
ch <- LB.readFile chPth
|
||||
let len' = len + LB.length ch
|
||||
(ch', sb') = LC.sbDecryptChunkLazy sb ch
|
||||
CF.hPut h ch'
|
||||
CF.hPut h $ LB.take (expectedLen - len) ch'
|
||||
pure (sb', len')
|
||||
decryptLastChunk h (!sb, !len) expectedLen = do
|
||||
ch <- LB.readFile lastPath
|
||||
|
||||
@@ -7,22 +7,27 @@
|
||||
module FileDescriptionTests where
|
||||
|
||||
import Control.Exception (bracket_)
|
||||
import Control.Monad (zipWithM_)
|
||||
import Control.Monad (forM_, zipWithM_)
|
||||
import Control.Monad.Trans.Except (runExceptT)
|
||||
import qualified Data.ByteString.Char8 as B
|
||||
import qualified Data.ByteString.Lazy.Char8 as LB
|
||||
import Data.Either (isRight)
|
||||
import qualified Data.Yaml as Y
|
||||
import Simplex.FileTransfer.Chunks (chunkSize0, chunkSize3, serverChunkSizes)
|
||||
import Simplex.FileTransfer.Client (prepareChunkSizes)
|
||||
import Simplex.FileTransfer.Crypto (sha512HashChunks)
|
||||
import Simplex.FileTransfer.Crypto (decryptChunks, encryptFile, sha512HashChunks)
|
||||
import Simplex.FileTransfer.Description
|
||||
import Simplex.FileTransfer.Protocol
|
||||
import Simplex.FileTransfer.Types (FileHeader (..), authTagSize)
|
||||
import qualified Simplex.Messaging.Crypto as C
|
||||
import qualified Simplex.Messaging.Crypto.File as CF
|
||||
import qualified Simplex.Messaging.Crypto.Lazy as LC
|
||||
import Simplex.Messaging.Encoding (smpEncode)
|
||||
import Simplex.Messaging.Encoding.String (StrEncoding (..))
|
||||
import Simplex.Messaging.Protocol (EntityId (..))
|
||||
import Simplex.Messaging.ServiceScheme (ServiceScheme (..))
|
||||
import System.Directory (removeFile)
|
||||
import System.Directory (getFileSize, removeFile)
|
||||
import System.IO (IOMode (..), withFile)
|
||||
import Test.Hspec hiding (fit, it)
|
||||
import Util
|
||||
|
||||
@@ -38,6 +43,7 @@ fileDescriptionTests = do
|
||||
it "limit chunk sizes, replicas and redirect description size" testFileDescriptionLimits
|
||||
it "serialize file description" testSerializeFileDescription
|
||||
it "hash chunk files one at a time" testHashChunks
|
||||
it "decrypt chunk files into the original file" testDecryptChunks
|
||||
describe "file description URIs" $ do
|
||||
it "round trip file description URI" testFileDescriptionURI
|
||||
it "round trip file description URI with extra JSON" testFileDescriptionURIExtras
|
||||
@@ -253,6 +259,27 @@ testHashChunks = do
|
||||
digest <- sha512HashChunks paths
|
||||
digest `shouldBe` LC.sha512Hash (LB.fromStrict $ B.concat chunks)
|
||||
|
||||
testDecryptChunks :: HasTestEnv => IO ()
|
||||
testDecryptChunks =
|
||||
forM_ ([kb 128 .. kb 128 + 16] <> [mb 2 + 1, mb 2 + kb 512 + 15]) $ \payloadSize -> do
|
||||
let fileHdr = smpEncode FileHeader {fileName = "testfile", fileExtra = Nothing}
|
||||
fileSize' = payloadSize - fileSizeLen - authTagSize
|
||||
content = B.replicate (fromIntegral fileSize' - B.length fileHdr) 'a'
|
||||
chunkSizes = prepareChunkSizes payloadSize
|
||||
encSize = sum $ map fromIntegral chunkSizes
|
||||
chunkPaths = map (testPath . ("chunk" <>) . show) [1 .. length chunkSizes]
|
||||
B.writeFile srcPath content
|
||||
runExceptT (encryptFile (CF.plain srcPath) fileHdr testSbKey testCbNonce fileSize' encSize encPath) `shouldReturn` Right ()
|
||||
withFile encPath ReadMode $ \h ->
|
||||
zipWithM_ (\path sz -> B.writeFile path =<< B.hGet h (fromIntegral sz)) chunkPaths chunkSizes
|
||||
runExceptT (decryptChunks encSize chunkPaths testSbKey testCbNonce $ \_ -> pure $ CF.plain dstPath) `shouldReturn` Right (CF.plain dstPath)
|
||||
getFileSize dstPath `shouldReturn` fromIntegral (B.length content)
|
||||
B.readFile dstPath `shouldReturn` content
|
||||
where
|
||||
srcPath = testPath "source"
|
||||
encPath = testPath "encrypted"
|
||||
dstPath = testPath "decrypted"
|
||||
|
||||
withRemoveTmpFile :: HasTestEnv => IO () -> IO ()
|
||||
withRemoveTmpFile =
|
||||
bracket_
|
||||
|
||||
Reference in new issue
Block a user