# Django + HTMX... any known issues around security/performance?

**URL:** <https://forum.djangoproject.com/t/django-htmx-any-known-issues-around-security-performance/21260>\
**Category:** Forms & APIs\
**Created:** [May 31, 2023, 9:40am UTC](https://forum.djangoproject.com/t/django-htmx-any-known-issues-around-security-performance/21260 "2023-05-31T09:40:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsvincent](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/wsvincent/32/52_2.png) [@wsvincent](https://forum.djangoproject.com/u/wsvincent)\
**Post date:** [May 31, 2023, 9:40am UTC](https://forum.djangoproject.com/t/django-htmx-any-known-issues-around-security-performance/21260/1 "2023-05-31T09:40:13Z")

</div>

We all seem to be quite taken with HTMX as a tool in our Django projects. There are a number of talks here at DjangoCon Europe on it as there were last fall at DjangoCon US.

I’m wondering if there are any known issues around it in the community? Especially at scale? I’ve not heard of any but surely there are some creeping around somewhere?

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [June 11, 2023, 9:28pm UTC](https://forum.djangoproject.com/t/django-htmx-any-known-issues-around-security-performance/21260/2 "2023-06-11T21:28:47Z")

</div>

I think it’s hard for there to be django-specific security issues. htmx makes requests to your server for HTML, so there’s the usual requirements for permissions checks, but you always need that when exposing data.

Several times users have suggested that django-htmx add a “htmx only” decorator for views to deny requests that don’t have the `HX-Request` header set. I have closed these suggestions because I think it’s at best unnecessary. Relying on blocking requests without that header may lead to laxer security standards.

As for performance, the Django Template Language is known to be relatively slow, so switching to Jinja2 would help. @orf did [some benchmarks](https://tomforb.es/just-how-slow-are-django-templates/) 10 years ago, and whilst there have been lots of optimizations in both libraries and Python itself, I think the gap is still there.

---

<div class="post-metadata">

**Author:** ![maxAlthaus](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maxalthaus/32/15359_2.png) [@maxAlthaus](https://forum.djangoproject.com/u/maxAlthaus)\
**Post date:** [August 14, 2023, 1:54pm UTC](https://forum.djangoproject.com/t/django-htmx-any-known-issues-around-security-performance/21260/3 "2023-08-14T13:54:00Z")

</div>

I found this some days ago and i wonder, if this is true. However, if so, what can be done do make htmx more secure, or is there a workaround by Django to ensure a secure web application?  
[HTMX not secure](https://youtu.be/j5LPDuFRDP0)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 14, 2023, 3:08pm UTC](https://forum.djangoproject.com/t/django-htmx-any-known-issues-around-security-performance/21260/4 "2023-08-14T15:08:03Z")

</div>

Did you actually watch and listen to the entire video?

Yes, if your server is compromised or you serve untrusted or unvalidated html via HTMX, you can subject your users to XSS attacks.

How is that any different from any other platform or web framework?

You _always_ need to validate or sanitize input supplied from the browser. You must _never_ _directly_ serve data supplied from a user to another user.

Django actually inhibits these types of attacks by escaping html-special characters by default. You have to disable those filters in order for user-supplied data to be rendered as html.

---

<div class="post-metadata">

**Author:** ![maxAlthaus](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maxalthaus/32/15359_2.png) [@maxAlthaus](https://forum.djangoproject.com/u/maxAlthaus)\
**Post date:** [August 14, 2023, 6:22pm UTC](https://forum.djangoproject.com/t/django-htmx-any-known-issues-around-security-performance/21260/5 "2023-08-14T18:22:37Z")

</div>

Thank you for your reply. Obviously i did not understand all. So it is great to have your explanation.  
That means, i could use htmx in my Django projects, right? Sounds good.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 14, 2023, 6:29pm UTC](https://forum.djangoproject.com/t/django-htmx-any-known-issues-around-security-performance/21260/6 "2023-08-14T18:29:57Z")

</div>

I do - it has been a game-changer for me. I’ve done some things with HTMX, Django, and Channels that I have never successfully implemented before.

---

<div class="post-metadata">

**Author:** ![maxAlthaus](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maxalthaus/32/15359_2.png) [@maxAlthaus](https://forum.djangoproject.com/u/maxAlthaus)\
**Post date:** [August 14, 2023, 6:42pm UTC](https://forum.djangoproject.com/t/django-htmx-any-known-issues-around-security-performance/21260/7 "2023-08-14T18:42:27Z")

</div>

Excelent. So i will start to learn. Although it is “just” a hobby for me, i really enjoy working on my django project.

---

<div class="post-metadata">

**Author:** ![satya](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/satya/32/10192_2.png) [@satya](https://forum.djangoproject.com/u/satya)\
**Post date:** [August 14, 2023, 8:43pm UTC](https://forum.djangoproject.com/t/django-htmx-any-known-issues-around-security-performance/21260/8 "2023-08-14T20:43:18Z")

</div>

I’m curious as well. I’ve disabled eval, see [htmx config](https://htmx.org/docs/#config). Not sure if there are other measures one could take.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 14, 2023, 10:49pm UTC](https://forum.djangoproject.com/t/django-htmx-any-known-issues-around-security-performance/21260/9 "2023-08-14T22:49:41Z")

</div>

From the docs at [Security](https://htmx.org/docs/#security)

> You should, of course, escape all 3rd party untrusted content that is injected into your site to prevent, among other issues, [XSS attacks](https://en.wikipedia.org/wiki/Cross-site_scripting).

What this translates to in Django terms is that any user-supplied data should either be rendered as part of a template, or passed through the [`escape`](https://docs.djangoproject.com/en/4.2/ref/utils/#django.utils.html.escape) function before being sent back out.

If you do that with all data that doesn’t originate from a known source, you’ll be ok from that perspective.

But this isn’t specific to HTMX - this is something that should be done for **all** processes returning untrusted data to browsers.
