Backtestsvuerun-20261007-145226#12124

Issue #12124 Merged fix PR #12125 Run 7 Oct 2026 · run-20261007-145226 vuejs/core

defineCustomElement can accidentally override members of the Element prototype

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Patch A fixes the root cause. Before defining a prop accessor, it checks the element's prototype chain (`isReservedCEKey`). If the member is read-only (such as `Element.prototype.tagName`), a method, or a VueElement internal, it skips the accessor and emits a dev warning. Writable native properties (e.g. `title`) and values set before upgrade can still be shadowed, so legitimate cases keep working. It also updates `patchProp` so that a Vue parent passing a conflicting prop sets it as an attribute instead, and the CE still receives the value. It tightens the `_setupExpose` check from `hasOwn` to `in`, so exposed members can no longer shadow native ones either. Its tests cover each of these paths: native read-only conflict, internal-member conflict, parent-rendered conflict, the writable-native allowance, and a pre-upgrade hyphenated prop. Each would fail without the change. One cost: conflicting props passed from a Vue parent become string attributes, so non-string values lose fidelity. That is a reasonable trade for not breaking the element. Patch B only adds a dev warning and still overwrites the native member. Its own tests assert that `e.tagName` becomes 'foo' and that `hasAttribute` is replaced by a plain object, which is exactly the breakage the issue complains about. A warning is one of the outcomes the reporter listed, so B is a minimal answer. But it leaves elements with broken `tagName` or methods in production, and its tests lock in the harmful behaviour. A prevents the problem while keeping the prop usable, so A is the better fix.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

11m 12s

Wall-clock time in the container

Full test suite

No new failures

3,670 passed · 6 skipped · 3,676 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

199vs 79 merged

Changed lines, ours vs merged

Vue version

3.4.5

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-aob5zf?file=index.html

Steps to reproduce

I defined a Vue component with tagName as a property, and used that, together with defineCustomElement to define the hello-world webcomponent.

0 is obviously already defined in the Element prototype.

What is expected?

I expected to maybe be able to create _only_ the tag-name attribute (and no matching tagName property), or a warning that I'm accidentally overwriting important Element properties. Or a way to remap the tag-name attribute to a different property.

What is actually happening?

Vue's defineCustomElement happily overwrites the tagName property of the Element prototype without warning.

I know this is user error, but I was surprised that Vue let me.

System Info

_No response_

Any additional comments?

In Lit, you can be explicit about what property/attribute something maps to, that would be super cool here too:

@property({ attribute: 'tag-name' }) nonConfictingPropertyName;

We managed to put us in a situation where the attribute (which is not problematic) is causing issues because Vue generated an override for the tagName property (which is problematic), while we don't use that often.

The two patches

