gpt4 book ai didi

javascript - 一个 JavaScript 漏洞 - "this"- 窃取 :

转载 作者:行者123 更新时间:2023-11-29 22:24:33 26 4
gpt4 key购买 nike

我正在学习 Caja,我对“this”-stealing 的概念感到困惑:

Another security vulnerability that Caja addresses is called “this” stealing – if an object’s clients can add methods to the object’s state which alias its “this” then the aforementioned protected “this” rule does not apply.

然后他们显示以下构造函数:

function Cell(value) {
this.x_ = "secret";
this.value = value;
}

有一个涉及“x_”的隐藏漏洞:

下面的代码可以让表达式揭示那个 secret 值:

(new Cell(
function (){
return this.x_;
})).value()

这是如何运作的?为什么会出现这样的问题?我很感激任何提示或建议。

最佳答案

简化它:

(new Cell( )).value()

我们正在从 Cell 构造函数创建一个新对象,并立即调用它的值方法。当然 value 方法还没有做任何事情,这就是我们下一部分的用武之地:

function (){ 
return this.x_;
}

这就是我们作为 value 参数传递给构造函数的内容。此函数分配给 Cell 构造函数中的 this.value

所以 Cell 构造函数现在看起来像这样:

function Cell(value) {
this.x_ = "secret";
this.value = function (){
return this.x_;
};
}

那么当您从 Cell 创建一个新对象并调用它的 value 成员时会发生什么?内部函数返回 Cell 对象的 x_ 值,从而揭示 secret 文本。

关于javascript - 一个 JavaScript 漏洞 - "this"- 窃取 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10360681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com