Backtestsvuerun-20261021-133314#12124

Issue #12124 Merged fix PR #12125 Run 21 Oct 2026 · run-20261021-133314 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.

Both patches satisfy the independent tests, and both add a dev warning when a declared prop collides with an inherited Element member. The difference is what happens after the warning. Patch B still defines the prop on the element and overwrites `tagName` (its own test asserts `e.tagName` becomes 'foo'), so in production builds nothing changes at all and the breakage the issue describes (a `tag-name` attribute clobbering `Element.prototype.tagName`) persists. Patch A addresses the root cause: it walks the prototype chain, refuses to shadow read-only accessors/non-writable values/methods, keeps the attribute-to-prop path working, and adds a `patchProp` branch so a Vue parent can still pass such props (via `_setProp`) without touching the native member. It also hardens the `expose` path with the same check while still permitting intentional method overrides. Its tests are substantive: they assert native `tagName`/`remove` survive, `hasOwnProperty` is false, attribute and parent-driven updates still flow to the prop, and writable reflected members like `title` keep the old mapping. Patch A is larger and has minor craft issues (a `@ts-expect-error` to reach a private field, a `_isVueCE`/`_isProtectedProp` coupling from `patchProp`), and Patch B's one-line `key in proto` check is admittedly simpler and lower risk. But B's test that overrides `hasAttribute` with an object illustrates the hazard of leaving the overwrite in place rather than resolving it. Patch B also includes an unrelated assertion tweak at line 237. Weighing correctness first, A resolves the issue while B only reports it, so A wins.

Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.

Solve

7m 46s

Wall-clock time in the container

Full test suite

No new failures

701 passed · 1 skipped · 702 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

