Skip to content

Commit 2f327ee

Browse files
committed
Add notes on security
1 parent 14f7849 commit 2f327ee

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

readme.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ Given a [**hast**][hast] [*tree*][tree] and an optional [vfile][] (for
6767
[positional info][position-information]), return a new parsed-again
6868
[**hast**][hast] [*tree*][tree].
6969

70+
## Security
71+
72+
Use of `hast-util-raw` can open you up to a [cross-site scripting (XSS)][xss]
73+
attack as `raw` nodes are unsafe.
74+
The following example shows how a raw node is used to inject a script that runs
75+
when loaded in a browser.
76+
77+
```js
78+
raw(u('root', [u('raw', '<script>alert(1)</script>')]))
79+
```
80+
81+
Yields:
82+
83+
```html
84+
<script>alert(1)</script>
85+
```
86+
87+
Do not use this utility in combination with user input or use
88+
[`hast-util-santize`][sanitize].
89+
7090
## Contribute
7191

7292
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
@@ -136,3 +156,7 @@ abide by its terms.
136156
[remark-rehype]: https://github.com/remarkjs/remark-rehype
137157

138158
[rehype-raw]: https://github.com/rehypejs/rehype-raw
159+
160+
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
161+
162+
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize

test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ test('raw', function(t) {
150150
'should pass raw nodes through even after textarea (#3)'
151151
)
152152

153+
t.deepEqual(
154+
raw(u('root', [u('raw', '<script>alert(1)</script>')])),
155+
u('root', {data: {quirksMode: false}}, [
156+
h('script', u('text', 'alert(1)'))
157+
]),
158+
'security: raw nodes (unsafe)'
159+
)
160+
161+
t.deepEqual(
162+
raw(u('root', [h('script', u('text', 'alert(1)'))])),
163+
u('root', {data: {quirksMode: false}}, [
164+
h('script', u('text', 'alert(1)'))
165+
]),
166+
'security: unsafe nodes (unsafe)'
167+
)
168+
153169
t.end()
154170
})
155171

0 commit comments

Comments
 (0)