界: Code Quality

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

93 找到的項目
弱點
Abstract
NaN 進行比較一律是錯誤。
Explanation
NaN 進行比較時,一律會估算為 false,除了 != 運算子之外,這個運算子一律會估算為 true,因為 NaN 處於未排序狀態。

範例 1:以下將嘗試確定變數不是 NaN


...
if (result == Double.NaN){
//something went wrong
throw new RuntimeException("Something went wrong, NaN found");
}
...


這會嘗試驗證 result 不是 NaN,不過使用 == 運算子搭配 NaN 一律會產生值 false,因此這項檢查永不會拋出異常。
References
[1] NUM07-J. Do not attempt comparisons with NaN CERT
[2] Java Language Specification Chapter 4. Types, Values, and Variables Oracle
[3] INJECT-9: Prevent injection of exceptional floating point values Oracle
[4] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[5] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 3
[6] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[7] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[8] Standards Mapping - Common Weakness Enumeration CWE ID 486
desc.structural.java.code_correctness_comparison_with_nan
Abstract
此類別的建構函式所呼叫的函數可被取代。
Explanation
當建構函式呼叫可取代的函數時,可能會在物件完全初始化之前,允許攻擊者存取 this 參考,這可進而導致弱點。

範例 1:以下呼叫可被取代的方法。


...
class User {
private String username;
private boolean valid;
public User(String username, String password){
this.username = username;
this.valid = validateUser(username, password);
}
public boolean validateUser(String username, String password){
//validate user is real and can authenticate
...
}
public final boolean isValid(){
return valid;
}
}


由於 validateUser 函數和類別不是 final,這表示它們可以被取代,然後將變數初始化為會取代此函數的子類別時,會允許規避 validateUser 功能。例如:


...
class Attacker extends User{
public Attacker(String username, String password){
super(username, password);
}
public boolean validateUser(String username, String password){
return true;
}
}
...
class MainClass{
public static void main(String[] args){
User hacker = new Attacker("Evil", "Hacker");
if (hacker.isValid()){
System.out.println("Attack successful!");
}else{
System.out.println("Attack failed");
}
}
}
Example 1 中的程式碼會列印「Attack successful!」,因為 Attacker 類別會覆寫從 User 超級類別之建構函式呼叫的 validateUser() 函數,並且 Java 會先查看子類別中是否有從建構函式呼叫的函數。
References
[1] MET05-J. Ensure that constructors do not call overridable methods CERT
[2] EXTEND-5: Limit the extensibility of classes and methods Oracle
[3] OBJECT-4: Prevent constructors from calling methods that can be overridden Oracle
[4] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[5] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 2
[6] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[7] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
desc.structural.java.code_correctness_constructor_invokes_overridable_function
Abstract
根據物件的類別名稱來判定物件類型可能會導致意想不到的行為或讓攻擊者注入惡意的類別。
Explanation
攻擊者可能故意複製類別名稱,來迫使程式執行惡意程式碼。因此,使用類別名稱來識別類型並不適合,所以不應據此對特定物件授予信任。

範例 1:以下程式碼根據物件類別名稱來判斷是否信任來自 inputReader 物件的輸入。如果一個攻擊者能提供執行惡意指令的 inputReader 實作,這個程式碼將無法區分物件是否為惡意版本。


