Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/4.plugins/1.built-in/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Framework renderers (Vue, React, Svelte, Angular) never forward `innerHTML`, `da

Renderers hand bound props to components, and Vue treats a plain object with a `template`, `render`, `ssrRender`, `setup`, `extends` or `mixins` key as a component definition, one with `__v_isVNode` as a ready-made element, and one with `__isTeleport`, `__isSuspense` or `__isKeepAlive` as a built-in component. Its template can then run during server rendering, or the element skips tag filtering. These values are always dropped:

- `as` and `is` (and their bindings) that are not strings, since they name the element or component to render: Vue mounts an array as a component, and React throws on one. `tag` is often a data prop, so only objects are dropped there.
- `as` and `is` (and their bindings) that are not strings, since they name the element or component to render: Vue mounts an array as a component, and React throws on one. Strings must be a plain tag or component name, since Vue's server renderer writes anything else (`img src=x onerror=…`) into the opening tag, and can't name a tag that runs code (`script`, `style`, `iframe`, `object`, `embed`, …). `tag` is often a data prop, so only objects are dropped there.
- Any object or array with one of those keys at any depth, such as `:icon='{"extends": {"template": "..."}}'` or `:icon='{"__v_isVNode": true, "type": "script"}'`.
- An `__v_isVNode` attribute on the element itself, bound or not. Vue would otherwise render the element's attributes as a ready-made element.

Expand Down
15 changes: 15 additions & 0 deletions packages/comark-vue/test/bound-component-props.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ describe('bound props cannot smuggle a component into a polymorphic slot', () =>
}
})

it('does not render an `as` or `is` that is not a plain tag name, without the security plugin', async () => {
for (const markdown of [
`::poly{as="img src=x onerror=alert(1)"}\n::`,
`::poly{:as='"svg onload=alert(1)"'}\n::`,
`::poly{as="iframe" srcdoc="<script>alert(1)</script>"}\n::`,
`::poly{is="script"}\n::`,
`::poly{as="OBJECT"}\n::`,
]) {
const html = await renderMarkdown(markdown)
expect(html).not.toMatch(/onerror|onload|<iframe|<script|<object/i)
// other attributes stay on the fallback `div`, where they are inert
expect(html).toMatch(/<div[^>]*>poly<\/div>/)
}
})

it('keeps a non-object `tag`', async () => {
const html = await renderMarkdown(`::poly{:tag='"section"'}\n::`)
expect(html).toContain('<section>poly</section>')
Expand Down
25 changes: 24 additions & 1 deletion packages/comark/src/internal/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,29 @@ export function isUnsafeRendererProp(attribute: string, value: unknown): boolean
return isComponentSelectorViolation(attribute, value) || hasUnsafeObjectKeys(value)
}

// A selector string ends up as an element name, so it must be a plain tag or component name: Vue's
// server renderer writes anything else (`img src=x onerror=…`) into the opening tag. These tags also
// run or load code from their content or attributes.
const SAFE_SELECTOR_NAME = /^[a-z][\w-]*$/i
const unsafeSelectorTags = new Set([
'script',
'style',
'iframe',
'frame',
'frameset',
'object',
'embed',
'applet',
'base',
'link',
'meta',
'noscript',
])

function isUnsafeSelectorName(value: string): boolean {
return !SAFE_SELECTOR_NAME.test(value) || unsafeSelectorTags.has(value.toLowerCase())
}

/**
* `as` and `is` name the element or component to render, so only a string (or nothing) may reach them:
* Vue mounts any object, arrays included, as a component, and React throws on a non-string element type.
Expand All @@ -445,7 +468,7 @@ export function isUnsafeRendererProp(attribute: string, value: unknown): boolean
*/
function isComponentSelectorViolation(attribute: string, value: unknown): boolean {
const name = normalizePropName(attribute)
if (tagSelectors.includes(name)) return value != null && typeof value !== 'string'
if (tagSelectors.includes(name)) return value != null && (typeof value !== 'string' || isUnsafeSelectorName(value))
if (name !== 'tag') return false
return typeof value === 'function' || (typeof value === 'object' && value !== null && !Array.isArray(value))
}
Expand Down
12 changes: 12 additions & 0 deletions packages/comark/test/plugins/security.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,18 @@ describe('security plugin — component-like values', () => {
expect((tree.nodes[1] as [string, any])[1]).toEqual({ as: 'section', ':tag': '5' })
})

it('strips an as or is that is not a plain tag name or names a tag that runs code', async () => {
const tree = makeTree([
['span', { as: 'img src=x onerror=alert(1)' }, 'x'],
['span', { ':as': '"iframe"', is: 'SCRIPT' }, 'x'],
['span', { as: 'section', ':is': '"prose-field"' }, 'x'],
])
await runPlugin(tree)
expect((tree.nodes[0] as [string, any])[1]).toEqual({})
expect((tree.nodes[1] as [string, any])[1]).toEqual({})
expect((tree.nodes[2] as [string, any])[1]).toEqual({ as: 'section', ':is': '"prose-field"' })
})

it('strips a forged VNode at any depth', async () => {
const tree = makeTree([
['span', { ':icon': '{"__v_isVNode":true,"type":"script","shapeFlag":1}' }, 'x'],
Expand Down
2 changes: 1 addition & 1 deletion test/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('package bundle size', { timeout: 60_000 }, () => {
"@comark/react": "42.9k (83 files)",
"@comark/svelte": "50.3k (91 files)",
"@comark/vue": "62.6k (89 files)",
"comark": "439k (171 files)",
"comark": "440k (171 files)",
}
`)
})
Expand Down
Loading