防伪令牌 + Web API(-MVC)

11

如何在不使用ASP.NET MVC的情况下使用Anti-Forgery Token与ASP.NET Web API?

Stephen Walther在http://stephenwalther.com/archive/2013/03/05/security-issues-with-single-page-apps中有一篇名为“使用ASP.NET MVC防止跨站点请求伪造攻击”的文章......但他的解决方案包括MVC/Razor,而我在前端没有计划包含它。 有很多类似的文章,其中解决方法是添加@ Html.AntiForgeryToken(),但这不能成为我的解决方案。

后来,我解决了另一个问题,即“同源策略”:http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api,这能预防CSRF吗? 我认为不行。


这就是我最终所做的 https://dev59.com/OGgu5IYBdhLWcg3wOUu- - ScottS
1个回答

3

我的问题是我不想使用MVC,只想用WebApi支持的静态HTML文件。这是我做的事情(这样行吗?)创建一个Http模块,在提供任何静态文件时设置随机cookie值。例如:

public class XSRFModule : IHttpModule {
    ...

    void context_EndRequest(object sender, EventArgs e) {
        if (Path.GetExtension(HttpContext.Current.Request.Path) == ".html") {
            HttpContext.Current.Response.Cookies.Add(new HttpCookie("XSRF-TOKEN", Guid.NewGuid().ToString()));
        }
    }
}

然后在您的 HTML 页面中,使用 JavaScript 在调用 API 时将 cookie 值添加到标头中:

function callApi() {
        xhr = new XMLHttpRequest();
        xhr.open("GET", "api/data", true);
        var regex = /\b(?:XSRF-TOKEN=)(.*?)(?=\s|$)/
        var match = regex.exec(document.cookie);
        xhr.setRequestHeader("X-XSRF-TOKEN", match[1]);
        xhr.send();
    }

最后,在您的HttpModule中,在处理任何对您的API的调用之前,请检查Cookie与标头是否匹配:

void context_BeginRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.Request.Path.StartsWith("/api"))
        {
            string fromCookie = HttpContext.Current.Request.Cookies.Get("XSRF-TOKEN").Value;
            string fromHeader = HttpContext.Current.Request.Headers["X-XSRF-TOKEN"];
            if (fromCookie != fromHeader)
            {
                HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                HttpContext.Current.Response.End();
            }
        }
    }

您需要将HttpOnly标志设置为FALSE,以便来自您域的JavaScript可以读取cookie并设置标头。我不是安全专家,因此我想从社区的其他成员那里获得一些关于这个解决方案的反馈。

编辑

如果您使用OWIN,可以使用全局操作过滤器和中间件插件:

Startup.cs

app.UseStaticFiles(new StaticFileOptions {
            OnPrepareResponse = (responseContext) => {
                responseContext.OwinContext.Response.Cookies.Append("XSRF-TOKEN", Guid.NewGuid().ToString());
            },
            FileSystem = "wwwroot"
        });

XsrfFilter.cs

public class XsrfFilter : ActionFilterAttribute {
    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) {

        string fromCookie = actionContext.Request.Headers.GetCookies("XSRF-TOKEN").FirstOrDefault()["XSRF-TOKEN"].Value;
        string fromHeader = actionContext.Request.Headers.GetValues("X-XSRF-TOKEN").FirstOrDefault();

        if (fromCookie == fromHeader) return;

        actionContext.Response = new HttpResponseMessage(HttpStatusCode.OK);
        actionContext.Response.ReasonPhrase = "bad request";
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接