Background REST API bulk writes are dropping request headers and query string
NetBox Edition
NetBox Community
NetBox Version
4.7.1
Python Version
3.12
Steps to Reproduce
- Install netbox-branching 1.2.1. Create a branch and wait until its status is Ready. Note its schema ID (
<schema_id>below). - In main, create two devices, A (ID 1) and B (ID 2).
- Make sure an RQ worker is running.
- Control. Delete device A in the branch with a synchronous bulk delete:
curl -X DELETE \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "X-NetBox-Branch: <schema_id>" \
"http://netbox:8000/api/dcim/devices/" \
--data '[{"id": 1}]'- Delete device B the same way, but in the background:
curl -X DELETE \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "X-NetBox-Branch: <schema_id>" \
"http://netbox:8000/api/dcim/devices/?background=true" \
--data '[{"id": 2}]'- Wait for the job to complete. Check device B in main and in the branch.
Expected Behavior
Device B is deleted in the branch only, as device A is in step 4. Main still has device B.
Observed Behavior
Step 4 works: device A is gone in the branch and still exists in main.
Step 5 returns 202 Accepted, and the job completes with status_code: 204. But the worker deletes device B in main. The branch still has device B, and main has a delete change record for it. The same happens when the branch is selected with ?background=true&_branch=<schema_id> instead of the header.
Suspected Cause
BackgroundOperationMixin._enqueue_bulk_job() snapshots the request with copy_safe_request() (mixins.py#L345-L347). Since #21468 (PR #21577), that snapshot keeps non-sensitive HTTP_* headers and QUERY_STRING (request.py#L51-L58). So X-NetBox-Branch is in the job payload.
The worker then rebuilds the request in AsyncAPIJob._build_request() (jobs.py#L291-L326). The new environ copies only the host and X-Forwarded-* keys (L317-L322). It drops all other headers and the query string. The action then runs inside apply_request_processors() (L386), but a plugin request processor such as netbox-branching's ActiveBranchContextManager cannot see the header or _branch parameter any more.
The enqueue step also sets request_copy.COOKIES = {} (mixins.py#L347), so a branch selected through the UI cookie is lost too.
Bulk create and bulk update use the same background path, so they are probably affected as well. I tested only bulk delete.
Proposed Fix
This is the same class of problem as netboxlabs/netbox-branching#434 (custom scripts ignored X-NetBox-Branch), which #21468 fixed for copy_safe_request().
A possible fix: in _build_request(), copy every HTTP_* key and QUERY_STRING from request_copy.META. copy_safe_request() already removes the sensitive headers.
Source: netbox-community/netbox