=================================== Implementing Trusted Types with CSP =================================== ``DOM Cross-site Scripting`` ============================ Cross-site scripting (XSS) is one of the most prevalent vulnerabilities on the web. Nonce-based CSP is used to prevent server-side XSS. Trusted Types are used to prevent client-side or DOM-XSS_. Trusted Types rely on the browser to enforce the policy that is provided to it. Currently, Trusted Types are supported on Chrome 83 and Android Webview. Many browsers are in the process of adding support. Check back for updated compatibility_. Follow the simple steps below to make your web application Trusted Types compliant. ``Step 1: Enable Trusted Types and Report Only Mode`` ===================================================== Trusted Types require data to be processed before being sent to a risky "sink" where DOM XSS might occur, such as when assigning to Element.innerHTML or calling document.write. When enforced, Trusted Types will tell the browser to block any data that is not properly processed. In order to avoid this, you must fix offending parts of your code. To see where adjustments will be required, turn on trusted types and report only mode. Configure django-csp so that ``CSP_REQUIRE_TRUSTED_TYPES_FOR`` is set to *‘script’*. Configure django-csp so that ``CSP_REPORT_ONLY`` is set to *True*. Configure django-csp so that ``CSP_REPORT_URI`` is set to an app or CSP report processing service that you control. Now trusted types violations will be reported to your ``CSP_REPORT_URI`` without blocking any of your application’s functionalities. ``Step 2: Fixing Trusted Types Violations`` =========================================== There are four ways to resolve trusted types violations. They are explained here in order of preference. Rewrite the Code ---------------- It may be possible for your code to be rewritten without using dangerous functions. For example, instead of dynamically placing an image using the dangerous ``innerHTML`` sink, the image could be created with ``document.createElement`` and placed using the ``appendChild`` function. Rewriting may be possible for any of the dangerous sinks, which are listed here. * Script manipulation: * ``