diff --git a/default/config.yaml b/default/config.yaml index d99ca0e98..ae672eff8 100644 --- a/default/config.yaml +++ b/default/config.yaml @@ -71,6 +71,25 @@ basicAuthUser: password: "password" # Enables CORS proxy middleware enableCorsProxy: false +# CORS settings (applied to all routes) +cors: + # Enable or disable CORS middleware + enabled: true + # Allowed origins. Use "null" to match the default browser file origin. + # You can set "*" to allow any origin, or a list of allowed origins. + origin: + - "null" + # Allowed methods + methods: + - "OPTIONS" + # Allowed request headers (optional) + allowedHeaders: [] + # Exposed response headers (optional) + exposedHeaders: [] + # Allow credentials (cookies, authorization headers) + credentials: false + # Preflight cache max age in seconds (optional) + maxAge: null # -- REQUEST PROXY CONFIGURATION -- requestProxy: # If a proxy is enabled, all outgoing HTTP/HTTPS requests will be routed through it. diff --git a/src/server-main.js b/src/server-main.js index 0d4c112cb..5869f741f 100644 --- a/src/server-main.js +++ b/src/server-main.js @@ -103,12 +103,32 @@ app.use(bodyParser.json({ limit: '500mb' })); app.use(bodyParser.urlencoded({ extended: true, limit: '500mb' })); // CORS Settings // -const CORS = cors({ - origin: 'null', - methods: ['OPTIONS'], -}); +const corsEnabled = getConfigValue('cors.enabled', true, 'boolean'); +if (corsEnabled) { + const corsOrigin = getConfigValue('cors.origin', 'null'); + const corsMethods = getConfigValue('cors.methods', ['OPTIONS']); + const corsAllowedHeaders = getConfigValue('cors.allowedHeaders', []); + const corsExposedHeaders = getConfigValue('cors.exposedHeaders', []); + const corsCredentials = getConfigValue('cors.credentials', false, 'boolean'); + const corsMaxAge = getConfigValue('cors.maxAge', null, 'number'); -app.use(CORS); + /** @type {cors.CorsOptions} */ + const corsOptions = { + origin: corsOrigin, + methods: corsMethods, + credentials: corsCredentials, + }; + if (Array.isArray(corsAllowedHeaders) && corsAllowedHeaders.length > 0) { + corsOptions.allowedHeaders = corsAllowedHeaders; + } + if (Array.isArray(corsExposedHeaders) && corsExposedHeaders.length > 0) { + corsOptions.exposedHeaders = corsExposedHeaders; + } + if (corsMaxAge !== null && Number.isInteger(corsMaxAge)) { + corsOptions.maxAge = corsMaxAge; + } + app.use(cors(corsOptions)); +} if (cliArgs.listen && cliArgs.basicAuthMode) { app.use(basicAuthMiddleware);