Skip to content

Commit 32cda87

Browse files
committed
Fix breaking out of comments
1 parent c9b6098 commit 32cda87

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = wrapper
88
var own = {}.hasOwnProperty
99

1010
var allData = 'data*'
11+
var commentEnd = '-->'
1112

1213
var nodeSchema = {
1314
root: {children: all},
@@ -326,7 +327,15 @@ function handleDoctype(schema) {
326327
}
327328

328329
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)
330339
}
331340

332341
// Sanitize `value`.

test.js

+16
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)