Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/system_manage/views/system_chat_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class BatchDelete(APIView):
get_operation_object=lambda r, k: get_batch_delete_user_operation_object(r.data.get("ids", [])),
)
def post(self, request: Request):
return result.success(ChatUserSerializer.BatchDeleteInstance({"ids": request.data}).batch_delete())
return result.success(ChatUserSerializer.BatchDeleteInstance(data={"ids": request.data}).batch_delete())

class BatchAddGroup(APIView):
authentication_classes = [TokenAuth]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changed line is likely a correct DRF fix, but there is a more serious data-shape issue nearby.

1. data= keyword change

If ChatUserSerializer.BatchDeleteInstance is a DRF Serializer subclass, this change is correct:

BatchDeleteInstance(data={...})

In DRF, the first positional argument is instance, not data:

Serializer(instance=None, data=empty, **kwargs)

So the original:

BatchDeleteInstance({"ids": request.data})

would pass the dict as instance, not as input data, which is wrong for validation/serialization.

Caveat: If BatchDeleteInstance is a custom class, confirm its constructor actually accepts data=. If its signature is something like __init__(self, payload), the keyword change will break.

2. Main issue: request.data is likely nested incorrectly

This part is suspicious:

data={"ids": request.data}

If the client sends:

{"ids": [1, 2, 3]}

then request.data is:

{"ids": [1, 2, 3]}

and the serializer receives:

{"ids": {"ids": [1, 2, 3]}}

So ids becomes a dict instead of a list. That is almost certainly a bug.

It is also inconsistent with the operation-object lambda above:

get_operation_object=lambda r, k: get_batch_delete_user_operation_object(r.data.get("ids", [])),

which correctly extracts request.data.get("ids", []).

Suggested correction

def post(self, request: Request):
    ids = request.data.get("ids", [])
    serializer = ChatUserSerializer.BatchDeleteInstance(data={"ids": ids})

    if hasattr(serializer, "is_valid"):
        serializer.is_valid(raise_exception=True)

    return result.success(serializer.batch_delete())

If request.data can also be a raw list, handle that explicitly, but prefer a consistent payload shape:

{"ids": [1, 2, 3]}

3. Additional potential issues

  • Empty ids: Ensure ids=[] does not accidentally mean “delete all”. Reject empty lists.
  • Validation: Verify ids is a list of integers, not a string, dict, or arbitrary objects.
  • Batch size limit: Add a maximum number of IDs per request to avoid abuse or long-running transactions.
  • Authorization: Check that the requesting user is allowed to delete every ID, not just the first one.
  • Transactions: Wrap the batch delete in a transaction if partial deletion would be inconsistent.
  • Bulk operations: If batch_delete() loops and deletes one by one, optimize with filter(pk__in=ids).delete() or a bulk update/soft-delete.
  • Audit consistency: Use the same validated ids list for the operation object and the deletion logic.
  • QueryDict handling: If form-encoded requests are supported, request.data.get("ids") may not return a list. Use getlist("ids") or require JSON.

Verdict

The data= change is probably correct for DRF. However, the bigger issue is that {"ids": request.data} likely nests the whole request body under ids. Use request.data.get("ids", []) instead, and validate/authorize the IDs before deleting.

Expand Down
Loading