Skip to content

Commit c329b02

Browse files
Fix CORS: support localhost and Codesignal preview domains
1 parent 1925c0a commit c329b02

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/main.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,25 @@ import { LogsService } from './common/logging/logs.service';
1212
async function bootstrap() {
1313
const app = await NestFactory.create<NestExpressApplication>(AppModule);
1414
app.enableCors({
15-
origin: [
16-
'http://localhost:3000',
17-
/\.preview\.codesignal\.dev$/, // allow any Codesignal preview frontend
18-
],
15+
origin: (origin, callback) => {
16+
// Allow same-origin requests (like curl or health checks where no origin header is present)
17+
if (!origin) {
18+
return callback(null, true);
19+
}
20+
21+
// Explicitly allow localhost:3000 for local dev
22+
if (origin === 'http://localhost:3000') {
23+
return callback(null, true);
24+
}
25+
26+
// Allow any Codesignal preview subdomain
27+
if (/^https:\/\/[a-z0-9-]+\.preview\.codesignal\.dev$/.test(origin)) {
28+
return callback(null, true);
29+
}
30+
31+
// Otherwise block
32+
return callback(new Error(`CORS blocked for origin: ${origin}`), false);
33+
},
1934
credentials: true,
2035
});
2136

@@ -43,7 +58,6 @@ async function bootstrap() {
4358
);
4459

4560
const port = Number(process.env.PORT) || 3001;
46-
4761
await app.listen(port);
4862
console.log(`Application is running on: http://localhost:${port}`);
4963
}

0 commit comments

Comments
 (0)