Debian Codegraph @

docs/sanitization.md

raw
1 title: Sanitization and Security
2
3 # Sanitizing HTML Output
4
5 The Python-Markdown library does ***not*** sanitize its HTML output. If you
6 are processing Markdown input from an untrusted source, it is your
7 responsibility to ensure that it is properly sanitized. See _[Markdown and
8 XSS]_ for an overview of some of the dangers and _[Improper markup sanitization
9 in popular software]_ for notes on best practices to ensure HTML is properly
10 sanitized. With those concerns in mind, some recommendations are provided
11 below to ensure that any input from an untrusted source is properly
12 sanitized.
13
14 That said, if you fully trust the source of your input, you may choose to do
15 nothing. Conversely, you may find solutions other than those suggested here.
16 However, you do so at your own risk.
17
18 ## Using `JustHTML`
19
20 [`JustHTML`][JustHTML] is recommended as a sanitizer on the output of `markdown.markdown`
21 or `Markdown.convert`. When you pass HTML output through `JustHTML`, it is
22 sanitized by default according to a strict [allow list policy]. The policy
23 can be [customized] if necessary.
24
25 ``` python
26 import markdown
27 from justhtml import JustHTML
28
29 html = markdown.markdown(text)
30 safe_html = JustHTML(html, fragment=True).to_html()
31 ```
32
33 ## Using `nh3` or `bleach`
34
35 If you cannot use `JustHTML` for some reason, some alternatives include [`nh3`][nh3] or
36 [`bleach`][bleach][^1]. However, be aware that these libraries will not be sufficient
37 in themselves and will require customization. Some useful lists of allowed
38 tags and attributes can be found in the [`bleach-allowlist`]
39 [bleach-allowlist] library, which should work with both `nh3` and `bleach` as `nh3`
40 mirrors `bleach`'s API.
41
42 ``` python
43 import markdown
44 import bleach
45 from bleach_allowlist import markdown_tags, markdown_attrs
46
47 html = markdown.markdown(text)
48 safe_html = bleach.clean(html, markdown_tags, markdown_attrs)
49 ```
50
51 [^1]: The [`bleach`][bleach] project has been [deprecated](https://github.com/mozilla/bleach/issues/698).
52 However, it may be the only option for some users as `nh3` is a set of Python bindings to a Rust library.
53
54 ## Sanitizing on the Command Line
55
56 Both Python-Markdown and `JustHTML` provide command line interfaces which read
57 from `STDIN` and write to `STDOUT`. Therefore, they can be used together to
58 ensure that the output from untrusted input is properly sanitized.
59
60 ```sh
61 echo "Some **Markdown** text." | python -m markdown | justhtml - --fragment > safe_output.html
62 ```
63
64 For more information on `JustHTML`'s Command Line Interface, see the
65 [documentation][JustHTML_CLI]. Use the `--help` option for a list of all available
66 options and arguments to the `markdown` command.
67
68 [Markdown and XSS]: https://michelf.ca/blog/2010/markdown-and-xss/
69 [Improper markup sanitization in popular software]: https://github.com/ChALkeR/notes/blob/master/Improper-markup-sanitization.md
70 [JustHTML]: https://emilstenstrom.github.io/justhtml/
71 [allow list policy]: https://emilstenstrom.github.io/justhtml/html-cleaning.html#default-sanitization-policy
72 [customized]: https://emilstenstrom.github.io/justhtml/html-cleaning.html#use-a-custom-sanitization-policy
73 [nh3]: https://nh3.readthedocs.io/en/latest/
74 [bleach]: http://bleach.readthedocs.org/en/latest/
75 [bleach-allowlist]: https://github.com/yourcelf/bleach-allowlist
76 [JustHTML_CLI]: https://emilstenstrom.github.io/justhtml/cli.html