Skip to content

Commit

Permalink
fix(markdown): XSS Prevention (#1832)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Jan 20, 2023
1 parent 1cb91f5 commit 67c9fcf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
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>

1 comment on commit 67c9fcf

@OhB00
Copy link

@OhB00 OhB00 commented on 67c9fcf Jan 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If XSS prevention is desired this is not the best way to go about it, payloads like <img src=# onerror=alert(1)> will work or even more subtle ones like <base href=//attacker.co>.

Please sign in to comment.