Skip to content

Commit 63804fb

Browse files
authored
Allow preflight requests for cloud template endpoints (#1709)
Fixes OPS-3165. <img width="1556" height="588" alt="image" src="https://github.com/user-attachments/assets/cf8be42b-a2ee-418c-a74e-774ed655e6e4" />
1 parent a581f2d commit 63804fb

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import {
44
} from '@fastify/type-provider-typebox';
55
import { AppSystemProp, logger, system } from '@openops/server-shared';
66
import { ALL_PRINCIPAL_TYPES, OpenOpsId } from '@openops/shared';
7-
import { allowAllOriginsHookHandler } from '../helper/allow-all-origins-hook-handler';
7+
import {
8+
allowAllOriginsHookHandler,
9+
registerOptionsEndpoint,
10+
} from '../helper/allow-all-origins-hook-handler';
811
import { getVerifiedUser } from '../user-info/cloud-auth';
912
import { flowTemplateService } from './flow-template.service';
1013

@@ -26,6 +29,8 @@ export const cloudTemplateController: FastifyPluginAsyncTypebox = async (
2629
// cloud templates are available on any origin
2730
app.addHook('onRequest', allowAllOriginsHookHandler);
2831

32+
registerOptionsEndpoint(app);
33+
2934
app.get(
3035
'/',
3136
{

packages/server/api/src/app/helper/allow-all-origins-hook-handler.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { onRequestHookHandler } from 'fastify/types/hooks';
1+
import { ALL_PRINCIPAL_TYPES } from '@openops/shared';
2+
import { FastifyInstance, onRequestHookHandler } from 'fastify';
23

34
export const allowAllOriginsHookHandler: onRequestHookHandler = (
45
request,
@@ -19,9 +20,20 @@ export const allowAllOriginsHookHandler: onRequestHookHandler = (
1920

2021
void reply.header('Access-Control-Allow-Credentials', 'true');
2122

22-
if (request.method === 'OPTIONS') {
23-
return void reply.status(204).send();
24-
}
25-
2623
done();
2724
};
25+
26+
export function registerOptionsEndpoint(app: FastifyInstance) {
27+
app.options(
28+
'*',
29+
{
30+
config: {
31+
allowedPrincipals: ALL_PRINCIPAL_TYPES,
32+
skipAuth: true,
33+
},
34+
},
35+
(_request, reply) => {
36+
return reply.status(204).send();
37+
},
38+
);
39+
}

packages/server/api/src/app/user-info/user-info.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
22
import { AppSystemProp, logger, system } from '@openops/server-shared';
33
import { ALL_PRINCIPAL_TYPES } from '@openops/shared';
4-
import { allowAllOriginsHookHandler } from '../helper/allow-all-origins-hook-handler';
4+
import {
5+
allowAllOriginsHookHandler,
6+
registerOptionsEndpoint,
7+
} from '../helper/allow-all-origins-hook-handler';
58
import { getVerifiedUser } from './cloud-auth';
69

710
export const userInfoModule: FastifyPluginAsyncTypebox = async (app) => {
@@ -23,6 +26,7 @@ export const userInfoController: FastifyPluginAsyncTypebox = async (app) => {
2326

2427
// user-info is available on any origin
2528
app.addHook('onRequest', allowAllOriginsHookHandler);
29+
registerOptionsEndpoint(app);
2630

2731
app.get(
2832
'/',

0 commit comments

Comments
 (0)