Codework Tech
then
Talk to us↗

Django SQL Injection in range and in Lookups: What to Patch Now

Django 6.1.4, 6.0.11 and 5.2.20 fix seven security issues, two of them high severity: a SQL injection you can hit through plain ORM lookups, and a file write that can lead to RCE on PostGIS.

Niti Jirakarnwuttikrai

5 min read

A code editor showing a Django __range filter line highlighted in red, with a dashed injection path running to a green database cylinder blocked by a shield with a check mark, labelled Django 6.1.4 and the fixed version numbers

Key takeaways

  • Django 6.1.4, 6.0.11 and 5.2.20 (2 December 2026) fix seven CVEs, two rated high: upgrade on your next deploy.
  • CVE-2026-132092 is a SQL injection reachable through __range and __in when the query involves an alias or key transform containing a percent sign.
  • The range lookup now requires exactly two right-hand-side values, the only backwards-incompatible change in this release.
  • CVE-2026-124827 only affects projects using RasterField on PostGIS, and the patch stops file writes but not SSRF.
  • If you use cache_page() or UpdateCacheMiddleware, flush the cache after upgrading: existing entries are not invalidated.

If your team writes Django and assumes the ORM takes SQL injection off the table, this release is worth ten minutes of your time. The Django SQL injection fixed on 2 December 2026 is reachable through __range and __in, with no raw SQL anywhere in sight. The team shipped security releases 6.1.4, 6.0.11 and 5.2.20 covering seven CVEs, two of them rated high.

So the job today isn't just pip install -U and move on. The fix carries a small breaking change that can fail your existing code, and one of the other issues needs a cache flush afterwards.

What's in the release

Seven CVEs, ordered by severity as the Django team published them:

  • CVE-2026-132092 (high) SQL injection in range and in lookups
  • CVE-2026-124827 (high) server-side file write via GeoDjango's RasterField, which can escalate to RCE depending on the raster driver
  • CVE-2026-115868 (moderate) XSS in truncatechars_html and Truncator.chars() with html=True
  • CVE-2026-125673 (low) cache poisoning through build_absolute_uri() mishandling . and .. path segments
  • CVE-2026-119642 (low) admin changelist data leak via the pk alias on a OneToOneField primary key
  • CVE-2026-127929 (low) admin changelist data leak via reverse relations, a regression introduced in Django 5.0.3
  • CVE-2026-132809 (low) heap buffer overflow in OGRGeometry.wkb for geometries larger than 4GB

Affected branches are main, 6.1, 6.0 and 5.2. If you're still on 5.1 or older there is no patch for you: the supported versions table now lists only 5.2 LTS, 6.0 and 6.1.

The Django SQL injection you can reach through the ORM

This is the interesting one, because it breaks the comfortable assumption that the ORM has SQL injection covered end to end. The 6.1.4 release notes spell out the conditions: the query has to involve an alias or key transform containing a percent sign, and then

  • __range is exploitable when more than two right-hand-side values are passed, which was already incorrect usage, except Django never complained about it before
  • __in is exploitable when a bilateral transform is involved

The shape that shows up in real codebases is code that forwards a list straight from the query string into a filter:

python
# values come from ?d=... and go into __range as-is
dates = request.GET.getlist("d")
Order.objects.filter(created_at__range=dates)

With three or more values, that used to run quietly. Now range requires exactly two right-hand-side values, which makes it the single backwards-incompatible change in this release. If you are passing more, you'll get an error the moment you upgrade, and honestly that's better than it silently working.

The other half to clean up is aliases and key transforms with a % in the name:

python
# an alias with a percent sign in it is exactly what the release note describes
Report.objects.annotate(**{"growth_%": F("growth")}).filter(...)

Percent signs in column aliases and key transforms have been deprecated since Django 6.0 and are scheduled for removal in Django 2028. If your code still has them, pay that debt now rather than waiting for it to break on you later.

RasterField can write files on your server

CVE-2026-124827 is narrower but nastier. It needs GeoDjango's RasterField, which only exists on PostGIS, so if your project doesn't touch raster data you can skip it. Plain geometry fields were not affected.

The problem: assigning a raster data definition with a user-controlled name and a non-default driver could make GDAL write a file to a location the attacker picks, and the advisory says that depending on the driver this could enable remote code execution. The reason it was easy to reach is that RasterField falls back to the CharField form field, so it was exposed by default in the admin, in ModelForms and in writable model serializers.

The fix works on two levels. RasterField now defaults editable to False, so it drops out of ModelForms unless you include it explicitly, and assigning a dict or JSON string that specifies a non-default driver raises GDALException. If you genuinely need another driver, wrap the definition in GDALRaster to mark the value as trusted.

Which version to move to, and what to check afterwards

Upgrade to 6.1.4, 6.0.11 or 5.2.20, matching the branch you're on. On 5.1 or older, the realistic plan is to land on 5.2 LTS first and climb from there.

bash
# pin the minor, take the patch
pip install -U "Django>=6.1.4,<6.2"
python -m django --version

# while you're here, sweep the rest of the dependency tree
pip install pip-audit && pip-audit

Five things worth walking through once the upgrade is in:

  1. Grep for __range everywhere and look for calls passing more than two values, or taking a list straight off the request.
  2. Grep for % in annotate() alias names and in JSONField key transforms, and rename them.
  3. If you use cache_page() or UpdateCacheMiddleware, clear the cache after upgrading. Existing poisoned entries are not invalidated for you.
  4. If you have a RasterField in the admin or a ModelForm, verify the form still does what you expect, because the field no longer appears on its own.
  5. Review staff accounts that only have view permission in the admin. The two remaining data-leak CVEs let that group enumerate values from the changelist, and multi-table inheritance models were affected by default.

How urgent is it? My take: the two high-severity issues should hit staging today and production on your next deploy, not next sprint. The preconditions are not exotic and the patch is small. The moderate and low ones ride along in the same upgrade; there's no reason to split them out.

The lesson worth keeping

The ORM protects the part it controls, which is the values it passes as parameters. What it does not protect is the names: column names, alias names, field names you assemble as strings yourself, plus anywhere you step out to extra(), RawSQL() or .raw(). Those are the spots that deserve extra attention in code review.

The practical rule: input from users may only ever be a value, never a name inside the query. When users genuinely need to pick a column or a sort order, map their input through an allow-list of constants in your code. It's the same shift the front-end tooling world is making with dev servers, which we wrote about in Vite 8.4's allow-list mode: stop guessing what's dangerous and state what's permitted instead.

The other half is making advisories come to you instead of you going to look for them, whether that's the Django mailing list, Dependabot or pip-audit in CI. Finding out about a SQL injection a week late is very different from finding out on day one. We made the same point about framework-level flaws in our write-up of the React Server Components RCE.

If your team is stuck on an old Django version and upgrades keep slipping, or you want dependency and advisory tracking handled properly, let's talk.

Talk to our team

Frequently asked questions

We use the ORM. Can we really still be hit by SQL injection?

Yes, when the conditions line up. The ORM parameterises the values you pass, but it does not sanitise alias names or key transforms that you build as strings. That's exactly where this flaw lives: an alias or key transform containing a percent sign, combined with __range getting more than two values or __in with a bilateral transform.

Our project is on Django 5.1. What now?

5.1 is not in the patched list. Supported series are 5.2 LTS, 6.0 and 6.1, so the shortest path is 5.2.20, which has extended support until April 2028, and then plan the move to 6.x.

What will break when we upgrade?

One backwards-incompatible change: range now takes exactly two values, so code passing more will raise an error immediately. The other behaviour change is RasterField no longer being editable by default, which removes it from ModelForms unless you add it explicitly.

We don't use GeoDjango at all. Do we still need this release?

Yes. CVE-2026-132092 is in the core ORM and has nothing to do with GIS. The RasterField and OGRGeometry issues are the ones you can skip if you're not on PostGIS.

Sources

  1. [1]Django Security Releases: 6.1.4, 6.0.11, 5.2.20 — Django Software Foundation, 2 Dec 2026
  2. [2]Django 6.1.4 release notes — Django Software Foundation, 2 Dec 2026
  3. [3]Download Django: supported versions — Django Software Foundation
  • Database
  • Django
  • OWASP
  • Python
  • Security Advisory

Written by

Niti Jirakarnwuttikrai

Full Stack Developer

Complex systems only create value when they turn into a simple experience.

Blog

Keep reading

All articles
Let's build together

Have an idea worth building?

Tell us about it, and we'll work out the best way to bring it to life.