Skip to content

Commit 1f4c17f

Browse files
authored
Merge branch 'master' into spec-options
2 parents 44dbeeb + 6bae3a2 commit 1f4c17f

15 files changed

+64
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Also read about:
3939

4040
## Usage
4141

42-
### Warning: 🚨 Marked does not [sanitize](https://marked.js.org/#/USING_ADVANCED.md#options) the output HTML by default 🚨
42+
### Warning: 🚨 Marked does not [sanitize](https://marked.js.org/#/USING_ADVANCED.md#options) the output HTML. Please use a sanitize library, like [DOMPurify](https://github.com/cure53/DOMPurify) (recommended), [sanitize-html](https://github.com/apostrophecms/sanitize-html) or [insane](https://github.com/bevacqua/insane) on the output HTML! 🚨
4343

4444
**CLI**
4545

docs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ These documentation pages are also rendered using marked 💯
2525

2626
<h2 id="usage">Usage</h2>
2727

28-
### Warning: 🚨 Marked does not [sanitize](https://marked.js.org/#/USING_ADVANCED.md#options) the output HTML by default 🚨
28+
### Warning: 🚨 Marked does not [sanitize](https://marked.js.org/#/USING_ADVANCED.md#options) the output HTML. Please use a sanitize library, like [DOMPurify](https://github.com/cure53/DOMPurify) (recommended), [sanitize-html](https://github.com/apostrophecms/sanitize-html) or [insane](https://github.com/bevacqua/insane) on the output HTML! 🚨
2929

3030
**CLI**
3131

