@@ -167,6 +167,69 @@ When strings are encountered, they are mapped to [`text`][text] nodes.
167167
168168[ ` Element ` ] [ element ] .
169169
170+ ## Security
171+
172+ Use of ` hastscript ` can open you up to a [ cross-site scripting (XSS)] [ xss ]
173+ attack as values are injected into the syntax tree.
174+ The following example shows how a script is injected that runs when loaded in a
175+ browser.
176+
177+ ``` js
178+ var tree = {type: ' root' , children: []}
179+
180+ tree .children .push (h (' script' , ' alert(1)' ))
181+ ```
182+
183+ Yields:
184+
185+ ``` html
186+ <script >alert (1 ) </script >
187+ ```
188+
189+ The following example shows how an image is injected that fails loading and
190+ therefore runs code in a browser.
191+
192+ ``` js
193+ var tree = {type: ' root' , children: []}
194+
195+ // Somehow someone injected these properties instead of an expected `src` and
196+ // `alt`:
197+ var otherProps = {src: ' x' , onError: ' alert(2)' }
198+
199+ tree .children .push (h (' img' , {src: ' default.png' , ... otherProps}))
200+ ```
201+
202+ Yields:
203+
204+ ``` html
205+ <img src =" x" onerror =" alert(2)" >
206+ ```
207+
208+ The following example shows how code can run in a browser because someone stored
209+ an object in a database instead of the expected string.
210+
211+ ``` js
212+ var tree = {type: ' root' , children: []}
213+
214+ // Somehow this isn’t the expected `'wooorm'`.
215+ var username = {
216+ type: ' element' ,
217+ tagName: ' script' ,
218+ children: [{type: ' text' , value: ' alert(3)' }]
219+ }
220+
221+ tree .children .push (h (' span.handle' , username))
222+ ```
223+
224+ Yields:
225+
226+ ``` html
227+ <span class =" handle" ><script >alert (3 ) </script ></span >
228+ ```
229+
230+ Either do not use user input in ` hastscript ` or use
231+ [ ` hast-util-santize ` ] [ sanitize ] .
232+
170233## Contribute
171234
172235See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
@@ -242,3 +305,7 @@ abide by its terms.
242305[ u ] : https://github.com/syntax-tree/unist-builder
243306
244307[ parse-selector ] : https://github.com/syntax-tree/hast-util-parse-selector
308+
309+ [ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
310+
311+ [ sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
0 commit comments