From 801da01de500bf913fbecf480f9d19a63d49c2c8 Mon Sep 17 00:00:00 2001 From: bquant Date: Mon, 2 Nov 2026 10:11:47 +0100 Subject: [PATCH] fix: parse UPLOAD_SAVE_EXTENSIONS from the environment pydantic-settings JSON-decodes list fields read from the environment before field validators run, so UPLOAD_SAVE_EXTENSIONS=.pdf,.png made the bot fail at startup with 'error parsing value'. Mark the field NoDecode and accept both a comma list and a JSON list; add tests that go through the environment. Co-Authored-By: Claude Opus 5.5 --- src/config/settings.py | 10 +++++++--- tests/unit/test_config.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/config/settings.py b/src/config/settings.py index bfb4a907..0b6d97d1 100644 --- a/src/config/settings.py +++ b/src/config/settings.py @@ -11,10 +11,10 @@ import json import re from pathlib import Path -from typing import Any, List, Literal, Optional +from typing import Annotated, Any, List, Literal, Optional from pydantic import Field, SecretStr, field_validator, model_validator -from pydantic_settings import BaseSettings, SettingsConfigDict +from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict from src.utils.constants import ( DEFAULT_CLAUDE_MAX_COST_PER_REQUEST, @@ -220,7 +220,9 @@ class Settings(BaseSettings): ) enable_git_integration: bool = Field(True, description="Enable git commands") enable_file_uploads: bool = Field(True, description="Enable file upload handling") - upload_save_extensions: List[str] = Field( + # NoDecode: pydantic-settings would otherwise JSON-decode the env value + # before parse_upload_save_extensions runs, rejecting ".pdf,.png". + upload_save_extensions: Annotated[List[str], NoDecode] = Field( default=[], description=( "Extra file extensions (for example .pdf) accepted as uploads in agentic " @@ -405,6 +407,8 @@ def parse_upload_save_extensions(cls, v: Any) -> List[str]: """Parse comma-separated extensions into lowercase '.ext' form.""" if v is None: return [] + if isinstance(v, str) and v.strip().startswith("["): + v = json.loads(v) items = v.split(",") if isinstance(v, str) else list(v) exts = [] for item in items: diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index db894c2e..2190fd5d 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -725,3 +725,17 @@ def test_upload_save_extensions_rejects_invalid(tmp_path, bad): def test_upload_save_dir_rejects_unsafe(tmp_path, bad): with pytest.raises(ValueError): create_test_config(approved_directory=str(tmp_path), upload_save_dir=bad) + + +@pytest.mark.parametrize( + "raw", [".pdf,.png", " .PDF , png ", '[".pdf", ".png"]', '[".PDF","png"]'] +) +def test_upload_save_extensions_from_environment(tmp_path, monkeypatch, raw): + """The env var is parsed as a comma list or a JSON list (regression: a comma + list made pydantic-settings fail with 'error parsing value' at startup).""" + monkeypatch.setenv("TELEGRAM_BOT_TOKEN", "test_token") + monkeypatch.setenv("TELEGRAM_BOT_USERNAME", "test_bot") + monkeypatch.setenv("APPROVED_DIRECTORY", str(tmp_path)) + monkeypatch.setenv("UPLOAD_SAVE_EXTENSIONS", raw) + settings = Settings(_env_file=None) + assert settings.upload_save_extensions == [".pdf", ".png"]