Skip to content

abort-on-property-write breaks on read too

The combination of abort-on-property-write and document.createElement easily breaks any page that simply tries to access document.createElement without trying to set it.

The reason is that Object.getOwnPropertyDescriptor(document, 'createElement') is undefined, since createElement is inherited from Document.prototype, meaning that setting a descriptor that misses the getter to return the initial value creates a trap on the object that makes it unusable.

Object.defineProperty(document, 'createElement', {set() { throw new Error('nope'); }});

document.createElement('script');
// throws because document.createElement is not a function

Possible solution

This snippet has been around for a while so I'd like to have extra eyes on the proposed solution, which is the following one:

  // file snippet.js inside wrapPropertyAccess
  if (value && (typeof value == "object" || typeof value == "function"))
  {
    if (!descriptor.get && descriptor.set)
    {
      let method = value[property];
      descriptor.get = () => method;
    }
    wrapPropertyAccess(value, property, descriptor);
  }

In above proposal we never nullify the object method through an override that doesn't exposes the initial value, which is exactly what's happening now.