缺少全局错误处理程序¶
ID: cs/web/missing-global-error-handler
Kind: problem
Security severity: 7.5
Severity: warning
Precision: high
Tags:
- security
- external/cwe/cwe-12
- external/cwe/cwe-248
Query suites:
- csharp-code-scanning.qls
- csharp-security-extended.qls
- csharp-security-and-quality.qls
将 customErrors
模式设置为 Off
且未在 global.asax.cs
文件中提供 Application_Error
方法的 Web.config
文件依赖于默认错误页面,这些页面会泄露堆栈跟踪等信息。
建议¶
将 customErrors
设置为 On
可防止显示默认错误页面,或者设置为 RemoteOnly
仅在本地访问应用程序时才显示默认错误页面。或者,在 global.asax.cs
页面中提供 Application_Error
方法的实现。
示例¶
以下示例显示了一个 Web.config
文件,其中自定义错误模式已设置为 Off
。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<customErrors mode="Off">
...
</customErrors>
</system.web>
</configuration>
可以通过在 Web.config
文件中指定其他模式(例如 On
)来解决此问题
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<customErrors mode="On">
...
</customErrors>
</system.web>
</configuration>
或者通过在 global.asax.cs
文件中定义 Application_Error
方法来解决此问题
using System;
using System.Web;
namespace WebApp
{
public class Global : HttpApplication
{
void Application_Error(object sender, EventArgs e)
{
// Handle errors here
}
}
}