Skip to content

Fix reading (boolean) settings from the environment, incl. SECURITY_TWO_FACTOR#2341

Open
Flix6x wants to merge 3 commits into
mainfrom
fix/security-two-factor-env-var
Open

Fix reading (boolean) settings from the environment, incl. SECURITY_TWO_FACTOR#2341
Flix6x wants to merge 3 commits into
mainfrom
fix/security-two-factor-env-var

Conversation

@Flix6x

@Flix6x Flix6x commented Jul 22, 2026

Copy link
Copy Markdown
Member

Description

Two related fixes to read_env_vars:

  1. SECURITY_TWO_FACTOR was never read from the environment, although the installation docs (and, since Document SECURITY_TWO_FACTOR and related 2FA config settings #2340, the configuration docs) advise enabling 2FA with export SECURITY_TWO_FACTOR=True. The app silently kept the default (False), which also meant SECURITY_TOTP_SECRETS from the environment was ignored (its handling in app_utils.set_totp_secrets is only reached when 2FA is on).
  2. Boolean settings from the environment were interpreted by string truthiness, so any non-empty value — including False or 0 — counted as enabled for DEBUG, and reached consumers as raw strings for MAIL_USE_TLS, MAIL_USE_SSL and FLEXMEASURES_JSON_COMPACT (the latter had ad-hoc string special-casing in app.py, now removed). Env values are now coerced to booleans whenever the setting's default is a boolean.

How to test

export SECURITY_TWO_FACTOR=True
export SECURITY_TOTP_SECRETS='{"1": "00000000000000000000000000000000"}'
export DEBUG=False
export MAIL_USE_TLS=False
flexmeasures run  # 2FA is on, DEBUG and TLS are actually off

Related Items

Surfaced while reviewing #2340; relates to #1760.


Sign-off

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on code under GPL or other license that is incompatible with FlexMeasures

Flix6x added 2 commits July 22, 2026 15:57
The installation docs (and now the configuration docs, via PR #2340)
advise enabling 2FA with 'export SECURITY_TWO_FACTOR=True', but
read_env_vars never read that variable, so the app silently stayed
on the default (False). Parse it explicitly as a boolean, since env
values arrive as strings.

Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@read-the-docs-community

read-the-docs-community Bot commented Jul 22, 2026

Copy link
Copy Markdown

Environment values are strings, so any non-empty value - including
'False' or '0' - counted as enabled for DEBUG, and reached consumers
raw for MAIL_USE_TLS, MAIL_USE_SSL and FLEXMEASURES_JSON_COMPACT.
Coerce env values to booleans whenever the setting's default is a
boolean, and drop the now-redundant string special-casing for
FLEXMEASURES_JSON_COMPACT in app.py.

Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x Flix6x changed the title Fix: read SECURITY_TWO_FACTOR from the environment, as documented Fix reading (boolean) settings from the environment, incl. SECURITY_TWO_FACTOR Jul 22, 2026
@Flix6x
Flix6x requested a review from Copilot July 22, 2026 19:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 improves FlexMeasures’ environment-variable configuration handling by ensuring boolean settings are correctly coerced from strings and by adding missing support for SECURITY_TWO_FACTOR when configured via the environment.

Changes:

  • Add boolean parsing for env vars whose config default is a boolean, and include SECURITY_TWO_FACTOR/DEBUG in env processing.
  • Simplify FLEXMEASURES_JSON_COMPACT handling in app initialization now that env parsing is centralized.
  • Add a changelog entry documenting the bugfix and behavior change.

Reviewed changes

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

File Description
flexmeasures/utils/config_utils.py Adds parse_bool_env and updates read_env_vars to coerce boolean env vars and read additional settings
flexmeasures/app.py Simplifies JSON compactness configuration by relying on the config value
documentation/changelog.rst Documents the environment-boolean parsing and SECURITY_TWO_FACTOR env-reading fix

Comment on lines +215 to +216
def parse_bool_env(value: str) -> bool:
return value.lower() in ("true", "1", "yes", "on")
Comment on lines 237 to 241
"LOGGING_LEVEL",
"MAPBOX_ACCESS_TOKEN",
"SENTRY_SDN",
"FLEXMEASURES_PLUGINS",
"FLEXMEASURES_JSON_COMPACT",
Comment thread flexmeasures/app.py
app.json.compact = True
else:
app.json.compact = False
app.json.compact = bool(app.config.get("FLEXMEASURES_JSON_COMPACT", False))
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.

2 participants