fix: support comma-separated CORS_ORIGINS environment variable
Some checks failed
CI / backend (push) Successful in 23s
CI / frontend (push) Successful in 41s
CI / docker (push) Failing after 56s

Add NoDecode annotation to cors_origins field in Settings to properly parse comma-separated values from environment variables. Add test to verify CORS_ORIGINS accepts comma-separated list.
This commit is contained in:
2026-06-04 10:43:15 +02:00
parent 9466c5a3b3
commit 10a06791db
2 changed files with 14 additions and 2 deletions

View File

@@ -1,7 +1,8 @@
from functools import lru_cache from functools import lru_cache
from typing import Annotated
from pydantic import field_validator from pydantic import field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict
class Settings(BaseSettings): class Settings(BaseSettings):
@@ -13,7 +14,7 @@ class Settings(BaseSettings):
jwt_secret_key: str jwt_secret_key: str
settings_secret_key: str settings_secret_key: str
cookie_secure: bool = True cookie_secure: bool = True
cors_origins: list[str] = ["http://localhost"] cors_origins: Annotated[list[str], NoDecode] = ["http://localhost"]
log_level: str = "INFO" log_level: str = "INFO"
default_timezone: str = "Europe/Vienna" default_timezone: str = "Europe/Vienna"
daily_worker_interval_seconds: int = 300 daily_worker_interval_seconds: int = 300

View File

@@ -1,3 +1,4 @@
from app.core.config import Settings
from app.core.security import hash_password, hash_token, verify_password from app.core.security import hash_password, hash_token, verify_password
@@ -12,3 +13,13 @@ def test_tokens_are_hashed() -> None:
assert hash_token("secret") == hash_token("secret") assert hash_token("secret") == hash_token("secret")
assert hash_token("secret") != "secret" assert hash_token("secret") != "secret"
def test_cors_origins_accept_comma_separated_env(monkeypatch) -> None:
monkeypatch.setenv("JWT_SECRET_KEY", "test-jwt-secret")
monkeypatch.setenv("SETTINGS_SECRET_KEY", "test-settings-secret")
monkeypatch.setenv("CORS_ORIGINS", "http://localhost,http://localhost:5173")
settings = Settings()
assert settings.cors_origins == ["http://localhost", "http://localhost:5173"]