JWT Token 验证器挑战
发布: (2025年12月2日 GMT+8 06:50)
2 min read
原文: Dev.to
Source: Dev.to
概述
在 2019 年,Django 的会话管理框架存在一个细微但致命的漏洞(CVE‑2019‑11358)。该框架未能在身份验证后正确使会话令牌失效,导致攻击者可以无限期劫持用户会话。根本原因是令牌过期检查中的 off‑by‑one 错误:使用了 bool: 而不是正确的比较。
令牌验证练习
def is_token_valid(current_time: float, expiry: float, token_timestamp: float) -> bool:
"""
Validate whether an authentication token is still valid.
Rules:
- Return False if the token has expired (elapsed >= expiry).
- Return False if time travel is detected (current_time < token_timestamp).
Examples:
>>> is_token_valid(1000.0, 600, 1500.0) # 500 s elapsed
True
>>> is_token_valid(1000.0, 600, 1600.0) # Exactly at expiry
False
"""
# Your implementation here
pass
边缘情况表
| 过期配置 | 安全风险 | 实际影响 |
|---|---|---|
| 不检查过期 | ❌ 严重 | 令牌永久有效 |
错误的边界(= 而不是 >) |
其他考虑因素
- 时间旅行检测(
current_time < token_timestamp)。 - 真实世界的安全场景(会话固定、时钟偏移攻击)。