Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions codeworld-server/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
41 changes: 39 additions & 2 deletions codeworld-server/src/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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

Expand Down
Loading