docs/USING_ADVANCED.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ console.log(marked(markdownString));
5050
|mangle |`boolean` |`true` |v0.3.4 |If true, autolinked email address is escaped with HTML character references.|
5151
|pedantic |`boolean` |`false` |v0.2.1 |If true, conform to the original `markdown.pl` as much as possible. Don't fix original markdown bugs or behavior. Turns off and overrides `gfm`.|
5252
|renderer |`object` |`new Renderer()`|v0.3.0|An object containing functions to render tokens to HTML. See [extensibility](USING_PRO.md) for more details.|
53-
|sanitize |`boolean` |`false` |v0.2.1 |If true, sanitize the HTML passed into `markdownString` with the `sanitizer` function.|
53+
|sanitize |`boolean` |`false` |v0.2.1 |If true, sanitize the HTML passed into `markdownString` with the `sanitizer` function.<br>**Warning**: This feature is deprecated and it should NOT be used as it cannot be considered secure.<br>Instead use a sanitize library, like [DOMPurify](https://github.com/cure53/DOMPurify) (recommended), [sanitize-html](https://github.com/apostrophecms/sanitize-html) or [insane](https://github.com/bevacqua/insane) on the output HTML! |
5454
|sanitizer |`function`|`null` |v0.3.4 |A function to sanitize the HTML passed into `markdownString`.|
5555
|silent |`boolean` |`false` |v0.2.7 |If true, the parser does not throw any exception.|
5656
|smartLists |`boolean` |`false` |v0.2.8 |If true, use smarter list behavior than those found in `markdown.pl`.|

lib/marked.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ Lexer.prototype.token = function(src, top) {
431431
: 'html',
432432
pre: !this.options.sanitizer
433433
&& (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'),
434-
text: cap[0]
434+
text: this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0])) : cap[0]
435435
});
436436
continue;
437437
}
@@ -725,7 +725,7 @@ InlineLexer.prototype.output = function(src) {
725725
if (cap = this.rules.link.exec(src)) {
726726
var lastParenIndex = findClosingBracket(cap[2], '()');
727727
if (lastParenIndex > -1) {
728-
var linkLen = cap[0].length - (cap[2].length - lastParenIndex) - (cap[3] || '').length;
728+
var linkLen = 4 + cap[1].length + lastParenIndex;
729729
cap[2] = cap[2].substring(0, lastParenIndex);
730730
cap[0] = cap[0].substring(0, linkLen).trim();
731731
cap[3] = '';
@@ -847,7 +847,7 @@ InlineLexer.prototype.output = function(src) {
847847
if (cap = this.rules.text.exec(src)) {
848848
src = src.substring(cap[0].length);
849849
if (this.inRawBlock) {
850-
out += this.renderer.text(cap[0]);
850+
out += this.renderer.text(this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0])) : cap[0]);
851851
} else {
852852
out += this.renderer.text(escape(this.smartypants(cap[0])));
853853
}
@@ -1536,6 +1536,12 @@ function findClosingBracket(str, b) {
15361536
return -1;
15371537
}
15381538

1539+
function checkSanitizeDeprecation(opt) {
1540+
if (opt && opt.sanitize && !opt.silent) {
1541+
console.warn('marked(): sanitize and sanitizer parameters are deprecated since version 0.7.0, should not be used and will be removed in the future. Read more here: https://marked.js.org/#/USING_ADVANCED.md#options');
1542+
}
1543+
}
1544+
15391545
/**
15401546
* Marked
15411547
*/
@@ -1557,6 +1563,7 @@ function marked(src, opt, callback) {
15571563
}
15581564

15591565
opt = merge({}, marked.defaults, opt || {});
1566+
checkSanitizeDeprecation(opt);
15601567

15611568
var highlight = opt.highlight,
15621569
tokens,
@@ -1621,6 +1628,7 @@ function marked(src, opt, callback) {
16211628
}
16221629
try {
16231630
if (opt) opt = merge({}, marked.defaults, opt);
1631+
checkSanitizeDeprecation(opt);
16241632
return Parser.parse(Lexer.lex(src, opt), opt);
16251633
} catch (e) {
16261634
e.message += '\nPlease report this to https://github.com/markedjs/marked.';

test/specs/new/links_paren.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p>(<a href="http://example.com/1">one</a>) (<a href="http://example.com/2">two</a>)</p>
2+
3+
<p>(<a href="http://example.com/1">one</a>) (<a href="http://example.com/2">two</a>)</p>
4+
5+
<p>(<a href="http://example.com/1" title="a">one</a>) (<a href="http://example.com/2" title="b">two</a>)</p>

test/specs/new/links_paren.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
([one](http://example.com/1)) ([two](http://example.com/2))
2+
3+
([one](http://example.com/1)) ([two](http://example.com/2))
4+
5+
([one](http://example.com/1 "a")) ([two](http://example.com/2 "b"))

test/specs/run-spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ function runSpecs(title, dir, showCompletionTable, options) {
1616
spec.options = Object.assign({}, options, (spec.options || {}));
1717
const example = (spec.example ? ' example ' + spec.example : '');
1818
const passFail = (spec.shouldFail ? 'fail' : 'pass');
19+
if (spec.options.sanitizer) {
20+
// eslint-disable-next-line no-eval
21+
spec.options.sanitizer = eval(spec.options.sanitizer);
22+
}
1923
(spec.only ? fit : (spec.skip ? xit : it))('should ' + passFail + example, () => {
2024
const before = process.hrtime();
2125
if (spec.shouldFail) {
@@ -40,3 +44,4 @@ runSpecs('CommonMark', './commonmark', true, { gfm: false, pedantic: false, head
4044
runSpecs('Original', './original', false, { gfm: false, pedantic: true });
4145
runSpecs('New', './new');
4246
runSpecs('ReDOS', './redos');
47+
runSpecs('Security', './security', false, { silent: true }); // silent - do not show deprecation warning
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<p>AAA&lt;script&gt; &lt;img &lt;script&gt; src=x onerror=alert(1) /&gt;BBB</p>
2+
3+
<p>AAA&lt;sometag&gt; &lt;img &lt;sometag&gt; src=x onerror=alert(1)BBB</p>
4+
5+
<p>&lt;a&gt;a2&lt;a2t&gt;a2&lt;/a&gt; b &lt;c&gt;c&lt;/c&gt; d</p>
6+
<h1 id="text"><img src="URL" alt="text"></h1>
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
sanitize: true
3+
---
4+
AAA<script> <img <script> src=x onerror=alert(1) />BBB
5+
6+
AAA<sometag> <img <sometag> src=x onerror=alert(1)BBB
7+
8+
<a>a2<a2t>a2</a> b <c>c</c> d
9+
# ![text](URL)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>a2a2 b c d</p>
2+
<h1 id="text"><img src="URL" alt="text"></h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
sanitize: true
3+
sanitizer: () => ''
4+
---
5+
<a>a2<a2t>a2</a> b <c>c</c> d
6+
# ![text](URL)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>AAA</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
sanitize: true
3+
sanitizer: () => ''
4+
---
5+
AAA<script> <img <script> src=x onerror=alert(1) />BBB
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>AAA &lt;img src=x onerror=alert(1)BBB</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
sanitize: true
3+
sanitizer: () => ''
4+
---
5+
AAA<sometag> <img <sometag> src=x onerror=alert(1)BBB

0 commit comments

Comments
 (0)