Closed Bug 2088814 Opened 27 days ago Closed 26 days ago

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)

defect
Points:
2

Tracking

(firefox161 fixed)

RESOLVED FIXED
161 Branch
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:

  1. session.subscribe to network.beforeRequestSent.
  2. network.addIntercept with phase beforeRequestSent, for a URL whose server echoes the request body.
  3. In the page: fetch(url, {method: "POST", body: "{'a': 1}"}) (8 bytes).
  4. On the blocked event: network.continueRequest with body: {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: nobody → jdescottes
Status: NEW → ASSIGNED
Pushed by jdescottes@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/7fe42eac8d6f https://hg.mozilla.org/integration/autoland/rev/3c4429ccc3a8 [bidi] Clear Content-Length when network.continueRequest replaces request body r=webdriver-reviewers,Sasha https://github.com/mozilla-firefox/firefox/commit/b0a650189091 https://hg.mozilla.org/integration/autoland/rev/1ce34a539236 [wdspec] Add test to check body received after network.continueRequest r=webdriver-reviewers,Sasha
Severity: -- → S3
Points: --- → 2
Priority: -- → P3
Whiteboard: [webdriver:m22]

Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/64759 for changes under testing/web-platform/tests

Whiteboard: [webdriver:m22] → [webdriver:m22], [wptsync upstream]
Status: ASSIGNED → RESOLVED
Closed: 26 days ago
Resolution: --- → FIXED
Target Milestone: --- → 161 Branch

Upstream PR merged by moz-wptsync-bot

Whiteboard: [webdriver:m22], [wptsync upstream] → [webdriver:m22][wptsync upstream][webdriver:relnote]

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.

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: