diff --git a/server/.env.example b/server/.env.example index e440692..40aaac1 100644 --- a/server/.env.example +++ b/server/.env.example @@ -13,6 +13,10 @@ # GNEXUS_BOOK_MCP_TOKEN= # GNEXUS_BOOK_MCP_KEY_RATE_LIMIT=30 # GNEXUS_BOOK_MCP_REQUEST_RATE_LIMIT=300 +# Comma-separated Host values accepted by FastMCP. Add the public hostname +# when the MCP endpoint is served behind nginx (e.g. book.gnexus.space). +# GNEXUS_BOOK_MCP_ALLOWED_HOSTS=127.0.0.1:*,localhost:*,[::1]:*,book.gnexus.space +# GNEXUS_BOOK_MCP_ALLOWED_ORIGINS=http://127.0.0.1:*,http://localhost:*,http://[::1]:* # Internal key-provisioning API (not exposed through nginx) # GNEXUS_BOOK_INTERNAL_KEY_SECRET= # GNEXUS_BOOK_INTERNAL_ALLOWLIST=127.0.0.1,::1 @@ -25,6 +29,7 @@ # GNEXUS_BOOK_AUTH_REDIRECT_URI=https://book.gnexus.space/auth/callback # GNEXUS_BOOK_AUTH_WEBHOOK_SECRET= # GNEXUS_BOOK_AUTH_SESSION_COOKIE=gnb_session +# GNEXUS_BOOK_AUTH_STATE_COOKIE=gnb_oauth_state # GNEXUS_BOOK_AUTH_COOKIE_SECURE=true # GNEXUS_BOOK_AUTH_SESSION_TTL_SECONDS=604800 # GNEXUS_BOOK_AUTH_SCOPES=openid email profile roles permissions diff --git a/server/app/config.py b/server/app/config.py index 57cbb76..17c3da2 100644 --- a/server/app/config.py +++ b/server/app/config.py @@ -5,6 +5,12 @@ from pathlib import Path STORAGE_BACKENDS = {"file", "db"} +DEFAULT_MCP_ALLOWED_HOSTS = ("127.0.0.1:*", "localhost:*", "[::1]:*") +DEFAULT_MCP_ALLOWED_ORIGINS = ( + "http://127.0.0.1:*", + "http://localhost:*", + "http://[::1]:*", +) class Settings: @@ -14,7 +20,12 @@ database_url: str | None = None, storage_backend: str | None = None, ) -> None: - self.repo_root = repo_root or Path(__file__).resolve().parents[2] + configured_root = repo_root or os.environ.get("GNEXUS_BOOK_REPO_ROOT") + self.repo_root = ( + Path(configured_root).expanduser().resolve() + if configured_root + else Path(__file__).resolve().parents[2] + ) self.docs_extensions = {".md"} self.inventory_dir = self.repo_root / "40-inventory" self.generated_dir = self.repo_root / "60-generated" @@ -35,6 +46,9 @@ self.auth_session_cookie = os.environ.get( "GNEXUS_BOOK_AUTH_SESSION_COOKIE", "gnb_session" ) + self.auth_state_cookie = os.environ.get( + "GNEXUS_BOOK_AUTH_STATE_COOKIE", "gnb_oauth_state" + ) self.auth_session_ttl_seconds = int( os.environ.get("GNEXUS_BOOK_AUTH_SESSION_TTL_SECONDS", str(7 * 24 * 3600)) ) @@ -64,6 +78,22 @@ self.mcp_request_rate_limit = int( os.environ.get("GNEXUS_BOOK_MCP_REQUEST_RATE_LIMIT", "300") ) + self.mcp_allowed_hosts = tuple( + item.strip() + for item in os.environ.get( + "GNEXUS_BOOK_MCP_ALLOWED_HOSTS", + ",".join(DEFAULT_MCP_ALLOWED_HOSTS), + ).split(",") + if item.strip() + ) or DEFAULT_MCP_ALLOWED_HOSTS + self.mcp_allowed_origins = tuple( + item.strip() + for item in os.environ.get( + "GNEXUS_BOOK_MCP_ALLOWED_ORIGINS", + ",".join(DEFAULT_MCP_ALLOWED_ORIGINS), + ).split(",") + if item.strip() + ) or DEFAULT_MCP_ALLOWED_ORIGINS self.internal_key_secret = os.environ.get("GNEXUS_BOOK_INTERNAL_KEY_SECRET") self.internal_allowlist = tuple( item.strip() diff --git a/server/tests/test_db_storage.py b/server/tests/test_db_storage.py index 35664f5..a4dbd97 100644 --- a/server/tests/test_db_storage.py +++ b/server/tests/test_db_storage.py @@ -45,6 +45,15 @@ assert isinstance(make_inventory_repository(settings), InventoryRepository) +def test_repo_root_env_is_honored(tmp_path, monkeypatch) -> None: + monkeypatch.setenv("GNEXUS_BOOK_REPO_ROOT", str(tmp_path)) + + settings = Settings() + + assert settings.repo_root == tmp_path.resolve() + assert settings.inventory_dir == tmp_path.resolve() / "40-inventory" + + def test_search_matches_file_backend(imported: tuple[Settings, Database]) -> None: settings, database = imported file_repo = DocsRepository(Settings(repo_root=settings.repo_root, storage_backend="file"))