|
| 1 | +--- |
| 2 | +title: "Configuring CORS in the Mendix Runtime" |
| 3 | +linktitle: "Configuring CORS" |
| 4 | +url: /refguide/configure-cors/ |
| 5 | +description: "Describes how to enable Cross-Origin Resource Sharing (CORS) in the Mendix Runtime, allowing browser-based clients on other domains to access the runtime." |
| 6 | +--- |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +Cross-Origin Resource Sharing (CORS) is a mechanism that allows a web application running on one domain to make requests to a server on a different domain. By default, browsers block such cross-origin requests for security reasons. If your Mendix front end is hosted on a different domain than the Mendix Runtime (for example, when using a separate single-page application or a microfrontend architecture), you need to configure CORS so the browser permits these requests. |
| 11 | + |
| 12 | +This document describes the custom runtime settings required to enable CORS in the Mendix Runtime. |
| 13 | + |
| 14 | +If you also enable CORS on a [published REST service](/refguide/published-rest-service/) and configure [Allowed Origins](/refguide/cors-settings/), access is granted to both the origins configured on the published REST service and the origins configured through the runtime settings described in this document. |
| 15 | + |
| 16 | +{{% alert color="info" %}} |
| 17 | + |
| 18 | +This feature is only supported in Mendix 11.10 and later. |
| 19 | + |
| 20 | +{{% /alert %}} |
| 21 | + |
| 22 | +## Settings to Configure {#settings} |
| 23 | + |
| 24 | +To enable CORS, configure the following custom runtime settings. For general information on how to set custom runtime settings, see [Runtime Customization](/refguide/custom-settings/). |
| 25 | + |
| 26 | +### Runtime Settings |
| 27 | + |
| 28 | +| Name | Value | Description | |
| 29 | +| --- | --- | --- | |
| 30 | +| `com.mendix.core.SameSiteCookies` | `None` | Allows cookie sharing between the runtime origin and the client origin. This is required for cross-origin authentication to work correctly. The value needs to be set to "None", not left with no value. | |
| 31 | + |
| 32 | +### Custom HTTP Response Headers |
| 33 | + |
| 34 | +In addition to the runtime settings above, you need to set the following custom HTTP response headers via the `Headers` setting: |
| 35 | + |
| 36 | +| Header | Value | Description | |
| 37 | +| --- | --- | --- | |
| 38 | +| `Access-Control-Allow-Credentials` | `true` | Indicates that the server allows credentials (cookies, authorization headers) to be included in cross-origin requests. | |
| 39 | +| `Access-Control-Allow-Headers` | `Content-Type, x-csrf-token` | Specifies which HTTP headers can be used in the actual request. Expand this list if your application uses additional custom headers. | |
| 40 | +| `Access-Control-Allow-Methods` | `POST, GET, OPTIONS` | Specifies the HTTP methods allowed when accessing the resource. Expand this list if your application uses additional methods (for example, `PUT` or `DELETE`). | |
| 41 | +| `Access-Control-Allow-Origin` | Your client domain (for example, `https://my-app.example.com`) | The origin from which the client application is served. This must match the exact domain, including the scheme and port. | |
| 42 | + |
| 43 | +{{% alert color="info" %}} |
| 44 | +If you change these settings, you need to restart your app to apply the changes. |
| 45 | +{{% /alert %}} |
| 46 | + |
| 47 | +## Example `m2ee.yaml` Configuration {#example} |
| 48 | + |
| 49 | +The following example shows how to configure CORS in an `m2ee.yaml` file. Replace `YOUR_ORIGIN` with the actual domain of your client application (for example, `https://my-app.example.com`): |
| 50 | + |
| 51 | +```yaml |
| 52 | +mxruntime: |
| 53 | + com.mendix.core.SameSiteCookies: None |
| 54 | + Headers: |
| 55 | + "Access-Control-Allow-Credentials": "true" |
| 56 | + "Access-Control-Allow-Headers": "Content-Type, x-csrf-token" |
| 57 | + "Access-Control-Allow-Methods": "POST, GET, OPTIONS" |
| 58 | + "Access-Control-Allow-Origin": "YOUR_ORIGIN" |
| 59 | +``` |
| 60 | +
|
| 61 | +## Troubleshooting |
| 62 | +
|
| 63 | +If CORS is not working as expected, check the following: |
| 64 | +
|
| 65 | +* **Browser console errors** — Look for CORS-related error messages in the browser developer tools console. These typically indicate which header is missing or misconfigured. |
| 66 | +* **Origin mismatch** — Ensure the value of `Access-Control-Allow-Origin` exactly matches the origin shown in the browser error, including the scheme (`https://`) and port number (if applicable). |
| 67 | +* **Missing `SameSiteCookies` setting** — Without `com.mendix.core.SameSiteCookies` set to `None`, cookies will not be sent on cross-origin requests, which can cause authentication failures. |
| 68 | +* **HTTPS requirement** — When `SameSiteCookies` is set to `None`, the `Secure` attribute is automatically added to cookies, meaning both the runtime and the client must be served over HTTPS. |
0 commit comments