Skip to content

Commit 8d591ef

Browse files
feat: Allow dynamic CORS origin for contributor dev environments
Co-authored-by: aider (vertex_ai/gemini-2.5-flash) <aider@aider.chat>
1 parent ae1f924 commit 8d591ef

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/cli/startLocal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ async function main(): Promise<void> {
5656
const repoRoot = path.resolve(process.cwd());
5757
const typedAiHome = process.env.TYPEDAI_HOME ? path.resolve(process.env.TYPEDAI_HOME) : null;
5858
const isDefaultRepo = typedAiHome ? repoRoot === typedAiHome : false;
59+
process.env.TYPEDAI_PORT_MODE = isDefaultRepo ? 'fixed' : 'dynamic';
5960

6061
// 2. Determine and set the backend server port.
6162
const parsedPort = process.env.PORT ? Number.parseInt(process.env.PORT, 10) : undefined;

src/fastify/fastifyApp.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,21 @@ async function loadPlugins(config: FastifyConfig) {
311311
await fastifyInstance.register(import('@fastify/jwt'), {
312312
secret: process.env.JWT_SECRET || 'your-secret-key',
313313
});
314+
// Determine CORS origin policy based on the port mode set during startup.
315+
let corsOrigin: string | boolean = new URL(process.env.UI_URL!).origin;
316+
317+
// In a contributor's local development setup, ports are dynamic to avoid conflicts.
318+
// In this "dynamic" mode, we cannot know the frontend's port at backend startup.
319+
// To avoid CORS issues that block development, we relax the policy.
320+
// `origin: true` reflects the request's origin, which is a safe way to allow any
321+
// origin for credentialed requests in a development context.
322+
// The 'fixed' mode is used for the default repository setup where ports are known and fixed.
323+
if (process.env.TYPEDAI_PORT_MODE === 'dynamic') {
324+
corsOrigin = true;
325+
}
326+
314327
await fastifyInstance.register(import('@fastify/cors'), {
315-
origin: [new URL(process.env.UI_URL!).origin],
328+
origin: corsOrigin,
316329
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
317330
allowedHeaders: ['Content-Type', 'Authorization', 'X-Goog-Iap-Jwt-Assertion', 'Enctype', 'Accept'],
318331
credentials: true,

0 commit comments

Comments
 (0)