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

- **ID:** `security/oauth2-implicit-grant-token-in-url`
- **领域:** security
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| OAuth 2.0 RFC 6749 | active | — | — |
| OAuth 2.0 Security BCP (RFC 9700) | active | — | — |

## 解决方案

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

## 无效尝试

- **** — Removing the token from the URL fragment entirely breaks the flow because the client expects it there. (70% 失败率)
- **** — Using HTTPS alone doesn't prevent token leakage via browser history or referrer headers. (50% 失败率)
- **** — Shortening token expiry doesn't solve the underlying exposure; tokens are still captured in logs. (30% 失败率)
