security protocol_error ai_generated true

OAuth2 隐式授权流程在 URL 片段中泄露访问令牌

OAuth2 implicit grant flow leaks access token in URL fragment

ID: security/oauth2-implicit-grant-token-in-url

其他格式: JSON · Markdown 中文 · English
88%修复率
85%置信度
1证据数
2023-11-15首次发现

版本兼容性

版本状态引入弃用备注
OAuth 2.0 RFC 6749 active
OAuth 2.0 Security BCP (RFC 9700) active

根因分析

OAuth2 隐式授权流程在 URL 片段中返回访问令牌,该片段会暴露在浏览器历史记录、引用标头和服务器日志中。

English

The OAuth2 implicit grant flow returns the access token in the URL fragment, which is exposed in browser history, referrer headers, and server logs.

generic

官方文档

https://oauth.net/2/grant-types/implicit/

解决方案

  1. 从隐式流程迁移到带 PKCE 的授权码流程。示例:使用 `response_type=code&code_challenge=...&code_challenge_method=S256` 代替 `response_type=token`。
  2. 如果无法迁移,添加 `Referrer-Policy: no-referrer` 标头,并通过页面上的 `Cache-Control: no-store` 阻止浏览器历史记录保留。
  3. 对移动应用使用自定义方案,以减少浏览器历史记录和引用标头泄露的风险。

无效尝试

常见但无效的做法:

  1. 70% 失败

    Removing the token from the URL fragment entirely breaks the flow because the client expects it there.

  2. 50% 失败

    Using HTTPS alone doesn't prevent token leakage via browser history or referrer headers.

  3. 30% 失败

    Shortening token expiry doesn't solve the underlying exposure; tokens are still captured in logs.