Skip to content

http: compute body chunk length from decoded bytes - #67615

Open
ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:http-encoded-chunk-length
Open

ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:http-encoded-chunk-length

Conversation

@ShogunPanda

Copy link
Copy Markdown
Contributor

OutgoingMessage computed the chunk length with Buffer.byteLength(string, encoding), which is only an upper bound for base64, base64url and hex input: whitespace and invalid characters are skipped while decoding. The announced chunk (or Content-Length) could therefore be larger than the bytes actually written.

This decodes such strings to a Buffer first, in write() and end(), so all length computations use the real byte count, and adds a test.

AI-assisted work. All changes reviewed by a human.

Convert base64, base64url and hex strings to a Buffer before computing
the chunk length, so the announced length matches the bytes written
even when the input contains characters skipped while decoding.

Assisted-By: OpenAI GPT-5.6 Sol <openai/gpt-5.6-sol>
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Nov 19, 2026
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.71%. Comparing base (d9e18d5) to head (02c9f1c).
⚠️ Report is 37 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67615      +/-   ##
==========================================
- Coverage   90.72%   90.71%   -0.02%     
==========================================
  Files         801      801              
  Lines      280809   280831      +22     
  Branches    54212    54217       +5     
==========================================
- Hits       254776   254753      -23     
- Misses      16209    16244      +35     
- Partials     9824     9834      +10     
Files with missing lines Coverage Δ
lib/internal/http/outgoing.js 98.02% <100.00%> (+0.02%) ⬆️

... and 34 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

// invalid pair).
function decodeLossyEncodedChunk(chunk, encoding) {
if (typeof chunk === 'string' && typeof encoding === 'string') {
switch (encoding.toLowerCase()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We have a normalizeEncoding method in internal/util. Would that be better?

@jairogonzalez79613-star

This comment was marked as off-topic.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

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

Labels

http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants