Skip to content

Commit b684082

Browse files
committed
Add notes on security
1 parent 7e02879 commit b684082

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"hast-util-is-element": "^1.0.0",
2525
"hast-util-whitespace": "^1.0.0",
2626
"html-void-elements": "^1.0.0",
27-
"property-information": "^5.0.0",
27+
"property-information": "^5.2.0",
2828
"space-separated-tokens": "^1.0.0",
2929
"stringify-entities": "^2.0.0",
3030
"unist-util-is": "^3.0.0",

Diff for: readme.md

+10
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ Allow `raw` nodes and insert them as raw HTML.
167167
When falsey, encodes `raw` nodes (`boolean`, default: `false`).
168168
**Note**: Only set this if you completely trust the content.
169169

170+
## Security
171+
172+
Use of `hast-util-to-html` can open you up to a
173+
[cross-site scripting (XSS)][xss] attack if the hast tree is unsafe.
174+
Use [`hast-util-santize`][sanitize] to make the hast tree safe.
175+
170176
## Related
171177

172178
* [`hast-util-sanitize`][hast-util-sanitize]
@@ -241,3 +247,7 @@ abide by its terms.
241247
[hast]: https://github.com/syntax-tree/hast
242248

243249
[element]: https://github.com/syntax-tree/hast#element
250+
251+
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
252+
253+
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize

Diff for: test/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ require('./omission')
1313
require('./omission-opening')
1414
require('./omission-closing')
1515
require('./svg')
16+
require('./security')
1617
/* eslint-enable import/no-unassigned-import */

Diff for: test/security.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict'
2+
3+
var test = require('tape')
4+
var h = require('hastscript')
5+
var u = require('unist-builder')
6+
var to = require('..')
7+
8+
test('security', function(t) {
9+
t.equal(
10+
to(u('root', [u('comment', '--><script>alert(1)</script><!--')])),
11+
'<!----><script>alert(1)</script><!---->',
12+
'comments can break out of their context (unsafe)'
13+
)
14+
15+
t.equal(
16+
to(u('root', [h('script', 'alert(1)')])),
17+
'<script>alert(1)</script>',
18+
'scripts render (unsafe)'
19+
)
20+
21+
t.equal(
22+
to(h('img', {src: 'x', onError: 'alert(1)'})),
23+
'<img src="x" onerror="alert(1)">',
24+
'event attributes render (unsafe)'
25+
)
26+
27+
t.equal(
28+
to(u('root', u('text', '<script>alert(1)</script>'))),
29+
'&#x3C;script>alert(1)&#x3C;/script>',
30+
'texts are encoded (safe)'
31+
)
32+
33+
t.end()
34+
})

0 commit comments

Comments
 (0)