Skip to content

Security: limit memory consumption in receiveBody#341

Open
tomaioo wants to merge 9 commits into
metarhia:masterfrom
tomaioo:fix/security/unbounded-memory-consumption-in-receiveb
Open

Security: limit memory consumption in receiveBody#341
tomaioo wants to merge 9 commits into
metarhia:masterfrom
tomaioo:fix/security/unbounded-memory-consumption-in-receiveb

Conversation

@tomaioo

@tomaioo tomaioo commented Jul 14, 2026

Copy link
Copy Markdown

Summary

Security: Unbounded Memory Consumption in receiveBody

Problem

Severity: High | File: lib/network.js:L3

The receiveBody function reads an incoming stream into memory by continuously pushing chunks into an array and concatenating them at the end. There is no limit on the maximum body size. An attacker could send a massive request body to exhaust server memory, leading to a Denial of Service (DoS).

Solution

Implement a maximum body size limit. Track the total length of received chunks and abort the request (or throw an error) if it exceeds a predefined threshold.

Changes

  • lib/network.js (modified)

The `receiveBody` function reads an incoming stream into memory by continuously pushing chunks into an array and concatenating them at the end. There is no limit on the maximum body size. An attacker could send a massive request body to exhaust server memory, leading to a Denial of Service (DoS).

Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>

@tshemsedinov tshemsedinov 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.

Looks fine except limit

Comment thread lib/network.js Outdated
'use strict';

const receiveBody = async (stream) => {
const MAX_BODY_SIZE = 0xffffffff;

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.

4Gb gives attacker gives the attacker great opportunities and node.js will fail allocating such an array

Comment thread lib/network.js Outdated
@tshemsedinov

Copy link
Copy Markdown
Member
  • Update .d.ts receiveBody(stream: IncomingMessage, limit?: number)
  • Need tests verifying limit
  • Update reamde

Comment thread lib/network.js
Co-authored-by: Timur Shemsedinov <timur.shemsedinov@gmail.com>
@tshemsedinov tshemsedinov changed the title Security: Unbounded Memory Consumption in receiveBody Security: limit memory consumption in receiveBody Jul 14, 2026
…great opportunities an

Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
Comment thread lib/network.js
tshemsedinov and others added 3 commits July 14, 2026 12:16
…great opportunities an

Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
const MAX_BODY_SIZE = 10 * 1024 * 1024;
```

Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
@tshemsedinov

Copy link
Copy Markdown
Member

Tests fails @tomaioo because you removed new line at the end of the file

Comment thread lib/network.js
tshemsedinov and others added 3 commits July 16, 2026 19:42
…great opportunities an

Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
const MAX_BODY_SIZE = 10 * 1024 * 1024;
```

Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
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.

2 participants