Backtestsvuerun-20261002-115633#12124

Issue #12124 Merged fix PR #12125 Run 2 Oct 2026 · run-20261002-115633 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 walks the prototype chain and skips read-only accessors and methods inherited from the element, such as `tagName` and `hasAttribute`. It warns in dev, and the prop stays settable through its attribute. It also updates `patchProp`, so a Vue parent passing a conflicting prop goes straight to `_setProp` rather than trying to assign the read-only native property, which would otherwise fail. Writable native members like `title` and `tabIndex` can still be set as properties, which preserves existing behavior. Its tests cover the attribute path, the Vue-parent path, and the writable-member case. They assert that `tagName` stays native (`'MY-EL-PROPS-CONFLICT'`) while the shadow content updates, so they would fail without the fix. Patch B only adds a dev warning and still overwrites the native member. Its own test asserts `e.tagName === 'foo'` and that `hasAttribute` is replaced by the prop value, which locks in the harmful behavior the issue reports. Overwriting methods like `hasAttribute` can break the DOM and Vue's own internals. B's check `key in Object.getPrototypeOf(this)` also warns on harmless writable reflected properties such as `id`. B is a symptom-level mitigation at best. A is more complex, but the added logic is targeted and well tested.

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

12m 45s

Wall-clock time in the container

Full test suite

No new failures

3,667 passed · 6 skipped · 3,673 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

