Escape link/image destinations and image alt text (#261)#262
Open
assinscreedFC wants to merge 1 commit into
Open
Escape link/image destinations and image alt text (#261)#262assinscreedFC wants to merge 1 commit into
assinscreedFC wants to merge 1 commit into
Conversation
convert_a, convert_img and convert_video emitted attribute values and generated labels into Markdown link/image syntax without escaping them for that context. A destination containing spaces or parentheses, or an image alt containing ']', could truncate the syntax so downstream parsers read different HTML than the original -- including substituting an attacker-controlled URL (e.g. alt='](http://attacker)'). Add escape_link_destination() (wrap destinations with whitespace or parentheses in angle brackets) and escape_link_text() (escape '[' and ']' in verbatim alt text), and apply them in the link/image converters. Fixes matthewwithanm#261
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.
Summary
Fixes #261.
convert_a,convert_imgandconvert_videodropped raw attribute values and generated labels into Markdown link/image syntax without escaping them for that context. As reported, this lets a destination oraltvalue truncate the syntax, so downstream Markdown parsers read different HTML than the original — including substituting an attacker-controlled URL:Changes
Two helpers in
markdownify/__init__.py:escape_link_destination(url)— wraps a destination containing whitespace or parentheses in angle brackets (CommonMark allows spaces and()inside<...>), escaping any</>. Applied tohref(convert_a),src(convert_img,convert_video) andposter(convert_video).escape_link_text(text)— escapes[and](and backslashes) inalttext taken verbatim from HTML, so it can't close the[...]label early. Applied toaltinconvert_img.Verified against all reporter cases:
<img src="/a" alt="]">![\]](/a)<img src="/a b" alt="x"><img src="/safe" alt="](http://attacker)">](/safe)<a href="/a)b">click</a>[click](</a)b>)Unaffected inputs (no spaces/parens/brackets) are emitted unchanged, so existing output is preserved.
Scope note
I left link text for
convert_a/convert_videoalone, since that is already-rendered Markdown from child nodes (escaped viaself.escape()), not a verbatim attribute. Happy to extend if you'd prefer.Test plan
test_a_escapes_destination,test_img_escapes_link_syntax,test_video_escapes_destinationintests/test_conversions.py.pytest— 86 passed.flake8 --ignore=E501,W503 markdownify tests— clean.