File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ module.exports = wrapper
88var own = { } . hasOwnProperty
99
1010var allData = 'data*'
11+ var commentEnd = '-->'
1112
1213var nodeSchema = {
1314 root : { children : all } ,
@@ -326,7 +327,15 @@ function handleDoctype(schema) {
326327}
327328
328329function handleComment ( schema ) {
329- return schema . allowComments ? { value : handleValue } : null
330+ return schema . allowComments ? { value : handleCommentValue } : null
331+ }
332+
333+ // See <https://html.spec.whatwg.org/multipage/parsing.html#serialising-html-fragments>
334+ function handleCommentValue ( schema , value ) {
335+ var val = typeof value === 'string' ? value : ''
336+ var index = val . indexOf ( commentEnd )
337+
338+ return index === - 1 ? val : val . slice ( 0 , index )
330339}
331340
332341// Sanitize `value`.
Original file line number Diff line number Diff line change @@ -68,6 +68,22 @@ test('sanitize()', function(t) {
6868 'should allow `comment`s with `allowComments: true`'
6969 )
7070
71+ st . equal (
72+ html ( sanitize ( u ( 'comment' , { toString : toString } ) , { allowComments : true } ) ) ,
73+ '<!---->' ,
74+ 'should ignore non-string `value`s with `allowComments: true`'
75+ )
76+
77+ st . equal (
78+ html (
79+ sanitize ( u ( 'comment' , 'alpha--><script>alert(1)</script><!--bravo' ) , {
80+ allowComments : true
81+ } )
82+ ) ,
83+ '<!--alpha-->' ,
84+ 'should not break out of comments with `allowComments: true`'
85+ )
86+
7187 st . end ( )
7288 } )
7389
You can’t perform that action at this time.
0 commit comments