Commit 60f85ef5 authored by Aryan Noshirvnai's avatar Aryan Noshirvnai

Add CORS middleware for the new stax_webapp frontend

No CORS configuration existed at all - every fetch() from a browser-based
frontend on a different origin (localhost:5173, or the dev server's own
:5173) would have been silently blocked. Auth here is a Bearer token in
localStorage, never a cookie, so allow_credentials isn't needed - origins
are read from CORS_ORIGINS (comma-separated), defaulting to the webapp's
known dev origins.

Verified: OPTIONS preflight to /auth/request_code from
http://144.76.210.145:5173 now gets a correct
Access-Control-Allow-Origin response.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019i3NAUkLkHNbsPg5vKfzF9
parent a250228b
Pipeline #1025 canceled with stage
...@@ -18,3 +18,7 @@ LOCAL_STORAGE_ROOT=./var/storage ...@@ -18,3 +18,7 @@ LOCAL_STORAGE_ROOT=./var/storage
# SQLAlchemy connection string. Defaults to sqlite:///./stax.db if unset. # SQLAlchemy connection string. Defaults to sqlite:///./stax.db if unset.
DATABASE_URL=sqlite:///./stax.db DATABASE_URL=sqlite:///./stax.db
# Comma-separated origins allowed to call this API from a browser (the stax_webapp frontend).
# Defaults to localhost:5173 + this dev server's IP:5173 if unset.
CORS_ORIGINS=http://localhost:5173,http://127.0.0.1:5173,http://144.76.210.145:5173
...@@ -4,13 +4,14 @@ import secrets ...@@ -4,13 +4,14 @@ import secrets
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from fastapi import Depends, FastAPI, Form, HTTPException, Request, status from fastapi import Depends, FastAPI, Form, HTTPException, Request, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
from fastapi.openapi.utils import get_openapi from fastapi.openapi.utils import get_openapi
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from sqlalchemy import text from sqlalchemy import text
from src.api.routers import auth, channels, discovery, library, music, remix, social from src.api.routers import auth, channels, discovery, library, music, remix, social
from src.core.config import DOCS_PASSWORD, DOCS_USERNAME from src.core.config import CORS_ORIGINS, DOCS_PASSWORD, DOCS_USERNAME
from src.core.database import SessionLocal from src.core.database import SessionLocal
from src.services.storage import DOWNLOADS_DIR from src.services.storage import DOWNLOADS_DIR
...@@ -74,6 +75,13 @@ def require_docs_auth(request: Request): ...@@ -74,6 +75,13 @@ def require_docs_auth(request: Request):
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
app.add_middleware(
CORSMiddleware,
allow_origins=CORS_ORIGINS,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/downloads", StaticFiles(directory=DOWNLOADS_DIR), name="downloads") app.mount("/downloads", StaticFiles(directory=DOWNLOADS_DIR), name="downloads")
app.include_router(auth.router) app.include_router(auth.router)
......
...@@ -14,6 +14,14 @@ LALAL_API_KEY = os.getenv("LALAL_API_KEY") ...@@ -14,6 +14,14 @@ LALAL_API_KEY = os.getenv("LALAL_API_KEY")
DOCS_USERNAME = os.getenv("DOCS_USERNAME") DOCS_USERNAME = os.getenv("DOCS_USERNAME")
DOCS_PASSWORD = os.getenv("DOCS_PASSWORD") DOCS_PASSWORD = os.getenv("DOCS_PASSWORD")
# Origins allowed to call this API from a browser (the stax_webapp frontend, dev and prod).
# Comma-separated in the env var. Auth here is a Bearer token in localStorage, never a
# cookie, so allowing these origins doesn't need allow_credentials.
CORS_ORIGINS = [o.strip() for o in os.getenv(
"CORS_ORIGINS",
"http://localhost:5173,http://127.0.0.1:5173,http://144.76.210.145:5173",
).split(",") if o.strip()]
# Signs every access/refresh JWT. Every request goes through this, so unlike the other keys # Signs every access/refresh JWT. Every request goes through this, so unlike the other keys
# above (which degrade gracefully per-request when absent) a missing secret must fail loudly # above (which degrade gracefully per-request when absent) a missing secret must fail loudly
# at startup rather than silently falling back to something guessable. # at startup rather than silently falling back to something guessable.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment