From 427d81195446df36ba37d28381bf249ceb734663 Mon Sep 17 00:00:00 2001 From: David Gil Date: Tue, 17 Feb 2026 21:58:08 +0100 Subject: [PATCH] security: restrict CORS to known local origins instead of wildcard The wildcard `allow_origins=["*"]` allows any website the user visits to make requests to the local voicebox backend, potentially triggering TTS generation or reading voice profiles without consent. Restrict to the known Tauri webview and Vite dev server origins by default. Users running in remote server mode can set VOICEBOX_CORS_ORIGINS to allow additional origins. --- backend/main.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/backend/main.py b/backend/main.py index 59fb9e18..05ce741f 100644 --- a/backend/main.py +++ b/backend/main.py @@ -36,10 +36,23 @@ app = FastAPI( version=__version__, ) -# CORS middleware +# CORS middleware - restrict to known local origins by default. +# Set VOICEBOX_CORS_ORIGINS env var to a comma-separated list of origins +# to allow additional origins (e.g. for remote server mode). +_default_origins = [ + "http://localhost:5173", # Vite dev server + "http://127.0.0.1:5173", + "http://localhost:17493", + "http://127.0.0.1:17493", + "tauri://localhost", # Tauri webview (macOS) + "https://tauri.localhost", # Tauri webview (Windows/Linux) +] +_env_origins = os.environ.get("VOICEBOX_CORS_ORIGINS", "") +_cors_origins = _default_origins + [o.strip() for o in _env_origins.split(",") if o.strip()] + app.add_middleware( CORSMiddleware, - allow_origins=["*"], # Configure appropriately for production + allow_origins=_cors_origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"],