{"title":"Clickjacking Protection","version":"2.2","locale":"el","docname":"ref/clickjacking","url":"/el/2.2/ref/clickjacking/","canonical":"https://djangodocs.dev/el/2.2/ref/clickjacking/","summary":"The clickjacking middleware and decorators provide easy-to-use protection against clickjacking . This type of attack occurs when a malicious site tricks a user into…","html":"<span id=\"clickjacking-protection\"></span><h1>Clickjacking Protection<a class=\"heading-anchor\" href=\"#module-django.middleware.clickjacking\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>The clickjacking middleware and decorators provide easy-to-use protection\nagainst <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Clickjacking\">clickjacking</a>.  This type of attack occurs when a malicious site\ntricks a user into clicking on a concealed element of another site which they\nhave loaded in a hidden frame or iframe.</p>\n<section id=\"an-example-of-clickjacking\">\n<h2>An example of clickjacking<a class=\"heading-anchor\" href=\"#an-example-of-clickjacking\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Suppose an online store has a page where a logged in user can click «Buy Now» to\npurchase an item. A user has chosen to stay logged into the store all the time\nfor convenience. An attacker site might create an «I Like Ponies» button on one\nof their own pages, and load the store’s page in a transparent iframe such that\nthe «Buy Now» button is invisibly overlaid on the «I Like Ponies» button. If the\nuser visits the attacker’s site, clicking «I Like Ponies» will cause an\ninadvertent click on the «Buy Now» button and an unknowing purchase of the item.</p>\n</section>\n<section id=\"preventing-clickjacking\">\n<span id=\"clickjacking-prevention\"></span><h2>Preventing clickjacking<a class=\"heading-anchor\" href=\"#preventing-clickjacking\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Modern browsers honor the <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options\">X-Frame-Options</a> HTTP header that indicates whether\nor not a resource is allowed to load within a frame or iframe. If the response\ncontains the header with a value of <code class=\"docutils literal notranslate\"><span class=\"pre\">SAMEORIGIN</span></code> then the browser will only\nload the resource in a frame if the request originated from the same site. If\nthe header is set to <code class=\"docutils literal notranslate\"><span class=\"pre\">DENY</span></code> then the browser will block the resource from\nloading in a frame no matter which site made the request.</p>\n<p>Django provides a few simple ways to include this header in responses from your\nsite:</p>\n<ol class=\"arabic simple\">\n<li><p>A simple middleware that sets the header in all responses.</p></li>\n<li><p>A set of view decorators that can be used to override the middleware or to\nonly set the header for certain views.</p></li>\n</ol>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code> HTTP header will only be set by the middleware or view\ndecorators if it is not already present in the response.</p>\n</section>\n<section id=\"how-to-use-it\">\n<h2>How to use it<a class=\"heading-anchor\" href=\"#how-to-use-it\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"setting-x-frame-options-for-all-responses\">\n<h3>Setting <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code> for all responses<a class=\"heading-anchor\" href=\"#setting-x-frame-options-for-all-responses\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>To set the same <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code> value for all responses in your site, put\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'django.middleware.clickjacking.XFrameOptionsMiddleware'</span></code> to\n<a class=\"reference internal\" href=\"/el/2.2/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a>:</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=\"n\">MIDDLEWARE</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"o\">...</span>\n    <span class=\"s1\">&#39;django.middleware.clickjacking.XFrameOptionsMiddleware&#39;</span><span class=\"p\">,</span>\n    <span class=\"o\">...</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>This middleware is enabled in the settings file generated by\n<a class=\"reference internal\" href=\"/el/2.2/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">startproject</span></code></a>.</p>\n<p>By default, the middleware will set the <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code> header to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">SAMEORIGIN</span></code> for every outgoing <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code>. If you want <code class=\"docutils literal notranslate\"><span class=\"pre\">DENY</span></code>\ninstead, set the <a class=\"reference internal\" href=\"/el/2.2/ref/settings/#std-setting-X_FRAME_OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">X_FRAME_OPTIONS</span></code></a> setting:</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=\"n\">X_FRAME_OPTIONS</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;DENY&#39;</span>\n</code></pre></div>\n<p>When using the middleware there may be some views where you do <strong>not</strong> want the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code> header set. For those cases, you can use a view decorator\nthat tells the middleware not to set the header:</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.views.decorators.clickjacking</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">xframe_options_exempt</span>\n\n<span class=\"nd\">@xframe_options_exempt</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">ok_to_load_in_a_frame</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;This page is safe to load in a frame on any site.&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Σημείωση</p>\n<p>If you want to submit a form or access a session cookie within a frame or\niframe, you may need to modify the <a class=\"reference internal\" href=\"/el/2.2/ref/settings/#std-setting-CSRF_COOKIE_SAMESITE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_COOKIE_SAMESITE</span></code></a> or\n<a class=\"reference internal\" href=\"/el/2.2/ref/settings/#std-setting-SESSION_COOKIE_SAMESITE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SESSION_COOKIE_SAMESITE</span></code></a> settings.</p>\n</aside>\n</section>\n<section id=\"setting-x-frame-options-per-view\">\n<h3>Setting <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code> per view<a class=\"heading-anchor\" href=\"#setting-x-frame-options-per-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>To set the <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code> header on a per view basis, Django provides these\ndecorators:</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.views.decorators.clickjacking</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">xframe_options_deny</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.views.decorators.clickjacking</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">xframe_options_sameorigin</span>\n\n<span class=\"nd\">@xframe_options_deny</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">view_one</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;I won&#39;t display in any frame!&quot;</span><span class=\"p\">)</span>\n\n<span class=\"nd\">@xframe_options_sameorigin</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">view_two</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;Display in a frame if it&#39;s from the same origin as me.&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Note that you can use the decorators in conjunction with the middleware. Use of\na decorator overrides the middleware.</p>\n</section>\n</section>\n<section id=\"limitations\">\n<h2>Limitations<a class=\"heading-anchor\" href=\"#limitations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code> header will only protect against clickjacking in a\nmodern browser. Older browsers will quietly ignore the header and need <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Clickjacking#Prevention\">other\nclickjacking prevention techniques</a>.</p>\n<section id=\"browsers-that-support-x-frame-options\">\n<h3>Browsers that support <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code><a class=\"heading-anchor\" href=\"#browsers-that-support-x-frame-options\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<ul class=\"simple\">\n<li><p>Internet Explorer 8+</p></li>\n<li><p>Firefox 3.6.9+</p></li>\n<li><p>Opera 10.5+</p></li>\n<li><p>Safari 4+</p></li>\n<li><p>Chrome 4.1+</p></li>\n</ul>\n</section>\n<section id=\"see-also\">\n<h3>See also<a class=\"heading-anchor\" href=\"#see-also\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>A <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options#Browser_compatibility\">complete list</a> of browsers supporting <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code>.</p>\n</section>\n</section>","rootId":"module-django.middleware.clickjacking","toc":[{"title":"An example of clickjacking","anchor":"an-example-of-clickjacking","children":[]},{"title":"Preventing clickjacking","anchor":"preventing-clickjacking","children":[]},{"title":"How to use it","anchor":"how-to-use-it","children":[{"title":"Setting X-Frame-Options for all responses","anchor":"setting-x-frame-options-for-all-responses","children":[]},{"title":"Setting X-Frame-Options per view","anchor":"setting-x-frame-options-per-view","children":[]}]},{"title":"Limitations","anchor":"limitations","children":[{"title":"Browsers that support X-Frame-Options","anchor":"browsers-that-support-x-frame-options","children":[]},{"title":"See also","anchor":"see-also","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/el/2.2/ref/"}],"prev":{"docname":"ref/class-based-views/flattened-index","title":"Class-based generic views - flattened index","url":"/el/2.2/ref/class-based-views/flattened-index/"},"next":{"docname":"ref/contrib/index","title":"contrib packages","url":"/el/2.2/ref/contrib/"},"formats":{"html":"/el/2.2/ref/clickjacking/","markdown":"/el/2.2/ref/clickjacking.md","json":"/el/2.2/ref/clickjacking.json"},"source":"https://github.com/django/django/blob/stable/2.2.x/docs/ref/clickjacking.txt","official":"https://docs.djangoproject.com/el/2.2/ref/clickjacking/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}