Open-source personal finance manager. Self-hosted, privacy-first.
fix: derive OAuth redirect URIs from FRONTEND_URL, gate podman-compose on 1.6.0 (#478)
Tássio Noronha0d7dca6d09e1
diff --git a/.env.example b/.env.exampleindex f6548690..b1026ff5 100644--- a/.env.example+++ b/.env.example@@ -16,8 +16,9 @@ PLUGGY_CLIENT_SECRET=ENABLE_BANKING_APP_ID=# Override only if you put the PEM somewhere other than the default below# ENABLE_BANKING_PRIVATE_KEY_FILE=/app/secrets/enable_banking_private.pem-# Must match exactly one of the Allowed Redirect URLs in your EB application-ENABLE_BANKING_OAUTH_REDIRECT_URI=http://localhost:${FRONTEND_PORT:-3000}/oauth/callback+# Defaults to ${FRONTEND_URL}/oauth/callback. Set it only when the URL you+# registered in your EB application differs from where the app is served.+# ENABLE_BANKING_OAUTH_REDIRECT_URI=https://securo.example.com/oauth/callback# OIDC login (optional) — Authentik, Pocket ID, or any standard OIDC provider## Redirect/callback URL to register in the provider:diff --git a/backend/app/core/config.py b/backend/app/core/config.pyindex 20150478..f45ae9d9 100644--- a/backend/app/core/config.py+++ b/backend/app/core/config.py@@ -28,14 +28,17 @@ class Settings(BaseSettings):# Pluggypluggy_client_id: str = ""pluggy_client_secret: SecretStr = SecretStr("")- pluggy_oauth_redirect_uri: str = "http://localhost:5173/oauth/callback"+ # Empty means "derive from FRONTEND_URL" (see BankProvider.redirect_uri).+ # Set explicitly only when the URL registered in the provider dashboard+ # differs from where the app is served.+ pluggy_oauth_redirect_uri: str = ""# Enable Banking (European PSD2 banks)enable_banking_app_id: str = ""enable_banking_private_key: SecretStr = SecretStr("") # raw PEM; supports \n-escaped envsenable_banking_private_key_file: str = "" # path to PEM file; takes precedenceenable_banking_api_url: str = "https://api.enablebanking.com"- enable_banking_oauth_redirect_uri: str = "http://localhost:5173/oauth/callback"+ enable_banking_oauth_redirect_uri: str = "" # empty derives from FRONTEND_URL# SimpleFIN Bridge (US/intl banks, paste-a-token flow). Off by default.# The bridge URL defaults to the beta/sandbox host so users can test withdiff --git a/backend/app/providers/base.py b/backend/app/providers/base.pyindex cb465a08..a0eb3486 100644--- a/backend/app/providers/base.py+++ b/backend/app/providers/base.py@@ -18,6 +18,20 @@ from typing import Literal, OptionalRefreshOutcome = Literal["refreshed", "skipped", "needs_user_action", "failed"]+def default_oauth_redirect_uri() -> str:+ """The OAuth callback URL implied by where the frontend is served.++ Providers fall back to this when their own ``*_OAUTH_REDIRECT_URI`` is+ unset, so a deployment on a custom port or domain works without setting one+ variable per provider. Deriving it here rather than in Compose also keeps+ the compose files free of nested ``${VAR:-${OTHER:-x}}`` interpolation,+ which podman-compose cannot parse (containers/podman-compose#1064).+ """+ from app.core.config import get_settings++ return f"{get_settings().frontend_url.rstrip('/')}/oauth/callback"++def mask_last4(value: Optional[str]) -> Optional[str]:"""Reduce a bank-assigned account identifier to its last four characters.@@ -240,11 +254,13 @@ class BankProvider(ABC):Each provider reads its own env var (e.g.``PLUGGY_OAUTH_REDIRECT_URI``, ``ENABLE_BANKING_OAUTH_REDIRECT_URI``)so different providers can register different URLs in their- respective dashboards.+ respective dashboards. When unset the URL is derived from+ ``FRONTEND_URL`` so a non-default port or domain works out of the box."""from app.core.config import get_settings- return get_settings().pluggy_oauth_redirect_uri+ settings = get_settings()+ return settings.pluggy_oauth_redirect_uri or default_oauth_redirect_uri()async def create_connect_token(self, client_user_id: str, item_id: str | None = Nonediff --git a/backend/app/providers/enable_banking.py b/backend/app/providers/enable_banking.pyindex edb25e9d..6beb83b1 100644--- a/backend/app/providers/enable_banking.py+++ b/backend/app/providers/enable_banking.py@@ -34,6 +34,7 @@ from app.providers.base import (ProviderUserActionRequired,SessionExpiredError,TransactionData,+ default_oauth_redirect_uri,mask_last4,)@@ -190,7 +191,10 @@ class EnableBankingProvider(BankProvider):@propertydef redirect_uri(self) -> str:- return get_settings().enable_banking_oauth_redirect_uri+ return (+ get_settings().enable_banking_oauth_redirect_uri+ or default_oauth_redirect_uri()+ )# ----- credentials -----diff --git a/backend/tests/test_providers.py b/backend/tests/test_providers.pyindex 1f04fde4..f87f6fe7 100644--- a/backend/tests/test_providers.py+++ b/backend/tests/test_providers.py@@ -96,3 +96,54 @@ def test_get_storage_provider_unsupported():get_storage_provider()finally:providers_mod._storage_provider = original+++class TestOAuthRedirectDefaults:+ """Redirect URIs fall back to FRONTEND_URL so a custom port/domain works.++ Deriving here rather than in docker-compose keeps the compose files free of+ nested `${VAR:-${OTHER:-x}}` interpolation, which podman-compose cannot+ parse (containers/podman-compose#1064).+ """++ def _settings(self, **overrides):+ from app.core.config import Settings++ defaults = {+ "frontend_url": "http://localhost:3000",+ "pluggy_oauth_redirect_uri": "",+ "enable_banking_oauth_redirect_uri": "",+ }+ return Settings(**{**defaults, **overrides})++ def test_default_derives_from_frontend_url(self):+ from app.providers.base import default_oauth_redirect_uri++ with patch("app.core.config.get_settings", return_value=self._settings()):+ assert default_oauth_redirect_uri() == "http://localhost:3000/oauth/callback"++ def test_default_honours_custom_port_and_domain(self):+ from app.providers.base import default_oauth_redirect_uri++ settings = self._settings(frontend_url="https://securo.example.com/")+ with patch("app.core.config.get_settings", return_value=settings):+ assert default_oauth_redirect_uri() == "https://securo.example.com/oauth/callback"++ def test_enable_banking_uses_derived_default_when_unset(self):+ from app.providers.enable_banking import EnableBankingProvider++ settings = self._settings(frontend_url="http://localhost:3132")+ with patch("app.core.config.get_settings", return_value=settings), \+ patch("app.providers.enable_banking.get_settings", return_value=settings):+ assert EnableBankingProvider().redirect_uri == "http://localhost:3132/oauth/callback"++ def test_explicit_value_still_wins(self):+ from app.providers.enable_banking import EnableBankingProvider++ settings = self._settings(+ frontend_url="http://localhost:3000",+ enable_banking_oauth_redirect_uri="https://registered.example.com/cb",+ )+ with patch("app.core.config.get_settings", return_value=settings), \+ patch("app.providers.enable_banking.get_settings", return_value=settings):+ assert EnableBankingProvider().redirect_uri == "https://registered.example.com/cb"diff --git a/docker-compose.prod.yml b/docker-compose.prod.ymlindex 6f9909a1..88004a19 100644--- a/docker-compose.prod.yml+++ b/docker-compose.prod.yml@@ -14,11 +14,11 @@ x-backend-env: &backend-envFRONTEND_URL: ${FRONTEND_URL:-http://localhost:3000}PLUGGY_CLIENT_ID: ${PLUGGY_CLIENT_ID:-}PLUGGY_CLIENT_SECRET: ${PLUGGY_CLIENT_SECRET:-}- PLUGGY_OAUTH_REDIRECT_URI: ${PLUGGY_OAUTH_REDIRECT_URI:-http://localhost:3000/oauth/callback}+ PLUGGY_OAUTH_REDIRECT_URI: ${PLUGGY_OAUTH_REDIRECT_URI:-}ENABLE_BANKING_APP_ID: ${ENABLE_BANKING_APP_ID:-}ENABLE_BANKING_PRIVATE_KEY_FILE: ${ENABLE_BANKING_PRIVATE_KEY_FILE:-/app/secrets/enable_banking_private.pem}ENABLE_BANKING_API_URL: ${ENABLE_BANKING_API_URL:-https://api.enablebanking.com}- ENABLE_BANKING_OAUTH_REDIRECT_URI: ${ENABLE_BANKING_OAUTH_REDIRECT_URI:-http://localhost:3000/oauth/callback}+ ENABLE_BANKING_OAUTH_REDIRECT_URI: ${ENABLE_BANKING_OAUTH_REDIRECT_URI:-}SIMPLEFIN_ENABLED: ${SIMPLEFIN_ENABLED:-false}SIMPLEFIN_API_URL: ${SIMPLEFIN_API_URL:-https://beta-bridge.simplefin.org}OIDC_ENABLED: ${OIDC_ENABLED:-false}diff --git a/docker-compose.yml b/docker-compose.ymlindex eba2ea29..5b59ec8e 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -17,14 +17,11 @@ x-backend-env: &backend-envWEBAUTHN_ORIGIN: ${WEBAUTHN_ORIGIN:-}PLUGGY_CLIENT_ID: ${PLUGGY_CLIENT_ID:-}PLUGGY_CLIENT_SECRET: ${PLUGGY_CLIENT_SECRET:-}- PLUGGY_OAUTH_REDIRECT_URI: http://localhost:${FRONTEND_PORT:-3000}/oauth/callback+ PLUGGY_OAUTH_REDIRECT_URI: ${PLUGGY_OAUTH_REDIRECT_URI:-}ENABLE_BANKING_APP_ID: ${ENABLE_BANKING_APP_ID:-}ENABLE_BANKING_PRIVATE_KEY_FILE: ${ENABLE_BANKING_PRIVATE_KEY_FILE:-/app/secrets/enable_banking_private.pem}ENABLE_BANKING_API_URL: ${ENABLE_BANKING_API_URL:-https://api.enablebanking.com}- # Compose does not support nested interpolation inside ${VAR:-default}.- # Keep this default parseable; override ENABLE_BANKING_OAUTH_REDIRECT_URI- # explicitly when testing Enable Banking with a non-default redirect URL.- ENABLE_BANKING_OAUTH_REDIRECT_URI: ${ENABLE_BANKING_OAUTH_REDIRECT_URI:-http://localhost:3000/oauth/callback}+ ENABLE_BANKING_OAUTH_REDIRECT_URI: ${ENABLE_BANKING_OAUTH_REDIRECT_URI:-}SIMPLEFIN_ENABLED: ${SIMPLEFIN_ENABLED:-false}SIMPLEFIN_API_URL: ${SIMPLEFIN_API_URL:-https://beta-bridge.simplefin.org}OIDC_ENABLED: ${OIDC_ENABLED:-false}diff --git a/install.sh b/install.shindex b9fab588..e42ddd9c 100755--- a/install.sh+++ b/install.sh@@ -143,8 +143,12 @@ check_podman_compose_version() {pc_version=$(podman-compose --version 2>&1 | grep -i 'podman-compose version' | awk '{print $NF}' || true)info "podman-compose ${pc_version:-unknown} found"- if [ -n "$pc_version" ] && ! version_at_least "$pc_version" "1.0.6"; then- warn "podman-compose $pc_version is old. Upgrade to >= 1.0.6 (ideally 1.5+) for reliable healthcheck support."+ # 1.6.0 is the first release that resolves nested ${VAR:-${OTHER:-x}}+ # interpolation. Below it, FRONTEND_URL is handed to the containers with the+ # inner ${FRONTEND_PORT:-3000} left as literal text, which silently breaks+ # CORS, passkeys, and the OAuth callbacks.+ if [ -n "$pc_version" ] && ! version_at_least "$pc_version" "1.6.0"; then+ warn "podman-compose $pc_version is old. Upgrade to >= 1.6.0: earlier versions leave FRONTEND_URL unresolved and break login callbacks."fi}