Skip to content

Commit 67c9fcf

Browse files
authored
fix(markdown): XSS Prevention (#1832)
1 parent 1cb91f5 commit 67c9fcf

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/runtime/components/ContentRendererMarkdown.vue

+16
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ function renderNode (node: MarkdownNode, h: CreateElement, documentMeta: ParsedC
115115
return h(Text, node.value)
116116
}
117117
118+
if (node.tag === 'script') {
119+
return renderToText(node)
120+
}
121+
118122
const originalTag = node.tag!
119123
// `_ignoreMap` is an special prop to disables tag-mapper
120124
const renderTag: string = (typeof node.props?.__ignoreMap === 'undefined' && documentMeta.tags[originalTag]) || originalTag
@@ -137,6 +141,18 @@ function renderNode (node: MarkdownNode, h: CreateElement, documentMeta: ParsedC
137141
)
138142
}
139143
144+
function renderToText (node: MarkdownNode) {
145+
if (node.type === 'text') {
146+
return node.value
147+
}
148+
149+
if (!node.children?.length) {
150+
return `<${node.tag}>`
151+
}
152+
153+
return `<${node.tag}>${node.children?.map(renderToText).join('') || ''}</${node.tag}>`
154+
}
155+
140156
function renderBinding (node: MarkdownNode, h: CreateElement, documentMeta: ParsedContentMeta, parentScope: any = {}): VNode {
141157
const data = {
142158
...parentScope,

test/features/renderer-markdown.ts

+5
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,10 @@ export const testMarkdownRenderer = () => {
8888
expect(html).not.contains('<meta property="og:image" content="https://picsum.photos/200/300">')
8989
expect(html).not.contains('<meta name="description" content="Description overwritten"><meta property="og:image" content="https://picsum.photos/200/300">')
9090
})
91+
92+
test('XSS Prevention', async () => {
93+
const html = await $fetch('/_partial/xss')
94+
expect(html).contains('&lt;script&gt;console.log(&#39;xss&#39;)&lt;/script&gt;')
95+
})
9196
})
9297
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script>console.log('xss')</script>

0 commit comments

Comments
 (0)