Skip to content

http.ServerResponse#strictContentLength = true is broken #57456

Description

@zefir-git

Version

v22.13.0

Platform

Linux razer 6.13.6-200.fc41.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Mar  7 21:33:48 UTC 2025 x86_64 GNU/Linux

Subsystem

http

What steps will reproduce the bug?

  1. Use following code
    import http from "node:http";
    http.createServer((req, res) => {
        res.strictContentLength = true;
        res.setHeader("content-length", "5");
        res.end("12345");
    }).listen(3000);
  2. Send any HTTP request to the server on port 3000

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior? Why is that the expected behavior?

Should not throw error.

What do you see instead?

Error [ERR_HTTP_CONTENT_LENGTH_MISMATCH]: Response body's content-length of 5 byte(s) does not match the content-length of 5 byte(s) set in header

node:_http_outgoing:1129
    throw new ERR_HTTP_CONTENT_LENGTH_MISMATCH(this[kBytesWritten], this._contentLength);
    ^

Error [ERR_HTTP_CONTENT_LENGTH_MISMATCH]: Response body's content-length of 5 byte(s) does not match the content-length of 5 byte(s) set in header
    at ServerResponse.end (node:_http_outgoing:1129:11)
    at Server.<anonymous> (file:///home/zefir/Projects/web-ts/test/test.js:5:9)
    at Server.emit (node:events:524:28)
    at parserOnIncoming (node:_http_server:1153:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17) {
  code: 'ERR_HTTP_CONTENT_LENGTH_MISMATCH'
}

Node.js v22.13.0

Additional information

No response

Activity

  1. marco-ippolito commented on Mar 14, 2025

    @marco-ippolito
    Member

    The issue is that you are passing the content-length value as a string, and its not being converted to number.
    Opened a PR to fix

  2. zefir-git commented on Mar 14, 2025

    @zefir-git
    Author

    Thanks. When setting headers with Headers (e.g. ServerResponse#setHeaders(Headers)) it’s impossible not to have the value as string.

  3. added a commit that references this issue on Mar 16, 2025
  4. added a commit that references this issue on Mar 23, 2025
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