Overview
Content Security Policy (CSP) is a critical security layer that helps detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS). Strict CSP directives often block MoEngage assets such as On-Site Messaging (OSM), Web Personalization (WebP), and Cards because these components traditionally rely on inline event handlers and dynamic script execution. To ensure seamless integration without compromising site security, the MoEngage Web SDK supports cryptographic nonces. When a nonce is provided during initialization, the SDK performs the following:- Attribute Injection: Automatically attaches the nonce to all dynamically created
<script>tags. - Handler Refactoring: Refactors inline event handlers of assets created by the SDK (for example,
onclick) into CSP-compliant internal scripts.
CSP Header Configuration
Before configuring the nonce, ensure MoEngage domains are whitelisted in your CSP header (for example, script-src, connect-src, and img-src). For more information, refer to Web SDK Integration.
Initialization and Configuration
To enable CSP support, the generated nonce must be passed to both the Web SDK integration script and the SDK initialization configuration.1. Web SDK Integration
Pass the nonce during the initialization phase so the SDK can apply it to subsequent DOM operations and component rendering.2. Web Personalization (WebP)
If utilizing Web Personalization, the nonce must be appended to the WebP script URL to prevent the browser from blocking personalized content deployed to the DOM.Update the script below with your specific Data Center value and app_id. For more information, refer to Web Personalization.
Framework and Platform Compatibility
This feature is available in the following Framework:- NPM
- GTM
- Flutter Web
Verification
You can verify the Implementation success by analyzing the console as mentioned below: Console Analysis: If the nonce is missing or mismatched, the browser logs aRefused to execute inline event handler because it violates the following Content Security Policy directive…” violation.