diff --git a/csp/tests/test_utils.py b/csp/tests/test_utils.py index 4bb0659..87b8908 100644 --- a/csp/tests/test_utils.py +++ b/csp/tests/test_utils.py @@ -249,6 +249,20 @@ def test_require_sri_for(): policy_eq("default-src 'self'; require-sri-for script", policy) +@override_settings(CSP_REQUIRE_TRUSTED_TYPES_FOR=["'script'"]) +def test_require_trusted_types_for(): + policy = build_policy() + policy_eq("default-src 'self'; require-trusted-types-for 'script'", policy) + + +@override_settings(CSP_TRUSTED_TYPES=["strictPolicy", "laxPolicy", + "default", "'allow-duplicates'"]) +def test_trusted_types(): + policy = build_policy() + policy_eq("default-src 'self'; trusted-types strictPolicy laxPolicy " + + "default 'allow-duplicates'", policy) + + @override_settings(CSP_UPGRADE_INSECURE_REQUESTS=True) def test_upgrade_insecure_requests(): policy = build_policy() diff --git a/csp/utils.py b/csp/utils.py index d960f30..a0d5c31 100644 --- a/csp/utils.py +++ b/csp/utils.py @@ -45,6 +45,11 @@ def from_settings(): 'report-uri': getattr(settings, 'CSP_REPORT_URI', None), 'report-to': getattr(settings, 'CSP_REPORT_TO', None), 'require-sri-for': getattr(settings, 'CSP_REQUIRE_SRI_FOR', None), + #trusted Types Directives + 'require-trusted-types-for': getattr( + settings, + 'CSP_REQUIRE_TRUSTED_TYPES_FOR', None), + 'trusted-types': getattr(settings, 'CSP_TRUSTED_TYPES', None), # Other Directives 'upgrade-insecure-requests': getattr( settings, 'CSP_UPGRADE_INSECURE_REQUESTS', False), diff --git a/docs/configuration.rst b/docs/configuration.rst index 8056f6d..7f1941e 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -95,6 +95,14 @@ These settings affect the policy in the header. The defaults are in ``CSP_UPGRADE_INSECURE_REQUESTS`` Include ``upgrade-insecure-requests`` directive. A boolean. *False* See: upgrade-insecure-requests_ +``CSP_REQUIRE_TRUSTED_TYPES_FOR`` + Include ``reuire-trusted-types-for`` directive. A tuple or list. *None* + Valid values: ``'script'`` + Note: This doesn't use default-src as a fall-back. +``CSP_TRUSTED_TYPES`` + Include ``trusted-types`` directive. A tuple of list. *This header is empty by default* + Valid values: a list of allowed policy names that may include ``default`` and/or ``'allow-duplicates'`` + Note: This doesn't use default-src as a fall-back. ``CSP_BLOCK_ALL_MIXED_CONTENT`` Include ``block-all-mixed-content`` directive. A boolean. *False* See: block-all-mixed-content_ diff --git a/docs/index.rst b/docs/index.rst index 5f8cad4..5597cfb 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -23,6 +23,7 @@ Contents: configuration decorators nonce + trusted_types reports contributing diff --git a/docs/trusted_types.rst b/docs/trusted_types.rst new file mode 100644 index 0000000..638536c --- /dev/null +++ b/docs/trusted_types.rst @@ -0,0 +1,137 @@ +=================================== +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: + * ``