Asp.Net Web API 阻止 404 页面上的任意文本

Asp.Net Web API Block Arbitrary text on 404 page

在我的应用程序 (Asp.Net web api) 中,如果攻击者在 api url 之后插入任意文本,那么该文本将显示在 404 错误页面中.这可以被攻击者用来诱骗用户点击自定义链接或进行网络钓鱼攻击。

我想阻止任何任意文本出现在这样的错误页面上。这里要注意一点,我们还没有在应用程序中实现基于 GUID 的错误消息。

为了设置自定义 404 错误页面,将以下内容添加到 中的 web.config:

<customErrors mode="On" redirectMode="ResponseRewrite">
  <error statusCode="404" redirect="~/404.html"/>
</customErrors>

我设置了 mode="On" 以便我们可以在本地查看自定义错误页面。通常你只想在生产中显示这些,所以会设置 mode="RemoteOnly"

然后在 IIS 中添加自定义错误页面(请注意,这仅适用于 IIS 7+)。在 web.config 中添加以下 :

<httpErrors errorMode="Custom">
  <remove statusCode="404"/>
  <error statusCode="404" path="/404.html" responseMode="File"/>
</httpErrors>

如果没有帮助,因为我不知道你的应用程序的架构,你应该寻求路由匹配解决方案:

  • 实现智能 IHttpRouteConstraint
  • 将约束应用于属性路由
  • 将约束应用于集中路由

以下是实施细节: https://www.strathweb.com/2014/10/route-matching-overriding-404-asp-net-web-api/