[英]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.