Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use HTTP referrer validation or token verification to prevent CSRF attacks?

Tags:

People also ask

How can the referer field be used to defend against CSRF attacks?

Checking the referer is a commonly used method of preventing CSRF on embedded network devices because it does not require a per-user state. This makes a referer a useful method of CSRF prevention when memory is scarce.

How do you prevent CSRF attack in Web API?

To prevent CSRF attacks, use anti-forgery tokens with any authentication protocol where the browser silently sends credentials after the user logs in. This includes cookie-based authentication protocols, such as forms authentication, as well as protocols such as Basic and Digest authentication.

Can origin header prevent CSRF?

The Origin header in a HTTP request indicates where the request originated from. This can be useful in preventing cross-site request forgery.

What is referer header CSRF?

The referer header can in some circumstances be used as an effective protection against CSRF. However, with the introduction of referrer policy the attacker can determine whether or not this header is sent with the request, and CSRF protection mechanisms need to handle an empty referer header by blocking it.


I read about how to protect my web site from CSRF attacks in an ASP.NET MVC web application. They mentioned two ways to do so, either by:

  1. using Token Verification by using <@Html.AntiForgeryToken()> and [ValidateAntiforgeryToken]

  2. using HTTP referrer validation such as:

    public class IsPostedFromThisSiteAttribute : AuthorizeAttribute
        {
        public override void OnAuthorize(AuthorizationContext filterContext)
            {
            if (filterContext.HttpContext != null)
                {
                if (filterContext.HttpContext.Request.UrlReferrer == null)
                    throw new System.Web.HttpException("Invalid submission");
                if (filterContext.HttpContext.Request.UrlReferrer.Host !=
                    "mysite.com")
                    throw new System.Web.HttpException
                        ("This form wasn't submitted from this site!");
                }
            }
        }
    

    and

    [IsPostedFromThisSite]
    public ActionResult Register(…)
    

So I got confused about whether I should use both of them to protect my web site from CSRF attacks or whether I can select one of these methods?