module Ecluse.Core.Registry.Archive.Path (checkPath, resolveArchivePath) where
import Control.Monad (foldM)
import Data.Text qualified as T
import Ecluse.Core.Registry (ParseError, parseRefusal)
checkPath :: Text -> Either ParseError [Text]
checkPath :: Text -> Either ParseError [Text]
checkPath Text
path = do
let parts :: [Text]
parts = HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"/" (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
path (Text -> Text -> Maybe Text
T.stripSuffix Text
"/" Text
path))
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text -> Int -> Ordering
T.compareLength Text
path Int
4096 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT Bool -> Bool -> Bool
|| (Char -> Bool) -> Text -> Bool
T.any (\Char
c -> Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
< Char
' ' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\\' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
':' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\DEL') Text
path) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"unsafe archive path")
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ((Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text
"", Text
".", Text
".."]) [Text]
parts) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"unsafe archive path")
[Text] -> Either ParseError [Text]
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Text]
parts
resolveArchivePath :: [Text] -> Text -> Either ParseError Text
resolveArchivePath :: [Text] -> Text -> Either ParseError Text
resolveArchivePath [Text]
base Text
target = do
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text -> Bool
T.null Text
target Bool -> Bool -> Bool
|| Text -> Text -> Bool
T.isPrefixOf Text
"/" Text
target Bool -> Bool -> Bool
|| Text -> Int -> Ordering
T.compareLength Text
target Int
4096 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT Bool -> Bool -> Bool
|| (Char -> Bool) -> Text -> Bool
T.any (\Char
c -> Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
< Char
' ' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\\' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
':' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\DEL') Text
target) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"unsafe tar link target")
parts <- [Text] -> [Text]
forall a. [a] -> [a]
reverse ([Text] -> [Text])
-> Either ParseError [Text] -> Either ParseError [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Text] -> Text -> Either ParseError [Text])
-> [Text] -> [Text] -> Either ParseError [Text]
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM [Text] -> Text -> Either ParseError [Text]
forall {a}. (Eq a, IsString a) => [a] -> a -> Either ParseError [a]
step ([Text] -> [Text]
forall a. [a] -> [a]
reverse [Text]
base) (HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"/" Text
target)
void (checkPath (T.intercalate "/" parts))
pure (T.copy (T.intercalate "/" parts))
where
step :: [a] -> a -> Either ParseError [a]
step [a]
held a
"" = [a] -> Either ParseError [a]
forall a b. b -> Either a b
Right [a]
held
step [a]
held a
"." = [a] -> Either ParseError [a]
forall a b. b -> Either a b
Right [a]
held
step [] a
".." = Text -> Either ParseError [a]
forall a. Text -> Either ParseError a
parseRefusal Text
"tar link escapes its archive"
step (a
_ : [a]
rest) a
".." = [a] -> Either ParseError [a]
forall a b. b -> Either a b
Right [a]
rest
step [a]
held a
part = [a] -> Either ParseError [a]
forall a b. b -> Either a b
Right (a
part a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
held)