Skip to content

security: remove hardcoded Flask secret key and load from environment (fixes #362)#414

Merged
pradeeban merged 1 commit into
ControlCore-Project:devfrom
GaneshPatil7517:security/remove-hardcoded-secret-key
Feb 19, 2026
Merged

security: remove hardcoded Flask secret key and load from environment (fixes #362)#414
pradeeban merged 1 commit into
ControlCore-Project:devfrom
GaneshPatil7517:security/remove-hardcoded-secret-key

Conversation

@GaneshPatil7517
Copy link
Copy Markdown
Contributor

@GaneshPatil7517 GaneshPatil7517 commented Feb 18, 2026

Hello @pradeeban Sir,

Fixes #362.

This PR removes the hardcoded Flask secret_key and replaces it with a safer configuration using an environment variable.

Previously the application used:

app.secret_key = "secret key"

Using a fixed key like this can allow session forgery or hijacking, especially if the server is deployed publicly.

Changes in this PR:

  • Removed the hardcoded secret key from fri/server/main.py
  • Load the key from the FLASK_SECRET_KEY environment variable using os.getenv()
  • Added a guard so the server raises a RuntimeError if FLASK_SECRET_KEY is not set in production
  • In development or debug mode, generate a temporary key using secrets.token_hex(32)
  • Added a short security configuration section in README.md

Behavior after this change:

  • If FLASK_SECRET_KEY is set → the server uses that value
  • If not set in production → the server exits with a clear error
  • If running in development (FLASK_ENV=development) or debug mode → a temporary random key is generated

Security benefits:

  • Prevents session forgery caused by a shared default key
  • Ensures each deployment uses its own secret
  • Avoids accidental production deployments without a configured key

Scope:

  • fri/server/main.py - updated secret key handling
  • README.md - added configuration note

No other parts of the project were modified:

  • No changes to concore-lite
  • No changes to Verilog code

Testing done locally:

  • All existing tests pass (77/77)
  • Verified the app loads the key from FLASK_SECRET_KEY
  • Verified a random key is generated in development mode
  • Verified the server raises an error when running in production without a key

Example configuration for production:

export FLASK_SECRET_KEY=$(python -c "import secrets; print(secrets.token_hex(32))")

The secret key should be set through environment variables and not committed to the repository.
image

Copilot AI review requested due to automatic review settings February 18, 2026 20:32
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses issue #362 regarding Flask secret key security by loading the secret key from the FLASK_SECRET_KEY environment variable. However, the changes introduce a security regression by removing production environment validation that was present in the previous implementation.

Changes:

  • Modified Flask secret key initialization to use os.getenv() instead of os.environ.get()
  • Changed cryptographic fallback from os.urandom(32) to secrets.token_hex(32)
  • Removed production environment check that would fail-fast if secret key was not set in production
  • Added security configuration documentation in README.md

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
fri/server/main.py Modified Flask secret key initialization logic, removing production validation and simplifying fallback mechanism
README.md Added Security Configuration section documenting FLASK_SECRET_KEY environment variable usage

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread fri/server/main.py Outdated
Comment thread fri/server/main.py Outdated
@GaneshPatil7517 GaneshPatil7517 force-pushed the security/remove-hardcoded-secret-key branch from 829711c to 0487c24 Compare February 18, 2026 20:41
@pradeeban pradeeban merged commit c9d4a68 into ControlCore-Project:dev Feb 19, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants