Skip to content

Content-Length with trailing whitespace fails with UND_ERR_RES_CONTENT_LENGTH_MISMATCH (regression since #5124) #6135

Open

Description

@MattieTK

Bug Description

Since undici 8, a response whose Content-Length value is followed by trailing whitespace fails with UND_ERR_RES_CONTENT_LENGTH_MISMATCH, for both fetch() and request(), even though the body length matches.

Trailing optional whitespace is valid HTTP/1.1. RFC 9112 §5 defines field-line = field-name ":" OWS field-value OWS, so the whitespace isn't part of the value. Some embedded servers send it: Mongoose's mg_http_reply() reserves a fixed-width field and back-fills it with %-10lu, giving e.g. Content-Length: 25 . I hit this with a BUSY Bar device: every one of its HTTP API endpoints fails with Node 26's built-in fetch, while Node 24's fetch, node:http and curl all work.

The cause is the stateless Content-Length parsing added in #5124 (lib/dispatcher/client-h1.js):

for (let i = 0; i < buf.length; i++) {
  this.contentLength = this.contentLength * 10 + (buf[i] - 0x30)
}

llhttp passes the trailing spaces through in the header value, and each space (0x20) is folded in as a "digit" worth -16. 5 plus 9 spaces therefore yields a large garbage number instead of 5, and the body-length check fails. Before #5124 the value went through parseInt(), which ignores trailing whitespace.

Reproduction

Standalone reproduction script:

'use strict'

const { test } = require('node:test')
const { createServer } = require('node:net')
const { once } = require('node:events')
const { fetch, request } = require(process.env.UNDICI || 'undici')

// node:http normalizes header values, so the raw response is written with node:net.
// RFC 9112 §5: field-line = field-name ":" OWS field-value OWS — the trailing spaces are optional whitespace,
// not part of the Content-Length value. Mongoose's mg_http_reply() pads Content-Length this way.
const RESPONSE = 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5         \r\n\r\nhello'

async function startServer (t) {
  const server = createServer((socket) => {
    socket.once('data', () => socket.write(RESPONSE))
  })
  t.after(() => server.close())
  server.listen(0)
  await once(server, 'listening')
  return `http://127.0.0.1:${server.address().port}/`
}

test('fetch accepts Content-Length with trailing whitespace', { timeout: 60000 }, async (t) => {
  const url = await startServer(t)
  const res = await fetch(url)
  t.assert.strictEqual(await res.text(), 'hello')
})

test('request accepts Content-Length with trailing whitespace', { timeout: 60000 }, async (t) => {
  const url = await startServer(t)
  const { body } = await request(url)
  t.assert.strictEqual(await body.text(), 'hello')
})

node --test repro-ows.js: both tests fail on undici 8.12.3 and pass on 7.30.1 (UNDICI=<7.x install> node --test repro-ows.js).

Expected Behavior

The trailing optional whitespace is ignored: Content-Length is 5 and the body hello is returned, as in undici 7.x and node:http.

Actual Behavior

[cause]: Error [ResponseContentLengthMismatchError]: Response body length does not match content-length header
  code: 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH'

Environment

  • OS: macOS 27.2
  • Node.js version: v24.22.2 (npm undici) and v26.13.0 (built-in undici 8.12.2)
  • undici version: 8.12.3 (fails); 7.30.1 (works)

Additional context

A possible fix is to skip SP/HTAB in that loop. llhttp has already validated the header value by that point.

for (let i = 0; i < buf.length; i++) {
  const c = buf[i]
  if (c === 0x20 || c === 0x09) continue // trailing OWS, RFC 9112 §5
  this.contentLength = this.contentLength * 10 + (c - 0x30)
}

Activity

  1. hamarshehmhmd commented on Nov 6, 2026

    @hamarshehmhmd

    Confirmed on current main — trailing OWS on Content-Length is folded into the digit loop and produces a garbage length, then UND_ERR_RES_CONTENT_LENGTH_MISMATCH.

    Root cause: #5124 replaced parseInt() with a hand-rolled loop in lib/dispatcher/client-h1.js. llhttp passes trailing SP/HTAB through in the header value; each 0x20 is treated as a "digit" worth -16, so 5 + spaces becomes a huge number. Trailing OWS is valid per RFC 9112 §5.

    Fix:

    for (let i = 0; i < buf.length; i++) {
      const c = buf[i]
      // Trailing OWS is valid per RFC 9112 §5 (field-line = field-name ":" OWS field-value OWS).
      if (c === 0x20 || c === 0x09) continue
      this.contentLength = this.contentLength * 10 + (c - 0x30)
    }

    Verified (Node 22; undici needs zstd):

    • Added test/fetch/content-length-ows.js with the issue's raw node:net server (Content-Length: 5 + hello)
    • Both fetch and request paths return hello
    • node --test test/fetch/content-length-ows.js → 2 passed

    Happy to open a PR.

  2. added a commit that references this issue on Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions