界: Code Quality

程式碼品質不佳,會導致無法預料的行為。從使用者的角度來看,這通常表現為可用性不佳。對於攻擊者而言,這提供了以意想不到的方式向系統施加壓力的機會。

Solidity Bad Practices: Default Function Visibility

Abstract
函數未指定明確存取修飾符。
Explanation
在制訂 Solidity 智慧合約時,開發人員可以設定函數的存取修飾符來控制誰可以叫用它。如果開發人員未指定存取修飾符,則函數將預設為 public,從而允許惡意動作執行者在未經授權的情況下叫用該函數。

範例 1:以下程式碼無法在這兩個函數中設定存取修飾符,因此任何人都可以叫用它們,從而允許使用者略過對 require 的呼叫。


function withdrawWinnings() {
require(uint32(msg.sender) == 0);
_sendWinnings();
}

function _sendWinnings() {
msg.sender.transfer(this.balance);
}
References
[1] Enterprise Ethereum Alliance Code Linting
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 3
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 710
[7] Standards Mapping - Smart Contract Weakness Classification SWC-100
desc.structural.solidity.swc100