Skip to content

Commit 901b222

Browse files
committed
security: corrected patch of unsafe heading regex
1 parent 58ab719 commit 901b222

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lib/marked.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ var block = {
1616
code: /^( {4}[^\n]+\n*)+/,
1717
fences: noop,
1818
hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
19-
// TODO REDOS: Replace ' *([^\n]+?) *' with dedicated parsing.
20-
heading: /^ *(#{1,6}) *([^\n]+?) *(?:#+ *)?(?:\n+|$)/,
19+
// cap[2] might be ' HEADING # ' and must be trimmed appropriately.
20+
heading: /^ *(#{1,6})([^\n]*)(?:\n+|$)/,
2121
nptable: noop,
2222
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
2323
list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
@@ -94,8 +94,8 @@ block.normal = merge({}, block);
9494
block.gfm = merge({}, block.normal, {
9595
fences: /^ *(`{3,}|~{3,})[ \.]*(\S+)? *\n([\s\S]*?)\n? *\1 *(?:\n+|$)/,
9696
paragraph: /^/,
97-
// TODO REDOS: Replace ' *([^\n]+?) *' with dedicated parsing.
98-
heading: /^ *(#{1,6}) +([^\n]+?) *(?:#+ *)?(?:\n+|$)/
97+
// cap[2] might be ' HEADING # ' and must be trimmed appropriately.
98+
heading: /^ *(#{1,6}) ([^\n]+)(?:\n+|$)/
9999
});
100100

101101
block.gfm.paragraph = edit(block.paragraph)
@@ -237,10 +237,18 @@ Lexer.prototype.token = function(src, top) {
237237
// heading
238238
if (cap = this.rules.heading.exec(src)) {
239239
src = src.substring(cap[0].length);
240+
// cap[2] might be ' HEADING # '
241+
item = cap[2].trim();
242+
if (item.slice(-1) === '#') {
243+
// NB replace(/#+$/) is quadratic on mismatch because it's unanchored,
244+
// so we protect with if-check to ensure it won't mismatch.
245+
item = item.replace(/#+$/, '');
246+
}
247+
item = item.trim()
240248
this.tokens.push({
241249
type: 'heading',
242250
depth: cap[1].length,
243-
text: cap[2]
251+
text: item
244252
});
245253
continue;
246254
}

0 commit comments

Comments
 (0)