Backtestsvuerun-20261003-231210#12124

Issue #12124 Merged fix PR #12125 Run 3 Oct 2026 · run-20261003-231210 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. It never shadows readonly native members such as `tagName`, `nodeName` or `isConnected`. `isReadonlyNativeProp` walks the prototype chain and checks for a getter with no setter, or a non-writable data property. The declared prop still receives its value, through three paths: attribute changes via `_setAttr`, and parent bindings via a new `patchProp` branch that calls `_setShadowedProp`. That branch covers camelCase, kebab-case and `.prop` forms, plus unresolved async CEs. The same guard applies to exposed properties, which close a second override route. Its tests check that `e.tagName` keeps the real tag name, that no own descriptor is created, and that the prop still renders the attribute or bound value. They cover sync, async and expose cases, and would fail before the change. The main cost is some extra complexity in the hot `patchProp` path: `isPropOfVueCE` and `_setShadowedProp` run per key on Vue CEs. That is acceptable. Patch B only adds a dev warning and keeps the harmful behavior. Its own test asserts `e.tagName` becomes 'foo', so the element's `tagName` stays broken in production. Its check, `key in Object.getPrototypeOf(this)`, is also broad. It warns on writable reflected props like `id`, where overriding is mostly harmless. Its added assertion `expect(e.foo).toBe('foo2')` is unrelated. A warning is one of the issue's acceptable outcomes, but Patch A actually prevents the breakage the issue reports, so it is the more correct 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

8m 19s

Wall-clock time in the container

Full test suite

No new failures

3,668 passed · 6 skipped · 3,674 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

190vs 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+189 −1
packages/runtime-dom/__tests__/customElement.spec.tsindex 27c7121c2..1457477e1 100644@@ -683,6 +683,85 @@ describe('defineCustomElement', () => {       expect(prop1Calls).toBe(2)       expect(prop2Calls).toBe(1)     })++    test('should not override readonly 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', E)++      container.innerHTML = `<my-el-native-prop tag-name="foo" msg="hi"></my-el-native-prop>`+      const e = container.childNodes[0] as VueElement+      // native property is preserved+      expect(e.tagName).toBe('MY-EL-NATIVE-PROP')+      expect(Object.getOwnPropertyDescriptor(e, 'tagName')).toBeUndefined()+      // prop still receives the attribute value+      expect(e.shadowRoot!.innerHTML).toBe(`<div>foo hi</div>`)++      e.setAttribute('tag-name', 'bar')+      await nextTick()+      expect(e.tagName).toBe('MY-EL-NATIVE-PROP')+      expect(e.shadowRoot!.innerHTML).toBe(`<div>bar hi</div>`)++      // other declared props still get accessors+      ;(e as any).msg = 'hello'+      await nextTick()+      expect(e.shadowRoot!.innerHTML).toBe(`<div>bar hello</div>`)+    })++    test('should pass readonly native element property props from parent bindings', async () => {+      const E = defineCustomElement({+        props: {+          tagName: Object,+        },+        render() {+          return h('div', JSON.stringify(this.tagName))+        },+      })+      customElements.define('my-el-native-prop-binding', E)++      const tagName = ref<any>({ a: 1 })+      const App = {+        render() {+          return h('my-el-native-prop-binding', { tagName: tagName.value })+        },+      }+      const app = createApp(App)+      app.mount(container)+      const e = container.childNodes[0] as VueElement+      expect(e.tagName).toBe('MY-EL-NATIVE-PROP-BINDING')+      expect(e.shadowRoot!.innerHTML).toBe(`<div>{"a":1}</div>`)++      tagName.value = { b: 2 }+      await nextTick()+      expect(e.tagName).toBe('MY-EL-NATIVE-PROP-BINDING')+      expect(e.shadowRoot!.innerHTML).toBe(`<div>{"b":2}</div>`)+      app.unmount()++      // kebab-case and .prop modifier bindings+      render(+        h('my-el-native-prop-binding', { 'tag-name': { c: 3 } }),+        container,+      )+      await nextTick()+      const e2 = container.childNodes[0] as VueElement+      expect(e2.tagName).toBe('MY-EL-NATIVE-PROP-BINDING')+      expect(e2.shadowRoot!.innerHTML).toBe(`<div>{"c":3}</div>`)++      render(+        h('my-el-native-prop-binding', { '.tagName': { d: 4 } }),+        container,+      )+      await nextTick()+      expect(e2.tagName).toBe('MY-EL-NATIVE-PROP-BINDING')+      expect(e2.shadowRoot!.innerHTML).toBe(`<div>{"d":4}</div>`)+    })   })    describe('attrs', () => {@@ -1497,6 +1576,35 @@ describe('defineCustomElement', () => {       )     }) +    test('should not override readonly native element properties', async () => {+      const E = defineCustomElement(+        defineAsyncComponent(() => {+          return Promise.resolve({+            props: { tagName: Object },+            render(this: any) {+              return h('div', JSON.stringify(this.tagName))+            },+          })+        }),+      )+      customElements.define('my-el-async-native-prop', E)++      // bound before the async definition resolves+      render(h('my-el-async-native-prop', { tagName: { a: 1 } }), container)+      const e = container.childNodes[0] as VueElement+      expect(e.tagName).toBe('MY-EL-ASYNC-NATIVE-PROP')++      await new Promise(r => setTimeout(r))+      expect(e.tagName).toBe('MY-EL-ASYNC-NATIVE-PROP')+      expect(Object.getOwnPropertyDescriptor(e, 'tagName')).toBeUndefined()+      expect(e.shadowRoot!.innerHTML).toBe(`<div>{"a":1}</div>`)++      render(h('my-el-async-native-prop', { tagName: { b: 2 } }), container)+      await nextTick()+      expect(e.tagName).toBe('MY-EL-ASYNC-NATIVE-PROP')+      expect(e.shadowRoot!.innerHTML).toBe(`<div>{"b":2}</div>`)+    })+     test('set DOM property before resolve', async () => {       const E = defineCustomElement(         defineAsyncComponent(() => {@@ -2124,6 +2232,30 @@ describe('defineCustomElement', () => {         `[Vue warn]: Exposed property "value" already exists on custom element.`,       ).toHaveBeenWarned()     })++    test('should not override readonly native element properties when exposing', () => {+      const E = defineCustomElement({+        setup(_, { expose }) {+          expose({+            tagName: 'foo',+            nodeName: 'bar',+          })+          return () => h('div', 'hello')+        },+      })+      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(e.nodeName).toBe('MY-EL-EXPOSE-NATIVE')+      expect(+        `[Vue warn]: Exposed property "tagName" already exists on custom element.`,+      ).toHaveBeenWarned()+      expect(+        `[Vue warn]: Exposed property "nodeName" already exists on custom element.`,+      ).toHaveBeenWarned()+    })   })    test('async & nested custom elements', async () => {packages/runtime-dom/src/apiCustomElement.tsindex 9422f58d6..e7a7cecf2 100644@@ -200,6 +200,24 @@ const BaseClass = (  type InnerComponentDef = ConcreteComponent & CustomElementOptions +/**+ * Whether `key` resolves to a readonly property inherited from the element's+ * prototype chain (e.g. `tagName`, `nodeName`, `isConnected`). Such members+ * must never be shadowed by prop or expose accessors on the element.+ */+function isReadonlyNativeProp(el: object, key: string): boolean {+  if (hasOwn(el, key)) return false+  let proto = Object.getPrototypeOf(el)+  while (proto) {+    const desc = Object.getOwnPropertyDescriptor(proto, key)+    if (desc) {+      return desc.get || desc.set ? !desc.set : !desc.writable+    }+    proto = Object.getPrototypeOf(proto)+  }+  return false+}+ export class VueElement   extends BaseClass   implements ComponentCustomElementInterface@@ -454,7 +472,7 @@ export class VueElement     const exposed = this._instance && this._instance.exposed     if (!exposed) return     for (const key in exposed) {-      if (!hasOwn(this, key)) {+      if (!hasOwn(this, key) && !isReadonlyNativeProp(this, key)) {         // exposed properties are readonly         Object.defineProperty(this, key, {           // unwrap ref to be consistent with public instance behavior@@ -479,6 +497,10 @@ export class VueElement      // defining getter/setters on prototype     for (const key of declaredPropKeys.map(camelize)) {+      // do not override readonly native element members (e.g. `tagName`).+      // such props can still be passed via attributes or parent bindings,+      // see _setShadowedProp.+      if (isReadonlyNativeProp(this, key)) continue       Object.defineProperty(this, key, {         get(this: VueElement) {           return this._getProp(key)@@ -508,6 +530,18 @@ export class VueElement     return this._props[key]   } +  /**+   * @internal+   * Set a declared prop whose key collides with a readonly native element+   * property (e.g. `tagName`) and therefore has no accessor on the element.+   * Returns false if `key` is not such a property.+   */+  _setShadowedProp(key: string, val: any): boolean {+    if (!isReadonlyNativeProp(this, key)) return false+    this._setProp(key, val, true, !this._patching)+    return true+  }+   /**    * @internal    */packages/runtime-dom/src/patchProp.tsindex 095a89403..8ffe07306 100644@@ -40,6 +40,19 @@ export const patchProp: DOMRendererOptions['patchProp'] = (     if (!isModelListener(key)) {       patchEvent(el, key, prevValue, nextValue, parentComponent)     }+  } else if (+    // declared props of a Vue custom element that collide with readonly native+    // element properties (e.g. `tagName`) have no accessor on the element,+    // pass them to the element's props directly+    key[0] !== '^' &&+    (el as VueElement)._isVueCE &&+    isPropOfVueCE(el as VueElement, key[0] === '.' ? key.slice(1) : key) &&+    (el as VueElement)._setShadowedProp(+      camelize(key[0] === '.' ? key.slice(1) : key),+      nextValue,+    )+  ) {+    // already handled by _setShadowedProp   } else if (     key[0] === '.'       ? ((key = key.slice(1)), true)@@ -159,6 +172,15 @@ function shouldSetAsProp(   return key in el } +function isPropOfVueCE(el: VueElement, key: string): boolean {+  return (+    shouldSetAsPropForVueCE(el, key) ||+    // props of an unresolved async custom element are not known yet+    // @ts-expect-error _def is private+    !!el._def.__asyncLoader+  )+}+ 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)