简体   繁体   English

三层应用程序是否需要 AntiForgeryToken?

[英]Is AntiForgeryToken required in three tier application?

My security scan detected issue of type "Missing AntiForgeryToken implementation".我的安全扫描检测到类型为“Missing AntiForgeryToken implementation”的问题。 My application has a layer of .netCore WebApi services, a .netCore WebApi with RazorPages service and a Angular front-end application.我的应用程序有一层 .netCore WebApi 服务,一个带有 RazorPages 服务的 .netCore WebApi 和一个 Angular 前端应用程序。 Reading online all CSRF Token implementation refers to RazorPages or similar but not front-end applications.在线阅读所有 CSRF Token 实现均指 RazorPages 或类似但不是前端应用程序。 The question is: is the token require in three tier architecture?问题是:三层架构是否需要令牌? Do I have to implement the policy with my fe?我必须用我的 fe 实施该政策吗? Is it a real issue the tool is detecting?该工具检测到的是真正的问题吗?

Thanks.谢谢。

First of all, it is necessary to understand what is antiforgerytoken?首先,有必要了解什么是antiforgerytoken? This has nothing to do with n tier.这与n层无关。 If your project has web pages and you are making http requests, you should use AntiforgeryToken cause security.如果您的项目有 web 个页面并且您正在发出 http 次请求,您应该使用 AntiforgeryToken 以确保安全。

The architecture is irrelevant.架构无关紧要。

"Rule of thumb" is that if there is a user context in your application then you require an anti-forgery token implementation to prevent attacks such as: “经验法则”是,如果您的应用程序中有用户上下文,那么您需要一个防伪令牌实现来防止攻击,例如:

An attacker tricks the user into visiting a different web page (such as evil.com) with malignant code that secretly sends a malicious request to the application's web server (such as example-bank.com).攻击者使用恶意代码诱使用户访问另一个 web 页面(例如 evil.com),该恶意代码会秘密向应用程序的 web 服务器(例如 example-bank.com)发送恶意请求。

Assume the user is logged into the application at example-bank.com.假设用户在 example-bank.com 登录到应用程序。 The user opens an email and clicks a link to evil.com, which opens in a new tab.用户打开 email 并单击指向 evil.com 的链接,该链接将在新选项卡中打开。

The evil.com page immediately sends a malicious request to example-bank.com. evil.com 页面立即向 example-bank.com 发送恶意请求。 Perhaps it's a request to transfer money from the user's account to the attacker's account.也许这是一个将钱从用户账户转移到攻击者账户的请求。 The browser automatically sends the example-bank.com cookies (including the authentication cookie) with this request.浏览器随此请求自动发送 example-bank.com cookies(包括身份验证 cookie)。

If the example-bank.com server lacks XSRF protection, it can't tell the difference between a legitimate request from the application and the forged request from evil.com.如果 example-bank.com 服务器缺乏 XSRF 保护,它就无法区分来自应用程序的合法请求和来自 evil.com 的伪造请求。

reference参考

Angular's HttpClient enables XSRF Token support by default : Angular 的 HttpClient默认启用 XSRF Token 支持:

HttpClient supports a common mechanism used to prevent XSRF attacks. HttpClient 支持用于防止 XSRF 攻击的通用机制。 When performing HTTP requests, an interceptor reads a token from a cookie, by default XSRF-TOKEN, and sets it as an HTTP header, X-XSRF-TOKEN.当执行 HTTP 请求时,拦截器从 cookie 中读取令牌,默认为 XSRF-TOKEN,并将其设置为 HTTP header,X-XSRF-TOKEN。 Because only code that runs on your domain could read the cookie, the backend can be certain that the HTTP request came from your client application and not an attacker.因为只有在您的域上运行的代码才能读取 cookie,后端可以确定 HTTP 请求来自您的客户端应用程序,而不是攻击者。

By default, an interceptor sends this header on all mutating requests (such as POST) to relative URLs, but not on GET/HEAD requests or on requests with an absolute URL.默认情况下,拦截器将此 header 发送到相对 URL 的所有变异请求(例如 POST),但不会发送到 GET/HEAD 请求或具有绝对 URL 的请求。

To take advantage of this, your server needs to set a token in a JavaScript readable session cookie called XSRF-TOKEN on either the page load or the first GET request.要利用这一点,您的服务器需要在页面加载或第一个 GET 请求时在名为 XSRF-TOKEN 的 JavaScript 可读 session cookie 中设置一个令牌。 On subsequent requests the server can verify that the cookie matches the X-XSRF-TOKEN HTTP header, and therefore be sure that only code running on your domain could have sent the request.在后续请求中,服务器可以验证 cookie 是否与 X-XSRF-TOKEN HTTP header 匹配,因此请确保只有在您的域上运行的代码才能发送请求。 The token must be unique for each user and must be verifiable by the server;令牌对于每个用户必须是唯一的,并且必须能够被服务器验证; this prevents the client from making up its own tokens.这可以防止客户端制作自己的令牌。 Set the token to a digest of your site's authentication cookie with a salt for added security.将令牌设置为您站点的身份验证 cookie 的摘要,并加盐以增加安全性。

reference参考

It is relatively straight forward to implement on the .net side as well.在 .net 端实施也相对直接。

services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");

The underlying architecture does not matter.底层架构并不重要。 The real question is how your Angular client authenticates to whatever it talks to.真正的问题是您的 Angular 客户端如何对与之交谈的对象进行身份验证。

If it uses a method that is sent by the browser automatically (like cookies typically, but also for example Windows auth / SPNEGO, which might be relevant in your stack), then yes, you do need to implement protection against CSRF.如果它使用浏览器自动发送的方法(通常像 cookies,但也例如 Windows auth / SPNEGO,这可能与您的堆栈相关),那么是的,您确实需要针对 CSRF 实施保护。

However, if your authentication is based on tokens sent as something like a request header (or even in request bodies, but the point is, not automatically by the browser), then no, your application is inherently not vulnerable to typical CSRF, and you don't need further protection.但是,如果您的身份验证是基于作为请求 header 发送的令牌(或者甚至在请求主体中,但重点是,浏览器不会自动),那么不,您的应用程序本质上不易受到典型的 CSRF 攻击,并且您不需要进一步的保护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 三层架构,Spring和Angular - Three-tier architecture, Spring and Angular 如何为多层应用程序实现延迟加载 - How to implement lazy loading for a multi-tier application Angular2 ASP.NET Core AntiForgeryToken - Angular2 ASP.NET Core AntiForgeryToken 3 层架构上的 SSL 问题 - SSL issue on 3 tier architecture Angular 7应用程序中的kendo-dropdownlist必需的字段验证 - Required field validation for kendo-dropdownlist in angular 7 application Angular/AngularJS 混合应用程序需要 AngularJS 转换为 .ts 吗? - Is AngularJS Conversion to .ts required for Angular/AngularJS Hybrid Application? AADSTS50146:要求此应用程序配置有特定于应用程序的签名密钥 - AADSTS50146: This application is required to be configured with an application-specific signing key 3D bar chart in angular 8 application using either d3.js or plotly or three.js - 3D bar chart in angular 8 application using either d3.js or plotly or three.js 如何在Angular2应用程序中使三个圆圈响应屏幕尺寸的变化? - How to make three circles responsive with change in screen size in Angular2 application? 在ASP.Net MVC中使用@ Html.AntiForgeryToken()提供CSRF令牌的替代方法 - Alternative way to provide CSRF Token instead using @Html.AntiForgeryToken() in ASP.Net MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM