From 55efe5cb28242f8b268cb659a32db126d26cd6cb Mon Sep 17 00:00:00 2001 From: Baptiste Leproux Date: Wed, 11 Nov 2026 11:04:28 +0000 Subject: [PATCH] fix(security): drop the VNode marker from element attributes Amp-Thread-ID: https://ampcode.com/threads/T-01a1ca1d-1730-71da-90c0-d1422ce5d525 --- docs/content/4.plugins/1.built-in/security.md | 1 + .../comark-vue/test/bound-component-props.test.ts | 14 ++++++++++++++ packages/comark/src/internal/sanitize.ts | 7 ++++++- .../src/internal/stringify/html-attributes.ts | 6 +++--- packages/comark/test/plugins/security.test.ts | 10 ++++++++++ 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/docs/content/4.plugins/1.built-in/security.md b/docs/content/4.plugins/1.built-in/security.md index de1dfbbb..09291f12 100644 --- a/docs/content/4.plugins/1.built-in/security.md +++ b/docs/content/4.plugins/1.built-in/security.md @@ -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. diff --git a/packages/comark-vue/test/bound-component-props.test.ts b/packages/comark-vue/test/bound-component-props.test.ts index e3e4cb69..c67038c5 100644 --- a/packages/comark-vue/test/bound-component-props.test.ts +++ b/packages/comark-vue/test/bound-component-props.test.ts @@ -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(' { const html = await renderMarkdown('::poly{:as=\'"section"\'}\n::') expect(html).toContain('
poly
') diff --git a/packages/comark/src/internal/sanitize.ts b/packages/comark/src/internal/sanitize.ts index ccc52c95..2f8248a0 100644 --- a/packages/comark/src/internal/sanitize.ts +++ b/packages/comark/src/internal/sanitize.ts @@ -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 @@ -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 } diff --git a/packages/comark/src/internal/stringify/html-attributes.ts b/packages/comark/src/internal/stringify/html-attributes.ts index 236de57a..2cfd4fdd 100644 --- a/packages/comark/src/internal/stringify/html-attributes.ts +++ b/packages/comark/src/internal/stringify/html-attributes.ts @@ -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 { @@ -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 diff --git a/packages/comark/test/plugins/security.test.ts b/packages/comark/test/plugins/security.test.ts index 29cdc2d9..2143a486 100644 --- a/packages/comark/test/plugins/security.test.ts +++ b/packages/comark/test/plugins/security.test.ts @@ -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":"x"}}' }, 'x'],