You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Content Security Policy informs the client (browser) where your page loads resources from. Setting this can make your app more secure by declaring trusted sources for your resources. For more information, see the W3C recommendation [Content Security Policy Level 2](https://www.w3.org/TR/CSP2/).
324
+
A Content Security Policy (CSP) informs the client (browser) where your page loads resources from. Setting this can make your app more secure by declaring trusted sources for your resources. For more information, see the W3C recommendation [Content Security Policy Level 2](https://www.w3.org/TR/CSP2/).
325
325
326
326
{{% alert color="info" %}}
327
-
For full CSP support including nonce-based CSP, use the [Headers](/refguide/configuration/#headers) custom runtime setting instead of the HTTP Headers UI. The custom runtime setting method is recommended as it provides more comprehensive CSP capabilities. For detailed implementation guidance, see [Content Security Policy](/howto/security/csp/).
327
+
For complete CSP support, including nonce-based CSP, use the [Headers](/refguide/configuration/#headers) custom runtime setting instead of the HTTP Headers UI. For detailed implementation instructions, see [Content Security Policy](/howto/security/csp/).
328
328
{{% /alert %}}
329
329
330
330
The process for setting a full content security policy depends on what your app does. However, a starting point that declares the content security policy that works with a basic Mendix app is given below:
Copy file name to clipboardExpand all lines: content/en/docs/howto/security/csp.md
+48-40Lines changed: 48 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -176,38 +176,38 @@ After you finish testing locally, remember to remove the line of code in the `he
176
176
177
177
## CSP Support in Java Request Handlers
178
178
179
-
If you are developing Marketplace modules or custom Java actions that include request handlers, you may need to implement CSP support to ensure compatibility with strict CSP policies. This includes support for CSP Level 2+ features like nonces for inline scripts and styles.
179
+
When developing Marketplace modules or custom Java actions that include request handlers, you may need to implement CSP support to ensure compatibility with strict CSP policies. This includes support for CSP Level 2+ features such as nonces for inline scripts and styles.
180
180
181
181
{{% alert color="info" %}}
182
182
CSP support is only relevant for request handlers that serve static content such as HTML pages, not for API endpoints that return JSON or other data formats.
183
183
{{% /alert %}}
184
184
185
-
This section describes how to properly handle CSP headers in your Java request handlers when serving HTML content.
185
+
This section describes how to properly handle CSP headers in Java request handlers when serving HTML content.
186
186
187
187
### Available CSP APIs
188
188
189
-
Mendix provides two APIs for CSP support in Java request handlers:
189
+
Mendix provides two APIs to support CSP in Java request handlers:
190
190
191
191
#### IMxRuntimeResponse Methods
192
192
193
-
The `IMxRuntimeResponse` interface provides basic CSP methods:
193
+
The `IMxRuntimeResponse` interface provides these basic CSP methods:
194
194
195
-
* `addContentSecurityPolicyHeader()` - Adds the Content-Security-Policy header as configured in the application
196
-
* `getNonce()` - Returns a uniquely generated secure nonce for the response that can be used in CSP directives
197
-
* `addHeader(String key, String value)` - Adds a custom header to the response
195
+
* `addContentSecurityPolicyHeader()` – Adds the Content-Security-Policy header as configured in the application
196
+
* `getNonce()` – Returns a secure, uniquely generated nonce for the response that you can use in CSP directives
197
+
* `addHeader(String key, String value)` – Adds a custom header to the response
198
198
199
199
#### CspHelper Interface (Recommended)
200
200
201
-
The `CspHelper` interface provides additional utility methods for more sophisticated CSP handling:
201
+
The `CspHelper` interface provides additional utility methods for more advanced CSP handling:
202
202
203
-
* `getTemplate()` - Get the template used for the Content-Security-Policy header value
204
-
* `getNonce(IMxRuntimeResponse response)` - Get the generated nonce of the current HTTP response
205
-
* `hasNonce(IMxRuntimeResponse response)` - Returns true if the configured CSP template contains the `{{ NONCE }}` placeholder, for example: `Content-Security-Policy: script-src 'nonce-{{ NONCE }}'`
206
-
* `addHeader(IMxRuntimeResponse response)` - Add Content-Security-Policy header to the response using the configured template
203
+
* `getTemplate()` – Gets the template used for the Content-Security-Policy header value
204
+
* `getNonce(IMxRuntimeResponse response)` – Gets the generated nonce of the current HTTP response
205
+
* `hasNonce(IMxRuntimeResponse response)` – Returns true if the configured CSP template contains the `{{ NONCE }}` placeholder. For example: `Content-Security-Policy: script-src 'nonce-{{ NONCE }}'`
206
+
* `addHeader(IMxRuntimeResponse response)` – Adds Content-Security-Policy header to the response using the configured template
207
207
208
208
### Example Implementation
209
209
210
-
Here's how to implement CSP support in a Java request handler using the `CspHelper`:
210
+
Here's how to implement CSP support in a Java request handler using the `CspHelper` interface:
211
211
212
212
```java
213
213
package your.module.requesthandlers;
@@ -247,24 +247,24 @@ public class YourRequestHandler extends RequestHandler {
247
247
html.append("<!DOCTYPE html>\n");
248
248
html.append("<html>\n");
249
249
html.append("<head>\n");
250
-
html.append("<title>Your Module</title>\n");
250
+
html.append("<title>Your Module</title>\n");
251
251
252
252
// Only use nonce if it's configured in the CSP template
@@ -277,45 +277,53 @@ public class YourRequestHandler extends RequestHandler {
277
277
278
278
When implementing CSP support in your request handlers, follow these best practices:
279
279
280
-
1.**Use CspHelper for conditional nonce support**- Always check if nonce is configured before using it:
280
+
***Use `CspHelper`for conditional nonce support** – Always check if nonce is configured before using it:
281
+
281
282
```java
282
283
if (Core.csp().hasNonce(response)) {
283
-
String nonce = Core.csp().getNonce(response);
284
-
// Use nonce for inline content
284
+
String nonce = Core.csp().getNonce(response);
285
+
// Use nonce for inline content
285
286
} else {
286
-
// Use external resources or alternative approach
287
+
// Use external resources or alternative approach
287
288
}
288
289
```
289
290
290
-
2.**Always add CSP headers**- Use `Core.csp().addHeader(response)` to ensure your module respects the application's CSP configuration when serving HTML content.
291
+
***Always add CSP headers**– Use `Core.csp().addHeader(response)` to ensure your module respects the application's CSP configuration when serving HTML content.
291
292
292
-
3. **CSP is only needed for HTML content** - Only implement CSP support in request handlers that serve HTML pages. API endpoints returning JSON, XML, or other data formats do not need CSP headers.
293
+
* **CSP is only needed for HTML content** – Only implement CSP support in request handlers that serve HTML pages. API endpoints returning JSON, XML, or other data formats do not need CSP headers.
293
294
294
-
4. **Avoid inline scripts and styles when possible** - Prefer external files that can be loaded via `'self'` directive.
295
+
* **Avoid inline scripts and styles when possible** – Use external files that can be loaded via `'self'` directive.
295
296
296
-
5. **Test with strict CSP** - Test your request handlers with `default-src: 'self'` to ensure they work with the strictest CSP settings.
297
+
* **Test with strict CSP** – Test your request handlers with `default-src: 'self'` to ensure they work with the strictest CSP settings.
297
298
298
299
### Common CSP Issues in Request Handlers
299
300
300
301
When working with CSP in request handlers, you may encounter these common issues:
301
302
302
303
#### Base64 Images
303
-
If your request handler generates inline Base64 images, these will be blocked by strict CSP. Consider these alternatives:
304
-
- Serving images as separate endpoints
305
-
- Using external image hosting
306
-
- Adding `data:` to `img-src` directive (less secure)
304
+
305
+
Strict CSP blocks inline Base64 images that your request handler generates. Consider these alternatives:
306
+
307
+
* Serve images as separate endpoints
308
+
* Use external image hosting
309
+
* Add `data:` to `img-src` directive (less secure)
307
310
308
311
#### Dynamic Script Generation
312
+
309
313
Avoid generating `<script>` tags dynamically without nonces. Instead:
310
-
- Use the provided nonce for any inline scripts
311
-
- Move logic to external JavaScript files
312
-
- Use data attributes and external scripts to handle dynamic behavior
314
+
315
+
* Use the provided nonce for any inline scripts
316
+
* Move logic to external JavaScript files
317
+
* Use data attributes and external scripts to handle dynamic behavior
313
318
314
319
#### Third-party Resources
315
-
If your module loads external resources, ensure they're allowed by the CSP or provide configuration options for developers to whitelist them.
320
+
321
+
If your module loads external resources, make sure they are allowed by the CSP or provide configuration options for developers to whitelist them.
316
322
317
323
#### Error Handling
324
+
318
325
When CSP violations occur, implement proper error handling:
326
+
319
327
```java
320
328
// Log CSP-related errors for debugging
321
329
if (Core.csp().hasNonce(response)) {
@@ -327,7 +335,7 @@ if (Core.csp().hasNonce(response)) {
327
335
328
336
## Enabling the Header in the Cloud
329
337
330
-
There are two ways to enable the header in the Cloud:
338
+
There are two ways to enable the header in the cloud:
331
339
332
-
1.Using the [Headers](/refguide/configuration/#headers) custom runtime setting (Recommended ✅).Usethisif you need nonce-based CSPsupport. You can configurethisin the Developer Portal under [Custom Runtime Settings](/developerportal/deploy/environments-details/#custom-runtime-settings).
333
-
2.Using the [HTTP Headers](/developerportal/deploy/environments-details/#http-headers) UIin the *Environment Details*section. Can be used for basic CSP support.
340
+
1. **[Headers](/refguide/configuration/#headers) custom runtime setting (Recommended)**: Use this if you need nonce-based CSP support. Configure this in the Developer Portal under [Custom Runtime Settings](/developerportal/deploy/environments-details/#custom-runtime-settings).
341
+
2. **HTTP Headers UI:** This method works for basic CSP support. For more details, refer to the [HTTP Headers](/developerportal/deploy/environments-details/#http-headers) section of *Environment Details*.
Copy file name to clipboardExpand all lines: content/en/docs/refguide/runtime/custom-settings/_index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -72,7 +72,7 @@ The following custom settings can be configured:
72
72
| <aid="ScheduledEventExecution"href="#ScheduledEventExecution">ScheduledEventExecution</a> | Specify which scheduled events should be executed. Choices are `ALL`, `NONE`, or `SPECIFIED`. In the case of `SPECIFIED`, enumerate the scheduled events using the `MyScheduledEvents` configuration option described below. {{% alert color="warning" %}}This setting cannot be configured when running locally. To enable and disable scheduled events when running locally, please use the 'Enabled' setting on the [Scheduled Events execution properties](/refguide/scheduled-events/) in Studio Pro.{{% /alert %}} | NONE |
73
73
| <aid="SessionKeepAliveUpdatesInterval"href="#SessionKeepAliveUpdatesInterval">SessionKeepAliveUpdatesInterval</a> | Defines how often a runtime writes session LastActive dates in its memory back to the database. | one sixth of the value configured for the `SessionTimeout` setting; if the `SessionTimeout` is not set, this value defaults to 100000 (100 seconds) |
74
74
| <aid="SessionTimeout"href="#SessionTimeout">SessionTimeout</a> | Defines after how much time a session becomes invalid (in milliseconds). After that timeout, a session becomes eligible for removal. The session will not be destroyed until the next time a scheduled task runs to clean up the active sessions. <br> {{% alert color="warning" %}} Logging out can also be triggered by a query to the runtime after the session becomes eligible for removal. Navigating between pages is not enough to trigger a query to the runtime. To force a query to the runtime, use microflows. For example, create a microflow that shows the Home page, then configure your app's navigation to call this microflow rather than relying on the navigation to directly show the page itself. This will ensure the runtime is queried and the user is logged out of their session. {{% /alert %}} | 600000 (10 minutes) |
75
-
| <aid="Headers"href="#Headers">Headers</a> | A JSON object containing custom headers (key-value). For example, can be used to define a `Content-Security-Policy`, e.g.:`{ "Content-Security-Policy": "script-src 'nonce-{{ NONCE }}'" }`. |`{}`|
75
+
| <aid="Headers"href="#Headers">Headers</a> | A JSON object containing custom headers as key/value pairs, and can be used to define a `Content-Security-Policy`. For example,`{ "Content-Security-Policy": "script-src 'nonce-{{ NONCE }}'" }`. |`{}`|
76
76
| <aid="TaskQueueShutdownGracePeriod"href="#TaskQueueShutdownGracePeriod">TaskQueue.<wbr>ShutdownGracePeriod</a> | Time in ms to wait for task in a task queue to finish when shutting down. | 10000 (10 seconds) |
77
77
| <aid="TempPath"href="#TempPath">TempPath</a> | The location of the temporary files. |[deployment folder]\data\tmp |
78
78
| <aid="TrackWebServiceUserLastLogin"href="#TrackWebServiceUserLastLogin">TrackWebServiceUserLastLogin</a> | Defines whether to update a user's `LastLogin` field on each login when logging in to a published REST, OData, or web service. When this happens a database update query has to be sent and this can have performance consequences on heavy load systems. When this setting is set to false, no database interaction is necessary. | true |
0 commit comments