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
1 change: 1 addition & 0 deletions docs/content/4.plugins/1.built-in/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Renderers hand bound props to components, and Vue treats a plain object with a `

- `as`, `is` and `tag` (and their bindings) that are objects. Strings, arrays, numbers and booleans stay: Vue renders nothing for the last three.
- 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.

Data props keep working, for example `:items='[{"label": "a"}]'`. The Vue renderer also only resolves `as` to a component when it is a string. Frontmatter is checked for component options only, so data such as `tag: [vue, nuxt]` stays; a binding that passes a frontmatter object to `as`, `is` or `tag` is still dropped.

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

it("does not render an element's own attributes as a forged VNode", async () => {
// `h(tag, props)` renders a props bag marked as a VNode as a child, whatever the tag filters allow.
const attrs = '{:__v_isVNode="true" type="script" :shapeFlag="9" children="probe"}'
for (const markdown of [`::wrapper${attrs}\n::`, `x :wrapper${attrs}`]) {
for (const plugins of [[], [security({ allowedTags: ['wrapper', 'p'] })]]) {
const tree = await parseMarkdown(markdown, { plugins })
const app = createSSRApp({ setup: () => () => h(MarkdownDocument, { value: tree }) })
const html = await renderToString(app as any)
expect(html).not.toContain('<script')
expect(html).toContain('<wrapper')
}
}
})

it('keeps string tags for `as`', async () => {
const html = await renderMarkdown('::poly{:as=\'"section"\'}\n::')
expect(html).toContain('<section>poly</section>')
Expand Down
7 changes: 6 additions & 1 deletion packages/comark/src/internal/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ export function isHtmlSinkKey(key: string): boolean {
return htmlSinkProps.has(key.replace(/^[.^]/, '').toLowerCase())
}

/** Whether attribute `key` is Vue's VNode marker: `h(tag, props)` renders a props bag that has it as a forged child. */
export function isVNodeMarkerKey(key: string): boolean {
return key.replace(/^(?::|v-bind:)/, '').toLowerCase() === '__v_isvnode'
}

/**
* Whether a decoded prop value is unsafe to hand a renderer: `as` / `is` / `tag`
* must not be objects, and no object at any depth may carry component options or
Expand Down Expand Up @@ -400,7 +405,7 @@ export function isUnsafeTagSelector(key: string, value: unknown, isFilteredTag:

export function validateProp(attribute: string, value: unknown, options: PropsValidationOptions = {}): unknown {
const name = normalizePropName(attribute)
if (isUnsafePropName(name)) {
if (isUnsafePropName(name) || isVNodeMarkerKey(attribute)) {
return REJECTED_PROP
}

Expand Down
6 changes: 3 additions & 3 deletions packages/comark/src/internal/stringify/html-attributes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { escapeHtml, get } from '../../utils/index.ts'
import { isHtmlSinkKey, isUnsafeRendererProp, isUnsafeUrlValue, isUrlPropKey } from '../sanitize.ts'
import { isHtmlSinkKey, isUnsafeRendererProp, isUnsafeUrlValue, isUrlPropKey, isVNodeMarkerKey } from '../sanitize.ts'
import type { NodeRenderData } from '../../types.ts'

export interface ResolveAttributesOptions {
Expand Down Expand Up @@ -43,8 +43,8 @@ export function resolveAttributes(
const value = attrs[key]
const isBinding = key.charCodeAt(0) === 58 /* ':' */
const outKey = isBinding ? key.slice(1) : key
// HTML sinks are never forwarded from document attributes; see `isHtmlSinkKey`
if (isHtmlSinkKey(outKey)) continue
// HTML sinks and the VNode marker are never forwarded from document attributes
if (isHtmlSinkKey(outKey) || isVNodeMarkerKey(outKey)) continue

let outValue: unknown
let resultKey = key
Expand Down
10 changes: 10 additions & 0 deletions packages/comark/test/plugins/security.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,16 @@ describe('security plugin — component-like values', () => {
expect((tree.nodes[1] as [string, any])[1]).toEqual({ ':config': '{"label":"a"}' })
})

it("strips the VNode marker from an element's own attributes", async () => {
const tree = makeTree([
['wrapper', { ':__v_isVNode': 'true', type: 'script', ':shapeFlag': '9' }],
['wrapper', { __v_isVNode: true, 'v-bind:__v_isVNode': 'true', title: 'a' }],
])
await runPlugin(tree)
expect((tree.nodes[0] as [string, any])[1]).toEqual({ type: 'script', ':shapeFlag': '9' })
expect((tree.nodes[1] as [string, any])[1]).toEqual({ title: 'a' })
})

it('strips a bound object carrying component options', async () => {
const tree = makeTree([
['span', { ':icon': '{"extends":{"template":"<b>x</b>"}}' }, 'x'],
Expand Down
Loading