我想知道下面的函數如何設置'this'元素並獲取數組單元格?請。JavaScript安全問題
//my secure code
var priv = ['item-0','item-1'];
var api = {push: function(x){priv.push(x)}}
api.store = function(i,x){priv[i] = x}
//the attaker script
var result;
api.store('push',function(){result = this[0]});
api.push();
//the result is cell 0 of private array
//how?
//if i change the 'push' parameter then the result is empty!
document.write(result)
根據上下文,'this'關鍵字可能有不同的含義。您還沒有在任何地方聲明'result'變量,所以默認情況下它具有全局範圍,這就是爲什麼它可以在函數外部使用。你能否提供關於你的問題的更多細節 – CROSP
@CROSP我有一些關鍵的內容,我想保證它們的安全。 我的一位朋友向我發送了這段代碼,我意識到他可以使用我的API對象訪問私有變量。 – easa
@easa如果攻擊者可以在同一個環境中執行任意代碼,通常你已經丟失了。你究竟想要保護什麼? – Bergi