简体   繁体   English

JavaScript-使代理无法检测

[英]JavaScript - Make Proxy undetectable

As I understand, the ES spec says that Proxy (global constructor for proxifying objects, functions and classes) is not detectable. 据我了解,ES规范指出Proxy (用于代理对象,函数和类的全局构造函数)不可检测。 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. 例如, new Proxy(a=>a,{apply:a=>a})+''会引发错误。 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. 但是, typeof new Proxy(a=>a,{apply:a=>a})类型确实是"function" ,但是以某种方式无法对代理进行字符串化。 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. Function.prototype.toString能够区分代理函数和非代理函数。

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:

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. 如果有人将Function.prototype.toString绑定到我的代理函数,则将发生错误,因此他可以检测到我的函数实际上是代理。 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 . 因此,我尝试代理FunctionFunction.prototype以及Function.prototype.toString ,但是后来我意识到我无法代理Function因为即使我重写了全局属性Function ,也有人可以使用(a=>a).constructor访问它(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? 在ES规范中,它明确指出“代理是不可检测的” ,因此,作为一个附带的问题,为什么然后代理可检测的功能?

Edit 编辑

The reason I'm trying to achieve this is because I'm working on enhanced advertisement blocking extension for Chrome. 我之所以要实现这一目标,是因为我正在开发适用于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. 我正在处理一个非常激进的网站,该网站利用大量的JavaScript技巧来检测我是否正在查看广告。 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. 因此,我尝试代理document.getElementById ,但是他们检查它是否为代理,如果不是,则无法访问该网站,因此必须使其无法检测。

I don't think what you're trying to do is possible with Proxies. 我认为您使用Proxies不可能做到。 The spec for Function.prototype.toString clearly defines the TypeError -throwing behavior. Function.prototype.toString规范明确定义了TypeError行为。 Since there's no way to give a Proxy an [[ECMAScriptCode]] "internal slot", it'll always throw when called on a Proxy . 由于无法为Proxy提供[[ECMAScriptCode]] “内部插槽”,因此在Proxy上调用时,它将始终抛出。

I also see no mention of any " Proxy is undetectable " statement in the spec; 我也没有在规范中提到任何“ Proxy is notdetectable ”的说法。 the string 'detectable' doesn't show up anywhere in the document. 字符串“ detectable”不会显示在文档中的任何位置。 Where did you find this claim? 您在哪里找到这个要求?

Maybe you can overwrite functions (and their .toString properties) to achieve your goal? 也许您可以覆盖函数(及其.toString属性)以实现您的目标? Roughly: 大致:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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