GraphQL 和 CSRF 保护

GraphQL and CSRF protection

我读了很多书:

  1. https://github.com/pillarjs/understanding-csrf
  2. https://security.stackexchange.com/questions/10227/csrf-with-json-post
  3. Are JSON web services vulnerable to CSRF attacks?
  4. (ApolloServer 站点上没有任何内容:https://www.apollographql.com/docs/apollo-server/

但是,我还无法理解我们的端点 ("/graphql") 是否受到此类攻击的保护,或者是否有必要使用这样的解决方案来保护它:https://github.com/expressjs/csurf

我不清楚的是这里:https://github.com/pillarjs/understanding-csrf 他们说:

When you're using CSRF tokens incorrectly: ... Adding them to JSON AJAX calls As noted above, if you do not support CORS and your APIs are strictly JSON, there is absolutely no point in adding CSRF tokens to your AJAX calls.

如果我们将端点限制为仅使用 Content-Type: application/json 我们安全吗?

If we restrict our endpoint to just use Content-Type: application/json are we safe?

JSON 不能免受 CSRF 攻击(但需要攻击者做一些额外的工作),而且如果配置不当,GraphQL 也不会免疫。如果将其分解为请求/响应,CSRF 的常见场景将适用于此:

  1. 受害者使用您的 GraphQL Web 服务进行身份验证。
  2. 攻击者向受害者发送恶意link。
  3. 受害者点击 link 并访问攻击者的恶意网站。
  4. 攻击者的网站使用受害者 cookie 发送 JSON 请求。
  5. Web 服务收到 JSON 具有有效会话令牌/cookie 的请求,并且受害者 运行 在他们不知情的情况下使用了该功能。

在这种情况下,您的服务容易受到 CSRF 攻击。确保将 CORS 配置为仅允许来自受信任域白名单的请求,并确保正在使用 CSRF 令牌。实施多重保护将降低攻击成功的风险。

下面的 link 更详细,您甚至可以自己尝试: https://blog.appsecco.com/exploiting-csrf-on-json-endpoints-with-flash-and-redirects-681d4ad6b31b

这个答案也是相关的: Are JSON web services vulnerable to CSRF attacks?