156vs 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+156 −0
packages/runtime-dom/__tests__/customElement.spec.tsindex 27c7121c2..6c86a4442 100644@@ -170,6 +170,105 @@ describe('defineCustomElement', () => {       expect((el as any).value).toEqual({ x: 1 })     }) +    // #10180+    test('props that conflict with existing element members', async () => {+      const E = defineCustomElement({+        props: {+          tagName: String,+          foo: String,+        },+        render() {+          return h('div', null, `${this.tagName}-${this.foo}`)+        },+      })+      customElements.define('my-el-props-conflict', E)++      container.innerHTML = `<my-el-props-conflict tag-name="span" foo="bar"></my-el-props-conflict>`+      const e = container.childNodes[0] as VueElement & {+        tagName: string+        foo: string+      }+      expect(+        `Prop "tagName" conflicts with an existing property`,+      ).toHaveBeenWarned()+      // native Element property must not be overridden+      expect(e.tagName).toBe('MY-EL-PROPS-CONFLICT')+      expect(e.shadowRoot!.innerHTML).toBe('<div>span-bar</div>')++      // still updatable via attribute+      e.setAttribute('tag-name', 'p')+      await nextTick()+      expect(e.tagName).toBe('MY-EL-PROPS-CONFLICT')+      expect(e.shadowRoot!.innerHTML).toBe('<div>p-bar</div>')++      // non-conflicting props still exposed as properties+      e.foo = 'baz'+      await nextTick()+      expect(e.shadowRoot!.innerHTML).toBe('<div>p-baz</div>')+    })++    // #10180+    test('props that conflict with existing element members, passed from a Vue parent', async () => {+      const E = defineCustomElement({+        props: {+          tagName: String,+          title: String,+        },+        render() {+          return h('div', null, `${this.tagName}-${this.title}`)+        },+      })+      customElements.define('my-el-props-conflict-parent', E)++      const tag = ref('span')+      const title = ref('a')+      render(+        h({+          render: () =>+            h('my-el-props-conflict-parent', {+              tagName: tag.value,+              title: title.value,+            }),+        }),+        container,+      )+      const e = container.childNodes[0] as VueElement+      expect(`Prop "tagName" conflicts`).toHaveBeenWarned()+      expect(`Prop "title" conflicts`).not.toHaveBeenWarned()+      expect(`Failed setting prop`).not.toHaveBeenWarned()+      expect(e.tagName).toBe('MY-EL-PROPS-CONFLICT-PARENT')+      expect(e.shadowRoot!.innerHTML).toBe('<div>span-a</div>')++      tag.value = 'p'+      title.value = 'b'+      await nextTick()+      expect(e.tagName).toBe('MY-EL-PROPS-CONFLICT-PARENT')+      expect(e.shadowRoot!.innerHTML).toBe('<div>p-b</div>')+    })++    // #10180+    test('props matching writable element members remain settable as properties', async () => {+      const E = defineCustomElement({+        props: {+          title: Object,+          tabIndex: Number,+        },+        render() {+          return h('div', null, `${this.title?.x}-${this.tabIndex}`)+        },+      })+      customElements.define('my-el-props-writable-member', E)++      container.innerHTML = `<my-el-props-writable-member></my-el-props-writable-member>`+      const e = container.childNodes[0] as any+      e.title = { x: 1 }+      e.tabIndex = 3+      await nextTick()+      expect(`conflicts with an existing property`).not.toHaveBeenWarned()+      expect(e.title).toEqual({ x: 1 })+      expect(e.shadowRoot!.innerHTML).toBe('<div>1-3</div>')+    })+     test('props via attribute', async () => {       // bazQux should map to `baz-qux` attribute       container.innerHTML = `<my-el-props foo="hello" baz-qux="bye"></my-el-props>`packages/runtime-dom/src/apiCustomElement.tsindex 9422f58d6..1f7938c9f 100644@@ -38,6 +38,7 @@ import {   hasOwn,   hyphenate,   isArray,+  isFunction,   isPlainObject,   toNumber, } from '@vue/shared'@@ -479,6 +480,22 @@ export class VueElement      // defining getter/setters on prototype     for (const key of declaredPropKeys.map(camelize)) {+      // do not shadow read-only members or methods inherited from the+      // element's prototype chain (e.g. Element.prototype.tagName) - the prop+      // remains settable via its attribute, but the native member must keep+      // working.+      if (!hasOwn(this, key) && isProtectedInheritedMember(this, key)) {+        if (__DEV__) {+          warn(+            `Prop "${key}" conflicts with an existing property on the ` ++              `custom element (e.g. a built-in HTMLElement property). ` ++              `It will not be exposed as an element property and can only ` ++              `be set via the "${hyphenate(key)}" attribute or by a parent ` ++              `Vue component.`,+          )+        }+        continue+      }       Object.defineProperty(this, key, {         get(this: VueElement) {           return this._getProp(key)@@ -803,6 +820,24 @@ export class VueElement   } } +/**+ * Whether `key` is an inherited read-only property or method of the element,+ * which must not be shadowed by a prop accessor.+ */+function isProtectedInheritedMember(el: object, key: string): boolean {+  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+}+ export function useHost(caller?: string): VueElement | null {   const instance = getCurrentInstance()   const el = instance && (instance.ce as VueElement)packages/runtime-dom/src/patchProp.tsindex 095a89403..629aad497 100644@@ -5,6 +5,7 @@ import { patchDOMProp } from './modules/props' import { patchEvent } from './modules/events' import {   camelize,+  hasOwn,   isFunction,   isModelListener,   isOn,@@ -40,6 +41,20 @@ export const patchProp: DOMRendererOptions['patchProp'] = (     if (!isModelListener(key)) {       patchEvent(el, key, prevValue, nextValue, parentComponent)     }+  } else if (+    (el as VueElement)._isVueCE &&+    isVueCEPropConflictingWithElement(el as VueElement, key)+  ) {+    // declared CE prop whose name collides with a native member inherited by+    // the element (e.g. `tagName`): never assign the native property, pass the+    // value to the custom element's props directly instead.+    ;(el as VueElement)._setProp(+      camelize(key),+      nextValue,+      true,+      // @ts-expect-error _patching is private+      !(el as VueElement)._patching,+    )   } else if (     key[0] === '.'       ? ((key = key.slice(1)), true)@@ -159,6 +174,13 @@ function shouldSetAsProp(   return key in el } +function isVueCEPropConflictingWithElement(el: VueElement, key: string) {+  const camelKey = camelize(key)+  return (+    camelKey in el && !hasOwn(el, camelKey) && shouldSetAsPropForVueCE(el, key)+  )+}+ function shouldSetAsPropForVueCE(el: VueElement, key: string) {   const props = // @ts-expect-error _def is private     el._def.props as Record<string, unknown> | string[] | undefined 
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)