188vs 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+187 −1
packages/runtime-dom/__tests__/customElement.spec.tsindex 27c7121c2..eb602fc2c 100644@@ -2124,6 +2124,128 @@ describe('defineCustomElement', () => {         `[Vue warn]: Exposed property "value" already exists on custom element.`,       ).toHaveBeenWarned()     })++    test('should not override read-only element members when exposing', () => {+      const focus = vi.fn()+      const E = defineCustomElement({+        setup(_, { expose }) {+          expose({+            tagName: 'exposed',+            focus,+          })+          return () => h('div', 'hi')+        },+      })+      customElements.define('my-el-expose-builtin', E)++      container.innerHTML = `<my-el-expose-builtin></my-el-expose-builtin>`+      const e = container.childNodes[0] as VueElement+      expect(e.tagName).toBe('MY-EL-EXPOSE-BUILTIN')+      expect(+        `[Vue warn]: Exposed property "tagName" already exists on custom element.`,+      ).toHaveBeenWarned()+      // methods can still be intentionally overridden via expose+      e.focus()+      expect(focus).toHaveBeenCalledTimes(1)+      expect(+        `[Vue warn]: Exposed property "focus" already exists on custom element.`,+      ).not.toHaveBeenWarned()+    })+  })++  test('should not override read-only members or methods of the element prototype with props', async () => {+    const E = defineCustomElement({+      props: {+        tagName: String,+        remove: String,+        title: String,+      },+      setup(props) {+        return () => h('div', `${props.tagName}|${props.remove}|${props.title}`)+      },+    })+    customElements.define('my-el-builtin-props', E)++    container.innerHTML = `<my-el-builtin-props tag-name="foo" remove="bar" title="baz"></my-el-builtin-props>`+    const e = container.childNodes[0] as VueElement & { title: string }++    // built-in members are left intact+    expect(e.tagName).toBe('MY-EL-BUILTIN-PROPS')+    expect(typeof e.remove).toBe('function')+    expect(e.hasOwnProperty('tagName')).toBe(false)+    expect(e.hasOwnProperty('remove')).toBe(false)+    // props are still settable via attributes+    expect(e.shadowRoot!.innerHTML).toBe(`<div>foo|bar|baz</div>`)+    e.setAttribute('tag-name', 'qux')+    await nextTick()+    expect(e.shadowRoot!.innerHTML).toBe(`<div>qux|bar|baz</div>`)++    // writable reflected members keep being mapped to the prop+    e.title = 'updated'+    await nextTick()+    expect(e.shadowRoot!.innerHTML).toBe(`<div>qux|bar|updated</div>`)+    expect(e.getAttribute('title')).toBe('updated')++    expect(+      `Prop "tagName" conflicts with an existing read-only property or method`,+    ).toHaveBeenWarned()+    expect(+      `Prop "remove" conflicts with an existing read-only property or method`,+    ).toHaveBeenWarned()+    expect(+      `Prop "title" conflicts with an existing read-only property or method`,+    ).not.toHaveBeenWarned()++    e.remove()+    expect(container.childNodes.length).toBe(0)+  })++  test('should set read-only member or method named props from a Vue parent', async () => {+    const E = defineCustomElement({+      props: {+        tagName: String,+        remove: Object,+      },+      setup(props) {+        return () =>+          h('div', `${props.tagName}|${props.remove && props.remove.foo}`)+      },+    })+    customElements.define('my-el-builtin-props-parent', E)++    const tagName = ref('foo')+    const remove = ref({ foo: 'bar' })+    const App = {+      render: () =>+        h('my-el-builtin-props-parent', {+          tagName: tagName.value,+          remove: remove.value,+        }),+    }+    const root = document.createElement('div')+    container.appendChild(root)+    createApp(App).mount(root)+    const e = root.childNodes[0] as VueElement++    expect(e.tagName).toBe('MY-EL-BUILTIN-PROPS-PARENT')+    expect(typeof e.remove).toBe('function')+    expect(e.hasOwnProperty('remove')).toBe(false)+    expect(e.shadowRoot!.innerHTML).toBe(`<div>foo|bar</div>`)++    tagName.value = 'baz'+    remove.value = { foo: 'qux' }+    await nextTick()+    expect(e.shadowRoot!.innerHTML).toBe(`<div>baz|qux</div>`)+    expect(e.getAttribute('tag-name')).toBe('baz')+    expect(typeof e.remove).toBe('function')+    expect(e.hasOwnProperty('remove')).toBe(false)+    expect(`Failed setting prop`).not.toHaveBeenWarned()+    expect(+      `Prop "tagName" conflicts with an existing read-only property or method`,+    ).toHaveBeenWarned()+    expect(+      `Prop "remove" conflicts with an existing read-only property or method`,+    ).toHaveBeenWarned()   })    test('async & nested custom elements', async () => {packages/runtime-dom/src/apiCustomElement.tsindex 9422f58d6..f0f82fd11 100644@@ -454,7 +454,10 @@ export class VueElement     const exposed = this._instance && this._instance.exposed     if (!exposed) return     for (const key in exposed) {-      if (!hasOwn(this, key)) {+      // also check the prototype chain so that read-only built-in element+      // members (e.g. `tagName`) are never shadowed. Methods (e.g. `focus`)+      // may still be intentionally overridden by exposed functions.+      if (!hasOwn(this, key) && !isProtectedElementMember(this, key, false)) {         // exposed properties are readonly         Object.defineProperty(this, key, {           // unwrap ref to be consistent with public instance behavior@@ -479,6 +482,19 @@ export class VueElement      // defining getter/setters on prototype     for (const key of declaredPropKeys.map(camelize)) {+      // avoid overriding read-only members and methods inherited from the+      // element prototype chain (e.g. `tagName`, `shadowRoot`, `remove`) -+      // the prop can still be set via its attribute.+      if (this._isProtectedProp(key)) {+        if (__DEV__) {+          warn(+            `Prop "${key}" conflicts with an existing read-only property or ` ++              `method on the custom element and will not be exposed as a ` ++              `property. It can still be set via its attribute.`,+          )+        }+        continue+      }       Object.defineProperty(this, key, {         get(this: VueElement) {           return this._getProp(key)@@ -490,6 +506,15 @@ export class VueElement     }   } +  /**+   * @internal+   * whether a declared prop can't be defined as a property on the element+   * because it would override a read-only member or method it inherits.+   */+  _isProtectedProp(key: string): boolean {+    return !hasOwn(this, key) && isProtectedElementMember(this, key)+  }+   protected _setAttr(key: string): void {     if (key.startsWith('data-v-')) return     const has = this.hasAttribute(key)@@ -831,3 +856,27 @@ export function useShadowRoot(): ShadowRoot | null {   const el = __DEV__ ? useHost('useShadowRoot') : useHost()   return el && el.shadowRoot }++/**+ * Returns true if `key` is inherited from the prototype chain as a read-only+ * property (getter without setter / non-writable value) or, unless+ * `includeMethods` is false, a method.+ */+function isProtectedElementMember(+  el: object,+  key: string,+  includeMethods = true,+): boolean {+  let proto = Object.getPrototypeOf(el)+  while (proto) {+    const desc = Object.getOwnPropertyDescriptor(proto, key)+    if (desc) {+      if (desc.get || desc.set) return !desc.set+      return (+        !desc.writable || (includeMethods && typeof desc.value === 'function')+      )+    }+    proto = Object.getPrototypeOf(proto)+  }+  return false+}packages/runtime-dom/src/patchProp.tsindex 095a89403..845da76b0 100644@@ -40,6 +40,21 @@ export const patchProp: DOMRendererOptions['patchProp'] = (     if (!isModelListener(key)) {       patchEvent(el, key, prevValue, nextValue, parentComponent)     }+  } else if (+    // declared CE props that would override a read-only member or method+    // inherited by the element (e.g. `tagName`, `remove`) are not defined as+    // properties on the element, so set them on the CE directly+    (el as VueElement)._isVueCE &&+    shouldSetAsPropForVueCE(el as VueElement, key) &&+    (el as VueElement)._isProtectedProp(camelize(key))+  ) {+    ;(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) 
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)