Skip to content

Commit beca120

Browse files
committed
Add notes on security
1 parent 4c70618 commit beca120

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

readme.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Whether to compile list-items tightly (`boolean?`, default: `false`).
109109
Add a prefix to links to headings in the table of contents (`string?`,
110110
default: `null`).
111111
Useful for example when later going from [mdast][] to [hast][] and sanitizing
112-
with [`hast-util-sanitize`][hast-util-sanitize].
112+
with [`hast-util-sanitize`][sanitize].
113113

114114
###### `options.parents`
115115

@@ -144,6 +144,39 @@ An object representing the table of contents.
144144
`null` if no table of contents could be created, either because
145145
no heading was found or because no following headings were found
146146

147+
## Security
148+
149+
Use of `mdast-util-toc` does not involve [**hast**][hast], user content, or
150+
change the tree, so there are no openings for [cross-site scripting (XSS)][xss]
151+
attacks.
152+
153+
Injecting `map` into the syntax tree may open you up to XSS attacks as existing
154+
nodes are copied into the table of contents.
155+
The following example shows how an existing script is copied into the table of
156+
contents.
157+
158+
For the following Markdown:
159+
160+
```markdown
161+
# Alpha
162+
163+
## Bravo<script>alert(1)</script>
164+
165+
## Charlie
166+
```
167+
168+
Yields in `map`:
169+
170+
```markdown
171+
- [Alpha](#alpha)
172+
173+
- [Bravo<script>alert(1)</script>](#bravoscriptalert1script)
174+
- [Charlie](#charlie)
175+
```
176+
177+
Always use [`hast-util-santize`][sanitize] when transforming to
178+
[**hast**][hast].
179+
147180
## Contribute
148181

149182
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
@@ -202,7 +235,7 @@ abide by its terms.
202235

203236
[hast]: https://github.com/syntax-tree/hast
204237

205-
[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize
238+
[sanitize]: https://github.com/syntax-tree/hast-util-sanitize
206239

207240
[is]: https://github.com/syntax-tree/unist-util-is
208241

@@ -219,3 +252,5 @@ abide by its terms.
219252
[blockquote]: https://github.com/syntax-tree/mdast#blockquote
220253

221254
[parents]: #optionsparents
255+
256+
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting

0 commit comments

Comments
 (0)