fix(center): enable CORS for auth preflight and desktop origins
This commit is contained in:
23
src/main.ts
23
src/main.ts
@@ -28,6 +28,29 @@ async function bootstrap() {
|
|||||||
validateEnv();
|
validateEnv();
|
||||||
|
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
|
const corsOrigins = (process.env.FABRIC_BACKEND_CENTER_CORS_ORIGINS ?? '')
|
||||||
|
.split(',')
|
||||||
|
.map((x) => x.trim())
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
app.enableCors({
|
||||||
|
origin: (origin, callback) => {
|
||||||
|
// no Origin header: curl/server-to-server/most desktop local calls
|
||||||
|
if (!origin) return callback(null, true);
|
||||||
|
|
||||||
|
// desktop/electron local file origin
|
||||||
|
if (origin === 'null') return callback(null, true);
|
||||||
|
|
||||||
|
// empty allowlist => allow all origins
|
||||||
|
if (!corsOrigins.length) return callback(null, true);
|
||||||
|
|
||||||
|
if (corsOrigins.includes(origin)) return callback(null, true);
|
||||||
|
return callback(new Error('CORS origin not allowed'), false);
|
||||||
|
},
|
||||||
|
methods: ['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS'],
|
||||||
|
allowedHeaders: ['Content-Type', 'Authorization', 'x-client-name', 'x-request-id', 'x-api-key'],
|
||||||
|
credentials: false,
|
||||||
|
});
|
||||||
app.setGlobalPrefix('api');
|
app.setGlobalPrefix('api');
|
||||||
const metrics = app.get(MetricsService);
|
const metrics = app.get(MetricsService);
|
||||||
app.use(createRequestContextMiddleware('center', metrics));
|
app.use(createRequestContextMiddleware('center', metrics));
|
||||||
|
|||||||
Reference in New Issue
Block a user