on master help sign in
Merge pull request #165 from nagisa/uri-sanitise

Better sanitising for URIs

commit832c6d3f9a150ed17c230a156230ca5556aa2250 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateSun 16 Jun 2013, 09:05 (13 years ago) parent537e72372ed15e
3 files changed+19−10
hakyll.cabal+4−2 src/Hakyll/Web/Html.hs+6−4 tests/Hakyll/Web/Html/Tests.hs+9−4
hakyll.cabal+4−2
@@ -162,7 +162,8 @@ Library
162162 regex-tdfa >= 1.1 && < 1.2,
163163 tagsoup >= 0.12.6 && < 0.13,
164164 text >= 0.11 && < 1.12,
165− time >= 1.1 && < 1.5
165+ time >= 1.1 && < 1.5,
166+ network >= 2.0 && < 2.5
166167
167168 If flag(previewServer)
168169 Build-depends:
@@ -238,7 +239,8 @@ Test-suite hakyll-tests
238239 regex-tdfa >= 1.1 && < 1.2,
239240 tagsoup >= 0.12.6 && < 0.13,
240241 text >= 0.11 && < 1.12,
241− time >= 1.1 && < 1.5
242+ time >= 1.1 && < 1.5,
243+ network >= 2.0 && < 2.5
242244
243245 If flag(previewServer)
244246 Build-depends:
src/Hakyll/Web/Html.hs+6−4
@@ -30,6 +30,7 @@ import System.FilePath (joinPath, splitPath,
3030import Text.Blaze.Html (toHtml)
3131import Text.Blaze.Html.Renderer.String (renderHtml)
3232import qualified Text.HTML.TagSoup as TS
33+import Network.URI (isUnreserved, escapeURIString)
3334
3435
3536--------------------------------------------------------------------------------
@@ -105,10 +106,11 @@ toUrl url = case url of
105106 ('/' : xs) -> '/' : sanitize xs
106107 xs -> '/' : sanitize xs
107108 where
108− -- This probably needs to be a separate function
109− sanitize = concatMap $ \c -> case c of
110− ' ' -> "%20"
111− _ -> [c]
109+ -- Everything but unreserved characters should be escaped as we are
110+ -- sanitising the path therefore reserved characters which have a
111+ -- meaning in URI does not appear. Special casing for `/`, because it has
112+ -- a special meaning in FilePath as well as in URI.
113+ sanitize = escapeURIString (\c -> c == '/' || isUnreserved c)
112114
113115
114116--------------------------------------------------------------------------------
tests/Hakyll/Web/Html/Tests.hs+9−4
@@ -43,10 +43,15 @@ tests = testGroup "Hakyll.Web.Html.Tests" $ concat
4343 ]
4444
4545 , fromAssertions "toUrl"
46− [ "/foo/bar.html" @=? toUrl "foo/bar.html"
47− , "/" @=? toUrl "/"
48− , "/funny-pics.html" @=? toUrl "/funny-pics.html"
49− , "/funny%20pics.html" @=? toUrl "funny pics.html"
46+ [ "/foo/bar.html" @=? toUrl "foo/bar.html"
47+ , "/" @=? toUrl "/"
48+ , "/funny-pics.html" @=? toUrl "/funny-pics.html"
49+ , "/funny%20pics.html" @=? toUrl "funny pics.html"
50+ -- Test various reserved characters (RFC 3986, section 2.2)
51+ , "/%21%2A%27%28%29%3B%3A%40%26.html" @=? toUrl "/!*'();:@&.html"
52+ , "/%3D%2B%24%2C/%3F%23%5B%5D.html" @=? toUrl "=+$,/?#[].html"
53+ -- Test various characters that are nor reserved, nor unreserved.
54+ , "/%E3%81%82%F0%9D%90%87%E2%88%80" @=? toUrl "\12354\119815\8704"
5055 ]
5156
5257 , fromAssertions "toSiteRoot"