Skip to content

Flask SECRET_KEY not enforced via environment variable, defaults enable session forgery #573

Description

@anshul23102

Bug Description

The Flask application does not raise an error when SECRET_KEY is missing from the environment. The app either uses Flask's built-in insecure default or a hardcoded fallback, allowing an attacker to forge session cookies.

Steps to Reproduce

  1. Start the server without setting SECRET_KEY.
  2. Obtain a session cookie and run:
flask-unsign --decode --cookie '<cookie>'
flask-unsign --sign --secret '' --cookie '{"user": "admin"}'
  1. Observe: the forged cookie is accepted by the server.

Root Cause

# app.py
app.secret_key = os.environ.get('SECRET_KEY', 'changeme')

Impact

Session forgery grants attacker any session identity, bypassing authentication.

Proposed Fix

secret = os.environ.get('SECRET_KEY')
if not secret or len(secret) < 32:
    raise RuntimeError('SECRET_KEY must be set to at least 32 random characters')
app.secret_key = secret

Metadata

Metadata

Assignees

Labels

GSSoCOpen Source EventbugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions