python
config_error
ai_generated
true
RuntimeError: 会话不可用,因为未设置密钥。请将应用的 secret_key 设置为唯一且保密的值。
RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.
ID: python/flask-secret-key-missing
80%修复率
89%置信度
0证据数
2024-01-22首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
Flask 的会话 Cookie 签名需要 app.secret_key,但在使用 session 或 flash 之前未配置。
English
Flask's session cookie signing requires app.secret_key, but it was never configured before using session or flash.
解决方案
-
95% 成功率
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
-
90% 成功率
app.secret_key = secrets.token_hex(32) for dev only
无效尝试
常见但无效的做法:
-
90% 失败
Flask treats empty string as falsy and raises the same error.
-
85% 失败
No such option; sessions still require a key.