if (inputReader.GetType().FullName == "CompanyX.Transaction.Monetary")
{
processTransaction(inputReader);
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 3
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 486
desc.dataflow.dotnet.code_correctness_erroneous_class_compare
Abstract
根據物件的類別名稱來判定物件類型可能會導致意想不到的行為或讓攻擊者插入惡意的類別。
Explanation
攻擊者可能故意複製類別名稱,來迫使程式執行惡意程式碼。因此,使用類別名稱來識別類型並不適合,所以不應據此對特定物件授予信任。

範例 1: 以下程式碼根據物件類別名稱來判斷是否信任來自 inputReader 物件的輸入。如果一個攻擊者能提供執行惡意指令的 inputReader 實作,這個程式碼將無法區分物件是否為惡意版本。


if (inputReader.getClass().getName().equals("com.example.TrustedClass")) {
input = inputReader.getInput();
...
}
References
[1] OBJ09-J. Compare classes and not class names CERT
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 3
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 486
desc.dataflow.java.code_correctness_erroneous_class_compare
Abstract
根據物件的類別名稱判斷物件的類型時,可能會導致意外行為,或允許攻擊者注入惡意類別。
Explanation
攻擊者可能故意複製類別名稱,來迫使程式執行惡意程式碼。因此,使用類別名稱來識別類型並不適合,所以不應據此對特定物件授予信任。

範例 1: 以下程式碼根據物件類別名稱來判斷是否信任來自 inputReader 物件的輸入。如果一個攻擊者能提供執行惡意指令的 inputReader 實作,這個程式碼將無法區分物件是否為惡意版本。


if (inputReader::class.qualifiedName == "com.example.TrustedClass") {
input = inputReader.getInput()
...
}
References
[1] OBJ09-J. Compare classes and not class names CERT
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 3
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 486
desc.dataflow.kotlin.code_correctness_erroneous_class_compare
Abstract
為欄位錯誤地指派了負值。
Explanation
已使用 FortifyNonNegative 註解此欄位,用來表示負值並不是允許的值。
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 3
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 20
[6] Standards Mapping - Common Weakness Enumeration Top 25 2021 [4] CWE ID 020
[7] Standards Mapping - Common Weakness Enumeration Top 25 2022 [4] CWE ID 020
[8] Standards Mapping - Common Weakness Enumeration Top 25 2023 [6] CWE ID 020
[9] Standards Mapping - OWASP Application Security Verification Standard 4.0 5.1.3 Input Validation Requirements (L1 L2 L3), 5.1.4 Input Validation Requirements (L1 L2 L3)
[10] Standards Mapping - SANS Top 25 2009 Insecure Interaction - CWE ID 020
desc.structural.java.erroneous_negative_value_field
Abstract
表示式 x = NULLx != NULL 將永遠為 false。
Explanation
在 PL/SQL 中,NULL 的值不確定。此值沒有任何對等的值,甚至也不等於另一個 NULL 值。另外,null 值絕對不會與其他值相等。

範例 1:下列陳述將永遠為 false。


checkNull BOOLEAN := x = NULL;
範例 2:下列陳述將永遠為 false。


checkNotNull BOOLEAN := x != NULL;
References
[1] Steven Feuerstein Oracle PL/SQL Best Practices O'Reilly
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 3
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 480
desc.structural.sql.code_correctness_erroneous_null_comparison_plsql
Abstract
應使用 equals() 方法來比較字串,而不是 ==!=
Explanation
此程式使用 ==!= 來比較兩個字串是否相等,其為比較兩個物件是否相同,而非它們的值。因此,若使用這個方法,兩個參照將永遠不會相等。

範例 1:永遠不會執行以下分支語句。


if (args[0] == STRING_CONSTANT) {
logger.info("miracle");
}


當使用 ==!= 運算子來比較相同物件中的字串時,它們只會如預期般的運作。要出現這種情況,最常見的方法就是將字串控制在內部,這樣,將字串增加到由 String 類別維護的物件池中。一旦將字串控制在內部,每次使用這個字串時都將使用相同的物件,並且相等運算子會如預期般運作。所有字串文字和以字串當作值的常數會自動控制在內部。其他字串可以透過呼叫 String.intern() 來以手動控制在內部,這將會回傳目前字串的標準實例,必要時也會建立一個實例。
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 3
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 597
desc.structural.java.code_correctness_erroneous_string_compare
Abstract
為變數錯誤地指派了零值。
Explanation
已使用 FortifyNonZero 註解此欄位,用來表示零並不是允許的值。
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 3
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 20
[6] Standards Mapping - Common Weakness Enumeration Top 25 2021 [4] CWE ID 020
[7] Standards Mapping - Common Weakness Enumeration Top 25 2022 [4] CWE ID 020
[8] Standards Mapping - Common Weakness Enumeration Top 25 2023 [6] CWE ID 020
[9] Standards Mapping - OWASP Application Security Verification Standard 4.0 5.1.3 Input Validation Requirements (L1 L2 L3), 5.1.4 Input Validation Requirements (L1 L2 L3)
[10] Standards Mapping - SANS Top 25 2009 Insecure Interaction - CWE ID 020
desc.structural.java.erroneous_zero_value_field