Skip to content

Commit

Permalink
add, web: disallow unsafe trailing dot paths on windows (fix #1056)
Browse files Browse the repository at this point in the history
On Windows, ensureJournalFileExists now rejects file paths
containing any problematic trailing dots, to prevent data loss.
This affects the add command and hledger-web's add form.
  • Loading branch information
simonmichael committed Jun 26, 2019
1 parent 2e4f047 commit 014db15
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions hledger-lib/Hledger/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import System.Directory (doesFileExist, getHomeDirectory)
import System.Environment (getEnv)
import System.Exit (exitFailure)
import System.FilePath
import System.Info (os)
import System.IO
import Text.Printf

Expand Down Expand Up @@ -128,15 +129,29 @@ requireJournalFileExists f = do
exitFailure

-- | Ensure there is a journal file at the given path, creating an empty one if needed.
-- On Windows, also ensure that the path contains no trailing dots
-- which could cause data loss (see 'isWindowsUnsafeDotPath').
ensureJournalFileExists :: FilePath -> IO ()
ensureJournalFileExists f = do
when (os/="mingw32" && isWindowsUnsafeDotPath f) $ do
hPrintf stderr "Part of file path %s\n ends with a dot, which is unsafe on Windows; please use a different path.\n" (show f)
exitFailure
exists <- doesFileExist f
when (not exists) $ do
hPrintf stderr "Creating hledger journal file %s.\n" f
-- note Hledger.Utils.UTF8.* do no line ending conversion on windows,
-- we currently require unix line endings on all platforms.
newJournalContent >>= writeFile f

-- | Does any part of this path contain non-. characters and end with a . ?
-- Such paths are not safe to use on Windows (cf #1056).
isWindowsUnsafeDotPath :: FilePath -> Bool
isWindowsUnsafeDotPath =
not . null .
filter (not . all (=='.')) .
filter ((=='.').last) .
splitDirectories

-- | Give the content for a new auto-created journal file.
newJournalContent :: IO String
newJournalContent = do
Expand Down

0 comments on commit 014db15

Please sign in to comment.