Skip to content

Commit

Permalink
Allow extracting data: URIs even in PandocPure (--sandbox).
Browse files Browse the repository at this point in the history
These don't require IO, so we should allow it in sandboxed
mode.  Closes #10249.
  • Loading branch information
jgm committed Oct 1, 2024
1 parent c3eec89 commit 7048254
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
12 changes: 3 additions & 9 deletions src/Text/Pandoc/Class/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import System.IO.Error
import System.Random (StdGen)
import Text.Pandoc.Class.CommonState (CommonState (..))
import Text.Pandoc.Class.PandocMonad
(PandocMonad, getsCommonState, getMediaBag, report)
(PandocMonad, getsCommonState, getMediaBag, report, extractURIData)
import Text.Pandoc.Definition (Pandoc, Inline (Image))
import Text.Pandoc.Error (PandocError (..))
import Text.Pandoc.Logging (LogMessage (..), messageVerbosity, showLogMessage)
Expand Down Expand Up @@ -128,14 +128,8 @@ newUniqueHash = hashUnique <$> liftIO Data.Unique.newUnique
openURL :: (PandocMonad m, MonadIO m) => Text -> m (B.ByteString, Maybe MimeType)
openURL u
| Just (URI{ uriScheme = "data:",
uriPath = upath }) <- parseURI (T.unpack u) = do
let (mimespec, rest) = break (== ',') $ unEscapeString upath
let contents = UTF8.fromString $ drop 1 rest
case break (== ';') (filter (/= ' ') mimespec) of
(mime, ";base64") ->
return (decodeLenient contents, Just (T.pack mime))
(mime, _) ->
return (contents, Just (T.pack mime))
uriPath = upath }) <- parseURI (T.unpack u)
= pure $ extractURIData upath
| otherwise = do
let toReqHeader (n, v) = (CI.mk (UTF8.fromText n), UTF8.fromText v)
customHeaders <- map toReqHeader <$> getsCommonState stRequestHeaders
Expand Down
9 changes: 5 additions & 4 deletions src/Text/Pandoc/Class/PandocMonad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ downloadOrRead :: PandocMonad m
-> m (B.ByteString, Maybe MimeType)
downloadOrRead s = do
sourceURL <- getsCommonState stSourceURL
case (sourceURL >>= parseURIReference' .
ensureEscaped, ensureEscaped s) of
case (sourceURL >>= parseURIReference' . ensureEscaped, ensureEscaped s) of
(Just u, s') -> -- try fetching from relative path at source
case parseURIReference' s' of
Just u' -> openURL $ T.pack $ show $ u' `nonStrictRelativeTo` u
Expand All @@ -348,8 +347,10 @@ downloadOrRead s = do
Nothing -> openURL s' -- will throw error
(Nothing, s') ->
case parseURI (T.unpack s') of -- requires absolute URI
Just u' | uriScheme u' == "file:" ->
readLocalFile $ uriPathToPath (T.pack $ uriPath u')
Just URI{ uriScheme = "file:", uriPath = upath}
-> readLocalFile $ uriPathToPath (T.pack upath)
Just URI{ uriScheme = "data:", uriPath = upath}
-> pure $ extractURIData upath
-- We don't want to treat C:/ as a scheme:
Just u' | length (uriScheme u') > 2 -> openURL (T.pack $ show u')
_ -> readLocalFile fp -- get from local file system
Expand Down

0 comments on commit 7048254

Please sign in to comment.