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-not-set

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-02-05首次发现

版本兼容性

版本状态引入弃用备注
2.x active — — —
3.x active — — —

根因分析

未配置 `app.secret_key`,Flask 无法对会话 Cookie 进行签名。

English

`app.secret_key` was never configured, so Flask cannot sign session cookies.

generic

解决方案

  1. 97% 成功率
    import os
    app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
  2. 95% 成功率
    import secrets
    app.config['SECRET_KEY'] = secrets.token_hex(32)

无效尝试

常见但无效的做法:

  1. 60% 失败

    Hard-coded weak key leaks via git and is insecure in production.

  2. 95% 失败

    Cookie secure flag is unrelated to key availability.