3ece4758c6
Empty filenames were already filtered out as not allowed. But before the change to ByteString, a NUL could appear in an Arbitrary String, and so Arbitrary AssociatedFile sometimes generated illegal filenames, as NUL never appears in a filename. The change to ByteString meant the String was run through toRawFilePath, which assumes a filename never contains a NUL. That truncated the String at the NUL, which could result in an AssociatedFile being generated with an empty filename. The filtering of NUL added here is not really necessary, because of the truncation, but it makes explicit that NUL is not allowed. The real fix is that the suchThat now applies to the final AssociatedFile, so will catch any empty ones however generated. This raises the more general question of whether toRawFilePath might truncate other strings that later get used as filenames. I think new bugs probably won't be introduced by that. Before, a FilePath that got read from somewhere (eg an attacker) and contained a NUL would perhaps be printed out by git-annex, including the NUL, or written to disk inside a file, or what have you. But as soon as that FilePath gets passed to any IO action that treats it as a filename, it gets truncated after the NUL. Eg, writeFile "foo\NULbar" "bar" writes to file "foo". Now toRawFilePath will make the truncation happen earler, but at most this will affect what gets printed out or is written to disk inside a file; actually using the RawFilePath as a filename will not change from using the FilePath as a filename.
109 lines
2.9 KiB
Haskell
109 lines
2.9 KiB
Haskell
{- git-annex Keys
|
|
-
|
|
- Copyright 2011-2019 Joey Hess <id@joeyh.name>
|
|
-
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
-}
|
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
module Key (
|
|
Key,
|
|
KeyData(..),
|
|
AssociatedFile(..),
|
|
fromKey,
|
|
mkKey,
|
|
alterKey,
|
|
keyParser,
|
|
serializeKey,
|
|
serializeKey',
|
|
deserializeKey,
|
|
deserializeKey',
|
|
nonChunkKey,
|
|
chunkKeyOffset,
|
|
isChunkKey,
|
|
isKeyPrefix,
|
|
splitKeyNameExtension,
|
|
|
|
prop_isomorphic_key_encode
|
|
) where
|
|
|
|
import qualified Data.Text as T
|
|
import qualified Data.ByteString as S
|
|
import qualified Data.Attoparsec.ByteString as A
|
|
|
|
import Common
|
|
import Types.Key
|
|
import Utility.QuickCheck
|
|
import Utility.Bloom
|
|
import Utility.Aeson
|
|
import qualified Utility.SimpleProtocol as Proto
|
|
|
|
-- Gets the parent of a chunk key.
|
|
nonChunkKey :: Key -> Key
|
|
nonChunkKey k
|
|
| fromKey keyChunkSize k == Nothing && fromKey keyChunkNum k == Nothing = k
|
|
| otherwise = alterKey k $ \d -> d
|
|
{ keyChunkSize = Nothing
|
|
, keyChunkNum = Nothing
|
|
}
|
|
|
|
-- Where a chunk key is offset within its parent.
|
|
chunkKeyOffset :: Key -> Maybe Integer
|
|
chunkKeyOffset k = (*)
|
|
<$> fromKey keyChunkSize k
|
|
<*> (pred <$> fromKey keyChunkNum k)
|
|
|
|
isChunkKey :: Key -> Bool
|
|
isChunkKey k = isJust (fromKey keyChunkSize k) && isJust (fromKey keyChunkNum k)
|
|
|
|
serializeKey :: Key -> String
|
|
serializeKey = decodeBS' . serializeKey'
|
|
|
|
serializeKey' :: Key -> S.ByteString
|
|
serializeKey' = keySerialization
|
|
|
|
deserializeKey :: String -> Maybe Key
|
|
deserializeKey = deserializeKey' . encodeBS'
|
|
|
|
deserializeKey' :: S.ByteString -> Maybe Key
|
|
deserializeKey' = either (const Nothing) Just . A.parseOnly keyParser
|
|
|
|
instance Arbitrary KeyData where
|
|
arbitrary = Key
|
|
<$> (encodeBS <$> (listOf1 $ elements $ ['A'..'Z'] ++ ['a'..'z'] ++ ['0'..'9'] ++ "-_\r\n \t"))
|
|
<*> (parseKeyVariety . encodeBS <$> (listOf1 $ elements ['A'..'Z'])) -- BACKEND
|
|
<*> ((abs <$>) <$> arbitrary) -- size cannot be negative
|
|
<*> ((abs . fromInteger <$>) <$> arbitrary) -- mtime cannot be negative
|
|
<*> ((abs <$>) <$> arbitrary) -- chunksize cannot be negative
|
|
<*> ((succ . abs <$>) <$> arbitrary) -- chunknum cannot be 0 or negative
|
|
|
|
-- AssociatedFile cannot be empty, and cannot contain a NUL
|
|
-- (but can be Nothing)
|
|
instance Arbitrary AssociatedFile where
|
|
arbitrary = (AssociatedFile . fmap mk <$> arbitrary)
|
|
`suchThat` (/= AssociatedFile (Just S.empty))
|
|
where
|
|
mk = toRawFilePath . filter (/= '\NUL')
|
|
|
|
instance Arbitrary Key where
|
|
arbitrary = mkKey . const <$> arbitrary
|
|
|
|
instance Hashable Key where
|
|
hashIO32 = hashIO32 . serializeKey'
|
|
hashIO64 = hashIO64 . serializeKey'
|
|
|
|
instance ToJSON' Key where
|
|
toJSON' = toJSON' . serializeKey
|
|
|
|
instance FromJSON Key where
|
|
parseJSON (String t) = maybe mempty pure $ deserializeKey $ T.unpack t
|
|
parseJSON _ = mempty
|
|
|
|
instance Proto.Serializable Key where
|
|
serialize = serializeKey
|
|
deserialize = deserializeKey
|
|
|
|
prop_isomorphic_key_encode :: Key -> Bool
|
|
prop_isomorphic_key_encode k = Just k == (deserializeKey . serializeKey) k
|
|
|