Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion app/apps/server/communication/uikit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from 'express';
import cors from 'cors';
import rateLimit from 'express-rate-limit';
import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
Expand All @@ -14,6 +15,25 @@ const apiServer = express();

apiServer.disable('x-powered-by');

let corsEnabled = false;
let allowListOrigins = [];

settings.get('API_Enable_CORS', (_, value) => { corsEnabled = value; });

settings.get('API_CORS_Origin', (_, value) => {
allowListOrigins = value ? value.trim().split(',').map((origin) => String(origin).trim().toLocaleLowerCase()) : [];
});

const corsOptions = {
origin: (origin, callback) => {
if (!origin || (corsEnabled && (allowListOrigins.includes('*') || allowListOrigins.includes(origin))) || origin === settings.get('Site_Url')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be aware there's an open PR #22106 that aims to modify Site_Url setting)

callback(null, true);
} else {
callback('Not allowed by CORS', false);
}
},
};

WebApp.connectHandlers.use(apiServer);

// eslint-disable-next-line new-cap
Expand Down Expand Up @@ -59,7 +79,7 @@ router.use((req, res, next) => {
next();
});

apiServer.use('/api/apps/ui.interaction/', router);
apiServer.use('/api/apps/ui.interaction/', cors(corsOptions), router);

const getPayloadForType = (type, req) => {
if (type === UIKitIncomingInteractionType.BLOCK) {
Expand Down