yesodweb / haskell-xss-sanitize Goto Github PK
View Code? Open in Web Editor NEWThis project forked from gregwebs/haskell-xss-sanitize
prevent XSS attacks by sanitizing html (this is different then escaping!)
License: Other
This project forked from gregwebs/haskell-xss-sanitize
prevent XSS attacks by sanitizing html (this is different then escaping!)
License: Other
garbetsp@biostat1427:~/Projects$ cabal install xss-sanitize
Resolving dependencies...
Downloading xss-sanitize-0.3.5.7...
Failed to install xss-sanitize-0.3.5.7
Build log ( /home/garbetsp/.cabal/logs/xss-sanitize-0.3.5.7.log ):
cabal: /home/garbetsp/.cabal/logs/xss-sanitize-0.3.5.7.log: does not exist
I tried a direct download from git and this fails as well. It creates no log and aborts on install. Tried the earlier 0.3.5.6 release and got the same result.
I am looking into rendering some user-submitted html, so unsurprisingly I'm planning on using this library to sanitize it.
However:
There is no guarantee for how a browser will display invalid HTML, so there is no guarantee that this function will protect your HTML from being broken by a user's html.
This makes me pretty nervous. Preventing invalid HTML from breaking the rest of the page is pretty essential.
How difficult would it be to add a functionality that would get rid of or fix invalid html to avoid this problem? Or at least invalid html that causes problems in practice.
Are there any known examples of html that is problematic even after sanitization? I was hoping that a parent div with some overflow: hidden
would be sufficient.
Currently sanitizing script and style stags preserves the internal content as escaped html:
sanitize "<script>console.log('foo');</script>"
-- "console.log('foo');"
sanitize "<style>* { color: red }</style>"
-- "* { color: red }"
This is of course perfectly safe. However it seems very unlikely to be the desired resulting html.
Accordingly it seems like an explicit clear-list of html tags that should be emptied instead of escaped would be useful, with ["script", "style"]
as the default list.
One could reasonably argue that <head>
should make the list as well.
In particular, network
has hit 2.6+ on hackage and could use a bump.
This was discovery when using Gitit, see jgm/gitit#479.
Valid MathML tags are sanitezed.
Generate a simple MathML element with Pandoc.
$ pandoc -f markdown -t html --mathml <<EOF
\$x\$
EOF
<p><math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mi>x</mi><annotation encoding="application/x-tex">x</annotation></semantics></math></p>
Copy the MathML element and sanitize it.
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi>x</math>
This MathML element is invalid.
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mi>x</mi><annotation encoding="application/x-tex">x</annotation></semantics></math></p>
This is the unchanged output from Pandoc.
The semantics
and annotation
tags are valid MathML 3, see http://www.w3.org/TR/MathML/chapter5.html#mixing.semantic.annotations for more information.
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.8.3
$ ghc-pkg list gitit
gitit-0.10.6.1
$ ghc-pkg list pandoc
pandoc-1.13.3
$ ghc-pkg list texmath
texmath-0.8.0.1
$ ghc-pkg list xss-sanitize
xss-sanitize-0.3.5.4
*Text.HTML.SanitizeXSS> filterTags safeTags "text more text"
"text&nbsp;more text"
This would display as "text  more text" instead of "text more text".
If you add optEscape = id
to the renderOptions
then TagSoup will stop trying to escape &"<>
*Text.HTML.SanitizeXSS> filterTags safeTags "text more text"
"text more text"
If you are ok with this fix, I'll create a pull request with my changes.
rejecting: text-2.1/installed-4d9a (conflict: xss-sanitize => text>=0.11 && <2.1)
I'm using imgsrc to support high-resolution display images: https://webkit.org/demos/srcset/
<img src="image.jpg" srcset="image-1x.jpg 1x, image-2x.jpg 2x, image-3x.jpg 3x">
Your XSS strips this down to
<img src="image.jpg">
It just gets rid of the srcset
. Why, and if srcset
isn't really dangerous, can you consider whitelisting it?
https://html.spec.whatwg.org/multipage/syntax.html#optional-tags
Minimal incorrect example:
-- current
sanitizeBalance "<td>foo<td>bar" == "<td>foo<td>bar</td></td>"
-- correct
sanitizeBalance "<td>foo<td>bar" == "<td>foo</td><td>bar</td>"
-- potentially an option depending on semantics of "balanced"
sanitizeBalance "<td>foo<td>bar" == "<td>foo<td>bar"
If html4 / xhtml5 sanitizing is also desired then separate functions/modules may be needed.
The Cabal file says the library is released under BSD3, but the LICENSE file shipped with the code actually contains a BSD2 license.
It would be nice to use http://hackage.haskell.org/package/fast-tagsoup
Currently, text-2.0
is excluded by constraint text < 2
.
Here are snippets of what I observed on the stackage build server. I can try to provide better repro instructions upon request.
yesod-markdown-0.12.6.11
Yesod.Markdown
converts Markdown to sanitized HTML FAILED [1]
converts Markdown to unsanitized HTML
Failures:
test/Spec.hs:33:9:
1) Yesod.Markdown converts Markdown to sanitized HTML
expected: "<h1 id=\"title\">Title</h1><ul><li>one</li><li>two</li><
li>three</li></ul>\n alert('xxs');\n"
but got: "<h1 id=\"title\">Title</h1><ul><li>one</li><li>two</li><
li>three</li></ul>"
markdown-0.1.17.4
test/main.hs:230:26:
1) html block xss
expected: "alert("evil")"
but got: ""
sanitizeBalance
sometimes still includes the content within the style tag.
Here's a minimum reproducible example that shows the issue:
λ> sanitizeBalance "<!DOCTYPE><html><style>html{width:100%;max-width:100%}</style></html></DOCTYPE>"
"html{width:100%;max-width:100%}"
This should just return "" and a simple example like this works:
λ> sanitizeBalance "<style>html{width:100%}</style>"
The sanitizeAttribute
function is exposed with the following comment:
low-level API if you have your own HTML parser
But safeTagName
or sanitaryTags
aren't exposed :-(
From the documentation:
You can insert your own custom filtering, but make sure you compose your filtering function with [safeTags] or [safeTagsCustom]
Really this should say:
You can insert your own custom filtering, but make sure you compose your filtering function with (safeTags . clearTags) or (safeTagsCustom . clearTagsCustom)
The problem is, without applying clearTags
first, the input may not be sanitized:
Prelude Text.HTML.SanitizeXSS> filterTags safeTags "<iframe></iframe>"
""
Prelude Text.HTML.SanitizeXSS> filterTags safeTags "<script><iframe></iframe>"
"<iframe></iframe>"
This isn't an issue with sanitizeXSS
because it composes safeTags
with clearTags
.
Prelude Text.HTML.SanitizeXSS> sanitizeXSS "<script><iframe></iframe>"
""
I'm happy to make a pull request to fix this.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.