fix: prevent HTML injections to code blocks (#1165)
This commit is contained in:
parent
1a4fd19720
commit
c15df78cbb
5 changed files with 88 additions and 25 deletions
|
@ -136,6 +136,39 @@ describe('content-rich', () => {
|
|||
"
|
||||
`)
|
||||
})
|
||||
|
||||
it ('block with injected html, without language', async () => {
|
||||
const { formatted } = await render(`
|
||||
<pre>
|
||||
<code>
|
||||
<a href="javascript:alert(1)">click me</a>
|
||||
</code>
|
||||
</pre>
|
||||
`)
|
||||
expect(formatted).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it ('block with injected html, with an unknown language', async () => {
|
||||
const { formatted } = await render(`
|
||||
<pre>
|
||||
<code class="language-xyzzy">
|
||||
<a href="javascript:alert(1)">click me</a>
|
||||
</code>
|
||||
</pre>
|
||||
`)
|
||||
expect(formatted).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it ('block with injected html, with a known language', async () => {
|
||||
const { formatted } = await render(`
|
||||
<pre>
|
||||
<code class="language-js">
|
||||
<a href="javascript:alert(1)">click me</a>
|
||||
</code>
|
||||
</pre>
|
||||
`)
|
||||
expect(formatted).toMatchSnapshot()
|
||||
})
|
||||
})
|
||||
|
||||
async function render(content: string, options?: ContentParseOptions) {
|
||||
|
@ -173,23 +206,11 @@ vi.mock('~/composables/dialog.ts', () => {
|
|||
return {}
|
||||
})
|
||||
|
||||
vi.mock('~/components/content/ContentCode.vue', () => {
|
||||
vi.mock('shiki-es', async (importOriginal) => {
|
||||
const mod = await importOriginal()
|
||||
return {
|
||||
default: defineComponent({
|
||||
props: {
|
||||
code: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
lang: {
|
||||
type: String,
|
||||
},
|
||||
},
|
||||
setup(props) {
|
||||
const raw = computed(() => decodeURIComponent(props.code).replace(/'/g, '\''))
|
||||
return () => h('pre', { lang: props.lang }, raw.value)
|
||||
},
|
||||
}),
|
||||
...(mod as any),
|
||||
setCDN() {},
|
||||
}
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue