File tree 2 files changed +26
-1
lines changed
2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ module.exports = wrapper
8
8
var own = { } . hasOwnProperty
9
9
10
10
var allData = 'data*'
11
+ var commentEnd = '-->'
11
12
12
13
var nodeSchema = {
13
14
root : { children : all } ,
@@ -326,7 +327,15 @@ function handleDoctype(schema) {
326
327
}
327
328
328
329
function 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 )
330
339
}
331
340
332
341
// Sanitize `value`.
Original file line number Diff line number Diff line change @@ -68,6 +68,22 @@ test('sanitize()', function(t) {
68
68
'should allow `comment`s with `allowComments: true`'
69
69
)
70
70
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
+
71
87
st . end ( )
72
88
} )
73
89
You can’t perform that action at this time.
0 commit comments