界: API Abuse

API 就像是呼叫者與被呼叫者之間簽訂的規定。最常見的 API 濫用形式是由呼叫者這一當事方未能遵守此規定所造成的。例如,如果程式在呼叫 chroot() 後無法呼叫 chdir(),即違反規範如何以安全方式變更使用中根目錄的規定。程式庫濫用的另一個好例子是期待被呼叫者向呼叫者傳回值得信賴的 DNS 資訊。在這種情況下,呼叫者是透過對其行為做出某些假設 (傳回值可用於驗證目的) 來濫用被呼叫者 API。另一方也可能違反呼叫者與被呼叫者間的規定。例如,如果編碼器衍生出子類別 SecureRandom 並傳回一個非隨機值,則違反了規定。

81 找到的項目
弱點
Abstract
已在未實作 Equals() 的物件上呼叫 Equals()
Explanation
比較物件時,開發人員通常會想要比較物件的特性。不過,在未明確執行 Equals() 的類別 (或任何上層類別/介面) 上呼叫 Equals(),會造成對繼承自 System.ObjectEquals() 方法的呼叫。Object.Equals() 會比較兩個物件實例來確認物件是否相同,而不是比較物件成員欄位或其他特性。雖然有合法的 Object.Equals() 使用,但是這通常表示程式碼錯誤。

範例 1:

public class AccountGroup
{
private int gid;

public int Gid
{
get { return gid; }
set { gid = value; }
}
}
...
public class CompareGroup
{
public bool compareGroups(AccountGroup group1, AccountGroup group2)
{
return group1.Equals(group2); //Equals() is not implemented in AccountGroup
}
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 2
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
desc.structural.dotnet.code_correctness_class_does_not_implement_equals
Abstract
已在未實作 equals() 的物件上呼叫 equals() 方法。
Explanation
比較物件時,開發人員通常會想要比較物件的特性。不過,在未明確執行 equals() 的類別 (或任何上層類別/介面) 上呼叫 equals(),會造成對繼承自 java.lang.Objectequals() 方法的呼叫。Object.equals() 會比較兩個物件實例來確認物件是否相同,而不是比較物件成員欄位或其他特性。雖然有合法的 Object.equals() 使用,但是這通常表示程式碼錯誤。

範例 1:

public class AccountGroup
{
private int gid;

public int getGid()
{
return gid;
}

public void setGid(int newGid)
{
gid = newGid;
}
}
...
public class CompareGroup
{
public boolean compareGroups(AccountGroup group1, AccountGroup group2)
{
return group1.equals(group2); //equals() is not implemented in AccountGroup
}
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 2
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
desc.structural.java.code_correctness_class_does_not_implement_equals
Abstract
finalize() 方法應該呼叫 super.finalize()
Explanation
Java 語言編程規範 (Java Language Specification) 中指出,在 finalize() 方法中呼叫 super.finalize() 方法是一種非常好的做法 [1]。

範例 1: 下列方法沒有呼叫 super.finalize()


protected void finalize() {
discardNative();
}
References
[1] J. Gosling, B. Joy, G. Steele, G. Bracha The Java Language Specification, Second Edition Addison-Wesley
[2] MET12-J. Do not use finalizers CERT
[3] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[4] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[5] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[6] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[7] Standards Mapping - Common Weakness Enumeration CWE ID 568
desc.structural.java.code_correctness_erroneous_finalize_method
Abstract
針對序列化中所使用的方法,使用不正確的方法簽章,可能會導致永遠不會呼叫此方法。
Explanation
當可序列化類別建立序列化或還原序列化函數但未遵循正確的簽章時,會發生「程式碼正確性:不正確的可序列化方法簽章」問題:


private void writeObject(java.io.ObjectOutputStream out) throws IOException;
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
private void readObjectNoData() throws ObjectStreamException;


違反序列化所需要的方法簽章,可能意味著在序列化/還原序列化期間永遠不會呼叫此方法,進而導致序列化/還原序列化不完整,也可能意味著,不可信賴的程式碼可能會取得物件的存取權。
若有異常未拋出,則可能意味著序列化/還原序列化失敗並導致應用程式當機,或者甚至可能悄然失敗以致物件可能僅部分正確建構,從而導致對漏洞進行除錯變得極為困難。呼叫者應捕捉這些異常,以便不正確的序列化/還原序列化能夠得以適當處理,且不會出現當機或部分建構的物件。
References
[1] SER01-J. Do not deviate from the proper signatures of serialization methods CERT
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
desc.structural.java.code_correctness_incorrect_serializable_method_signature
Abstract
Servlet 的輸出串流交付之後,將串流緩衝區重設或執行重新交付給串流的任何其他動作等,都是錯誤的。同樣地,在呼叫 getOutputStream 之後呼叫 getWriter() 或 (反之亦然) 也是錯誤的。
Explanation
發送 HttpServletRequest、重新導向 HttpServletResponse 或排清 Servlet 的輸出串流緩衝區等,都會造成相關的串流交付。任何後續的緩衝區重設或串流交付時 (例如其他排清或重新導向),將造成 IllegalStateException

此外,Java Servlet 會允許使用 ServletOutputStreamPrintWriter (但不是兩者同時),將資料重新寫入回應串流。呼叫 getOutputStream() 之後呼叫 getWriter() (反之亦然) 也會造成 IllegalStateException



在執行階段,IllegalStateException 可防止回應處理常式執行完成,以有效撤銷回應。這樣可能會造成伺服器不穩定,而這是錯誤執行的 Servlet 徵兆。

範例 1:以下程式碼在 Servlet 回應輸出串流緩衝區排清後,將 Servlet 回應重新導向。

public class RedirectServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
...
OutputStream out = res.getOutputStream();
...
// flushes, and thereby commits, the output stream
out.flush();
out.close(); // redirecting the response causes an IllegalStateException
res.sendRedirect("http://www.acme.com");
}
}
範例 2:相反地,以下程式碼嘗試在發送要求之後寫入和排清 PrintWriter 的緩衝區。

public class FlushServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
...
// forwards the request, implicitly committing the stream
getServletConfig().getServletContext().getRequestDispatcher("/jsp/boom.jsp").forward(req, res);
...

// IllegalStateException; cannot redirect after forwarding
res.sendRedirect("http://www.acme.com/jsp/boomboom.jsp");

PrintWriter out = res.getWriter();

// writing to an already-committed stream will not cause an exception,
// but will not apply these changes to the final output, either
out.print("Writing here does nothing");

// IllegalStateException; cannot flush a response's buffer after forwarding the request
out.flush();
out.close();
}
}
References
[1] IllegalStateException in a Servlet - when & why do we get?
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 2
[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 398
[7] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001094
[8] Standards Mapping - NIST Special Publication 800-53 Revision 4 SC-5 Denial of Service Protection (P1)
[9] Standards Mapping - NIST Special Publication 800-53 Revision 5 SC-5 Denial of Service Protection
[10] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002400 CAT II
[11] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002400 CAT II
[12] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002400 CAT II
[13] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002400 CAT II
[14] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002400 CAT II
[15] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002400 CAT II
[16] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002400 CAT II
[17] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002400 CAT II
[18] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002400 CAT II
[19] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002400 CAT II
[20] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002400 CAT II
[21] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002400 CAT II
[22] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002400 CAT II
[23] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002400 CAT II
desc.controlflow.java.code_correctness_multiple_stream_commits
Abstract
Content-Length 表頭設為負數。
Explanation
在多數狀況下,設定要求的 Content-Length 表頭即表示開發人員想要
溝通傳送到伺服器的 POST 資料長度。但是,此表頭應為 0 或者
正整數。

範例 1:下列程式碼將設定不正確的 Content-Length

URL url = 新 URL(「http://www.example.com」);
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
huc.setRequestProperty("Content-Length", "-1000");
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 3
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
[6] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.6
[7] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.6
[8] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.6
[9] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.6
desc.structural.java.api_abuse_code_correctness_negative_content_length
Abstract
Content-Length 表頭設為負數。
Explanation
在多數狀況下,設定要求的 Content-Length 表頭即表示開發人員想要
溝通傳送到伺服器的 POST 資料長度。但是,此表頭應為 0 或者
正整數。

範例 1:下列程式碼將 Content-Length 表頭錯設為負數:

xhr.setRequestHeader("Content-Length", "-1000");
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 3
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
[6] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.6
[7] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.6
[8] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.6
[9] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.6
desc.structural.javascript.api_abuse_code_correctness_negative_content_length
Abstract
ToString() 在陣列上受到呼叫。
Explanation
在多數案例中,於陣列上呼叫 ToString() 表示開發人員有意將陣列內容做為字串回傳。不過,在陣列上直接呼叫 ToString(),將會回傳包含陣列類型的字串值。

範例 1:以下程式碼將輸出 System.String[]

String[] stringArray = { "element 1", "element 2", "element 3", "element 4" };
System.Diagnostics.Debug.WriteLine(stringArray.ToString());
References
[1] Class Arrays Microsoft
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[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 398
desc.structural.dotnet.code_correctness_tostring_on_array
Abstract
toString() 在陣列上受到呼叫。
Explanation
在多數案例中,於陣列上呼叫 toString() 表示開發人員有意將陣列內容做為字串回傳。不過,在陣列上直接呼叫 toString(),將會回傳記憶體中包含陣列類型和雜湊碼的字串值。
範例 1:下列程式碼將輸出 [Ljava.lang.String;@1232121

String[] strList = new String[5];
...
System.out.println(strList);
References
[1] Class Arrays Sun Microsystems
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[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 398
desc.structural.java.code_correctness_tostring_on_array
Abstract
已將此欄位註解為危險。會標記所有用法。
Explanation
FortifyDangerous 註解已套用至此欄位。會使用此方式來指出危險,而且應該檢查所有用法的安全性。
desc.structural.java.dangerous_field