繁体   English   中英

是 javascript function object 不可变的实例

[英]is an instance of a javascript function object immutable

我是否可以说 function object 的实例是不可变的,因为我们无法在创建 function 后对其进行修改?

无论如何,重新表述我的问题:

  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] 浏览器中这不安全,因为......”这样的答案。

我知道,由于代码是在客户端运行的,如果客户端有恶意,他将能够为所欲为。但是这个问题专门针对保护“没有恶意的用户”,在换句话说..一个普通用户(如果用户是一个黑客,我不介意让他乱用他想要的功能,因为他会把所有的异常都扔到他的脸上,这不关我的事)

不:

var f = new Function("");
f.prop = 1;

您无法阻止变量g1在所有浏览器上重新分配。 一些浏览器允许您将g1定义为常量,因此:

const g1 = G(f1);

这将防止名称g1被反弹,您可以使用Object.defineProperty在其他人上定义window只读全局属性,但通常在 Z686155AF70C10A0F6E9E98D 中没有const定义。

为了更清楚,请考虑两种情况:

(1) 攻击者可以在声明了f1的 scope 中运行代码,然后其他代码读取f1

var f1 = ...;   // You define f1
f1 = function () { return false; };  // Attacker code runs
doSomethingWith(f1());  // Naive code reads f1 and calls it.

在这种情况下,攻击者成功地混淆了天真的代码,因为他们更改了f1的值。

(2) 攻击者在读取f1后在 scope 中运行代码。

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

暂无
暂无

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

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