Skip to content

fix(request): reject non-ok responses in Request.prototype.text - #1254

Open
dennev wants to merge 1 commit into
nodejs:mainfrom
dennev:fix/request-text-http-error
Open

dennev wants to merge 1 commit into
nodejs:mainfrom
dennev:fix/request-text-http-error

Conversation

@dennev

@dennev dennev commented Nov 25, 2026

Copy link
Copy Markdown

Fixes #1253

Request.prototype.text() previously returned res.text() without checking res.ok. When getCollaborators() fetches https://raw.githubusercontent.com/nodejs/node/HEAD/README.md, Commit Queue failure comments on nodejs/node#67292 (workflow run 45238634578 at 1a8cf16, landed at 642300f7aa), #63746 (workflow run 28791968081), and #62795 (workflow run 24673051566) record the README parse error while those workflow runs finished with overall success and #67292 had #### TSC voting members at both pinned revisions. Because live requests fetched the mutable HEAD URL without logging raw HTTP details, their actual status and body remain unknown; a mocked 503 Service Unavailable HTML response reproduces the Couldn't find #### TSC voting members in the README failure because Request.prototype.text() passes the response body to parseCollaborators(), while rejections from request.text() or fs.readFileSync(readme) bypassed the try/catch spinner cleanup.

Changes:

  • Extract a module-private fetchResponse() helper in lib/request.js shared by Request.prototype.text() and Request.prototype.json(), preserving Object.create(Request.prototype) callers in tests.
  • Check if (!res.ok) in Request.prototype.text(), guard await res.body?.cancel() in try/catch (attaching cancellation rejections, including falsy rejection reasons, as cause on the HTTP status error so body cleanup never masks it), omit trailing whitespace on empty res.statusText, and throw Unable to fetch <url>: <status>.
  • Keep Request.prototype.json() reading res.text() directly so callers handling non-2xx JSON payloads (query(), getTriagedReports(), PRBuild.prototype.getResults(), and CNA helpers) keep their existing behavior.
  • Move README loading inside the try/catch block in lib/collaborators.js so request.text() or fs.readFileSync(readme) failures stop the spinner cleanly, and report 'nodejs/node/README.md' when argv.readme is omitted (omitting the option selects the remote README).
  • Wrap this.req.text(...) in VotingSession.prototype.decryptKeyPart() (lib/voting_session.js) with try/catch so rejecting HTTP responses stop the download spinner as failed before runPromise() exits in components/git/vote.js.
  • Add unit tests in test/unit/request.test.js, test/unit/collaborators.test.js, and test/unit/voting_session.test.js covering ok text, 503 with body cancellation, 502 without res.body, errored ReadableStream cancel failure (TypeError('terminated') and falsy false/undefined reasons), empty statusText, non-ok request.json(), request.text() rejection spinner cleanup, missing file at the supplied argv.readme path (ENOENT in a t.after-cleaned directory), non-node argv.repo spinner reporting, and VotingSession.prototype.decryptKeyPart() HTTP 503 spinner cleanup.

Verification:

  • Linux container (Node v24.22.2): before the fix (on v7.8.0 and main 27468f5), 10 of the 12 new tests across test/unit/request.test.js, test/unit/collaborators.test.js, and test/unit/voting_session.test.js fail (49 total, 39 passing, 10 failing: non-ok request.text() body-present and missing-body cases resolve instead of rejecting; the TypeError('terminated') stream case rejects with 'terminated'; the empty-statusText case throws TypeError: res.text is not a function; the falsy-cancel test rejects with false where err.message === undefined; getCollaborators() throws Couldn't find #### TSC voting members in the README on mocked 503 HTML or skips stopSpinner on fetch rejection or ENOENT, including a fetch rejection for another repo; and VotingSession.prototype.decryptKeyPart() completes the mocked '<html>503</html>' fetch with 'Download complete' (Service Unavailable statusText) and throws TypeError: Cannot read properties of undefined (reading 'map') instead of rejecting with HTTP 503 and failing the spinner), and on final commit fadebda682 npm test, npm run lint, and npm run coverage:ci passed (793 of 793 tests passing, 0 failed).

AI disclosure: AI-assisted coding tools were used to investigate the cause, draft the fix, tests, and PR text, and review edge cases. I reviewed the diff, checked the failure logs and callers myself, and wrote/edited this description personally.

Happy to adjust the error message or scope if maintainers prefer another approach.

Check res.ok in Request.prototype.text() and throw an HTTP status error
instead of returning non-ok error bodies to plain-text callers,
guarding res.body?.cancel() so cancellation failures attach as cause
on the thrown HTTP status error instead of masking it. Omit trailing
whitespace when statusText is empty. Keep Request.prototype.json()
reading res.text() directly so callers can still parse non-2xx JSON
payloads.

Move README loading in getCollaborators() inside its try/catch so
fetch and file read errors stop the spinner as failed and rethrow
the original error rather than leaving the spinner running, report
nodejs/node/README.md for failed remote fetches, and stop the
download spinner as failed in VotingSession.prototype.decryptKeyPart().

Fixes: nodejs#1253
Signed-off-by: dennev <dennev13@gmail.com>

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.

Request.prototype.text() ignores non-ok HTTP statuses, causing getCollaborators() to report a misleading README parse error

1 participant