Skip to content

Commit bd962bb

Browse files
authored
fix(shared): prevent SSR comment escaping from creating closing delimiters (#15045)
Refs GHSA-9rff-vwfp-83hv.
1 parent 164460a commit bd962bb

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

‎packages/server-renderer/__tests__/render.spec.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,12 @@ function testRender(type: string, render: typeof renderToString) {
728728
createCommentVNode('->foo'),
729729
createCommentVNode('<!--foo-->'),
730730
createCommentVNode('--!>foo<!-'),
731+
createCommentVNode('--<!--><img src=x onerror=alert(1)>'),
731732
]),
732733
),
733-
).toBe(`<div><!--foo--><!--foo--><!--foo--><!--foo--></div>`)
734+
).toBe(
735+
`<div><!--foo--><!--foo--><!--foo--><!--foo--><!--<img src=x onerror=alert(1)>--></div>`,
736+
)
734737
})
735738

736739
test('Static', async () => {

‎packages/shared/__tests__/escapeHtml.spec.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ describe('escapeHtml', () => {
2323
expect(result).toEqual(' Comment 1 Hello ! Comment 2 World!')
2424
})
2525

26+
test('ssr: escapeHTMLComment removes comment close sequences created by stripping', () => {
27+
const input = '--<!--><img src=x onerror=alert(1)>'
28+
const result = escapeHtmlComment(input)
29+
expect(result).toEqual('<img src=x onerror=alert(1)>')
30+
})
31+
2632
test('should not affect non-comment strings', () => {
2733
const input = 'Hello World'
2834
const result = escapeHtmlComment(input)

‎packages/shared/src/escapeHtml.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ export function escapeHtml(string: unknown): string {
4545
}
4646

4747
// https://www.w3.org/TR/html52/syntax.html#comments
48-
const commentStripRE = /^-?>|<!--|-->|--!>|<!-$/g
48+
const commentStripRE = /^(?:-?>)+|<!--|-->|--!>|<!-$/g
4949

5050
export function escapeHtmlComment(src: string): string {
51-
return src.replace(commentStripRE, '')
51+
let prev: string
52+
do {
53+
prev = src
54+
src = src.replace(commentStripRE, '')
55+
} while (src !== prev)
56+
return src
5257
}
5358

5459
export const cssVarNameEscapeSymbolsRE: RegExp =

0 commit comments

Comments
 (0)