Our patch+197 −2
packages/runtime-dom/__tests__/customElement.spec.tsindex 27c7121c2..e13d69ece 100644@@ -2124,6 +2124,154 @@ describe('defineCustomElement', () => {         `[Vue warn]: Exposed property "value" already exists on custom element.`,       ).toHaveBeenWarned()     })++    test('should not override native element properties when exposing', () => {+      const E = defineCustomElement({+        setup(_, { expose }) {+          expose({+            tagName: 'exposed',+          })+          return () => h('div', 'hi')+        },+      })+      customElements.define('my-el-expose-native', E)++      container.innerHTML = `<my-el-expose-native></my-el-expose-native>`+      const e = container.childNodes[0] as VueElement+      expect(e.tagName).toBe('MY-EL-EXPOSE-NATIVE')+      expect(Object.prototype.hasOwnProperty.call(e, 'tagName')).toBe(false)+      expect(+        `[Vue warn]: Exposed property "tagName" already exists on custom element.`,+      ).toHaveBeenWarned()+    })+  })++  describe('props conflicting with existing element properties', () => {+    test('should not override native Element properties', async () => {+      const E = defineCustomElement({+        props: {+          tagName: String,+          msg: String,+        },+        render() {+          return h('div', `${this.tagName}:${this.msg}`)+        },+      })+      customElements.define('my-el-native-prop-conflict', E)++      container.innerHTML = `<my-el-native-prop-conflict tag-name="foo" msg="hi"></my-el-native-prop-conflict>`+      const e = container.childNodes[0] as VueElement & { msg: string }++      // native property is untouched+      expect(e.tagName).toBe('MY-EL-NATIVE-PROP-CONFLICT')+      expect(Object.prototype.hasOwnProperty.call(e, 'tagName')).toBe(false)+      // the prop is still settable via its attribute+      expect(e.shadowRoot!.innerHTML).toBe(`<div>foo:hi</div>`)+      e.setAttribute('tag-name', 'bar')+      await nextTick()+      expect(e.shadowRoot!.innerHTML).toBe(`<div>bar:hi</div>`)+      // non-conflicting props are still exposed as element properties+      expect(e.msg).toBe('hi')+      e.msg = 'hello'+      await nextTick()+      expect(e.shadowRoot!.innerHTML).toBe(`<div>bar:hello</div>`)++      expect(+        `Prop "tagName" conflicts with an existing property on the custom element`,+      ).toHaveBeenWarned()+    })++    test('should not override VueElement internal members', async () => {+      const E = defineCustomElement({+        props: ['_setProp'],+        render() {+          return h('div', 'x')+        },+      })+      customElements.define('my-el-internal-prop-conflict', E)++      container.innerHTML = `<my-el-internal-prop-conflict></my-el-internal-prop-conflict>`+      const e = container.childNodes[0] as VueElement+      expect(Object.prototype.hasOwnProperty.call(e, '_setProp')).toBe(false)+      expect(typeof e._setProp).toBe('function')+      expect(+        `Prop "_setProp" conflicts with an existing property on the custom element`,+      ).toHaveBeenWarned()+    })++    test('should pass conflicting props from a Vue parent via attribute', async () => {+      const E = defineCustomElement({+        props: {+          tagName: String,+        },+        render() {+          return h('div', this.tagName)+        },+      })+      customElements.define('my-el-native-prop-parent', E)++      const tagName = ref('foo')+      render(+        h(() => h('my-el-native-prop-parent', { tagName: tagName.value })),+        container,+      )+      const e = container.childNodes[0] as VueElement+      expect(e.tagName).toBe('MY-EL-NATIVE-PROP-PARENT')+      expect(e.getAttribute('tag-name')).toBe('foo')+      expect(e.shadowRoot!.innerHTML).toBe(`<div>foo</div>`)++      tagName.value = 'bar'+      await nextTick()+      await nextTick()+      expect(e.tagName).toBe('MY-EL-NATIVE-PROP-PARENT')+      expect(e.shadowRoot!.innerHTML).toBe(`<div>bar</div>`)+      expect(`Failed setting prop`).not.toHaveBeenWarned()+      expect(+        `Prop "tagName" conflicts with an existing property on the custom element`,+      ).toHaveBeenWarned()+    })++    test('should still allow writable native properties as props', async () => {+      const E = defineCustomElement({+        props: {+          title: Object,+        },+        render() {+          return h('div', this.title && (this.title as any).text)+        },+      })+      customElements.define('my-el-writable-native-prop', E)++      container.innerHTML = `<my-el-writable-native-prop></my-el-writable-native-prop>`+      const e = container.childNodes[0] as VueElement & { title: any }+      e.title = { text: 'foo' }+      await nextTick()+      expect(e.title).toEqual({ text: 'foo' })+      expect(e.shadowRoot!.innerHTML).toBe(`<div>foo</div>`)++      e.title = { text: 'bar' }+      await nextTick()+      expect(e.shadowRoot!.innerHTML).toBe(`<div>bar</div>`)+      expect(`conflicts with an existing property`).not.toHaveBeenWarned()+    })++    test('should define accessor for hyphenated prop set before upgrade', async () => {+      const E = defineCustomElement({+        props: ['foo-bar'],+        render() {+          return h('div', (this as any).fooBar)+        },+      })+      const e = document.createElement('my-el-pre-upgrade-hyphen') as any+      e.fooBar = 'pre'+      container.appendChild(e)+      customElements.define('my-el-pre-upgrade-hyphen', E)++      e.fooBar = 'post'+      await nextTick()+      expect(e.shadowRoot!.innerHTML).toBe(`<div>post</div>`)+      expect(`conflicts with an existing property`).not.toHaveBeenWarned()+    })   })    test('async & nested custom elements', async () => {packages/runtime-dom/src/apiCustomElement.tsindex 9422f58d6..0a140de92 100644@@ -35,13 +35,13 @@ import { import {   camelize,   extend,-  hasOwn,   hyphenate,   isArray,   isPlainObject,   toNumber, } from '@vue/shared' import { createApp, createSSRApp, render } from '.'+import { isReservedCEKey } from './patchProp'  // marker for attr removal const REMOVAL = {}@@ -454,7 +454,7 @@ export class VueElement     const exposed = this._instance && this._instance.exposed     if (!exposed) return     for (const key in exposed) {-      if (!hasOwn(this, key)) {+      if (!(key in this)) {         // exposed properties are readonly         Object.defineProperty(this, key, {           // unwrap ref to be consistent with public instance behavior@@ -479,6 +479,19 @@ export class VueElement      // defining getter/setters on prototype     for (const key of declaredPropKeys.map(camelize)) {+      // avoid shadowing existing members of the element (e.g. native+      // `Element.prototype.tagName` or VueElement internals). The prop can+      // still be set via its attribute.+      if (isReservedCEKey(this, key)) {+        if (__DEV__) {+          warn(+            `Prop "${key}" conflicts with an existing property on the custom ` ++              `element and will not be exposed as an element property. ` ++              `It can still be set via the corresponding attribute.`,+          )+        }+        continue+      }       Object.defineProperty(this, key, {         get(this: VueElement) {           return this._getProp(key)packages/runtime-dom/src/patchProp.tsindex 095a89403..4feda4fc9 100644@@ -5,6 +5,8 @@ import { patchDOMProp } from './modules/props' import { patchEvent } from './modules/events' import {   camelize,+  hasOwn,+  hyphenate,   isFunction,   isModelListener,   isOn,@@ -40,6 +42,14 @@ export const patchProp: DOMRendererOptions['patchProp'] = (     if (!isModelListener(key)) {       patchEvent(el, key, prevValue, nextValue, parentComponent)     }+  } else if (+    // props shadowing reserved element members (e.g. `tagName`) are not+    // exposed as element properties, so they must be set as attributes+    (el as VueElement)._isVueCE &&+    shouldSetAsPropForVueCE(el as VueElement, key) &&+    isReservedCEKey(el, camelize(key))+  ) {+    patchAttr(el, hyphenate(key), nextValue, isSVG, parentComponent)   } else if (     key[0] === '.'       ? ((key = key.slice(1)), true)@@ -171,3 +181,27 @@ function shouldSetAsPropForVueCE(el: VueElement, key: string) {     ? props.some(prop => camelize(prop) === camelKey)     : Object.keys(props).some(prop => camelize(prop) === camelKey) }++/**+ * Whether defining a prop accessor for `key` would shadow an element member+ * that must not be overridden: VueElement internals, methods, or read-only+ * properties such as `Element.prototype.tagName`. Writable native properties+ * (e.g. `title`) and own values set pre-upgrade can be safely shadowed.+ * @internal+ */+export function isReservedCEKey(el: Element, key: string): boolean {+  if (hasOwn(el, key)) {+    return key[0] === '_'+  }+  let proto = Object.getPrototypeOf(el)+  while (proto) {+    const desc = Object.getOwnPropertyDescriptor(proto, key)+    if (desc) {+      return desc.get || desc.set+        ? !desc.set+        : !desc.writable || isFunction(desc.value)+    }+    proto = Object.getPrototypeOf(proto)+  }+  return false+} 
Merged fix · PR #12125+78 −1
packages/runtime-dom/__tests__/customElement.spec.tsindex 27c7121c2..8763625bd 100644@@ -237,6 +237,7 @@ describe('defineCustomElement', () => {       await nextTick()       expect(e.shadowRoot!.innerHTML).toBe('<div>foo2</div><div>bar2</div>')       expect(e.getAttribute('foo')).toBe('foo2')+      expect(e.foo).toBe('foo2')       expect(e.hasAttribute('bar')).toBe(false)        // change prop then attr@@ -2489,6 +2490,76 @@ describe('defineCustomElement', () => {     expect(e.shadowRoot!.innerHTML).toBe(`false,boolean`)   }) +  test('prop name conflicts with native property', () => {+    const E = defineCustomElement({+      props: {+        tagName: {+          type: String,+        },+      },+      render() {+        return this.tagName+      },+    })+    customElements.define('el-attr-tag-name', E)+    container.innerHTML = '<el-attr-tag-name tag-name="foo">'+    const e = container.childNodes[0] as VueElement+    expect(e.shadowRoot!.innerHTML).toBe(`foo`)+    expect(e.tagName).toBe('foo')+    expect(+      `[Vue warn]: Custom element prop "tagName" conflicts with an existing property on the element and will overwrite it.`,+    ).toHaveBeenWarned()+  })++  test('prop name conflicts with reflected native property', async () => {+    const E = defineCustomElement({+      props: {+        id: String,+      },+      render() {+        return this.id+      },+    })+    customElements.define('el-attr-id', E)+    container.innerHTML = '<el-attr-id id="foo">'+    const e = container.childNodes[0] as VueElement++    e.setAttribute('id', 'bar')+    await nextTick()+    expect(e.shadowRoot!.innerHTML).toBe(`bar`)+    expect(e.id).toBe('bar')+    expect(+      `[Vue warn]: Custom element prop "id" conflicts with an existing property on the element and will overwrite it.`,+    ).toHaveBeenWarned()+  })++  test('native method conflict when rendered by Vue', () => {+    const value = { text: 'foo' }+    const E = defineCustomElement({+      props: {+        hasAttribute: Object,+      },+      render() {+        return this.hasAttribute!.text+      },+    })+    customElements.define('el-vue-has-attribute', E)++    render(+      h('el-vue-has-attribute', {+        'has-attribute': value,+      }),+      container,+    )++    const e = container.firstChild as VueElement+    expect(e.shadowRoot!.innerHTML).toBe('foo')+    expect((e as any).hasAttribute).toBe(value)+    expect(+      `[Vue warn]: Custom element prop "hasAttribute" conflicts with an existing property on the element and will overwrite it.`,+    ).toHaveBeenWarned()+  })+   test('hyphenated attr removal', async () => {     const E = defineCustomElement({       props: {packages/runtime-dom/src/apiCustomElement.tsindex 9422f58d6..ede68a516 100644@@ -477,8 +477,14 @@ export class VueElement       }     } -    // defining getter/setters on prototype+    // define getter/setters for declared props     for (const key of declaredPropKeys.map(camelize)) {+      if (__DEV__ && key in Object.getPrototypeOf(this)) {+        warn(+          `Custom element prop "${key}" conflicts with an existing property ` ++            `on the element and will overwrite it.`,+        )+      }       Object.defineProperty(this, key, {         get(this: VueElement) {           return this._getProp(key)