信任边界违反¶
ID: java/trust-boundary-violation
Kind: path-problem
Security severity: 8.8
Severity: error
Precision: medium
Tags:
- security
- external/cwe/cwe-501
Query suites:
- java-security-extended.qls
- java-security-and-quality.qls
信任边界违反发生在将值从信任度较低的上下文传递到信任度较高的上下文时。
例如,由信任度较低的来源(例如用户)生成的值可能被传递给信任度较高的来源(例如系统进程)。如果信任度较低的来源是恶意的,那么该值可能会被伪造以利用信任度较高的来源。
信任边界违反通常是由于未能验证输入造成的。例如,如果 Web 应用程序接受来自用户的 Cookie,那么该应用程序应该在使用 Cookie 之前验证 Cookie。如果 Cookie 未经验证,则用户可能能够伪造一个恶意 Cookie 来利用该应用程序。
建议¶
为了维护信任边界,请在使用来自信任度较低的来源的数据之前验证数据。
示例¶
在第一个(不好的)示例中,服务器接受来自用户的参数,然后使用它来设置用户名,而无需验证。
public void doGet(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("username");
// BAD: The input is written to the session without being sanitized.
request.getSession().setAttribute("username", username);
}
在第二个(好的)示例中,服务器验证来自用户的参数,然后使用它来设置用户名。
public void doGet(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("username");
if (validator.isValidInput("HTTP parameter", username, "username", 20, false)) {
// GOOD: The input is sanitized before being written to the session.
request.getSession().setAttribute("username", username);
}
}