diff --git a/docs/content/4.plugins/1.built-in/security.md b/docs/content/4.plugins/1.built-in/security.md index 6d7cfd2d..0fd339f8 100644 --- a/docs/content/4.plugins/1.built-in/security.md +++ b/docs/content/4.plugins/1.built-in/security.md @@ -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. diff --git a/packages/comark-vue/test/bound-component-props.test.ts b/packages/comark-vue/test/bound-component-props.test.ts index 75d577a4..6c43dbf2 100644 --- a/packages/comark-vue/test/bound-component-props.test.ts +++ b/packages/comark-vue/test/bound-component-props.test.ts @@ -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=""}\n::`, + `::poly{is="script"}\n::`, + `::poly{as="OBJECT"}\n::`, + ]) { + const html = await renderMarkdown(markdown) + expect(html).not.toMatch(/onerror|onload|]*>poly<\/div>/) + } + }) + it('keeps a non-object `tag`', async () => { const html = await renderMarkdown(`::poly{:tag='"section"'}\n::`) expect(html).toContain('
poly
') diff --git a/packages/comark/src/internal/sanitize.ts b/packages/comark/src/internal/sanitize.ts index 87e031d1..b9694430 100644 --- a/packages/comark/src/internal/sanitize.ts +++ b/packages/comark/src/internal/sanitize.ts @@ -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. @@ -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)) } diff --git a/packages/comark/test/plugins/security.test.ts b/packages/comark/test/plugins/security.test.ts index 1ceb6a17..f328b66e 100644 --- a/packages/comark/test/plugins/security.test.ts +++ b/packages/comark/test/plugins/security.test.ts @@ -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'], diff --git a/test/bundle.test.ts b/test/bundle.test.ts index b0d64568..aac8fcb8 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -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)", } `) })