- CVE-2026-124827, nicknamed Django4Shell
- Vulnerable component:
django.contrib.gis.db.models.RasterField(GeoDjango) - Affected: Django 6.1 before 6.1.4, 6.0 before 6.0.11, 5.2 before 5.2.20 — 5.1, 5.0 and 4.2 were not evaluated
- Fixed in: 6.1.4 / 6.0.11 / 5.2.20 (2026-12-02)
- CVSS 3.1: 8.8 (High)
- CWE-73 (External Control of File Name or Path)
- Reported by Mohammed Fathy (Robusta Technology Group)
What follows is one HTTP request, traced from the moment it arrives to the moment it leaves a file sitting on disk. On paper the request is a plain admin form submission. There is no database attack in it, no file-upload endpoint. By the end of it, though, content the attacker chose has been written to a path the attacker chose. Let's follow it.
The request arrives
The attacker is already logged in with a staff account. The target is the admin add/change view for a model that declares a RasterField. RasterField only exists on the PostGIS backend, but — and this matters — it has no form field of its own. Field.formfield() falls back to a plain forms.CharField when no form_class is set, and the GIS admin mixin only checks isinstance(db_field, models.GeometryField); RasterField is a sibling of that class, not a subclass. So in the admin, this field renders as an ordinary text box.
Into that text box the attacker pastes the following JSON:
{
"driver": "EHdr",
"name": "/app/sitecustomize.py",
"width": 64,
"height": 1,
"srid": 4326,
"nr_of_bands": 1,
"datatype": 1,
"origin": [0, 0],
"scale": [1, -1],
"bands": [{"data": [105, 109, 112, 111, 114, 116, 32]}]
}
It doesn't matter yet what this JSON does. What matters is that this string, sitting in a form field, is now on its way to the server as POST /admin/app/rastermodel/add/.
Stored, but nothing has happened yet
The server's first encounter with this value is in ModelForm._post_clean(), inside construct_instance(). What happens there is a plain assignment:
setattr(instance, "rast", '{"driver": "EHdr", ...}')
rast isn't an ordinary attribute — it's a descriptor called SpatialProxy. And its __set__ looks like this:
# django/contrib/gis/db/models/proxy.py — SpatialProxy (before the fix)
def __set__(self, instance, value):
...
instance.__dict__[self.field.attname] = value # stored as-is, no checks
Worth pausing here. Why no checks? Performance. When GeoDjango pulls back a queryset of thousands of raster rows, it doesn't want to eagerly convert every one of them into a GDAL object — that would be slow. So __set__ keeps the value as raw material to be converted later, and defers the actual conversion to whoever eventually reads it. A reasonable design — except that it splits "storing a value" and "turning that value into a file" into two separate events, and nothing in this snippet tells you when the second one happens.
Validated, but reading is enough
ModelForm.is_valid() calls instance.full_clean() from inside _post_clean(). full_clean() checks every model field in turn, and checking a field means reading it.
# django/db/models/base.py — Model.clean_fields()
for f in self._meta.fields:
if f.name in exclude or f.generated:
continue
raw_value = getattr(self, f.attname) # the descriptor's __get__ fires here
That one getattr closes the gap between "stored" and "turned into a file." SpatialProxy.__get__ takes the stored string and hands it to a loader function to produce the real GDAL object — and that loader is gdal.GDALRaster.
def __get__(self, instance, cls=None):
...
geo_obj = self._load_func(geo_value) # == GDALRaster(the JSON string we stored earlier)
Laid out as a call stack:
POST /admin/app/rastermodel/add/
└─ ModelForm.is_valid()
└─ _post_clean()
├─ construct_instance() # setattr → __set__, still harmless
└─ instance.full_clean()
└─ clean_fields()
└─ getattr(instance, "rast") # __get__ fires
└─ GDALRaster(dict) # danger starts here
Notice what's missing: model.save() has not been called. No database connection, no table, no PostGIS extension appears anywhere in this chain. Validation — supposedly the safest step in the whole pipeline — is the step where the actual damage happens.
Where GDALRaster decides to write
GDALRaster.__init__ branches on input type, and a dict takes this path:
# django/contrib/gis/gdal/raster/source.py — GDALRaster.__init__ (dict branch)
elif isinstance(ds_input, dict):
self._write = 1
driver = Driver(ds_input.get("driver", "MEM")) # in-memory by default
...
self._ptr = capi.create_ds(
driver._ptr,
force_bytes(ds_input.get("name", "")), # this string becomes the path
ds_input["width"], ds_input["height"],
...
)
The branch that decides everything is driver. If it's left at the default, MEM, name is just a label — nothing touches disk. The attacker set driver to "EHdr" instead. The instant that happens, name is promoted from a label to an actual file path, and the entity making that decision is not the server — it's the attacker-supplied input itself. Whether this request writes a file at all is the attacker's call, not Django's.
EHdr (ESRI .hdr Labelled) is a raw binary raster driver that ships in essentially every standard GDAL build. GDAL's own creation code (frmts/raw/ehdrdataset.cpp) shows exactly why that matters:
VSILFILE *fp = VSIFOpenL(pszFilename, "wb"); // opens the given name verbatim, no extension added
VSIFWriteL(bandBytes, ...) // band data written as-is
No extension gets appended. Put /app/sitecustomize.py in name, and a file by that exact name is created. With datatype: 1 (GDT_Byte), the band array is just a byte array, so whatever goes in there can be Python source code. Decode the payload's bands[0].data array, [105, 109, 112, 111, 114, 116, 32], as ASCII and you get "import " — in a real attack, that array holds the full byte sequence of a reverse shell or a backdoor.
The path, drawn out
The chain so far — one request, stored, read during validation, turned into a file — looks like this:
After the file lands, it's Python's problem
The choice of /app/sitecustomize.py isn't arbitrary. That file is imported automatically at interpreter startup, before any application code runs — whether it sits in the working directory or anywhere else on sys.path. Dropping a .pth file into site-packages achieves the same thing.
So the attacker doesn't even need to wait around. A gunicorn worker restart, a worker fork during a rolling deploy, a runserver reload triggered by a code change, or a single manage.py cron run from the same codebase — any one of these is enough for the attacker's code to execute with the privileges of the web server's OS user. The same primitive can just as easily overwrite application source or drop a web shell into a static directory.
Why this is the third time this sink got patched
This isn't the first fix in this area — it's the third.
-
2026-08-04 (CVE-2026-15307): closed the query (lookup) path —
filter(rast=...)— wheredict/strvalues could flow straight intoGDALRaster. The advisory at the time stated plainly that "assignments to model fields are unaffected and still accept these input types." -
2026-10-06 (CVE-2026-87890): closed a
bytes-value bypass on that same lookup path, where a crafted VRT document could trigger an outbound request. The advisory described it as something "overlooked in the fix for CVE-2026-15307." - 2026-12-02 (this CVE): closes the assignment path — the exact path August's advisory said was unaffected.
August's fix locked the door on values flowing through a query, on the premise that assigning a value to a model field was the application's own responsibility to validate. This third patch is a quiet admission that the premise didn't hold up in practice. GDALRaster(ds_input) has no way to know, on its own, whether its input is trustworthy — so defenses keep getting bolted onto whichever caller is discovered to be missing one.
What the patch actually changes
The patch (main commit a3934abd, plus three backports) adds two different kinds of defense to a single class:
class RasterField(BaseSpatialField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("editable", False) # (1) excluded from forms by default
def contribute_to_class(self, cls, name, **kwargs):
...
setattr(cls, self.attname,
SpatialProxy(gdal.GDALRaster, self, self.init_raster)) # (2) new read hook
def init_raster(self, ds_input):
value = gdal.GDALRaster._preprocess_input(ds_input)
if (isinstance(value, dict)
and (driver := value.get("driver", "MEM")) is not None
and Driver(driver).name != "MEM"):
raise GDALException(
"Only in-memory (MEM) data definitions can be assigned to "
f"RasterField (got: {driver!r}). Wrap the definition in "
"GDALRaster() to use another driver.")
return gdal.GDALRaster(value)
Map this back onto the call stack we traced and it's clear exactly where each piece breaks the chain. With editable=False, a ModelForm no longer includes the field at all — so back at "the request arrives," there's no text box left for the attacker to put a value into. But some code paths call full_clean() directly on an instance, where the validation-exclusion list is empty and that getattr still fires — that's the path init_raster closes. Now, at "validated, but reading is enough," the descriptor no longer silently passes through a dict/JSON with a non-default driver; it raises instead. It doesn't matter whether the caller is the admin, a serializer, or a management command — the same check applies.
What the patch doesn't close is just as clear. The string branch of GDALRaster.__init__ checks os.path.exists() to decide whether it's looking at a local path, but a /vsicurl/ or /vsizip/ prefix skips that check entirely and fires off a network request. This patch only restricts the driver on a dict, so a string input can still cause SSRF — the advisory says exactly that. And because editable=False is now the default, any existing application that edited rasters through the admin quietly loses that ability on upgrade, which is worth checking before deploying.
What to do now
If you can patch, that's the first move.
pip install -U "Django>=6.1.4" # >=6.0.11 on the 6.0 line, >=5.2.20 on 5.2
If an immediate upgrade isn't possible, this bug needs three things at once — the field exposed in a form, a value assigned, that value read — so cutting any one of them helps.
- Remove
RasterFieldfrom the admin viaexclude/readonly_fields, and fromModelForm.Meta.exclude. - If a writable serializer accepts the field, make it read-only, or reject incoming
dict/JSON during validation. - Run the process with
GDAL_SKIP=EHdr. Treat this as a secondary measure only —GTiff,PNG,HFAand other writable drivers are still there. - In containers, mount the application directory and
site-packagesread-only, so there's nowhere for asitecustomize.pyor.pthfile to land.
There's one detection clue worth knowing. The EHdr driver writes a sidecar .hdr text file next to the data file. A successful attack is likely to leave a stray .hdr file — containing things like ncols, nrows, cellsize — somewhere in the application directory.
find /app /srv -xdev -name '*.hdr' -newermt '-14 days' -exec head -c 200 {} \;
find / -xdev -name 'sitecustomize.py' -newer /app/manage.py 2>/dev/null
A sitecustomize.py can legitimately exist, so the question isn't whether one is there but when it appeared and who wrote it. Cross-reference against staff-account admin POSTs from the same window, worker restarts, and any unexpected outbound process from the container.


Top comments (0)