Skip to content

Commit b180319

Browse files
authored
fix: refuse to start with default secret on non debug envs (#23186)
1 parent 7196e87 commit b180319

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

UPDATING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ assists people when migrating to a new version.
2424

2525
## Next
2626

27+
- [23186](https://github.com/apache/superset/pull/23186): Superset will refuse to start if a default `SECRET_KEY` is detected on a non Flask debug setting.
2728
- [22022](https://github.com/apache/superset/pull/22022): HTTP API endpoints `/superset/approve` and `/superset/request_access` have been deprecated and their HTTP methods were changed from GET to POST
2829
- [20606](https://github.com/apache/superset/pull/20606): When user clicks on chart title or "Edit chart" button in Dashboard page, Explore opens in the same tab. Clicking while holding cmd/ctrl opens Explore in a new tab. To bring back the old behaviour (always opening Explore in a new tab), flip feature flag `DASHBOARD_EDIT_CHART_IN_NEW_TAB` to `True`.
2930
- [20799](https://github.com/apache/superset/pull/20799): Presto and Trino engine will now display tracking URL for running queries in SQL Lab. If for some reason you don't want to show the tracking URL (for example, when your data warehouse hasn't enabled access for to Presto or Trino UI), update `TRACKING_URL_TRANSFORMER` in `config.py` to return `None`.

docker/.env-non-dev

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ REDIS_PORT=6379
4242
FLASK_ENV=production
4343
SUPERSET_ENV=production
4444
SUPERSET_LOAD_EXAMPLES=yes
45+
SUPERSET_SECRET_KEY=TEST_NON_DEV_SECRET
4546
CYPRESS_CONFIG=false
4647
SUPERSET_PORT=8088
4748
MAPBOX_API_KEY=''

docs/docs/installation/configuring-superset.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ SUPERSET_WEBSERVER_PORT = 8088
2323
# Your App secret key will be used for securely signing the session cookie
2424
# and encrypting sensitive information on the database
2525
# Make sure you are changing this key for your deployment with a strong key.
26-
# You can generate a strong key using `openssl rand -base64 42`
27-
26+
# You can generate a strong key using `openssl rand -base64 42`.
27+
# Alternatively you can set it with `SUPERSET_SECRET_KEY` environment variable.
2828
SECRET_KEY = 'YOUR_OWN_RANDOM_GENERATED_SECRET_KEY'
2929
3030
# The SQLAlchemy connection string to your database backend

superset/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,11 @@ def _try_json_readsha(filepath: str, length: int) -> Optional[str]:
188188
SQLALCHEMY_TRACK_MODIFICATIONS = False
189189
# ---------------------------------------------------------
190190

191-
# Your App secret key. Make sure you override it on superset_config.py.
191+
# Your App secret key. Make sure you override it on superset_config.py
192+
# or use `SUPERSET_SECRET_KEY` environment variable.
192193
# Use a strong complex alphanumeric string and use a tool to help you generate
193194
# a sufficiently random sequence, ex: openssl rand -base64 42"
194-
SECRET_KEY = CHANGE_ME_SECRET_KEY
195+
SECRET_KEY = os.environ.get("SUPERSET_SECRET_KEY") or CHANGE_ME_SECRET_KEY
195196

196197
# The SQLAlchemy connection string.
197198
SQLALCHEMY_DATABASE_URI = "sqlite:///" + os.path.join(DATA_DIR, "superset.db")

superset/initialization/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import logging
2020
import os
21+
import sys
2122
from typing import Any, Callable, Dict, TYPE_CHECKING
2223

2324
import wtforms_json
@@ -458,7 +459,7 @@ def init_app_in_ctx(self) -> None:
458459
self.init_views()
459460

460461
def check_secret_key(self) -> None:
461-
if self.config["SECRET_KEY"] == CHANGE_ME_SECRET_KEY:
462+
def log_default_secret_key_warning() -> None:
462463
top_banner = 80 * "-" + "\n" + 36 * " " + "WARNING\n" + 80 * "-"
463464
bottom_banner = 80 * "-" + "\n" + 80 * "-"
464465
logger.warning(top_banner)
@@ -471,6 +472,20 @@ def check_secret_key(self) -> None:
471472
)
472473
logger.warning(bottom_banner)
473474

475+
if self.config["SECRET_KEY"] == CHANGE_ME_SECRET_KEY:
476+
if (
477+
self.superset_app.debug
478+
or self.superset_app.config["TESTING"]
479+
# There must be a better way
480+
or "pytest" in sys.modules
481+
):
482+
logger.warning("Debug mode identified with default secret key")
483+
log_default_secret_key_warning()
484+
return
485+
log_default_secret_key_warning()
486+
logger.error("Refusing to start due to insecure SECRET_KEY")
487+
sys.exit(1)
488+
474489
def init_app(self) -> None:
475490
"""
476491
Main entry point which will delegate to other methods in

0 commit comments

Comments
 (0)