Skip to content

Commit 0487c24

Browse files
security: remove hardcoded Flask secret key and load from environment (fixes #362)
1 parent 0467ce5 commit 0487c24

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ _concore_ supports customization through configuration files in the `CONCOREPATH
7777

7878
Tool paths can also be set via environment variables (e.g., `CONCORE_CPPEXE=/usr/bin/g++`). Priority: config file > env var > defaults.
7979

80+
### Security Configuration
81+
82+
Set a secure secret key for the Flask server before running in production:
83+
84+
```bash
85+
export FLASK_SECRET_KEY=$(python -c "import secrets; print(secrets.token_hex(32))")
86+
```
87+
88+
Do **NOT** commit your secret key to version control. If `FLASK_SECRET_KEY` is not set, a temporary random key will be generated automatically (suitable for local development only).
89+
8090
For a detailed and more scientific documentation, please read our extensive [open-access research paper on CONTROL-CORE](https://doi.org/10.1109/ACCESS.2022.3161471). This paper has a complete discussion on the CONTROL-CORE architecture and deployment, together with the commands to execute the studies in different programming languages and programming environments (Ubuntu, Windows, MacOS, Docker, and distributed execution).
8191

8292

fri/server/main.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from werkzeug.utils import secure_filename
33
import xml.etree.ElementTree as ET
44
import os
5+
import secrets
56
import subprocess
67
from subprocess import call,check_output
78
from pathlib import Path
@@ -86,15 +87,19 @@ def get_error_output(e):
8687

8788

8889
app = Flask(__name__)
89-
secret_key = os.environ.get("FLASK_SECRET_KEY")
90-
if not secret_key:
91-
# In production, require an explicit FLASK_SECRET_KEY to be set.
92-
# For local development and tests, fall back to a per-process random key
93-
# so that importing this module does not fail hard.
94-
if os.environ.get("FLASK_ENV") == "production":
95-
raise RuntimeError("FLASK_SECRET_KEY environment variable not set in production")
96-
secret_key = os.urandom(32)
97-
app.secret_key = secret_key
90+
app.secret_key = os.getenv("FLASK_SECRET_KEY")
91+
92+
if not app.secret_key:
93+
# In production, require an explicit secret key to avoid session issues
94+
flask_env = os.getenv("FLASK_ENV", "").lower()
95+
if flask_env in ("development", "dev") or app.debug:
96+
# Generate temporary key for development environments where a secret key
97+
# has not been explicitly configured.
98+
app.secret_key = secrets.token_hex(32)
99+
else:
100+
raise RuntimeError(
101+
"FLASK_SECRET_KEY environment variable must be set in production."
102+
)
98103

99104
cors = CORS(app)
100105
app.config['CORS_HEADERS'] = 'Content-Type'

0 commit comments

Comments
 (0)