问题描述
|
我是说函数对象的实例是不可变的,因为一旦创建函数,我们就无法修改它?
无论如何,改写我的问题:
var f1=function(){
return true;
}
//Now i pass **f1** into the function **G**,storing it to **g1**
function G(f){
return function(){
return f();
}
}
var g1=G(f1);
//I will try to hack/do anything i can to **f1**
//Now i will pass f1 to hacked piece of injection code which (assumingly) will try to hack f1
g1(); // but I can be 100% sure this will still return me true
所以现在我可以确定,无论我对f1做什么,g1()都将永远返回true?
尽管对至少具有0.5%互联网用户市场份额的浏览器感兴趣,但我还是欢迎回答:“在[x]浏览器中,这是不安全的,因为..“
我知道,由于代码是在客户端运行的,因此,如果客户端有恶意的意图,他将能够做他想做的任何事情。但是,这个问题专门针对保护“没有恶意意图的用户”。 ,换句话说,..普通用户(如果该用户是黑客,那么我不介意让他搞乱他想要的功能,因为他会把所有异常都扔在他的脸上,这就是不关我的事)
解决方法
您不能停止在所有浏览器上重新分配变量“ 1”。某些浏览器允许您将ѭ1定义为常量,因此:
const g1 = G(f1);
这样可以防止名称ѭ1反弹,您可以使用Object.defineProperty
在其他对象上定义window
的只读全局属性,但通常在JavaScript中没有const
定义。
为了更加清楚,请考虑以下两种情况:
(1)攻击者可以在声明“ 8”的范围内运行代码,然后其他代码读取“ 8”。
var f1 = ...; // You define f1
f1 = function () { return false; }; // Attacker code runs
doSomethingWith(f1()); // Naive code reads f1 and calls it.
在这种情况下,攻击者成功地混淆了幼稚的代码,因为他们更改了f1
的值。
(2)攻击者在读取“ 8”之后在范围内运行代码。
var f1 = ...; // You define f1
// Cautious code reads and stores f1 in a safe place for later use.
(function () {
var f = f1;
setTimeout(0,function () { doSomethingWith(f()); });
})();
f1 = function () { return false; }; // Attacker code runs.
在这种情况下,攻击者失败了,因为在攻击者更改存储在f1
中的值之前,谨慎的代码读取了f1
的值,因此私有f
继续返回true
。
,没有:
var f = new Function(\"\");
f.prop = 1;