From 9ca83d66b85ae8a018856182cf0a95539e91f79e Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Wed, 17 Jun 2015 19:21:45 +0300 Subject: [PATCH] Handle parent directory checks on Windows --- src/Path.hs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Path.hs b/src/Path.hs index 9df53af..7565f11 100644 --- a/src/Path.hs +++ b/src/Path.hs @@ -83,9 +83,7 @@ parseAbsDir filepath = if FilePath.isAbsolute filepath && not (null (normalizeDir filepath)) && not (isPrefixOf "~/" filepath) && - not (isSuffixOf "/.." filepath) && - not (isInfixOf "/../" filepath) && - not (isPrefixOf "../" filepath) + not (hasParentDir filepath) && then return (Path (normalizeDir filepath)) else throwM (InvalidAbsDir filepath) @@ -100,9 +98,7 @@ parseRelDir filepath = if not (FilePath.isAbsolute filepath) && not (null filepath) && not (isPrefixOf "~/" filepath) && - not (isPrefixOf "../" filepath) && - not (isSuffixOf "/.." filepath) && - not (isInfixOf "/../" filepath) && + not (hasParentDir filepath) && not (null (normalizeDir filepath)) && filepath /= ".." then return (Path (normalizeDir filepath)) @@ -118,9 +114,7 @@ parseAbsFile filepath = if FilePath.isAbsolute filepath && not (FilePath.hasTrailingPathSeparator filepath) && not (isPrefixOf "~/" filepath) && - not (isPrefixOf "../" filepath) && - not (isSuffixOf "/.." filepath) && - not (isInfixOf "/../" filepath) && + not (hasParentDir filepath) && not (null (normalizeFile filepath)) && filepath /= ".." then return (Path (normalizeFile filepath)) @@ -137,15 +131,25 @@ parseRelFile filepath = FilePath.hasTrailingPathSeparator filepath) && not (null filepath) && not (isPrefixOf "~/" filepath) && - not (isPrefixOf "../" filepath) && - not (isInfixOf "/../" filepath) && - not (isSuffixOf "/.." filepath) && - not (isInfixOf "/../" filepath) && + not (hasParentDir filepath) && not (null (normalizeFile filepath)) && filepath /= ".." then return (Path (normalizeFile filepath)) else throwM (InvalidRelFile filepath) +-- | Helper function: check if the filepath has any parent directories in it. +-- This handles the logic of checking for different path separators on Windows. +hasParentDir :: FilePath -> Bool +hasParentDir filepath' = + (isSuffixOf "/.." filepath) || + (isInfixOf "/../" filepath) || + (isPrefixOf "../" filepath) + where + filepath = + case FilePath.pathSeparator of + '/' -> filepath' + x -> map (\y -> if x == y then '/' else y) filepath' + -------------------------------------------------------------------------------- -- Constructors