|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +module Tests.MediaBag (tests) where |
| 3 | + |
| 4 | +import Test.Tasty |
| 5 | +import Test.Tasty.HUnit |
| 6 | +-- import Tests.Helpers |
| 7 | +import Text.Pandoc.Class.IO (extractMedia) |
| 8 | +import Text.Pandoc.Class (fillMediaBag, runIOorExplode) |
| 9 | +import System.IO.Temp (withTempDirectory) |
| 10 | +import System.FilePath |
| 11 | +import Text.Pandoc.Builder as B |
| 12 | +import System.Directory (doesFileExist, copyFile, setCurrentDirectory, getCurrentDirectory) |
| 13 | + |
| 14 | +tests :: [TestTree] |
| 15 | +tests = [ |
| 16 | + testCase "test fillMediaBag & extractMedia" $ |
| 17 | + withTempDirectory "." "extractMediaTest" $ \tmpdir -> do |
| 18 | + olddir <- getCurrentDirectory |
| 19 | + setCurrentDirectory tmpdir |
| 20 | + copyFile "../../test/lalune.jpg" "moon.jpg" |
| 21 | + let d = B.doc $ |
| 22 | + B.para (B.image "../../test/lalune.jpg" "" mempty) <> |
| 23 | + B.para (B.image "moon.jpg" "" mempty) <> |
| 24 | + B.para (B.image "data://image/png;base64,cHJpbnQgImhlbGxvIgo=;.lua+%2f%2e%2e%2f%2e%2e%2fa%2elua" "" mempty) <> |
| 25 | + B.para (B.image "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" "" mempty) |
| 26 | + runIOorExplode $ do |
| 27 | + fillMediaBag d |
| 28 | + extractMedia "foo" d |
| 29 | + exists1 <- doesFileExist ("foo" </> "moon.jpg") |
| 30 | + assertBool "file in directory extract with original name" exists1 |
| 31 | + exists2 <- doesFileExist ("foo" </> "f9d88c3dbe18f6a7f5670e994a947d51216cdf0e.jpg") |
| 32 | + assertBool "file above directory extracted with hashed name" exists2 |
| 33 | + exists3 <- doesFileExist ("foo" </> "2a0eaa89f43fada3e6c577beea4f2f8f53ab6a1d.lua") |
| 34 | + exists4 <- doesFileExist "a.lua" |
| 35 | + assertBool "data uri with malicious payload does not get written to arbitrary location" |
| 36 | + (exists3 && not exists4) |
| 37 | + exists5 <- doesFileExist ("foo" </> "d5fceb6532643d0d84ffe09c40c481ecdf59e15a.gif") |
| 38 | + assertBool "data uri with gif is properly decoded" exists5 |
| 39 | + setCurrentDirectory olddir |
| 40 | + ] |
0 commit comments