Skip to content

Commit

Permalink
Add HSTS_MODE_ENABLED setting
Browse files Browse the repository at this point in the history
  • Loading branch information
apragacz committed Dec 5, 2022
1 parent d413cec commit 5a4121a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
10 changes: 8 additions & 2 deletions fzw/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import dj_database_url # type: ignore

from fzw.util.common import smart_bool, smart_bool_or_none

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATA_DIR = os.path.join(BASE_DIR, 'data')
Expand All @@ -26,7 +28,7 @@
SECRET_KEY = os.environ.get('SECRET_KEY', 'abracadabra')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = bool(int(os.environ.get('DEBUG', '0')))
DEBUG = smart_bool(os.getenv("DEBUG"))

BACKEND_HOST = os.environ.get('BACKEND_HOST', '*')
DATABASE_URL = os.environ.get(
Expand Down Expand Up @@ -178,7 +180,11 @@
},
}

if not DEBUG:
HSTS_MODE_ENABLED = smart_bool_or_none(os.getenv("HSTS_MODE_ENABLED"))
if HSTS_MODE_ENABLED is None:
HSTS_MODE_ENABLED = not DEBUG

if HSTS_MODE_ENABLED:
SECURE_HSTS_SECONDS = 60
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_SSL_REDIRECT = True
Expand Down
Empty file added fzw/util/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions fzw/util/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import Any, Optional

_TRUTHY_VALUES = {"t", "true", "1"}
_FALSY_VALUES = {"f", "false", "0"}


def smart_bool_or_none(value: Any) -> Optional[bool]:
if isinstance(value, str):
normalized_value = value.lower()
if normalized_value in _TRUTHY_VALUES:
return True
if normalized_value in _FALSY_VALUES:
return False
return None
if value is None:
return None
return bool(value)


def smart_bool(value: Any) -> bool:
return smart_bool_or_none(value) or False
Empty file added tests/util/__init__.py
Empty file.
55 changes: 55 additions & 0 deletions tests/util/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest

from fzw.util.common import smart_bool, smart_bool_or_none


@pytest.mark.parametrize(
"input,output",
(
(False, False),
(True, True),
(0, False),
(1, True),
(1.0, True),
(0.1, True),
("", False),
("none", False),
("nil", False),
("0", False),
("false", False),
("False", False),
("f", False),
("1", True),
("true", True),
("True", True),
("t", True),
),
)
def test_smart_bool(input, output):
assert smart_bool(input) == output


@pytest.mark.parametrize(
"input,output",
(
(False, False),
(True, True),
(0, False),
(1, True),
(1.0, True),
(0.1, True),
("", None),
("none", None),
("nil", None),
("0", False),
("false", False),
("False", False),
("f", False),
("1", True),
("true", True),
("True", True),
("t", True),
),
)
def test_smart_bool_or_none(input, output):
assert smart_bool_or_none(input) == output

0 comments on commit 5a4121a

Please sign in to comment.