用户控制的对敏感方法的绕过¶
ID: cs/user-controlled-bypass
Kind: path-problem
Security severity: 7.5
Severity: error
Precision: high
Tags:
- security
- external/cwe/cwe-807
- external/cwe/cwe-247
- external/cwe/cwe-350
Query suites:
- csharp-code-scanning.qls
- csharp-security-extended.qls
- csharp-security-and-quality.qls
许多 C# 结构允许有条件地执行代码语句,例如,if
语句和 for
语句。如果语句包含重要的身份验证或登录代码,并且用户控制的数据决定是否执行代码,则攻击者可能会绕过安全系统。
建议¶
切勿根据可能由用户控制的数据来决定是否对用户进行身份验证。如有必要,请确保在执行任何身份验证检查之前,对输入的数据进行广泛验证。
仍然可以拥有“记住”用户的系统,从而不要求用户在每次交互时都登录。例如,可以在未经身份验证的情况下应用个性化设置,因为这不是敏感信息。但是,应该只允许用户在完全通过身份验证后才能执行敏感操作。
示例¶
此示例显示了两种决定是否对用户进行身份验证的方法。第一种方法显示了基于 Cookie 值的决定。Cookie 可以由用户轻松控制,因此这允许用户在不提供有效凭据的情况下通过身份验证。第二种更安全的方法显示了基于在安全数据库中查找用户的决定。
public boolean doLogin(HttpCookie adminCookie, String user, String password)
{
// BAD: login is executed only if the value of 'adminCookie' is 'false',
// but 'adminCookie' is controlled by the user
if (adminCookie.Value == "false")
return login(user, password);
return true;
}
public boolean doLogin(HttpCookie adminCookie, String user, String password)
{
// GOOD: use server-side information based on the credentials to decide
// whether user has privileges
bool isAdmin = queryDbForAdminStatus(user, password);
if (!isAdmin)
return login(user, password);
return true;
}