Skip to content

Commit dd08e69

Browse files
authored
Merge pull request #272 from DevanshuNEU/infra/preview-deploy-cors
infra: CORS regex for Vercel preview deploys (OPE-56)
2 parents ef30258 + afa7755 commit dd08e69

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+
# Scoped to our project name so only our previews can call the backend
45+
# ALLOW_ORIGIN_REGEX=https://opencodeintel.*\.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 so PRs can be tested against prod backend.
79+
# Set to project-scoped regex: https://opencodeintel.*\.vercel\.app
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)