Skip to content

Handle cancellation, upload failures, and interim responses in mesh fetch - #941

Open
ersinkoc wants to merge 4 commits into
google:mainfrom
ersinkoc:audit/js-mesh-fetch
Open

ersinkoc wants to merge 4 commits into
google:mainfrom
ersinkoc:audit/js-mesh-fetch

Conversation

@ersinkoc

Copy link
Copy Markdown

Fix three independently reproduced failure modes in the JavaScript mesh HTTP client.

  • Reject an already-aborted request before opening a libp2p stream.
  • Abort the opened stream if preparing the request body fails.
  • Read past informational HTTP responses such as 103 Early Hints and return the final response.

Each fix is a separate commit with a deterministic regression. The tests use local fakes and ordinary HTTP response sequences.

Linux and Node.js 22 passed all 157 SDK tests, the type check, the package and example builds, and the browser bundle.

@ersinkoc
ersinkoc requested a review from aojea as a code owner November 24, 2026 23:07

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request improves request abort handling, prevents stream leaks on unreadable request bodies, and adds support for handling 1xx informational responses (excluding 101 protocol switching) in fetchOverStream. It also includes comprehensive unit tests for these scenarios. The review feedback recommends limiting the maximum number of consecutive informational responses to prevent potential denial of service (DoS) or infinite loop attacks from malicious peers.

Comment thread sdk/js/src/libp2p-http.ts Outdated
Comment on lines +663 to +668
while (head.status >= 100 && head.status < 200) {
if (head.status === 101) {
throw new HTTPParseError("protocol switching is not supported by mesh fetch");
}
head = await readResponseHead(reader);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

security-medium medium

To prevent potential denial of service (DoS) or infinite loops from a misconfigured or malicious peer sending an endless sequence of 1xx informational responses, we should limit the maximum number of informational responses we are willing to parse before throwing an error.

    let head = await readResponseHead(reader);
    let infoResponsesCount = 0;
    while (head.status >= 100 && head.status < 200) {
      if (head.status === 101) {
        throw new HTTPParseError("protocol switching is not supported by mesh fetch");
      }
      if (++infoResponsesCount > 10) {
        throw new HTTPParseError("too many informational responses");
      }
      head = await readResponseHead(reader);
    }

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed, taken in c7ab00a. Without a caller deadline, a peer that answers with 1xx heads and never a final response kept the fetch reading indefinitely. The loop now reads at most MAX_INFORMATIONAL_RESPONSES (10) of them, which leaves room for a 100 Continue, a 103 Early Hints and a few 102 Processing, and then aborts the stream with an HTTPParseError. The test pins the edge: ten informational heads followed by a 200 are read through, and eleven are refused with the stream aborted.

A peer that answered with 1xx heads and never a final response kept
fetchOverStream reading for as long as the caller had no deadline. Read
at most MAX_INFORMATIONAL_RESPONSES of them, then abort the stream with
an HTTPParseError; the test covers ten heads accepted and eleven refused.
@ersinkoc
ersinkoc force-pushed the audit/js-mesh-fetch branch from 82d32f5 to c7ab00a Compare November 25, 2026 13:46

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant