Skip to content

Commit 8588955

Browse files
committed
Document packWith and unpackWith
1 parent e2b3bb7 commit 8588955

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

Codec/Archive/Tar/Check/Internal.hs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ import qualified System.FilePath.Posix as FilePath.Posix
6767
-- link target. A failure in any entry terminates the sequence of entries with
6868
-- an error.
6969
--
70-
checkSecurity :: MonadThrow m
71-
=> Maybe LinkTarget -- ^ OtherEntryType 'K' discovered before the actual entry
72-
-> Maybe FilePath -- ^ OtherEntryType 'L' discovered before the actual entry
73-
-> Entry
74-
-> m ()
70+
checkSecurity :: CheckSecurityCallback
7571
checkSecurity mLink mPath e = do
7672
let path = fromMaybe (entryPath e) mPath
7773
check path
@@ -120,6 +116,7 @@ data FileNameError
120116
= InvalidFileName FilePath
121117
| AbsoluteFileName FilePath
122118
| UnsafeLinkTarget FilePath
119+
-- ^ @since 0.6.0.0
123120
deriving (Typeable)
124121

125122
instance Show FileNameError where
@@ -150,12 +147,7 @@ showFileNameError mb_plat err = case err of
150147
-- Note: This check must be used in conjunction with 'checkSecurity'
151148
-- (or 'checkPortability').
152149
--
153-
checkTarbomb :: MonadThrow m
154-
=> FilePath
155-
-> Maybe LinkTarget -- ^ OtherEntryType 'K' discovered before the actual entry
156-
-> Maybe FilePath -- ^ OtherEntryType 'L' discovered before the actual entry
157-
-> Entry
158-
-> m ()
150+
checkTarbomb :: FilePath -> CheckSecurityCallback
159151
checkTarbomb expectedTopDir _mLink _mPath entry = do
160152
case entryContent entry of
161153
OtherEntryType 'g' _ _ -> pure () --PAX global header
@@ -205,11 +197,7 @@ instance Show TarBombError where
205197
-- includes characters that are valid in both systems and the \'/\' vs \'\\\'
206198
-- directory separator conventions.
207199
--
208-
checkPortability :: MonadThrow m
209-
=> Maybe LinkTarget -- ^ OtherEntryType 'K' discovered before the actual entry
210-
-> Maybe FilePath -- ^ OtherEntryType 'L' discovered before the actual entry
211-
-> Entry
212-
-> m ()
200+
checkPortability :: CheckSecurityCallback
213201
checkPortability _mLink _mPath entry
214202
| entryFormat entry `elem` [V7Format, GnuFormat]
215203
= throwM $ NonPortableFormat (entryFormat entry)

Codec/Archive/Tar/Pack.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ pack :: FilePath -- ^ Base directory
6868
-> IO [Entry]
6969
pack = packWith checkSecurity
7070

71+
-- | Like 'pack', but does not perform any sanity/security checks on the input.
72+
-- You can do so yourself, e.g.: @packWith@ 'checkSecurity' @dir@ @files@.
73+
--
74+
-- @since 0.6.0.0
7175
packWith :: CheckSecurityCallback
7276
-> FilePath -- ^ Base directory
7377
-> [FilePath] -- ^ Files and directories to pack, relative to the base dir

Codec/Archive/Tar/Types.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,4 +635,13 @@ instance NFData e => NFData (Entries e) where
635635
rnf Done = ()
636636
rnf (Fail e) = rnf e
637637

638-
type CheckSecurityCallback = forall m. MonadThrow m => Maybe LinkTarget -> Maybe FilePath -> Entry -> m ()
638+
-- | @since 0.6.0.0
639+
type CheckSecurityCallback =
640+
forall m.
641+
MonadThrow m
642+
=> Maybe LinkTarget
643+
-- ^ OtherEntryType 'K' discovered before the actual entry
644+
-> Maybe FilePath
645+
-- ^ OtherEntryType 'L' discovered before the actual entry
646+
-> Entry
647+
-> m ()

Codec/Archive/Tar/Unpack.hs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,15 @@ import Control.Exception as Exception
7676
-- part-way.
7777
--
7878
-- On its own, this function only checks for security (using 'checkSecurity').
79-
-- You can do other checks by applying checking functions to the 'Entries' that
80-
-- you pass to this function. For example:
81-
--
82-
-- > unpack dir (checkTarbomb expectedDir entries)
83-
--
84-
-- If you care about the priority of the reported errors then you may want to
85-
-- use 'checkSecurity' before 'checkTarbomb' or other checks.
79+
-- Use 'unpackWith' if you need more checks.
8680
--
8781
unpack :: Exception e => FilePath -> Entries e -> IO ()
8882
unpack = unpackWith checkSecurity
8983

9084
-- | Like 'unpack', but does not perform any sanity/security checks on the tar entries.
91-
-- You can do so yourself, e.g.:
85+
-- You can do so yourself, e.g.: @unpackRaw@ 'checkSecurity' @dir@ @entries@.
9286
--
93-
-- > unpackRaw dir (checkPortability . checkSecurity $ entries)
87+
-- @since 0.6.0.0
9488
unpackWith :: Exception e => CheckSecurityCallback -> FilePath -> Entries e -> IO ()
9589
unpackWith secCB baseDir entries = do
9690
uEntries <- unpackEntries Nothing Nothing [] entries

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ See also http://pvp.haskell.org/faq
1717
* Ignore FAT32 errors when setting modification time
1818
* Switch to trailer parsing mode only after a full block of `NUL`
1919
* Drop deprecated `emptyIndex` and `finaliseIndex`
20+
* Extend `FileNameError` with `UnsafeLinkTarget` constructor
21+
* Add `CheckSecurityCallback`, `packWith`, `unpackWith`
2022

2123
0.5.1.1 Herbert Valerio Riedel <[email protected]> August 2019
2224

0 commit comments

Comments
 (0)