Skip to content

fix: escape text inside stripped pre, code, kbd and samp tags - #306

Open
Chandan3456 wants to merge 1 commit into
matthewwithanm:developfrom
Chandan3456:strip-preformatted-escaping
Open

Chandan3456 wants to merge 1 commit into
matthewwithanm:developfrom
Chandan3456:strip-preformatted-escaping

Conversation

@Chandan3456

Copy link
Copy Markdown
Contributor

process_tag adds the _noformat pseudo-tag for every <pre>, <code>, <kbd> and <samp> whether or not the tag is converted, and process_text skips escape() for everything under it on the assumption that convert_pre or convert_code will put the text in a fence or code span. when the tag is stripped with strip= or left out of a convert= list, no converter runs and the contents come out as plain text with no escaping at all, so escape_misc=True and the default */_ escaping are both skipped there:

>>> md('<p>run <code>&lt;img src=x onerror=alert(1)&gt;</code></p>', strip=['code'], escape_misc=True)
'run <img src=x onerror=alert(1)>'
>>> md('<pre>&lt;script&gt;alert(1)&lt;/script&gt;</pre>', convert=['p', 'b', 'i', 'a'], escape_misc=True)
'<script>alert(1)</script>'

markdown-it, commonmark.py, mistune and python-markdown all render both as live html, while the same text outside those tags comes out as \<img src\=x onerror\=alert(1)\>. an allowlist like the readme's convert=['b'] leaves all four tags out unless they are listed, so that setup hits this without stripping anything explicitly. it is an output-escaping hole (CWE-116) like #299, but with a different cause: here no converter runs at all.

_noformat is now only added when should_convert_tag() says the tag is converted, so a stripped tag's contents are escaped and formatted like any other text, which is what strip already does for every other tag. converted tags behave exactly as before. a <code> inside a stripped <pre> now becomes an ordinary code span, so a line break inside it is the case #303 handles.

checked against develop on 4,000 random documents: with options that keep the four tags converted (defaults, escape_misc, strip=['a', 'b'], a convert list that includes them, wrap) all 20,000 conversions are identical; with all four stripped or left out of convert=, develop renders a raw <img> or <script> in 3,310 of 8,000 conversions in each of markdown-it, commonmark.py and mistune, and this branch in none. the new test_strip_preformatted in tests/test_args.py fails on develop and passes here. pytest (96 passed), flake8, restructuredtext-lint, mypy and the wheel/sdist build are clean on python 3.15.

process_tag marked every <pre>, <code>, <kbd> and <samp> with the
'_noformat' pseudo-tag, so process_text skipped escaping for their
contents even when the tag was stripped or left out of a convert list.
No fence or code span is emitted in that case, so the text came out
with no escaping at all and escape_misc=True was bypassed:
'<code>&lt;img src=x onerror=alert(1)&gt;</code>' with strip=['code']
became a raw <img> tag. Only add '_noformat' when the tag is converted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant