Open Bug 1776555 Opened 2 years ago Updated 2 years ago

DOMException.data sometimes throws error on assignment

Categories

(Core :: DOM: Events, defect)

defect

Tracking

()

REOPENED

People

(Reporter: toasted.nutbread, Unassigned)

Details

Attachments

(1 file)

Attached file domexception-bug.html

DOMException's data field cannot always be assigned to, and this seems to depend on whether the function is part of a class or not. The error thrown is:

TypeError: setting getter-only property "data"

A test case file is included which has two functions: Test.test() and test(). The non-class function does not exhibit the bug and the class function does have the bug. The bodies for both functions are identical. Code is also included below:

// This works
function test() {
  const error1 = new DOMException();
  console.log({error1});
  try {
    error1.data = 'data';
  } catch (error2) {
    console.log({error2});
  }
}

// This throws error2
class Test {
  static test() {
    const error1 = new DOMException();
    console.log({error1});
    try {
      error1.data = 'data';
    } catch (error2) {
      console.log({error2});
    }
  }
}

Object.defineProperty can be used to avoid the error, but this feels like a hack.


Something which may or may not be related is that passing these errors across frame boundaries can generate this warning on the destination if it tries to read the object. I had this happen in the context of passing the DOMException from a WebExtension's background page to a frame on a webpage.

Security wrapper denied access to property "error" on privileged Javascript object. Support for exposing privileged objects to untrusted content via __exposedProps__ has been removed - use WebIDL bindings or Components.utils.cloneInto instead. Note that only the first denied property access from a given global object will be reported.

The Bugbug bot thinks this bug should belong to the 'WebExtensions::Untriaged' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.

Product: Firefox → WebExtensions

This is the expected behavior. The data property is meant to be readonly.

The reason that you see different behavior in the class version is that class functions are automatically compiled in strict mode, which throws an error for assignments to readonly properties rather than failing silently.

Status: UNCONFIRMED → RESOLVED
Closed: 2 years ago
Component: Untriaged → DOM: Events
Product: WebExtensions → Core
Resolution: --- → INVALID
Version: Firefox 103 → unspecified

Thanks, that makes sense with regard to the different behaviour. Is this documented anywhere? I'm assuming that this is a legacy Firefox-only thing, but wasn't able to find anything about it online.

(In reply to toasted.nutbread from comment #3)

Thanks, that makes sense with regard to the different behaviour. Is this documented anywhere? I'm assuming that this is a legacy Firefox-only thing, but wasn't able to find anything about it online.

The behavior related to strict mode and readonly properties, and the fact that classes default to strict mode, is part of the ECMAScript specification and is documented in a number of places.

Looking closer, the data property isn't part of any specification, and it could be a valid expando property that might be accepted by other implementations, so perhaps we should consider making it writable. That said, adding unprifixed expandos to native types is always risky, particularly in the face of future interface changes.

Status: RESOLVED → REOPENED
Ever confirmed: true
Resolution: INVALID → ---

Sorry, I should have clarified that I was only referring to the existence of the data property; the strict mode stuff I understand and makes sense.

Severity: -- → S3
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: