diff --git a/codeworld-server/src/Main.hs b/codeworld-server/src/Main.hs index 7379b3d48..4ac3ba748 100644 --- a/codeworld-server/src/Main.hs +++ b/codeworld-server/src/Main.hs @@ -301,16 +301,19 @@ shareFolderHandler = private $ \userId ctx -> do shareContentHandler :: CodeWorldHandler shareContentHandler = private $ \userId ctx -> do mode <- getBuildMode - Just shash <- getParam "shash" + shash <- getValidHashParam "shash" sharingFolder <- liftIO $ B.readFile - (shareRootDir mode shareLink (ShareId $ T.decodeUtf8 shash)) + (shareRootDir mode shareLink (ShareId shash)) + folder <- case T.decodeUtf8' sharingFolder of + Right t | isValidShareTarget mode (T.unpack t) -> return (T.unpack t) + _ -> pass Just name <- fmap T.decodeUtf8 <$> getParam "name" let dirPath = dirBase $ nameToDirId name liftIO $ ensureUserBaseDir mode userId dirPath liftIO - $ copyDirIfExists (T.unpack (T.decodeUtf8 sharingFolder)) + $ copyDirIfExists folder $ userProjectDir mode userId dirPath liftIO $ T.writeFile @@ -394,16 +397,19 @@ errorCheckHandler = public $ \ctx -> do modifyResponse $ setContentType "text/plain" writeBS output +getValidHashParam :: B.ByteString -> Snap Text +getValidHashParam name = + getParam name >>= maybe pass return . (decodeValidHashParam =<<) + getHashParam :: Bool -> BuildMode -> Snap ProgramId getHashParam allowDeploy mode = do maybeHash <- getParam "hash" case maybeHash of - Just h -> return (ProgramId (T.decodeUtf8 h)) + Just h -> maybe pass (return . ProgramId) (decodeValidHashParam h) Nothing | allowDeploy -> do - Just dh <- getParam "dhash" - let deployId = DeployId (T.decodeUtf8 dh) - liftIO $ resolveDeployId mode deployId + dh <- getValidHashParam "dhash" + liftIO $ resolveDeployId mode (DeployId dh) | otherwise -> pass loadXMLHandler :: CodeWorldHandler @@ -438,12 +444,15 @@ runHandler = public $ \ctx -> do runBaseHandler :: CodeWorldHandler runBaseHandler = public $ \ctx -> do - maybeVer <- fmap T.decodeUtf8 <$> getParam "version" + maybeVer <- getParam "version" hasProgram <- (\mode hash dhash -> mode && (hash || dhash)) <$> hasParam "mode" <*> hasParam "hash" <*> hasParam "dhash" case maybeVer of - Just ver -> serveFile (baseCodeFile ver) + Just verBS -> + case decodeValidHashParam verBS of + Just ver -> serveFile (baseCodeFile ver) + Nothing -> pass Nothing | hasProgram -> do mode <- getBuildMode programId <- getHashParam True mode @@ -489,10 +498,15 @@ indentHandler = public $ \ctx -> do galleryHandler :: CodeWorldHandler galleryHandler = public $ const $ do mode <- getBuildMode - Just shareHash <- getParam "shareHash" - let shareId = ShareId (T.decodeUtf8 shareHash) + shareHash <- getValidHashParam "shareHash" + let shareId = ShareId shareHash + sharingFolder <- + liftIO $ + B.readFile (shareRootDir mode shareLink shareId) + folder <- case T.decodeUtf8' sharingFolder of + Right t | isValidShareTarget mode (T.unpack t) -> return (T.unpack t) + _ -> pass gallery <- liftIO $ do - folder <- T.unpack . T.decodeUtf8 <$> B.readFile (shareRootDir mode shareLink shareId) files <- sort <$> projectFileNames folder Gallery <$> mapM (galleryItemFromProject mode folder) files writeLBS $ encode gallery diff --git a/codeworld-server/src/Util.hs b/codeworld-server/src/Util.hs index 95fa664ea..b20f9b1c6 100644 --- a/codeworld-server/src/Util.hs +++ b/codeworld-server/src/Util.hs @@ -28,7 +28,8 @@ import Data.ByteString (ByteString) import qualified Data.ByteString as B import qualified Data.ByteString.Base64 as B64 import qualified Data.ByteString.Lazy as LB -import Data.List (sort, sortOn) +import Data.Char (isAlphaNum, isAscii) +import Data.List (isPrefixOf, sort, sortOn) import Data.Maybe import Data.Text (Text) import qualified Data.Text as T @@ -70,6 +71,36 @@ newtype ShareId = ShareId type BaseVersion = Text +-- | Validates that a hash or version identifier consists of non-empty ASCII +-- base64url-style characters ([A-Za-z0-9_-]) within the expected length bound. +isValidHashParam :: Text -> Bool +isValidHashParam t = + not (T.null t) + && T.length t <= 128 + && T.all isValidChar t + where + isValidChar c = isAscii c && (isAlphaNum c || c == '_' || c == '-') + +-- | Decodes a UTF-8 byte string and validates it with 'isValidHashParam'. +decodeValidHashParam :: ByteString -> Maybe Text +decodeValidHashParam bs = + case T.decodeUtf8' bs of + Right t | isValidHashParam t -> Just t + _ -> Nothing + +-- | Validates that a directory path from a share link is a relative path +-- inside projectRootDir for the given BuildMode. +isValidShareTarget :: BuildMode -> FilePath -> Bool +isValidShareTarget mode path = + isRelative path + && ".." `notElem` comps + && "." `notElem` comps + && rootComps `isPrefixOf` comps + && length comps > length rootComps + where + comps = splitDirectories path + rootComps = splitDirectories (projectRootDir mode) + autocompletePath :: FilePath autocompletePath = "web/codeworld-base.txt" @@ -288,7 +319,13 @@ writeDeployLink mode deployId (ProgramId p) = do f = deployRootDir mode deployLink deployId resolveDeployId :: BuildMode -> DeployId -> IO ProgramId -resolveDeployId mode deployId = ProgramId . T.decodeUtf8 <$> B.readFile f +resolveDeployId mode deployId = do + unless (isValidHashParam (unDeployId deployId)) $ + ioError (userError "Invalid deploy ID") + bs <- B.readFile f + case decodeValidHashParam bs of + Just p -> return (ProgramId p) + Nothing -> ioError (userError "Invalid program ID in deploy link") where f = deployRootDir mode deployLink deployId