Skip to content

Commit c9d4a68

Browse files
authored
Merge pull request #414 from GaneshPatil7517/security/remove-hardcoded-secret-key
security: remove hardcoded Flask secret key and load from environment (fixes #362)
2 parents 519ee66 + 0487c24 commit c9d4a68

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
@@ -106,15 +107,19 @@ def require_api_key():
106107

107108

108109
app = Flask(__name__)
109-
secret_key = os.environ.get("FLASK_SECRET_KEY")
110-
if not secret_key:
111-
# In production, require an explicit FLASK_SECRET_KEY to be set.
112-
# For local development and tests, fall back to a per-process random key
113-
# so that importing this module does not fail hard.
114-
if os.environ.get("FLASK_ENV") == "production":
115-
raise RuntimeError("FLASK_SECRET_KEY environment variable not set in production")
116-
secret_key = os.urandom(32)
117-
app.secret_key = secret_key
110+
app.secret_key = os.getenv("FLASK_SECRET_KEY")
111+
112+
if not app.secret_key:
113+
# In production, require an explicit secret key to avoid session issues
114+
flask_env = os.getenv("FLASK_ENV", "").lower()
115+
if flask_env in ("development", "dev") or app.debug:
116+
# Generate temporary key for development environments where a secret key
117+
# has not been explicitly configured.
118+
app.secret_key = secrets.token_hex(32)
119+
else:
120+
raise RuntimeError(
121+
"FLASK_SECRET_KEY environment variable must be set in production."
122+
)
118123

119124
cors = CORS(app)
120125
app.config['CORS_HEADERS'] = 'Content-Type'

0 commit comments

Comments
 (0)