Opened 3 weeks ago

Closed 3 weeks ago

#37264 closed Cleanup/optimization (fixed)

Admin popup add view crashes on malformed `_source_model` value

Reported by: Adam Johnson Owned by: Adam Johnson
Component: contrib.admin Version: 6.1
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Regression in b1ffa9a9d78b0c2c5ad6ed5a1d84e380d5cfd010 (Fixed #13883 -- Rendered named choice groups with <optgroup> in FilteredSelectMultiple)o.

ModelAdmin.response_add() now reads a _source_model POST parameter and unpacks it with:

app_label, model_name = source_model_name.split(".", 1)

…before any error handling.
A value without an app_label.model_name dot separator raises ValueError: not enough values to unpack (expected 2, got 1), returning an HTTP 500 after the object has already been saved.
Any user with add permission can trigger it via a crafted POST or third-party JavaScript setting a bad value.
In Django 6.0, unknown POST keys were ignored and popup add always succeeded.

Minimal reproduction: as a user with add permission, POST to any admin add view with an otherwise valid form plus _popup=1&_source_model=foo (no dot).
The object is saved, then the response is a 500.

Change History (3)

comment:1 by Jacob Walls, 3 weeks ago

Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Thanks for finding this missed case; let's fix it. I would just chop the release note, as I don't think we really need to backport this. Additional views welcome.

comment:2 by Jacob Walls, 3 weeks ago

Triage Stage: AcceptedReady for checkin

comment:3 by Jacob Walls <jacobtylerwalls@…>, 3 weeks ago

Resolution: fixed
Status: assignedclosed

In 616e8c5:

Fixed #37264 -- Handled further malformed _source_model values in admin popups.

ModelAdmin.response_add() unpacked the _source_model POST parameter with
source_model_name.split(".", 1) before any error handling, so a value
without a dot separator, like _source_model=foo, raised ValueError and
returned an HTTP 500 after the object had already been saved.

This fix handles the ValueError together with the existing LookupError branch for invalid values.

Follow-up to b1ffa9a9d78b0c2c5ad6ed5a1d84e380d5cfd010.

Note: See TracTickets for help on using tickets.
Back to Top