fix: escape HTML entities in link href and image src attributes - #4065
Closed
madebysaira wants to merge 1 commit into
Closed
fix: escape HTML entities in link href and image src attributes#4065madebysaira wants to merge 1 commit into
madebysaira wants to merge 1 commit into
Conversation
The renderer wrote cleanUrl(href) directly into href/src attributes without escaping & to &. This caused silent link-target corruption for URLs containing valid HTML entity sequences (e.g. <, &, ©). A browser decodes these inside attributes, so the link pointed to a different URL than was written. The fix has two parts: 1. Tokenizer: decode HTML entities in inline link destinations before rendering, per CommonMark spec (entity references are recognized in link destinations but not in autolinks). 2. Renderer: escape & to & in href/src attributes after cleanUrl, ensuring the output is valid HTML that browsers interpret correctly. Added decodeHtmlEntities helper and 5 regression tests covering autolinks, inline links, images, and entity sequences. Fixes markedjs#4052
|
@madebysaira is attempting to deploy a commit to the MarkedJS Team on Vercel. A member of the Team first needs to authorize it. |
Member
|
This is already being fixed in #4053 |
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 #4052
The renderer wrote
cleanUrl(href)directly intohref/ attributes without escaping&to&. This caused silent link-target corruption for URLs containing valid HTML entity sequences (<,&,©, etc.).The bug
Any URL containing a valid entity sequence silently changes the link target. This also affects CommonMark example 595.
Root cause
Two issues:
cleanUrl()runsencodeURI()which never encodes&, so the raw&reaches the HTML attribute unescaped.[text](url)) kept HTML entities literal (&stayed as&), which is correct per CommonMark, but the renderer never escaped them on output.Fix (2 parts)
Tokenizer (
src/Tokenizer.ts) — Decode HTML entities in inline link destinations before rendering, per CommonMark spec (entity references are recognized in link destinations but not in autolinks).Renderer (
src/Renderer.ts) — Escape&→&inhref/ attributes aftercleanUrl(), ensuring valid HTML output.Helper (
src/helpers.ts) — AddeddecodeHtmlEntities()function for named/numeric character reference decoding.Test results
Regression tests added
5 new test cases in
test/unit/marked.test.jscovering:&(CommonMark example 595)&entity&<entity&in src