值遮蔽:服务器变量¶
ID: cs/web/ambiguous-server-variable
Kind: problem
Security severity:
Severity: warning
Precision: medium
Tags:
- security
- maintainability
- frameworks/asp.net
- external/cwe/cwe-348
Query suites:
- csharp-security-extended.qls
- csharp-security-and-quality.qls
依赖 HttpRequest
来提供对特定服务器变量的访问是不安全的,因为它可以被客户端覆盖。HttpRequest
类实现了一个索引器,以便按特定顺序(QueryString
、Form
、Cookies
或 ServerVariables
集合)提供对其的简化组合访问。搜索变量时,将返回第一个匹配项:因此,QueryString
参数会取代来自表单、Cookie 和服务器变量的值,依此类推。这是一个严重的攻击途径,因为攻击者可以在查询字符串中注入您不期望的值,该值会取代您实际尝试检查的服务器变量的值。
建议¶
将搜索明确限制在 ServerVariables
集合。
示例¶
在本例中,服务器尝试确保用户使用的是 HTTPS 连接。由于程序员使用了 HttpRequest
索引器,因此像 http://www.example.org/?HTTPS=ON
这样的 URL 看起来像是来自安全连接,即使它们实际上并非如此。
class ValueShadowingServerVariable
{
public bool isHTTPS(HttpRequest request)
{
String https = request["HTTPS"];
return https == "ON";
}
}
通过明确指定我们要查找的是服务器变量,可以轻松解决此问题。
class ValueShadowingServerVariableFix
{
public bool isHTTPS(HttpRequest request)
{
String https = request.ServerVariables["HTTPS"];
return https == "ON";
}
}
参考¶
MSDN:HttpRequest.Item。
MSDN:IIS 服务器变量。
常见弱点枚举:CWE-348。