[Django] #36160: Django serve view does not respect X_FRAME_OPTIONS = "SAMEORIGIN"

3 views
Skip to first unread message

Django

unread,
Jan 30, 2025, 6:26:56 AMJan 30
to django-...@googlegroups.com
#36160: Django serve view does not respect X_FRAME_OPTIONS = "SAMEORIGIN"
-------------------------------------+-------------------------------------
Reporter: miratcan | Type:
| Uncategorized
Status: new | Component:
| contrib.staticfiles
Version: 5.1 | Severity: Normal
Keywords: static, headers | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
In my Django project, I needed to display an uploaded file inside an
<iframe>. However, I noticed that Django's built-in serve view does not
respect the X_FRAME_OPTIONS = "SAMEORIGIN" setting, which prevents the
file from being embedded in an iframe.

After investigating, I found that the serve view in Django does not modify
the response headers related to X_FRAME_OPTIONS, meaning it defaults to
Django's global security settings.

**Serve view:**

https://github.com/django/django/blob/c6ace896a2da73356f7c9a655bbe32a0e3ce0435/django/views/static.py#L28

**FileResponse implementation:**

https://github.com/django/django/blob/12b9ef38b3ff7f5b8b24a5f42e8923fdb6db44bb/django/http/response.py#L572

Since serve acts as a wrapper around FileResponse, I expected it to either
respect X_FRAME_OPTIONS or provide a way to override it.
Would it be possible to modify the serve view to allow overriding
X_FRAME_OPTIONS explicitly?
--
Ticket URL: <https://code.djangoproject.com/ticket/36160>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 30, 2025, 6:57:22 AMJan 30
to django-...@googlegroups.com
#36160: Django serve view does not respect X_FRAME_OPTIONS = "SAMEORIGIN"
-------------------------------------+-------------------------------------
Reporter: Mirat Can Bayrak | Owner: (none)
Type: Uncategorized | Status: closed
Component: contrib.staticfiles | Version: 5.1
Severity: Normal | Resolution: needsinfo
Keywords: static, headers | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => needsinfo
* status: new => closed

Comment:

Can you share a minimal reproducible example/code snippets?
There is a
[https://docs.djangoproject.com/en/5.1/ref/clickjacking/#setting-x-frame-
options-for-all-responses section which documents] how you can use either
the `X_FRAME_OPTIONS` setting or the `@xframe_options_exempt` decorator to
customize the behavior. I would need to know whether you are using these
and what the results are.
--
Ticket URL: <https://code.djangoproject.com/ticket/36160#comment:1>
Reply all
Reply to author
Forward
0 new messages