Repository navigation
fix: escape text inside stripped pre, code, kbd and samp tags - #306
Open
Chandan3456 wants to merge 1 commit into
Open
Chandan3456 wants to merge 1 commit into
Chandan3456 wants to merge 1 commit into
Conversation
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><img src=x onerror=alert(1)></code>' with strip=['code'] became a raw <img> tag. Only add '_noformat' when the tag is converted.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
process_tag adds the
_noformatpseudo-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 withstrip=or left out of aconvert=list, no converter runs and the contents come out as plain text with no escaping at all, soescape_misc=Trueand the default*/_escaping are both skipped there: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'sconvert=['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._noformatis 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 ofconvert=, 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.