简体   繁体   中英

JavaScript - Make Proxy undetectable

As I understand, the ES spec says that Proxy (global constructor for proxifying objects, functions and classes) is not detectable. It means that if I proxify a function, nobody who uses that proxified function can detect that I used Proxy. However, apparently I misunderstood it, becuase proxifying functions is detectable.

For example, new Proxy(a=>a,{apply:a=>a})+'' throws an error. It says

Uncaught TypeError: Function.prototype.toString requires that 'this' be a Function

However, typeof new Proxy(a=>a,{apply:a=>a}) is indeed "function" , but it somehow fails to stringify the proxy. So, obviously, here is a situation when proxified function doesn't behave as non-proxified one should. Function.prototype.toString is able to distinguish proxified and non-proxified function.

My goal is to proxify a function such that it simple become undetectable. My first idea is to literally proxify the Proxy like so:

Proxy.toString = (a => () => a)(Proxy + '');
Proxy = new Proxy(Proxy, {
  construct: (f, args) => {
    if(typeof args[0] == 'function'){
      var a = args[0] + '';
      args[0].toString = () => a;
    }
    return new f(...args);
  }
});

But, sadly, this is detectable. If someone call Function.prototype.toString binded to my proxified function, the error will occur and he can therefore detect that my function is actually a proxy. So, I tried to proxify the Function and Function.prototype and also Function.prototype.toString , but then I realized I cannot proxify the Function because even if I override the global property Function , someone may access it using (a=>a).constructor .

So, this is why I am asking it here because I ran out of ideas. How to proxify a function to make it completelly undetectable? In the ES spec it explicitly says that "Proxy is undetectable" , so as a side question, why is then proxifying a function detectable?

Edit

The reason I'm trying to achieve this is because I'm working on enhanced advertisement blocking extension for Chrome. I am dealing with very agressive website which exploits a huge amount of JavaScript tricks to detect if I'm viewing the ads or not. So, basically, I deleted an advertisement, and then their script checks if there is specific element, if not, then I cannot visit the website. So, I tried to proxify document.getElementById , but they check if it is proxified and if it is, I cannot visit the website, so I must make it undetectable.

I don't think what you're trying to do is possible with Proxies. The spec for Function.prototype.toString clearly defines the TypeError -throwing behavior. Since there's no way to give a Proxy an [[ECMAScriptCode]] "internal slot", it'll always throw when called on a Proxy .

I also see no mention of any " Proxy is undetectable " statement in the spec; the string 'detectable' doesn't show up anywhere in the document. Where did you find this claim?

Maybe you can overwrite functions (and their .toString properties) to achieve your goal? Roughly:

var original_getElementById = document.getElementById;
document.getElementById = function(id) {
  if (...) {
    return original_getElementById(id);
  } else {
    // special handling here
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM