You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Define a non-writeable non-configurable own .toString() on the Trusted* objects #613
The various Trusted* interfaces (e.g. TrustedScript) are objects that contain a string that is marked as trusted. Then using these objects in place of strings, e.g. in eval(), or in .innerHTML, the browser needs to look at what is this internal string and use it instead of the object.
All of them also have a .toString() method on the prototype, which returns the trusted string.
.toString() however can be replaced/shadowed, either by replacing the one on the prototype or by defining an own .toString property on one of the instances themselves:
This can cause problems in code that assumes that a trusted object's stringified version is the trusted string, both in userland and potentially in browser implementations that might try to implement some optimizations on trusted types.
To avoid potential security issues, we can make it so that one cannot tamper with the .toString() behavior of this object, bye making .toString() an own (so, non-shadowable) non-writable non-configurable property. Effectively, a TrustedHTML becomes something like
classTrustedHTML{constructor(data){// the constructor is not actually exposed to usersthis.#data =data;Object.defineProperty(this,"toString",{value: TrustedHTML.#originalToString,enumerable: false,writable: false,configurable: false,});}toString(){returnthis.#data;}toJSON(){returnthis.#data;}static #originalToString =TrustedHTML.prototype.toString;}
Is there a nice way to do this via WebIDL? because this is currently just defined via the stringifier WebIDL concept. Note though that it's only eval() and new Function() where this function is was ever looked at. In all "web" code it just gets the raw data value.
I'm not sure if it really matters given its now only userland code that ever looks at this?
Activity
lukewarlow commentedon Aug 25, 2026
Is there a nice way to do this via WebIDL? because this is currently just defined via the
stringifierWebIDL concept. Note though that it's onlyeval()andnew Function()where this functioniswas ever looked at. In all "web" code it just gets the raw data value.I'm not sure if it really matters given its now only userland code that ever looks at this?
annevk commentedon Aug 26, 2026
[LegacyUnforgeable]which we kinda did not want to use anymore, but maybe this is a valid exception? Hmm.