Skip to content

Fix backslash-escaped quotes breaking link titles with smarty-pants - #779

Open
SwastikTripathi wants to merge 2 commits into
trentm:masterfrom
SwastikTripathi:fix/smarty-pants-title-quotes
Open

SwastikTripathi wants to merge 2 commits into
trentm:masterfrom
SwastikTripathi:fix/smarty-pants-title-quotes

Conversation

@SwastikTripathi

Copy link
Copy Markdown
Contributor

With the smarty-pants extra, a backslash-escaped double quote in an inline link or image title closes the title attribute early:

>>> markdown2.markdown('[a](http://x "Say \\"hi\\"")', extras=["smarty-pants"])
'<p><a href="http://x" title="Say "hi"">a</a></p>\n'

The rest of the title then lands in the tag as stray attributes ("x\" onclick=\"y" gives title="x" onclick="y"). With safe_mode on as well, the quote's hash leaks into the output instead: title="Saymd5-19d5...hi...".

smarty-pants adds " to the escape table, so _encode_backslash_escapes has already replaced \" with that hash by the time the link is built. _xml_escape_attr never sees a quote to escape, and unhashing at the end puts back a raw " inside the attribute. The fix turns the hash back into a quote before the title is escaped, so it comes out as &quot;, the same as without the extra.

Test: new smarty_pants_escaped_title_quotes tm-case (inline link and image). It fails before the fix and passes after. python test.py has the same 14 failures as on master, and only the new case differs between the two runs.

@SwastikTripathi

Copy link
Copy Markdown
Contributor Author

The 4 red jobs are the timing-based ReDoS check (redos/issue493 took 4.1s on the macOS and Windows runners). The run on the first commit passed every job, and the only change since is the CHANGES.md line, so it looks like runner slowness. Could someone re-run them? I don't have permission to.

@SwastikTripathi

Copy link
Copy Markdown
Contributor Author

The build (3.11, macos-latest) failure is a timing fluke: markdown2/redos/issue493 ran in 4.130s against a 4-second timeout on that runner. The test is a ReDoS benchmark for bold/italic nesting patterns; our change only touches link title processing when smarty-pants is active. It passes on ubuntu-latest, windows-latest, and all other Python versions on macOS. Running the suite locally completes issue493 in 1.2s. The macOS 3.11 GitHub runner was just slow on this run.

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