Skip to content

Commit 8b97d27

Browse files
committed
infra: add ALLOW_ORIGIN_REGEX for Vercel preview deploy CORS (OPE-56)
Vercel preview deploys get dynamic URLs like: opencodeintel-git-feat-xxx.vercel.app These need to call the Railway backend but get CORS blocked because ALLOWED_ORIGINS only has the production domain. FastAPI CORSMiddleware supports allow_origin_regex -- now configurable via ALLOW_ORIGIN_REGEX env var. Set on Railway to: https://.*\.vercel\.app This allows all Vercel preview URLs to call the backend while keeping the explicit ALLOWED_ORIGINS list for production. Also added GITHUB_TOKEN to optional startup vars (used by /repos/analyze).
1 parent ef30258 commit 8b97d27

3 files changed

Lines changed: 9 additions & 0 deletions

File tree

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ GITHUB_REDIRECT_URI=http://localhost:3000/auth/github/callback
4040
# CORS Configuration (Security)
4141
# Comma-separated list of allowed origins
4242
ALLOWED_ORIGINS=http://localhost:3000
43+
# Regex for dynamic CORS origins (Vercel preview deploys)
44+
# Set on Railway to allow PR preview URLs to call the backend
45+
# ALLOW_ORIGIN_REGEX=https://.*\.vercel\.app
4346

4447
# Redis (auto-configured in Docker, set REDIS_URL in Railway)
4548
REDIS_HOST=redis

backend/config/startup_checks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
("GITHUB_CLIENT_ID", "GitHub OAuth client ID", "GitHub repo import disabled"),
3030
("GITHUB_CLIENT_SECRET", "GitHub OAuth client secret", "GitHub repo import disabled"),
3131
("DISCORD_FEEDBACK_WEBHOOK", "Discord webhook for feedback", "Feedback notifications disabled"),
32+
("ALLOW_ORIGIN_REGEX", "CORS regex for preview deploys", "Only explicit origins allowed"),
33+
("GITHUB_TOKEN", "GitHub API token for repo analysis", "Using unauthenticated rate limit (60/hr)"),
3234
]
3335

3436

backend/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ async def dispatch(self, request: Request, call_next):
7575
app.add_middleware(RequestSizeLimitMiddleware)
7676

7777
ALLOWED_ORIGINS = os.getenv("ALLOWED_ORIGINS", "http://localhost:3000").split(",")
78+
# Allow Vercel preview deploys (dynamic subdomains) so PRs can be tested
79+
# against the production backend without CORS issues
80+
ALLOW_ORIGIN_REGEX = os.getenv("ALLOW_ORIGIN_REGEX", "")
7881
app.add_middleware(
7982
CORSMiddleware,
8083
allow_origins=ALLOWED_ORIGINS,
84+
allow_origin_regex=ALLOW_ORIGIN_REGEX or None,
8185
allow_credentials=True,
8286
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
8387
allow_headers=["Authorization", "Content-Type"],

0 commit comments

Comments
 (0)