I'm on the Chrome Extensions team, and we've run into a problem where extensions override Array.prototype.forEach in a way that breaks our internal JS.A workaround we've done is to write our own forEach method, but this problem is widespread - extensions also override JSON, document.createElement, etc - the vector for accidental breakage is as widespread as all of the JS and DOM libraries.
What is the best way to protect against this in a general way?
The only safe thing I can think of is to run all our code in a separate context, but I've been told that creating contexts is an expensive operation. How expensive?
Alternatively, apparently v8 has solved this problem internally by guaranteeing that it's running the builtin libraries - is/can this be exposed?
Cheers,Ben.--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
On 13 March 2013 14:56, Michael Schwartz <myk...@gmail.com> wrote:Can Harmony Proxies be used to detect when the prototypes or builtins are
being overridden?
If so, you could save the original and provide a new API to fetch the
original.
I don't see how proxies help here. Saving the originals certainly doesn't require proxies (nor private symbols).
I'm on the Chrome Extensions team, and we've run into a problem where extensions override Array.prototype.forEach in a way that breaks our internal JS.A workaround we've done is to write our own forEach method, but this problem is widespread - extensions also override JSON, document.createElement, etc - the vector for accidental breakage is as widespread as all of the JS and DOM libraries.
What is the best way to protect against this in a general way? The only safe thing I can think of is to run all our code in a separate context, but I've been told that creating contexts is an expensive operation. How expensive? Alternatively, apparently v8 has solved this problem internally by guaranteeing that it's running the builtin libraries - is/can this be exposed?
Welcome to JavaScript! Have you considered using a language with a sane specification?
Have you tried benchmarking it? If you can't measure it, it's not important. If you can, you'll have data to help decide whether the difference is a problem in your use case or not.
Alternatively, apparently v8 has solved this problem internally by guaranteeing that it's running the builtin libraries - is/can this be exposed?
Sorry, no.You can lobby for an official way to retrieve the original unpatched implementations to be included in ECMAScript 7. The general problem with introducing sanity, however, is that you can't break existing code, which basically means that all the good stuff has to be opt-in, which in turn means that the original problem doesn't just go away, instead you'll still have to support it until it slowly dies out, if ever.
Crazy idea: how about a rule for the Chrome web store that forbids monkey-patching builtins? :-)
Not really feasible, monkey patching is an idiom that JS developers would get very upset about being taken away from them. Plus lots of libraries do it. We had a hard enough problem (and in fact failed) forbidding eval. And likewise freezing globals etc. Philosophically it's just sad if we forbid existing features of a platform because it's too hard to implement.
Lastly - I am not really sure about how harmony proxies work, but it sounds like we're told after they change not before they change? If not, that would certainly be a neater solution than what we're doing for JSON (saving function references on load), but I can see it getting messy with prototypes.
Well, but "too hard to implement" is not the reason. You're not asking how you can implement monkey-patching; you're asking how you can stop/limit/blacklist/undo/circumvent it because it's harmful. The way I see it, the platform is shooting itself in the foot, repeatedly, but it gets angry at us when we suggest to take away its gun.
Lastly - I am not really sure about how harmony proxies work, but it sounds like we're told after they change not before they change? If not, that would certainly be a neater solution than what we're doing for JSON (saving function references on load), but I can see it getting messy with prototypes.
I guess the best you can do is before you hand the context over to the extension, create a copy of all the library functions/objects you care about. Then you can restore that as necessary when control comes back to your code. It should be possible to package this behavior relatively nicely; maybe a custom alternate global object helps (so you wouldn't have to restore anything, you'd just mechanically s/Array/original_global.Array/g in your code). You don't need proxies for this, nor any other way to observe what the extension is mucking with.
--
*If* you know your scripts will run first and *if* it is acceptable to deny external scripts the ability to modify built-ins, you could consider using SES. https://code.google.com/p/es-lab/wiki/SecureEcmaScript