SECRET_KEY 到底保护什么?cookie 里的内容能被用户看到吗?key 泄漏后果多严重?怎么轮换而不让所有人下线?这篇用真实的伪造、轮换和猜测把每个问题验证一遍。
HTTP 500 (传播出来的异常) RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to
上一篇:Streaming JSON;下一篇:Session Cookie 安全属性。
cookie: eyJyb2xlIjoidXNlciIsInVzZXIiOiJhbm4ifQ.ar9AiQ.hP1CkQxFNkgTMgvoqMb20U_P…
不用密钥解出的内容: {"role":"user","user":"ann"}
cookie 的三段: [38, 6, 27] (载荷.时间戳.签名) 用正确的 key 伪造的 cookie → {'role': 'admin', 'user': 'mallory'}
用错误的 key 伪造的 cookie → {'role': None, 'user': None} (签名不对,当作没有 session)
篡改签名的 cookie → {'role': None, 'user': None} 换 key 之前: {'role': 'user', 'user': 'ann'}
换 key 之后(同一个 cookie): {'role': None, 'user': None} 新 key + FALLBACKS=[旧 key],带旧 cookie → {'role': 'user', 'user': 'ann'}
之后服务端下发的 cookie 已经用新 key 签名: 旧 cookie 能被旧 key 验证: True | 新下发的 cookie 能被新 key 验证: True | 能被旧 key 验证: False
只有新 key、没有 FALLBACKS,带旧 cookie → {'role': None, 'user': None}字典猜测:猜中 'dev',用时 0.1 ms 强 key(64 个十六进制字符):试了 200010 个候选,结果 None,用时 2462 ms(约 12.3 µs/个) 随机 32 字节 key 有 2^256 种可能;按每秒试 81243 个算,穷举不可行(这里没有真的穷举) 生成方式:secrets.token_hex(32) → 74b2d59ed6e401c9… (64 个字符)
只对自己生成的 cookie 做猜测,没有对任何别人的系统做过测试。