{"title":"Hur man använder Djangos Content Security Policy","version":"6.1","locale":"sv","docname":"howto/csp","url":"/sv/6.1/howto/csp/","canonical":"https://djangodocs.dev/sv/6.1/howto/csp/","summary":"Så här aktiverar du Content Security Policy (CSP) i ditt Django-projekt: Lägg till CSP-mellanprogramvaran till din MIDDLEWARE-inställning; Konfigurera…","html":"<h1>Hur man använder Djangos Content Security Policy<a class=\"heading-anchor\" href=\"#how-to-use-django-s-content-security-policy\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<section id=\"basic-config\">\n<span id=\"csp-config\"></span><h2>Grundläggande konfiguration<a class=\"heading-anchor\" href=\"#basic-config\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Så här aktiverar du Content Security Policy (CSP) i ditt Django-projekt:</p>\n<ol class=\"arabic\">\n<li><p>Lägg till CSP-mellanprogramvaran till din <a class=\"reference internal\" href=\"/sv/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\">MIDDLEWARE</code></a>-inställning:</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>MIDDLEWARE <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"s2\">&quot;django.middleware.csp.ContentSecurityPolicyMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"c1\"># ...</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n</li>\n<li><p>Konfigurera CSP-policyerna i din <code class=\"docutils literal notranslate\">settings.py</code> med antingen <a class=\"reference internal\" href=\"/sv/6.1/ref/settings/#std-setting-SECURE_CSP\"><code class=\"xref std std-setting docutils literal notranslate\">SECURE_CSP</code></a> eller <a class=\"reference internal\" href=\"/sv/6.1/ref/settings/#std-setting-SECURE_CSP_REPORT_ONLY\"><code class=\"xref std std-setting docutils literal notranslate\">SECURE_CSP_REPORT_ONLY</code></a> (eller båda). <a class=\"reference internal\" href=\"/sv/6.1/ref/csp/#csp-settings\"><span class=\"std std-ref\">CSP-inställningsdokumentationen</span></a> ger mer information om skillnaderna mellan dessa två:</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=\"nn\">django.utils.csp</span> <span class=\"kn\">import</span> CSP\n\n<span class=\"c1\"># To enforce a CSP policy:</span>\nSECURE_CSP <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;default-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>CSP<span class=\"o\">.</span>SELF<span class=\"p\">],</span>\n    <span class=\"c1\"># Add more directives to be enforced.</span>\n<span class=\"p\">}</span>\n\n<span class=\"c1\"># Or for report-only mode:</span>\nSECURE_CSP_REPORT_ONLY <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;default-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>CSP<span class=\"o\">.</span>SELF<span class=\"p\">],</span>\n    <span class=\"c1\"># Add more directives as needed.</span>\n    <span class=\"s2\">&quot;report-uri&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;/path/to/reports-endpoint/&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n</li>\n</ol>\n</section>\n<section id=\"nonce-config\">\n<span id=\"csp-nonce-config\"></span><h2>Nonce-konfiguration<a class=\"heading-anchor\" href=\"#nonce-config\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>För att använda nonces i din CSP-policy måste du, förutom den grundläggande konfigurationen, göra följande:</p>\n<ol class=\"arabic\">\n<li><p>Inkludera platshållarvärdet <a class=\"reference internal\" href=\"/sv/6.1/ref/csp/#django.utils.csp.CSP.NONCE\" title=\"django.utils.csp.CSP.NONCE\"><code class=\"xref py py-attr docutils literal notranslate\">NONCE</code></a> i CSP-inställningarna. Detta gäller endast direktiven <code class=\"docutils literal notranslate\">script-src</code> eller <code class=\"docutils literal notranslate\">style-src</code>:</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=\"nn\">django.utils.csp</span> <span class=\"kn\">import</span> CSP\n\nSECURE_CSP <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;default-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>CSP<span class=\"o\">.</span>SELF<span class=\"p\">],</span>\n    <span class=\"c1\"># Allow self-hosted scripts and script tags with matching `nonce` attr.</span>\n    <span class=\"s2\">&quot;script-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>CSP<span class=\"o\">.</span>SELF<span class=\"p\">,</span> CSP<span class=\"o\">.</span>NONCE<span class=\"p\">],</span>\n    <span class=\"c1\"># Example of the less secure &#39;unsafe-inline&#39; option.</span>\n    <span class=\"s2\">&quot;style-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>CSP<span class=\"o\">.</span>SELF<span class=\"p\">,</span> CSP<span class=\"o\">.</span>UNSAFE_INLINE<span class=\"p\">],</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n</li>\n<li><p>Lägg till <a class=\"reference internal\" href=\"/sv/6.1/ref/templates/api/#django.template.context_processors.csp\" title=\"django.template.context_processors.csp\"><code class=\"xref py py-func docutils literal notranslate\">csp()</code></a>-kontextprocessorn till din <a class=\"reference internal\" href=\"/sv/6.1/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\">TEMPLATES</code></a>-inställning. Detta gör det genererade nonce-värdet tillgängligt i Django-mallarna som kontextvariabeln <code class=\"docutils literal notranslate\">csp_nonce</code>:</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>TEMPLATES <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.template.backends.django.DjangoTemplates&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;context_processors&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n                <span class=\"c1\"># ...</span>\n                <span class=\"s2\">&quot;django.template.context_processors.csp&quot;</span><span class=\"p\">,</span>\n            <span class=\"p\">],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n</li>\n<li><p>Lägg till nonce-värdet i de element som behöver det i dina mallar:</p>\n<p>För infogade taggarna <code class=\"docutils literal notranslate\">&lt;style&gt;</code> och <code class=\"docutils literal notranslate\">&lt;script&gt;</code> använder du kontextvariabeln <code class=\"docutils literal notranslate\">csp_nonce</code> direkt:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">style</span> <span class=\"na\">nonce</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{{</span> <span class=\"nv\">csp_nonce</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>\n  <span class=\"c\">/* These inline styles will be allowed. */</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">style</span><span class=\"p\">&gt;</span>\n\n<span class=\"p\">&lt;</span><span class=\"nt\">script</span> <span class=\"na\">nonce</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{{</span> <span class=\"nv\">csp_nonce</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>\n  <span class=\"c1\">// This inline JavaScript will be allowed.</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">script</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>För externa elementen <code class=\"docutils literal notranslate\">&lt;script src=&quot;...&quot;&gt;</code> och <code class=\"docutils literal notranslate\">&lt;link rel=&quot;stylesheet&quot;&gt;</code> använder du malltaggen <a class=\"reference internal\" href=\"/sv/6.1/ref/templates/builtins/#std-templatetag-csp_nonce_attr\"><code class=\"xref std std-ttag docutils literal notranslate\">csp_nonce_attr</code></a>:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">script</span> <span class=\"na\">src</span><span class=\"o\">=</span><span class=\"s\">&quot;/path/to/script.js&quot;</span> <span class=\"cp\">{%</span> <span class=\"k\">csp_nonce_attr</span> <span class=\"cp\">%}</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">script</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">link</span> <span class=\"na\">rel</span><span class=\"o\">=</span><span class=\"s\">&quot;stylesheet&quot;</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;/path/to/style.css&quot;</span> <span class=\"cp\">{%</span> <span class=\"k\">csp_nonce_attr</span> <span class=\"cp\">%}</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>För att återge tillgångarna för ett <a class=\"reference internal\" href=\"/sv/6.1/topics/forms/media/#django.forms.Media\" title=\"django.forms.Media\"><code class=\"xref py py-class docutils literal notranslate\">Media</code></a>-objekt med nonce-värdet tillämpat på varje element skickar du objektet till taggen <a class=\"reference internal\" href=\"/sv/6.1/ref/templates/builtins/#std-templatetag-csp_nonce_attr\"><code class=\"xref std std-ttag docutils literal notranslate\">csp_nonce_attr</code></a>:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">csp_nonce_attr</span> <span class=\"nv\">form.media</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<aside class=\"version-note version-changed\" data-version=\"6.1\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 6.1</p><p>Malltaggen <a class=\"reference internal\" href=\"/sv/6.1/ref/templates/builtins/#std-templatetag-csp_nonce_attr\"><code class=\"xref std std-ttag docutils literal notranslate\">csp_nonce_attr</code></a> lades till, med stöd för att återge <a class=\"reference internal\" href=\"/sv/6.1/topics/forms/media/#django.forms.Media\" title=\"django.forms.Media\"><code class=\"xref py py-class docutils literal notranslate\">Media</code></a>-objekt.</p>\n</aside>\n</li>\n</ol>\n<aside class=\"admonition-caching-and-nonce-reuse admonition\" role=\"note\">\n<p class=\"admonition-title\">Cachning och återanvändning av nonce</p>\n<p><a class=\"reference internal\" href=\"/sv/6.1/ref/middleware/#django.middleware.csp.ContentSecurityPolicyMiddleware\" title=\"django.middleware.csp.ContentSecurityPolicyMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">ContentSecurityPolicyMiddleware</code></a> hanterar automatiskt genereringen av en unik nonce och infogar lämpligt <code class=\"docutils literal notranslate\">nonce-&lt;value&gt;</code>-källuttryck i <code class=\"docutils literal notranslate\">Content-Security-Policy</code>- (eller <code class=\"docutils literal notranslate\">Content-Security-Policy-Report-Only</code>-) rubriken när noncen används i en mall.</p>\n<p>För att säkerställa korrekt funktion, se till att både HTML-koden och rubriken genereras inom samma begäran och inte hämtas från cacheminnet. Se referensdokumentationen om <a class=\"reference internal\" href=\"/sv/6.1/ref/csp/#csp-nonce\"><span class=\"std std-ref\">Användning av nonce</span></a> för implementeringsdetaljer och viktiga överväganden kring cachning.</p>\n</aside>\n</section>","rootId":"how-to-use-django-s-content-security-policy","toc":[{"title":"Grundläggande konfiguration","anchor":"basic-config","children":[]},{"title":"Nonce-konfiguration","anchor":"nonce-config","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"Hur man gör-guider","url":"/sv/6.1/howto/"}],"prev":{"docname":"howto/auth-remote-user","title":"Hur man autentiserar med hjälp av REMOTE_USER","url":"/sv/6.1/howto/auth-remote-user/"},"next":{"docname":"howto/csrf","title":"Så här använder du Djangos CSRF-skydd","url":"/sv/6.1/howto/csrf/"},"formats":{"html":"/sv/6.1/howto/csp/","markdown":"/sv/6.1/howto/csp.md","json":"/sv/6.1/howto/csp.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/howto/csp.txt","official":"https://docs.djangoproject.com/sv/6.1/howto/csp/","inVersions":["6.1","6.0"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}