network.continueRequest with a body keeps the original Content-Length header, server gets a truncated or incomplete body
Categories
(Remote Protocol :: WebDriver BiDi, defect, P3)
Tracking
(firefox161 fixed)
| Tracking | Status | |
|---|---|---|
| firefox161 | --- | fixed |
People
(Reporter: jdescottes, Assigned: jdescottes)
References
Details
(Whiteboard: [webdriver:m22][wptsync upstream][webdriver:relnote])
Attachments
(2 external files)
The spec says network.continueRequest with body sets the request body to the given bytes: https://w3c.github.io/webdriver-bidi/#command-network-continueRequest. The server should get the whole new body, with a matching Content-Length.
In Firefox, the request keeps its original Content-Length header when the request already had a body. The new body is sent, but the header still gives the old length.
Steps to reproduce:
session.subscribetonetwork.beforeRequestSent.network.addInterceptwith phasebeforeRequestSent, for a URL whose server echoes the request body.- In the page:
fetch(url, {method: "POST", body: "{'a': 1}"})(8 bytes). - On the blocked event:
network.continueRequestwithbody: {type: "string", value: "{'a': 12345678}"}(15 bytes).
Expected: the server gets Content-Length: 15 and the body {'a': 12345678}.
Actual: the server gets Content-Length: 8 and the body {'a': 12.
With a shorter new body, the opposite happens. The server gets the old, larger Content-Length, waits for more bytes, and the request hangs.
Side effect on later requests: the extra or missing bytes stay on the keep-alive connection, so the next request on that connection is broken. With an 8-byte body replaced by an empty body, wptserve logs code 400, message Bad HTTP/0.9 request type ('driver/tests/bidi/network/support/empty.html'). The server read the first 8 bytes of the next request (GET /web) as the missing body. In local runs, a page fetch right after such a request also failed with NetworkError when attempting to fetch resource.
Cause: NetworkRequest.setRequestBody (remote/shared/NetworkRequest.sys.mjs) calls explicitSetUploadStream(stream, null, -1, method, false). With a length of -1, HttpBaseChannel::InternalSetUploadStream measures the new stream and calls HttpBaseChannel::ExplicitSetUploadStreamLength. That function updates mReqContentLength, but returns early if a Content-Length header is already set. nsHttpTransaction::Init then limits the body to mReqContentLength, so the body and the header disagree. When the original request had no body, there is no Content-Length header yet, and the new length is used correctly.
We can clear the Content-Length request header in setRequestBody before replacing the upload stream.
Tests: the wdspec test network/continue_request/body.py only checks bodySize in network.responseCompleted, so it does not catch this. Its case that replaces {'a': 1} with an empty body even causes the 400 above, and still passes. I have a local test, test_request_body_received_by_server, that posts to a new body_echo.py handler and checks the body and Content-Length the server received:
- Firefox (local build 20261104095934): "longer" and "shorter" fail.
- Chrome 156: all cases pass.
Found while reviewing the MDN PR for continueRequest: mdn/content#46203.
| Assignee | ||
Comment 1•27 days ago
|
||
| Assignee | ||
Comment 2•27 days ago
|
||
Updated•27 days ago
|
| Assignee | ||
Updated•26 days ago
|
Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/64759 for changes under testing/web-platform/tests
Comment 5•26 days ago
|
||
| bugherder | ||
https://hg.mozilla.org/mozilla-central/rev/3c4429ccc3a8
https://hg.mozilla.org/mozilla-central/rev/1ce34a539236
Upstream PR merged by moz-wptsync-bot
Updated•12 days ago
|
Comment 7•4 days ago
|
||
A similar issue was reported in https://github.com/puppeteer/puppeteer/issues/15848, but in that case, the request body was replaced with an empty body. We should probably extend the test added in this bug to cover that scenario as well. I'll file a follow-up bug for it.
Description
•