Skip to content

Commit 8f432f0

Browse files
authored
fix: escape character references in autolink destinations (#4053)
1 parent 44ddc31 commit 8f432f0

6 files changed

Lines changed: 81 additions & 6 deletions

File tree

‎src/Renderer.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,22 @@ export class _Renderer<ParserOutput = string, RendererOutput = string> {
157157
return `<del>${this.parser.parseInline(tokens)}</del>` as RendererOutput;
158158
}
159159

160-
link({ href, title, tokens }: Tokens.Link): RendererOutput {
161-
const text = this.parser.parseInline(tokens) as string;
160+
link({ href, title, text, tokens, autolink }: Tokens.Link): RendererOutput {
161+
// References are not resolved inside an autolink, so every `&` there is
162+
// literal. Elsewhere only an `&` that cannot start one needs escaping.
163+
const parsedText = autolink
164+
? escapeHtmlEntities(text, true)
165+
: this.parser.parseInline(tokens) as string;
162166
const cleanHref = cleanUrl(href);
163167
if (cleanHref === null) {
164-
return text as RendererOutput;
168+
return parsedText as RendererOutput;
165169
}
166-
href = cleanHref;
170+
href = escapeHtmlEntities(cleanHref, autolink);
167171
let out = '<a href="' + href + '"';
168172
if (title) {
169173
out += ' title="' + (escapeHtmlEntities(title)) + '"';
170174
}
171-
out += '>' + text + '</a>';
175+
out += '>' + parsedText + '</a>';
172176
return out as RendererOutput;
173177
}
174178

@@ -182,7 +186,7 @@ export class _Renderer<ParserOutput = string, RendererOutput = string> {
182186
}
183187
href = cleanHref;
184188

185-
let out = `<img src="${href}" alt="${escapeHtmlEntities(text)}"`;
189+
let out = `<img src="${escapeHtmlEntities(href)}" alt="${escapeHtmlEntities(text)}"`;
186190
if (title) {
187191
out += ` title="${escapeHtmlEntities(title)}"`;
188192
}

‎src/Tokenizer.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
937937
raw: cap[0],
938938
text,
939939
href,
940+
autolink: true,
940941
tokens: [
941942
{
942943
type: 'text',
@@ -974,6 +975,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
974975
raw: cap[0],
975976
text,
976977
href,
978+
autolink: true,
977979
tokens: [
978980
{
979981
type: 'text',

‎src/Tokens.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ export namespace Tokens {
136136
title?: string | null;
137137
text: string;
138138
tokens: Token[];
139+
/**
140+
* Set for autolinks and extended (GFM) urls, where character references are
141+
* not resolved, so the destination and text are literal.
142+
*/
143+
autolink?: boolean;
139144
}
140145

141146
export interface List {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<p><a href="https://example.com/?x=1&amp;lt;2">https://example.com/?x=1&amp;lt;2</a></p>
2+
<p><a href="https://example.com/?y=1&amp;amp;2">https://example.com/?y=1&amp;amp;2</a></p>
3+
<p><a href="https://example.com/?a=1&amp;b=2">https://example.com/?a=1&amp;b=2</a></p>
4+
<p><a href="https://example.com/?x=1&amp;lt;2">https://example.com/?x=1&amp;lt;2</a></p>
5+
<p><a href="https://example.com/?y=1&amp;amp;2">https://example.com/?y=1&amp;amp;2</a></p>
6+
<p><a href="https://example.com/?a=1&amp;b=2">https://example.com/?a=1&amp;b=2</a></p>
7+
<p><a href="https://example.com/?x=1&lt;2">https://example.com/?x=1&lt;2</a></p>
8+
<p><a href="https://example.com/?y=1&amp;2">https://example.com/?y=1&amp;2</a></p>
9+
<p><a href="https://example.com/?a=1&amp;b=2">https://example.com/?a=1&amp;b=2</a></p>
10+
<p><a href="https://example.com/?x=1&lt;2">https://example.com/?x=1&lt;2</a></p>
11+
<p><a href="https://example.com/?y=1&amp;2">https://example.com/?y=1&amp;2</a></p>
12+
<p><a href="https://example.com/?a=1&amp;b=2">https://example.com/?a=1&amp;b=2</a></p>
13+
<p><img src="https://example.com/?x=1&lt;2" alt="https://example.com/?x=1&lt;2"></p>
14+
<p><img src="https://example.com/?y=1&amp;2" alt="https://example.com/?y=1&amp;2"></p>
15+
<p><img src="https://example.com/?a=1&amp;b=2" alt="https://example.com/?a=1&amp;b=2"></p>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
# Character references resolve in a link destination but not in an autolink, so
3+
# the same query string has to be escaped differently depending on where it is
4+
# written.
5+
#
6+
# The `&lt;` inline links, reference links and images below do not match
7+
# CommonMark. It resolves the reference and percent-encodes the result, giving
8+
# `?x=1%3C2`, where marked keeps `?x=1&lt;2`. That difference comes from URL
9+
# encoding rather than from this escaping, and it is the same on `master`.
10+
# Everything else here matches CommonMark.
11+
renderExact: true
12+
---
13+
https://example.com/?x=1&lt;2
14+
15+
https://example.com/?y=1&amp;2
16+
17+
https://example.com/?a=1&b=2
18+
19+
<https://example.com/?x=1&lt;2>
20+
21+
<https://example.com/?y=1&amp;2>
22+
23+
<https://example.com/?a=1&b=2>
24+
25+
[https://example.com/?x=1&lt;2](https://example.com/?x=1&lt;2)
26+
27+
[https://example.com/?y=1&amp;2](https://example.com/?y=1&amp;2)
28+
29+
[https://example.com/?a=1&b=2](https://example.com/?a=1&b=2)
30+
31+
[https://example.com/?x=1&lt;2][link1]
32+
33+
[https://example.com/?y=1&amp;2][link2]
34+
35+
[https://example.com/?a=1&b=2][link3]
36+
37+
![https://example.com/?x=1&lt;2](https://example.com/?x=1&lt;2)
38+
39+
![https://example.com/?y=1&amp;2](https://example.com/?y=1&amp;2)
40+
41+
![https://example.com/?a=1&b=2](https://example.com/?a=1&b=2)
42+
43+
[link1]: https://example.com/?x=1&lt;2
44+
[link2]: https://example.com/?y=1&amp;2
45+
[link3]: https://example.com/?a=1&b=2

‎test/unit/Lexer.test.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,7 @@ paragraph
20422042
raw: '<https://example.com>',
20432043
text: 'https://example.com',
20442044
href: 'https://example.com',
2045+
autolink: true,
20452046
tokens: [
20462047
{
20472048
type: 'text',
@@ -2064,6 +2065,7 @@ paragraph
20642065
20652066
20662067
href: 'mailto:[email protected]',
2068+
autolink: true,
20672069
tokens: [
20682070
{
20692071
type: 'text',
@@ -2085,6 +2087,7 @@ paragraph
20852087
raw: 'https://example.com',
20862088
text: 'https://example.com',
20872089
href: 'https://example.com',
2090+
autolink: true,
20882091
tokens: [
20892092
{
20902093
type: 'text',
@@ -2107,6 +2110,7 @@ paragraph
21072110
21082111
21092112
href: 'mailto:[email protected]',
2113+
autolink: true,
21102114
tokens: [
21112115
{
21122116
type: 'text',

0 commit comments

Comments
 (0)