diff --git a/server/src/index.ts b/server/src/index.ts index 420cea8f..c7b7878a 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -31,6 +31,12 @@ export const corsOptions = { credentials: true, }; +if (corsOptions.origins.length > 1 && process.env.NODE_ENV === "production") { + console.warn( + "WARNING: socket.io only supports one cors origin, therefore only first origin will be registered.", + ); +} + app.use(cors(corsOptions)); app.use(csrf(corsOptions)); diff --git a/server/src/lib/cross-origin/share.ts b/server/src/lib/cross-origin/share.ts index b98e89d7..3f5402bc 100644 --- a/server/src/lib/cross-origin/share.ts +++ b/server/src/lib/cross-origin/share.ts @@ -1,4 +1,4 @@ -type Config = { +export type Config = { origins: string[]; // allowed origins methods?: string[]; // Access-Control-Allow-Methods credentials?: boolean; // Access-Control-Allow-Credentials @@ -47,4 +47,4 @@ function assertValidConfig(config: Config) { } } -export { validateConfig, type Config }; +export { validateConfig }; diff --git a/server/src/lib/socket/socket.ts b/server/src/lib/socket/socket.ts index 423e1008..eac76d2b 100644 --- a/server/src/lib/socket/socket.ts +++ b/server/src/lib/socket/socket.ts @@ -3,12 +3,17 @@ import type { Message, UserID } from "common/types"; import type { CorsOptions } from "cors"; import { type Socket, Server as SocketIOServer } from "socket.io"; import { getUserIdFromToken } from "../../firebase/auth/db"; +import type { Config as CorsConfig } from "../cross-origin/share"; const users = new Map(); -export function initializeSocket(server: Server, corsOptions: CorsOptions) { +export function initializeSocket(server: Server, corsOptions: CorsConfig) { + const cors: CorsOptions = { + origin: corsOptions.origins[0], + ...corsOptions, + }; const io = new SocketIOServer(server, { - cors: corsOptions, + cors, connectionStateRecovery: {}, });