Skip to content

Commit 00667c7

Browse files
committed
Add checking for doc tarball uploads
This does not fix issue #56 however it does help because it will give people decent feedback when the upload the doc tarball, rather than when they try to read it later!
1 parent 5f311f1 commit 00667c7

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

Distribution/Server/Features/Documentation.hs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ import Distribution.Server.Framework.BlobStorage (BlobId)
1818
import qualified Distribution.Server.Framework.BlobStorage as BlobStorage
1919
import qualified Distribution.Server.Util.ServeTarball as ServerTarball
2020
import Data.TarIndex (TarIndex)
21+
import qualified Codec.Archive.Tar as Tar
22+
import qualified Codec.Archive.Tar.Check as Tar
2123

2224
import Distribution.Text
2325
import Distribution.Package
2426
import Distribution.Version (Version(..))
2527

28+
import qualified Data.ByteString.Lazy as BSL
2629
import qualified Data.Map as Map
2730
import Data.Function (fix)
2831

@@ -202,11 +205,13 @@ documentationFeature name
202205
-- * Drop the index for the old tar-file
203206
-- * Link the new documentation to the package
204207
fileContents <- expectUncompressedTarball
205-
blob <- liftIO $ BlobStorage.add store fileContents
206-
--TODO: validate the tarball here.
207-
-- Check all files in the tarball are under the dir foo-1.0-docs/
208-
void $ updateState documentationState $ InsertDocumentation pkgid blob
209-
noContent (toResponse ())
208+
mres <- liftIO $ BlobStorage.addWith store fileContents
209+
(\content -> return (checkDocTarball pkgid content))
210+
case mres of
211+
Left err -> errBadRequest "Invalid documentation tarball" [MText err]
212+
Right ((), blobid) -> do
213+
updateState documentationState $ InsertDocumentation pkgid blobid
214+
noContent (toResponse ())
210215

211216
{-
212217
To upload documentation using curl:
@@ -257,6 +262,18 @@ documentationFeature name
257262
index <- liftIO $ cachedTarIndex blob
258263
func pkgid blob index
259264

265+
-- Check the tar file is well formed and all files are within foo-1.0-docs/
266+
checkDocTarball :: PackageId -> BSL.ByteString -> Either String ()
267+
checkDocTarball pkgid =
268+
checkEntries
269+
. fmapErr (either id show) . Tar.checkTarbomb (display pkgid ++ "-docs")
270+
. fmapErr (either id show) . Tar.checkSecurity
271+
. fmapErr (either id show) . Tar.checkPortability
272+
. fmapErr show . Tar.read
273+
where
274+
fmapErr f = Tar.foldEntries Tar.Next Tar.Done (Tar.Fail . f)
275+
checkEntries = Tar.foldEntries (\_ remainder -> remainder) (Right ()) Left
276+
260277
{------------------------------------------------------------------------------
261278
Auxiliary
262279
------------------------------------------------------------------------------}

0 commit comments

Comments
 (0)