CodeQL 文档

用户控制的敏感方法绕过

ID: java/user-controlled-bypass
Kind: path-problem
Security severity: 7.8
Severity: error
Precision: medium
Tags:
   - security
   - external/cwe/cwe-807
   - external/cwe/cwe-290
Query suites:
   - java-security-extended.qls
   - java-security-and-quality.qls

点击查看 CodeQL 仓库中的查询

许多 Java 结构允许有条件地执行代码语句,例如 if 语句和 for 语句。如果这些语句包含重要的身份验证或登录代码,并且关于是否执行此代码的决定基于用户控制的数据,攻击者可能能够通过阻止执行此代码来绕过安全系统。

建议

永远不要根据可能由用户控制的数据来决定是否对用户进行身份验证。如果需要,请确保在执行任何身份验证检查之前对数据进行广泛验证。

仍然可以拥有一个“记住”用户的系统,因此用户不必在每次交互时都登录。例如,个性化设置可以在没有身份验证的情况下应用,因为这不是敏感信息。但是,只有在用户完全通过身份验证后才应允许用户执行敏感操作。

示例

此示例展示了两种决定是否对用户进行身份验证的方法。第一种方法展示了一个基于 Cookie 值的决定。Cookie 可以很容易地由用户控制,因此这允许用户在不提供有效凭据的情况下进行身份验证。第二种更安全的方法展示了一个基于在安全数据库中查找用户的决定。

public boolean doLogin(String user, String password) {
	Cookie adminCookie = getCookies()[0];

	// BAD: login is executed only if the value of 'adminCookie' is 'false', 
	// but 'adminCookie' is controlled by the user
	if(adminCookie.getValue()=="false")
		return login(user, password);
	
	return true;
}

public boolean doLogin(String user, String password) {
	Cookie adminCookie = getCookies()[0];
	
	// GOOD: use server-side information based on the credentials to decide
	// whether user has privileges
	boolean isAdmin = queryDbForAdminStatus(user, password);
	if(!isAdmin)
		return login(user, password);
	
	return true;
}

参考

  • ©GitHub, Inc.
  • 条款
  • 隐私