diff --git a/tubearchivist/config/settings.py b/tubearchivist/config/settings.py index 73aa701..436f659 100644 --- a/tubearchivist/config/settings.py +++ b/tubearchivist/config/settings.py @@ -18,6 +18,7 @@ import ldap from corsheaders.defaults import default_headers from django_auth_ldap.config import LDAPSearch from home.src.ta.config import AppConfig +from home.src.ta.helper import ta_host_parser # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -32,17 +33,7 @@ SECRET_KEY = PW_HASH.hexdigest() # SECURITY WARNING: don't run with debug turned on in production! DEBUG = bool(environ.get("DJANGO_DEBUG")) -ALLOWED_HOSTS = [] -if environ.get("TA_HOST"): - ALLOWED_HOSTS = [i.strip() for i in environ.get("TA_HOST").split()] - -CSRF_TRUSTED_ORIGINS = [] -for host in ALLOWED_HOSTS: - if host.startswith("http://") or host.startswith("https://"): - CSRF_TRUSTED_ORIGINS.append(host) - else: - CSRF_TRUSTED_ORIGINS.append(f"http://{host}") - +ALLOWED_HOSTS, CSRF_TRUSTED_ORIGINS = ta_host_parser(environ["TA_HOST"]) # Application definition diff --git a/tubearchivist/home/src/ta/helper.py b/tubearchivist/home/src/ta/helper.py index 2279790..2a9a95f 100644 --- a/tubearchivist/home/src/ta/helper.py +++ b/tubearchivist/home/src/ta/helper.py @@ -11,6 +11,7 @@ import string import subprocess import unicodedata from datetime import datetime +from urllib.parse import urlparse import requests @@ -148,6 +149,22 @@ def is_shorts(youtube_id): return response.status_code == 200 +def ta_host_parser(ta_host): + """parse ta_host env var for ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS""" + allowed_hosts = [] + csrf_trusted_origins = [] + for host in ta_host.split(): + host_clean = host.strip() + if not host_clean.startswith("http"): + host_clean = f"http://{host}" + + parsed = urlparse(host_clean) + allowed_hosts.append(f"{parsed.hostname}") + csrf_trusted_origins.append(f"{parsed.scheme}://{parsed.hostname}") + + return allowed_hosts, csrf_trusted_origins + + class DurationConverter: """ using ffmpeg to get and parse duration from filepath