Request.UrlReferrer很容易被绕过,不能可靠地用作保护?



我试图为某些操作做一个守卫,所以除非请求来自某个主机,否则无法访问它。下面是示例代码。

public ActionResult test()
{
if (Request.UrlReferrer == null || Request.UrlReferrer.Host != "mydomain.com") { return Content("Blocked!"); }
else { return Content("Authorized!"); }
}

一切似乎都运行良好,直到我转到mydomain.com"在地址栏中键入链接",打开浏览器控制台并键入

window.location.href = "https://domainholdingthatacion.whatever/ActionRoute/test"; //trying to get unauthorized access

成功了!它进入 else 分支。我需要您的输入,因为我不知道我是否使用它错误,因为Request.UrlReferrer不适合用于此目的,或者它本质上是脆弱的。

UrlReferrer 用于授权并不安全。任何浏览器或客户端都可以决定将引荐来源网址设置为他们想要的任何内容。此外,出于隐私原因,一些浏览器会阻止拒绝遵守此标头(尽管这大多仅在不同域之间是正确的(。

有专门针对此的授权注释。

  • ASP.NET MVC 中的授权属性
  • https://www.red-gate.com/simple-talk/dotnet/asp-net/thoughts-on-asp-net-mvc-authorization-and-security/
  • https://learn.microsoft.com/en-us/aspnet/core/security/authorization/roles?view=aspnetcore-3.1

相关内容

最新更新