OAuth 2.0: Security

 20th August 2020 at 2:19pm

RFC 6749 中有一节详细描述了 安全问题。我没有仔细去看。这里讲述一下在 authorization grant type 流程中对 state 的运用。

"A CSRF attack against the client's redirection URI allows an attacker to inject its own authorization code or access token, which can result in the client using an access token associated with the attacker's protected resources rather than the victim's (e.g., save the victim's bank account information to a protected resource controlled by the attacker)." Continue reading from the source RFC-6749.

state 的作用即是防止用户授权过程中,黑客想办法将用户的 authorization code 或者 access token 换成它自己的。结合前面的 联合身份(federated identity)场景,这意味着用户在 client application 以它自己的身份,看到及操作黑客在第三方服务中的数据。这是一种 CSRF。

state 的作用即是在第三方服务 callback 时,黑客无法伪造出一样的 state 来防范对于 authorization code 的篡改。试想下不存在 state 时的这个流程:

  1. 正常用户 A 登录(login)了 client application
  2. A 去到了黑客 E 的恶意网站,不小心访问了 E 构建了 /callback 链接;而 /callback 链接中带的 authentication code 是 E 的
  3. 于是 A 访问了 /callback。由于没有 state 可以校验,client application 并不知道这个链接是它自己生成的还是恶意的。它拿这个 /callback 中的 code 换取了 access token,并以为这个 access token 是 A 的
  4. A 在 client application 上操作第三方服务时,用的是黑客 E 的 access token,这可能造成信息泄漏

参考:State parameter - Pipedrive Public API