From e35b2e9fbf2434d968e096de0f338db10e821ba4 Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:40:12 +0000 Subject: [PATCH 1/3] add fallback to urlencode the postgres password if it fails --- mealie/core/settings/db_providers.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/mealie/core/settings/db_providers.py b/mealie/core/settings/db_providers.py index fff25bdcc14a..7de9a63c03e4 100644 --- a/mealie/core/settings/db_providers.py +++ b/mealie/core/settings/db_providers.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod from pathlib import Path +from urllib import parse as urlparse from pydantic import BaseModel, PostgresDsn from pydantic_settings import BaseSettings, SettingsConfigDict @@ -44,15 +45,28 @@ class PostgresProvider(AbstractDBProvider, BaseSettings): @property def db_url(self) -> str: host = f"{self.POSTGRES_SERVER}:{self.POSTGRES_PORT}" - return str( - PostgresDsn.build( + try: + url = PostgresDsn.build( scheme="postgresql", username=self.POSTGRES_USER, password=self.POSTGRES_PASSWORD, host=host, path=f"{self.POSTGRES_DB or ''}", ) - ) + except ValueError as outer_error: + try: + # if the password contains special characters, it needs to be URL encoded + url = PostgresDsn.build( + scheme="postgresql", + username=self.POSTGRES_USER, + password=urlparse.quote_plus(self.POSTGRES_PASSWORD), + host=host, + path=f"{self.POSTGRES_DB or ''}", + ) + except Exception: + raise outer_error + + return str(url) @property def db_url_public(self) -> str: From a384e6716d08b8bd2a67469405a647e75498787e Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:40:17 +0000 Subject: [PATCH 2/3] added test --- tests/unit_tests/test_config.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/unit_tests/test_config.py b/tests/unit_tests/test_config.py index 3d7a17205f52..552733757c1c 100644 --- a/tests/unit_tests/test_config.py +++ b/tests/unit_tests/test_config.py @@ -38,6 +38,15 @@ def test_pg_connection_args(monkeypatch): assert app_settings.DB_URL == "postgresql://mealie:mealie@postgres:5432/mealie" +def test_pg_connection_url_encode_password(monkeypatch): + monkeypatch.setenv("DB_ENGINE", "postgres") + monkeypatch.setenv("POSTGRES_SERVER", "postgres") + monkeypatch.setenv("POSTGRES_PASSWORD", "please,url#encode/this?password") + get_app_settings.cache_clear() + app_settings = get_app_settings() + assert app_settings.DB_URL == "postgresql://mealie:please%2Curl%23encode%2Fthis%3Fpassword@postgres:5432/mealie" + + @dataclass(slots=True) class SMTPValidationCase: host: str From 8db08c21e529f347f8e6048db2089d5817c6194c Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:58:03 +0000 Subject: [PATCH 3/3] removed try/catch --- mealie/core/settings/db_providers.py | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/mealie/core/settings/db_providers.py b/mealie/core/settings/db_providers.py index 7de9a63c03e4..9cd1a5aea101 100644 --- a/mealie/core/settings/db_providers.py +++ b/mealie/core/settings/db_providers.py @@ -45,28 +45,15 @@ class PostgresProvider(AbstractDBProvider, BaseSettings): @property def db_url(self) -> str: host = f"{self.POSTGRES_SERVER}:{self.POSTGRES_PORT}" - try: - url = PostgresDsn.build( + return str( + PostgresDsn.build( scheme="postgresql", username=self.POSTGRES_USER, - password=self.POSTGRES_PASSWORD, + password=urlparse.quote_plus(self.POSTGRES_PASSWORD), host=host, path=f"{self.POSTGRES_DB or ''}", ) - except ValueError as outer_error: - try: - # if the password contains special characters, it needs to be URL encoded - url = PostgresDsn.build( - scheme="postgresql", - username=self.POSTGRES_USER, - password=urlparse.quote_plus(self.POSTGRES_PASSWORD), - host=host, - path=f"{self.POSTGRES_DB or ''}", - ) - except Exception: - raise outer_error - - return str(url) + ) @property def db_url_public(self) -> str: