Skip to content

Commit b788eee

Browse files
committed
Fix escaping in autolinks, labels
Related to: aef5cce.
1 parent bd9c2be commit b788eee

File tree

5 files changed

+45
-17
lines changed

5 files changed

+45
-17
lines changed

lib/handle/image-reference.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ function imageReference(node, _, context) {
1010
var subexit = context.enter('label')
1111
var alt = safe(context, node.alt, {before: '[', after: ']'})
1212
var reference
13+
var currentStack
1314

1415
subexit()
15-
16+
// Hide the fact that we’re in phrasing, because escapes don’t work.
17+
currentStack = context.stack
18+
context.stack = []
1619
subexit = context.enter('reference')
1720
reference = safe(context, association(node), {before: '[', after: ']'})
1821
subexit()
19-
22+
context.stack = currentStack
2023
exit()
2124

2225
if (type !== 'full' && alt && alt === reference) {

lib/handle/link-reference.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ function linkReference(node, _, context) {
1111
var subexit = context.enter('label')
1212
var text = phrasing(node, context, {before: '[', after: ']'})
1313
var reference
14+
var currentStack
1415

1516
subexit()
16-
17+
// Hide the fact that we’re in phrasing, because escapes don’t work.
18+
currentStack = context.stack
19+
context.stack = []
1720
subexit = context.enter('reference')
1821
reference = safe(context, association(node), {before: '[', after: ']'})
1922
subexit()
20-
23+
context.stack = currentStack
2124
exit()
2225

2326
if (type !== 'full' && text && text === reference) {

lib/handle/link.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module.exports = link
22
link.peek = linkPeek
33

4-
var toString = require('mdast-util-to-string')
54
var checkQuote = require('../util/check-quote')
65
var formatLinkAsAutolink = require('../util/format-link-as-autolink')
76
var phrasing = require('../util/container-phrasing')
@@ -22,7 +21,7 @@ function link(node, _, context) {
2221
currentStack = context.stack
2322
context.stack = []
2423
exit = context.enter('autolink')
25-
value = '<' + toString(node) + '>'
24+
value = '<' + phrasing(node, context, {before: '<', after: '>'}) + '>'
2625
exit()
2726
context.stack = currentStack
2827
return value

lib/unsafe.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = [
3838
{atBreak: true, character: '#'},
3939
// Dollar sign and percentage are not used in markdown.
4040
// An ampersand could start a character reference.
41-
{character: '&', after: '[#A-Za-z]'},
41+
{character: '&', after: '[#A-Za-z]', inConstruct: 'phrasing'},
4242
// An apostrophe can break out of a title.
4343
{character: "'", inConstruct: 'titleApostrophe'},
4444
// A left paren could break out of a destination raw.

test.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,12 +1151,17 @@ test('imageReference', function (t) {
11511151

11521152
t.equal(
11531153
to({
1154-
type: 'imageReference',
1155-
alt: '&a;',
1156-
identifier: '&b;',
1157-
referenceType: 'full'
1154+
type: 'paragraph',
1155+
children: [
1156+
{
1157+
type: 'imageReference',
1158+
alt: '&a;',
1159+
identifier: '&b;',
1160+
referenceType: 'full'
1161+
}
1162+
]
11581163
}),
1159-
'![\\&a;][\\&b;]\n',
1164+
'![\\&a;][&b;]\n',
11601165
'should support incorrect character references'
11611166
)
11621167

@@ -1513,12 +1518,17 @@ test('linkReference', function (t) {
15131518

15141519
t.equal(
15151520
to({
1516-
type: 'linkReference',
1517-
children: [{type: 'text', value: '&a;'}],
1518-
identifier: '&b;',
1519-
referenceType: 'full'
1521+
type: 'paragraph',
1522+
children: [
1523+
{
1524+
type: 'linkReference',
1525+
children: [{type: 'text', value: '&a;'}],
1526+
identifier: '&b;',
1527+
referenceType: 'full'
1528+
}
1529+
]
15201530
}),
1521-
'[\\&a;][\\&b;]\n',
1531+
'[\\&a;][&b;]\n',
15221532
'should support incorrect character references'
15231533
)
15241534

@@ -2467,5 +2477,18 @@ test('roundtrip', function (t) {
24672477
'should roundtrip autolinks w/ potentially escapable characters'
24682478
)
24692479

2480+
doc = [
2481+
'A [primary][toString], [secondary][constructor], and [tertiary][__proto__] link.',
2482+
'',
2483+
'[toString]: http://primary.com',
2484+
'',
2485+
'[__proto__]: http://tertiary.com',
2486+
'',
2487+
'[constructor]: http://secondary.com',
2488+
''
2489+
].join('\n')
2490+
2491+
t.equal(to(from(doc)), doc, 'should roundtrip potential prototype injections')
2492+
24702493
t.end()
24712494
})

0 commit comments

Comments
 (0)