{"title":"Content Security Policy","version":"6.0","locale":"el","docname":"ref/csp","url":"/el/6.0/ref/csp/","canonical":"https://djangodocs.dev/el/6.0/ref/csp/","summary":"New in Django 6.0 Content Security Policy (CSP) is a web security standard that helps prevent content injection attacks by restricting the sources from which…","html":"<h1>Content Security Policy<a class=\"heading-anchor\" href=\"#content-security-policy\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"version-note version-added\" data-version=\"6.0\">\n<p class=\"version-note-title\">New in Django 6.0</p></aside>\n<p id=\"module-django.middleware.csp\">Content Security Policy (CSP) is a web security standard that helps prevent\ncontent injection attacks by restricting the sources from which content can be\nloaded. It plays an important role in a comprehensive <a class=\"reference internal\" href=\"/el/6.0/topics/security/#security-csp\"><span class=\"std std-ref\">security strategy</span></a>.</p>\n<p>For configuration instructions in a Django project, see the <a class=\"reference internal\" href=\"/el/6.0/howto/csp/#csp-config\"><span class=\"std std-ref\">Using CSP</span></a> documentation. For an HTTP guide about CSP, see the <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP\">MDN Guide on\nCSP</a>.</p>\n<section id=\"overview\">\n<span id=\"csp-overview\"></span><h2>Overview<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The <a class=\"reference external\" href=\"https://www.w3.org/TR/CSP3/\">Content-Security-Policy specification</a>\ndefines two complementary headers:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Security-Policy</span></code>: Enforces the CSP policy, blocking content that\nviolates the defined directives.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Security-Policy-Report-Only</span></code>: Reports CSP violations without\nblocking content, allowing for non-intrusive testing.</p></li>\n</ul>\n<p>Each policy is composed of one or more directives and their values, which\ntogether instruct the browser on how to handle specific types of content.</p>\n<p>When the <a class=\"reference internal\" href=\"/el/6.0/ref/middleware/#django.middleware.csp.ContentSecurityPolicyMiddleware\" title=\"django.middleware.csp.ContentSecurityPolicyMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ContentSecurityPolicyMiddleware</span></code></a> is\nenabled, Django automatically builds and attaches the appropriate headers to\neach response based on the configured <a class=\"reference internal\" href=\"#csp-settings\"><span class=\"std std-ref\">settings</span></a>, unless\nthey have already been set by another layer.</p>\n</section>\n<section id=\"settings\">\n<span id=\"csp-settings\"></span><h2>Settings<a class=\"heading-anchor\" href=\"#settings\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The <a class=\"reference internal\" href=\"/el/6.0/ref/middleware/#django.middleware.csp.ContentSecurityPolicyMiddleware\" title=\"django.middleware.csp.ContentSecurityPolicyMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ContentSecurityPolicyMiddleware</span></code></a> is\nconfigured using the following settings:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-SECURE_CSP\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CSP</span></code></a>: defines the <strong>enforced Content Security Policy</strong>.</p></li>\n<li><p><a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-SECURE_CSP_REPORT_ONLY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CSP_REPORT_ONLY</span></code></a>: defines a <strong>report-only Content Security\nPolicy</strong>.</p></li>\n</ul>\n<aside class=\"admonition-these-settings-can-be-used-independently-or-together admonition\">\n<p class=\"admonition-title\">These settings can be used independently or together</p>\n<ul class=\"simple\">\n<li><p>Use <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-SECURE_CSP\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CSP</span></code></a> alone to enforce a policy that has already been\ntested and verified.</p></li>\n<li><p>Use <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-SECURE_CSP_REPORT_ONLY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CSP_REPORT_ONLY</span></code></a> on its own to evaluate a new policy\nwithout disrupting site behavior. This mode does not block violations, it\nonly logs them. It’s useful for testing and monitoring, but provides no\nprotection against active threats.</p></li>\n<li><p>Use <em>both</em> to maintain an enforced baseline while experimenting with\nchanges. Even for well-established policies, continuing to collect reports\ncan help detect regressions, unexpected changes in behavior, or\npotential tampering in production environments.</p></li>\n</ul>\n</aside>\n</section>\n<section id=\"policy-violation-reports\">\n<span id=\"csp-reports\"></span><h2>Policy violation reports<a class=\"heading-anchor\" href=\"#policy-violation-reports\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When a CSP violation occurs, browsers typically log details to the developer\nconsole, providing immediate feedback during development. To also receive these\nreports programmatically, the policy must include a <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy#reporting_directives\">reporting directive</a>\nsuch as <code class=\"docutils literal notranslate\"><span class=\"pre\">report-uri</span></code> that specifies where violation data should be sent.</p>\n<p>Django supports configuring these directives via the\n<a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-SECURE_CSP_REPORT_ONLY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CSP_REPORT_ONLY</span></code></a> settings, but reports will only be issued by\nthe browser if the policy explicitly includes a valid reporting directive.</p>\n<p>Django does not provide built-in functionality to receive, store, or process\nviolation reports. To collect and analyze them, you must implement your own\nreporting endpoint or integrate with a third-party monitoring service.</p>\n</section>\n<section id=\"csp-constants\">\n<span id=\"id1\"></span><h2>CSP constants<a class=\"heading-anchor\" href=\"#csp-constants\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django provides predefined constants representing common CSP source expression\nkeywords such as <code class=\"docutils literal notranslate\"><span class=\"pre\">'self'</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">'none'</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">'unsafe-inline'</span></code>. These\nconstants are intended for use in the directive values defined in the settings.</p>\n<p>They are available through the <a class=\"reference internal\" href=\"#django.utils.csp.CSP\" title=\"django.utils.csp.CSP\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CSP</span></code></a> enum, and using\nthem is recommended over raw strings. This helps avoid common mistakes such as\ntypos, improper quoting, or inconsistent formatting, and ensures compliance\nwith the CSP specification.</p>\n<dl class=\"py class\" id=\"module-django.utils.csp\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">CSP</span></span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Enum providing standardized constants for common CSP source expressions.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.NONE\">\n<span class=\"sig-name descname\"><span class=\"pre\">NONE</span></span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.NONE\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\"><span class=\"pre\">'none'</span></code>. Blocks loading resources for the given directive.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.REPORT_SAMPLE\">\n<span class=\"sig-name descname\"><span class=\"pre\">REPORT_SAMPLE</span></span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.REPORT_SAMPLE\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\"><span class=\"pre\">'report-sample'</span></code>. Instructs the browser to include a sample\nof the violating code in reports. Note that this may expose sensitive\ndata.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.SELF\">\n<span class=\"sig-name descname\"><span class=\"pre\">SELF</span></span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.SELF\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\"><span class=\"pre\">'self'</span></code>. Allows loading resources from the same origin\n(same scheme, host, and port).</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.STRICT_DYNAMIC\">\n<span class=\"sig-name descname\"><span class=\"pre\">STRICT_DYNAMIC</span></span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.STRICT_DYNAMIC\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\"><span class=\"pre\">'strict-dynamic'</span></code>. Allows execution of scripts loaded by a\ntrusted script (e.g., one with a valid nonce or hash), without needing\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'unsafe-inline'</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.UNSAFE_EVAL\">\n<span class=\"sig-name descname\"><span class=\"pre\">UNSAFE_EVAL</span></span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.UNSAFE_EVAL\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\"><span class=\"pre\">'unsafe-eval'</span></code>. Allows use of <code class=\"docutils literal notranslate\"><span class=\"pre\">eval()</span></code> and similar\nJavaScript functions. Strongly discouraged.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.UNSAFE_HASHES\">\n<span class=\"sig-name descname\"><span class=\"pre\">UNSAFE_HASHES</span></span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.UNSAFE_HASHES\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\"><span class=\"pre\">'unsafe-hashes'</span></code>. Allows inline event handlers and some\n<code class=\"docutils literal notranslate\"><span class=\"pre\">javascript:</span></code> URIs when their content hashes match a policy rule.\nRequires CSP Level 3+.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.UNSAFE_INLINE\">\n<span class=\"sig-name descname\"><span class=\"pre\">UNSAFE_INLINE</span></span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.UNSAFE_INLINE\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\"><span class=\"pre\">'unsafe-inline'</span></code>. Allows execution of inline scripts,\nstyles, and <code class=\"docutils literal notranslate\"><span class=\"pre\">javascript:</span></code> URLs. Generally discouraged, especially for\nscripts.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.WASM_UNSAFE_EVAL\">\n<span class=\"sig-name descname\"><span class=\"pre\">WASM_UNSAFE_EVAL</span></span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.WASM_UNSAFE_EVAL\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\"><span class=\"pre\">'wasm-unsafe-eval'</span></code>. Permits compilation and execution of\nWebAssembly code without enabling <code class=\"docutils literal notranslate\"><span class=\"pre\">'unsafe-eval'</span></code> for scripts.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.NONCE\">\n<span class=\"sig-name descname\"><span class=\"pre\">NONCE</span></span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.NONCE\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Django-specific placeholder value (<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;&lt;CSP_NONCE_SENTINEL&gt;&quot;</span></code>) used in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">script-src</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">style-src</span></code> directives to activate nonce-based CSP.\nThis string is replaced at runtime by the\n<a class=\"reference internal\" href=\"/el/6.0/ref/middleware/#django.middleware.csp.ContentSecurityPolicyMiddleware\" title=\"django.middleware.csp.ContentSecurityPolicyMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ContentSecurityPolicyMiddleware</span></code></a> with a\nsecure, random nonce that is generated for each request. See detailed\nexplanation in <a class=\"reference internal\" href=\"#csp-nonce\"><span class=\"std std-ref\">Nonce usage</span></a>.</p>\n</dd></dl>\n\n</dd></dl>\n\n</section>\n<section id=\"module-django.views.decorators.csp\">\n<span id=\"decorators\"></span><h2>Decorators<a class=\"heading-anchor\" href=\"#module-django.views.decorators.csp\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django provides decorators to control the Content Security Policy headers on a\nper-view basis. These allow overriding or disabling the enforced or report-only\npolicy for specific views, providing fine-grained control when the global\nsettings are not sufficient. Applying these overrides fully replaces the base\nCSP: they do not merge with existing rules. They can be used alongside the\nconstants defined in <a class=\"reference internal\" href=\"#django.utils.csp.CSP\" title=\"django.utils.csp.CSP\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CSP</span></code></a>.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Προειδοποίηση</p>\n<p>Weakening or disabling a CSP policy on any page can compromise the security\nof the entire site. Because of the «same origin» policy, an attacker could\nexploit a vulnerability on one page to access other parts of the site.</p>\n</aside>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.decorators.csp.csp_override\">\n<span class=\"sig-name descname\"><span class=\"pre\">csp_override</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">config)(view</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.decorators.csp.csp_override\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Overrides the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Security-Policy</span></code> header for the decorated view\nusing directives in the same format as the <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-SECURE_CSP\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CSP</span></code></a> setting.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">config</span></code> argument must be a mapping with the desired CSP directives.\nIf <code class=\"docutils literal notranslate\"><span class=\"pre\">config</span></code> is an empty mapping (<code class=\"docutils literal notranslate\"><span class=\"pre\">{}</span></code>), no CSP enforcement header will\nbe added to the response returned by that view, effectively disabling CSP\nfor that view.</p>\n<p>Examples:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HttpResponse</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils.csp</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">CSP</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.views.decorators.csp</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">csp_override</span>\n\n\n<span class=\"nd\">@csp_override</span><span class=\"p\">(</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;default-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"n\">CSP</span><span class=\"o\">.</span><span class=\"n\">SELF</span><span class=\"p\">],</span>\n        <span class=\"s2\">&quot;img-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"n\">CSP</span><span class=\"o\">.</span><span class=\"n\">SELF</span><span class=\"p\">,</span> <span class=\"s2\">&quot;data:&quot;</span><span class=\"p\">],</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">)</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">my_view</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"s2\">&quot;Custom Content-Security-Policy header applied&quot;</span><span class=\"p\">)</span>\n\n\n<span class=\"nd\">@csp_override</span><span class=\"p\">({})</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">my_other_view</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"s2\">&quot;No Content-Security-Policy header added&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.decorators.csp.csp_report_only_override\">\n<span class=\"sig-name descname\"><span class=\"pre\">csp_report_only_override</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">config)(view</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.decorators.csp.csp_report_only_override\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Overrides the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Security-Policy-Report-Only</span></code> header for the\ndecorated view using directives in the same format as the\n<a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-SECURE_CSP_REPORT_ONLY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CSP_REPORT_ONLY</span></code></a> setting.</p>\n<p>Like <a class=\"reference internal\" href=\"#django.views.decorators.csp.csp_override\" title=\"django.views.decorators.csp.csp_override\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">csp_override()</span></code></a>, the <code class=\"docutils literal notranslate\"><span class=\"pre\">config</span></code> argument must be a mapping with\nthe desired CSP directives. If <code class=\"docutils literal notranslate\"><span class=\"pre\">config</span></code> is an empty mapping (<code class=\"docutils literal notranslate\"><span class=\"pre\">{}</span></code>), no\nCSP report-only header will be added to the response returned by that view,\neffectively disabling report-only CSP for that view.</p>\n<p>Examples:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HttpResponse</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils.csp</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">CSP</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.views.decorators.csp</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">csp_report_only_override</span>\n\n\n<span class=\"nd\">@csp_report_only_override</span><span class=\"p\">(</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;default-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"n\">CSP</span><span class=\"o\">.</span><span class=\"n\">SELF</span><span class=\"p\">],</span>\n        <span class=\"s2\">&quot;img-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"n\">CSP</span><span class=\"o\">.</span><span class=\"n\">SELF</span><span class=\"p\">,</span> <span class=\"s2\">&quot;data:&quot;</span><span class=\"p\">],</span>\n        <span class=\"s2\">&quot;report-uri&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;https://mysite.com/csp-report/&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">)</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">my_view</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"s2\">&quot;Custom Content-Security-Policy-Report-Only header applied&quot;</span><span class=\"p\">)</span>\n\n\n<span class=\"nd\">@csp_report_only_override</span><span class=\"p\">({})</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">my_other_view</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"s2\">&quot;No Content-Security-Policy-Report-Only header added&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<p>The examples above assume function-based views. For class-based views, see the\n<a class=\"reference internal\" href=\"/el/6.0/topics/class-based-views/intro/#id1\"><span class=\"std std-ref\">guide for decorating class-based views</span></a>.</p>\n</section>\n<section id=\"nonce-usage\">\n<span id=\"csp-nonce\"></span><h2>Nonce usage<a class=\"heading-anchor\" href=\"#nonce-usage\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A CSP nonce («number used once») is a unique, random value generated per HTTP\nresponse. Django supports nonces as a secure way to allow specific inline\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;script&gt;</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;style&gt;</span></code> elements to execute without relying on\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'unsafe-inline'</span></code>.</p>\n<p>Nonces are enabled by including the special placeholder\n<a class=\"reference internal\" href=\"#django.utils.csp.CSP.NONCE\" title=\"django.utils.csp.CSP.NONCE\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">NONCE</span></code></a> in the relevant directive(s) of your\n<a class=\"reference internal\" href=\"#csp-settings\"><span class=\"std std-ref\">CSP settings</span></a>, such as <code class=\"docutils literal notranslate\"><span class=\"pre\">script-src</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">style-src</span></code>.\nWhen present, the\n<a class=\"reference internal\" href=\"/el/6.0/ref/middleware/#django.middleware.csp.ContentSecurityPolicyMiddleware\" title=\"django.middleware.csp.ContentSecurityPolicyMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ContentSecurityPolicyMiddleware</span></code></a>\nwill generate a nonce and insert the corresponding <code class=\"docutils literal notranslate\"><span class=\"pre\">nonce-&lt;value&gt;</span></code> source\nexpression into the CSP header.</p>\n<p>To use this nonce in templates, the\n<a class=\"reference internal\" href=\"/el/6.0/ref/templates/api/#django.template.context_processors.csp\" title=\"django.template.context_processors.csp\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">csp()</span></code></a> context processor needs to be\nenabled. It adds a <code class=\"docutils literal notranslate\"><span class=\"pre\">csp_nonce</span></code> variable to the template context, allowing\ninline elements to include a matching <code class=\"docutils literal notranslate\"><span class=\"pre\">nonce=&quot;{{</span> <span class=\"pre\">csp_nonce</span> <span class=\"pre\">}}&quot;</span></code> attribute in\ninline scripts or styles.</p>\n<p>The browser will only execute inline elements that include a <code class=\"docutils literal notranslate\"><span class=\"pre\">nonce=&lt;value&gt;</span></code>\nattribute matching the one specified in the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Security-Policy</span></code> (or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Security-Policy-Report-Only</span></code>) header. This mechanism provides\nfine-grained control over which inline code is allowed to run.</p>\n<p>If a template includes <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">csp_nonce</span> <span class=\"pre\">}}</span></code> but the policy does not include\n<a class=\"reference internal\" href=\"#django.utils.csp.CSP.NONCE\" title=\"django.utils.csp.CSP.NONCE\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">NONCE</span></code></a>, the HTML will include a nonce attribute,\nbut the header will lack the required source expression. In this case, the\nbrowser will block the inline script or style (or report it for report-only\nconfigurations).</p>\n<section id=\"nonce-generation-and-caching\">\n<h3>Nonce generation and caching<a class=\"heading-anchor\" href=\"#nonce-generation-and-caching\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django’s nonce generation is <strong>lazy</strong>: the middleware only generates a nonce if\n<code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">csp_nonce</span> <span class=\"pre\">}}</span></code> is accessed during template rendering. This avoids\nunnecessary work for pages that do not use nonces.</p>\n<p>However, because nonces must be unique per request, extra care is needed when\nusing full-page caching (e.g., Django’s cache middleware, CDN caching). Serving\ncached responses with previously generated nonces may result in reuse across\nusers and requests. Although such responses may still appear to work (since the\nnonce in the CSP header and HTML content match), reuse defeats the purpose of\nthe nonce and weakens security.</p>\n<p>To ensure nonce-based policies remain effective:</p>\n<ul class=\"simple\">\n<li><p>Avoid caching full responses that include <code class=\"docutils literal notranslate\"><span class=\"pre\">{{</span> <span class=\"pre\">csp_nonce</span> <span class=\"pre\">}}</span></code>.</p></li>\n<li><p>If caching is necessary, use a strategy that injects a fresh nonce on each\nrequest, or consider refactoring your application to avoid inline scripts and\nstyles altogether.</p></li>\n</ul>\n</section>\n</section>","rootId":"content-security-policy","toc":[{"title":"Overview","anchor":"overview","children":[]},{"title":"Settings","anchor":"settings","children":[]},{"title":"Policy violation reports","anchor":"policy-violation-reports","children":[]},{"title":"CSP constants","anchor":"csp-constants","children":[]},{"title":"Decorators","anchor":"module-django.views.decorators.csp","children":[]},{"title":"Nonce usage","anchor":"nonce-usage","children":[{"title":"Nonce generation and caching","anchor":"nonce-generation-and-caching","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/el/6.0/ref/"}],"prev":{"docname":"ref/contrib/syndication","title":"The syndication feed framework","url":"/el/6.0/ref/contrib/syndication/"},"next":{"docname":"ref/csrf","title":"Cross Site Request Forgery protection","url":"/el/6.0/ref/csrf/"},"formats":{"html":"/el/6.0/ref/csp/","markdown":"/el/6.0/ref/csp.md","json":"/el/6.0/ref/csp.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/ref/csp.txt","official":"https://docs.djangoproject.com/el/6.0/ref/csp/","inVersions":["6.1","6.0"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}