Skip to content

Commit a619a57

Browse files
committed
Add bypassCors option
1 parent 75ca70f commit a619a57

3 files changed

Lines changed: 34 additions & 17 deletions

File tree

packages/server/api/src/app/app.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,33 +137,47 @@ export const setupApp = async (
137137

138138
await app.register(rateLimitModule);
139139

140-
await app.register(cors, {
141-
origin: (origin, callback) => {
142-
logger.info('Allow cors request plugin');
140+
await app.register(async (app) => {
141+
app.addHook('preHandler', async (req, _reply) => {
142+
const bypassCorsPlugin =
143+
req.routeOptions.config?.bypassCorsPlugin ?? false;
143144

144-
if (origin === system.get(SharedSystemProp.FRONTEND_URL)) {
145-
return callback(null, true);
145+
if (bypassCorsPlugin) {
146+
// reply.header('X-CORS-SKIPPED', 'true');
147+
logger.info('Bypass Cors Plugin');
148+
149+
return;
146150
}
151+
});
147152

148-
const allowedDomainsString = system.get(AppSystemProp.ALLOWED_DOMAINS);
153+
await app.register(cors, {
154+
origin: (origin, callback) => {
155+
logger.info('Allow cors request plugin');
149156

150-
if (allowedDomainsString) {
151-
if (allowedDomainsString === '*') {
157+
if (origin === system.get(SharedSystemProp.FRONTEND_URL)) {
152158
return callback(null, true);
153159
}
154160

155-
const allowedDomains = allowedDomainsString.split(',');
161+
const allowedDomainsString = system.get(AppSystemProp.ALLOWED_DOMAINS);
156162

157-
if (allowedDomains.includes(origin as string)) {
158-
return callback(null, true);
163+
if (allowedDomainsString) {
164+
if (allowedDomainsString === '*') {
165+
return callback(null, true);
166+
}
167+
168+
const allowedDomains = allowedDomainsString.split(',');
169+
170+
if (allowedDomains.includes(origin as string)) {
171+
return callback(null, true);
172+
}
159173
}
160-
}
161174

162-
return callback(null, false);
163-
},
164-
exposedHeaders: ['*'],
165-
methods: ['*'],
166-
credentials: true,
175+
return callback(null, false);
176+
},
177+
exposedHeaders: ['*'],
178+
methods: ['*'],
179+
credentials: true,
180+
});
167181
});
168182

169183
await app.register(fastifySocketIO, {

packages/server/api/src/app/flow-template/cloud-template.controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export const cloudTemplateController: FastifyPluginAsyncTypebox = async (
5353
{
5454
config: {
5555
allowedPrincipals: ALL_PRINCIPAL_TYPES,
56+
bypassCorsPlugin: true,
5657
skipAuth: true,
5758
},
5859
schema: {
@@ -94,6 +95,7 @@ export const cloudTemplateController: FastifyPluginAsyncTypebox = async (
9495
{
9596
config: {
9697
allowedPrincipals: ALL_PRINCIPAL_TYPES,
98+
bypassCorsPlugin: true,
9799
skipAuth: true,
98100
},
99101
schema: {

packages/server/api/types/fastify.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ declare module 'fastify' {
2323
allowedPrincipals?: PrincipalType[];
2424
rawBody?: boolean;
2525
skipAuth?: boolean;
26+
bypassCorsPlugin?: boolean;
2627
scope?: EndpointScope;
2728
permission?: Permission;
2829
}

0 commit comments

Comments
 (0)