2
votes

I've noticed something in Django formsets:

Each formset.form has a hidden field with the id of the model being edited. All one would need to do is change this id and submit, and the default formset clean() or save() methods don't bat an eye.

I would think that it wouldn't be too complicated to keep track of which objects were in the original formset queryset, and compare them on save. I wonder why Django doesn't do this already?

As for me, I'm checking permissions on formset save like:

if formset.is_valid():
    for fs in formset:
        report = fs.save(commit=False)
        if can_edit(request.user, report):
            report.save()

Anyone else notice this issue? I'd be curious to see how you've addressed it.

1
As far as I remember, it's not possible to edit an object with pk y (which isn't in the queryset) by changing the hidden id from x to y. If you have found otherwise, I think that's a security issue and it would be better to report it privately rather than posting on Stack Overflow. I don't think there's a specific reason for the current lack of error message - I think a patch would be welcomed. - Alasdair
It is possible (provided the request.user has edit permission on the model). I've just tried it. - Rob L
Regular model formsets do not check request.user at all. Are you talking about the Django admin? - Alasdair
No, I'm talking about a regular formset. I assumed Django was checking. In that case, it's even worse. - Rob L

1 Answers

0
votes

If you have the necessary permissions to edit that other object, then why should Django prevent you from doing so? HTTP requests are stateless, so when a POST comes in, Django will check if you are allowed to perform the request.

So, let's say you are authenticated and have permission to change Comment objects. Your form has a comment with id=1. You change the hidden field to id=2 and submit the form. Django will check your permissions and see that you are allowed to change any Comment instance and apply the POSTed change.