Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(markdown): XSS Prevention #1832

Merged
merged 2 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/runtime/components/ContentRendererMarkdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ function renderNode (node: MarkdownNode, h: CreateElement, documentMeta: ParsedC
return h(Text, node.value)
}

if (node.tag === 'script') {
return renderToText(node)
}

const originalTag = node.tag!
// `_ignoreMap` is an special prop to disables tag-mapper
const renderTag: string = (typeof node.props?.__ignoreMap === 'undefined' && documentMeta.tags[originalTag]) || originalTag
Expand All @@ -137,6 +141,18 @@ function renderNode (node: MarkdownNode, h: CreateElement, documentMeta: ParsedC
)
}

function renderToText (node: MarkdownNode) {
if (node.type === 'text') {
return node.value
}

if (!node.children?.length) {
return `<${node.tag}>`
}

return `<${node.tag}>${node.children?.map(renderToText).join('') || ''}</${node.tag}>`
}

function renderBinding (node: MarkdownNode, h: CreateElement, documentMeta: ParsedContentMeta, parentScope: any = {}): VNode {
const data = {
...parentScope,
Expand Down
5 changes: 5 additions & 0 deletions test/features/renderer-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,10 @@ export const testMarkdownRenderer = () => {
expect(html).not.contains('<meta property="og:image" content="https://picsum.photos/200/300">')
expect(html).not.contains('<meta name="description" content="Description overwritten"><meta property="og:image" content="https://picsum.photos/200/300">')
})

test('XSS Prevention', async () => {
const html = await $fetch('/_partial/xss')
expect(html).contains('&lt;script&gt;console.log(&#39;xss&#39;)&lt;/script&gt;')
})
})
}
1 change: 1 addition & 0 deletions test/fixtures/basic/content/_partial/xss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script>console.log('xss')</script>