简体   繁体   English

为什么__caller__不安全?

[英]Why is __caller__ unsafe?

The following seems to be a reasonable use of __caller__ : 以下似乎是__caller__的合理用法:

var foo=1;
function a() {
    var foo=2;
    function b() {
        var foo=3;
        foo; // 3
        this.foo; // 1, from global
        __caller__.foo // 2
    }
    b();
}
a(); // creates a's execution context

However, __caller__ is not available. 但是, __caller__不可用。 Why not? 为什么不? If the global context/variable object can be accessed using this , then why not a 's? 如果可以使用this来访问全局上下文/变量对象,那么为什么不能a

Doc says : Doc说

The special property __caller__, which returned the activation object of the caller thus allowing to reconstruct the stack, was removed for security reasons. 出于安全原因,删除了特殊属性__caller__,该属性返回了调用者的激活对象,从而可以重建堆栈。

And it is easy to see why this could be a security disaster in a browser where much of the UI is implemented in JavaScript. 而且很容易理解为什么在大多数UI用JavaScript实现的浏览器中这可能会造成安全隐患。 Imagine having one of your functions called by an add-on or other chrome. 想象一下,您的功能之一被附加组件或其他Chrome调用了。 You could look up the call stack and read callers' (potentially sensitive) variables, or even inject JavaScript values into caller functions, potentially subverting them to do something against the user's wishes. 您可以查找调用堆栈并读取调用方的(可能敏感的)变量,甚至将JavaScript值注入调用方函数中,从而有可能颠覆它们,从而违背用户的意愿。 Effectively every web page would get chrome security privileges and completely compromise the browser. 实际上,每个网页都将获得Chrome安全权限,并完全损害浏览器。

You certainly should never have used it in real JavaScript, because it was a non-standard Mozilla-only implementation detail, not to mention incredibly ugly. 您当然不应该在真正的JavaScript中使用过它,因为它是非标准的仅适用于Mozilla的实现细节,更不用说难看了。 It does not have the lexical behaviour you normally expect of JS. 它没有您通常期望的JS的词汇行为。

I am not really familiar with the subject, but have you tried arguments.callee.caller ? 我对这个主题不是很熟悉,但是您是否尝试过arguments.callee.caller

See here: Javascript how do you find the caller function? 参见此处: Javascript如何找到调用者函数?

In your example, you can argue that things in b should be able to address things in the active instance of a , and it seems reasonable because a encloses b . 在您的示例中,您可以辩称b中的事物应该能够处理b的活动实例中a事物,并且这似乎是合理的,因为a包围了b But if that weren't the case, say you define 但是如果不是这种情况,请说您定义

    function c() {
        var foo='hedgehog';
        b();
    }

that's something else entirely, so your argument looks like it applies to a pretty special case. 这完全是另外一回事,因此您的论点似乎适用于一个非常特殊的情况。

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

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