From 2d351deeaab6b77c0fd5f184c92547d2811bca22 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 16 Oct 2026 00:09:04 +0000 Subject: [PATCH] =?UTF-8?q?feat(api):=20CORS=20=ED=97=88=EC=9A=A9=20origin?= =?UTF-8?q?=EC=9D=84=20=ED=99=98=EA=B2=BD=EB=B3=80=EC=88=98=EB=A1=9C=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01DmBmpFCjUN5yzz3WPSEWva --- backend/README.md | 1 + backend/fancall/settings.py | 23 +++++++++++++++++++++++ backend/main.py | 4 ++-- backend/tests/unit/test_settings.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 backend/tests/unit/test_settings.py diff --git a/backend/README.md b/backend/README.md index bf9cf1e..625cad2 100644 --- a/backend/README.md +++ b/backend/README.md @@ -107,6 +107,7 @@ poetry run alembic revision --autogenerate -m "변경 설명" | `FANCALL_OPENAI_MODEL` | `gpt-4o-mini` | 사용할 OpenAI LLM 모델 | | `HEDRA_ENABLED` | `false` | Hedra 아바타 활성화 | | `HEDRA_API_KEY` | - | Hedra API 키 (enabled=true일 때 필수) | +| `CORS_ALLOWED_ORIGINS` | `http://localhost:3000` | 쿠키를 실은 요청을 허용할 프런트 origin (쉼표로 구분). 운영에서는 실제 도메인으로 설정 | > **참고**: LiveKit, 데이터베이스, 모델 등 추가 설정은 기본값으로 로컬 개발 가능합니다. > 변경이 필요한 경우 `fancall/settings.py`의 Settings 클래스를 참고하세요. diff --git a/backend/fancall/settings.py b/backend/fancall/settings.py index 43d85ae..aecb614 100644 --- a/backend/fancall/settings.py +++ b/backend/fancall/settings.py @@ -54,6 +54,29 @@ def check_timeline(self) -> "FancallCallSettings": return self +class CorsSettings(BaseSettings): + """CORS settings for the API server. + + The frontend fetches with ``credentials: "include"``, so browsers reject a + wildcard origin and every allowed origin must be listed explicitly. + + Attributes: + allowed_origins: Comma-separated list of origins allowed to make + credentialed requests (CORS_ALLOWED_ORIGINS). Defaults to the local + Next.js dev server. + """ + + allowed_origins: str = "http://localhost:3000" + + class Config: + env_prefix = "CORS_" + + @property + def origins(self) -> list[str]: + """Parsed list of allowed origins; blank entries are ignored.""" + return [o.strip() for o in self.allowed_origins.split(",") if o.strip()] + + class LiveKitSettings(BaseSettings): """Settings for LiveKit API integration diff --git a/backend/main.py b/backend/main.py index 40da22d..b53ea1d 100644 --- a/backend/main.py +++ b/backend/main.py @@ -36,7 +36,7 @@ from fancall.api.router import create_fancall_router from fancall.factories import LiveRoomRepositoryFactory -from fancall.settings import LiveKitSettings +from fancall.settings import CorsSettings, LiveKitSettings # Configure logging logging.basicConfig( @@ -85,7 +85,7 @@ # CORS middleware app.add_middleware( CORSMiddleware, - allow_origins=["*"], # Configure appropriately for production + allow_origins=CorsSettings().origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], diff --git a/backend/tests/unit/test_settings.py b/backend/tests/unit/test_settings.py new file mode 100644 index 0000000..973b493 --- /dev/null +++ b/backend/tests/unit/test_settings.py @@ -0,0 +1,28 @@ +""" +Unit tests for fancall settings +""" + +import os +import unittest +from unittest.mock import patch + +from fancall.settings import CorsSettings + + +class TestCorsSettings(unittest.TestCase): + """Test cases for CorsSettings""" + + def test_defaults_to_local_dev_origin(self) -> None: + """Without configuration only the local dev frontend is allowed""" + with patch.dict(os.environ, clear=True): + self.assertEqual(CorsSettings().origins, ["http://localhost:3000"]) + + def test_parses_comma_separated_origins(self) -> None: + """Origins come from the environment, trimmed, without blanks""" + env = {"CORS_ALLOWED_ORIGINS": "https://a.com, https://b.com,,"} + with patch.dict(os.environ, env, clear=True): + self.assertEqual(CorsSettings().origins, ["https://a.com", "https://b.com"]) + + +if __name__ == "__main__": + unittest.main()