-
Threat: Denial of Service (DoS) via Excessive Payload Size
- Description: An attacker sends a crafted HTTP request with an extremely large body (e.g., gigabytes of data). The attacker's goal is to overwhelm the server's resources (memory, CPU, disk I/O) by forcing
body-parser
to attempt to process this massive payload.body-parser
's default behavior, without explicit limits, is to attempt to buffer the entire request body. - Impact: The application becomes unresponsive, potentially crashing the server or making it unavailable to legitimate users. This results in a denial of service.
- Affected Component: All
body-parser
modules that handle request bodies:json()
,urlencoded()
,raw()
,text()
. The core issue is the handling of the request stream and buffering of data without sufficient limits. - Risk Severity: High
- Mitigation Strategies:
- Set
limit
option: This is the primary mitigation. Configure thelimit
option for each parser (e.g.,bodyParser.json({ limit: '100kb' })
) to a reasonable maximum size based on expected valid input. Use units like 'kb', 'mb', or 'gb'. This directly controlsbody-parser
's behavior. - Monitor Request Sizes: Implement monitoring to track request body sizes and alert on unusually large requests.
- Web Application Firewall (WAF): Use a WAF to enforce request size limits at the network edge, providing an additional layer of defense (though this is less direct).
- Set
- Description: An attacker sends a crafted HTTP request with an extremely large body (e.g., gigabytes of data). The attacker's goal is to overwhelm the server's resources (memory, CPU, disk I/O) by forcing
-
Threat: Denial of Service (DoS) via Content-Type Spoofing
- Description: An attacker sends a request with a large body and a deliberately incorrect
Content-Type
header. For instance, they might send a large text file but claim it'sapplication/json
. The attacker aims to forcebody-parser
to attempt parsing the data using an inappropriate parser, leading to excessive resource consumption or unexpected errors.body-parser
relies on theContent-Type
header to determine which parsing logic to apply. - Impact: Similar to the excessive payload size DoS, this can lead to resource exhaustion and application unavailability. It can also cause unexpected application behavior due to parsing errors.
- Affected Component: All
body-parser
modules:json()
,urlencoded()
,raw()
,text()
. The vulnerability lies in the reliance on theContent-Type
header for parser selection without sufficient validation. - Risk Severity: High
- Mitigation Strategies:
- Specify
type
option: Use thetype
option to explicitly define which content types each parser should handle (e.g.,bodyParser.json({ type: 'application/json' })
). This directly controls which requestsbody-parser
will process, preventing it from attempting to parse unexpected content types. - Validate
Content-Type
: Implement custom middleware beforebody-parser
to validate theContent-Type
header against a whitelist of allowed types if more complex logic is required (this is slightly less direct, but a good practice).
- Specify
- Description: An attacker sends a request with a large body and a deliberately incorrect
-
Threat: Denial of Service (DoS) via Slowloris (with
raw
parser)- Description: An attacker sends data very slowly, keeping the connection open for an extended period. This is particularly effective with the
raw
parser if no size limit is set. The attacker's goal is to tie up server resources (connections, threads) by maintaining many slow, incomplete requests.body-parser
'sraw
parser, without a limit, will continue to buffer the incoming data, even if it arrives very slowly. - Impact: The server becomes unable to handle legitimate requests due to resource exhaustion (connection limits, thread pool depletion). This results in a denial of service.
- Affected Component: Primarily the
raw()
parser, especially when used without alimit
. - Risk Severity: High
- Mitigation Strategies:
- Mandatory
limit
forraw()
: Always set a reasonablelimit
on theraw
parser (e.g.,bodyParser.raw({ limit: '1mb' })
). This directly limits the amount of databody-parser
will buffer. - Connection Timeouts: Configure server-level connection timeouts (e.g., in Node.js's HTTP server or a reverse proxy like Nginx) to automatically close connections that are idle or sending data too slowly (less direct, but important).
- Reverse Proxy/Load Balancer: Use a reverse proxy or load balancer that has built-in protection against Slowloris attacks (less direct).
- Mandatory
- Description: An attacker sends data very slowly, keeping the connection open for an extended period. This is particularly effective with the
-
Threat: Prototype Pollution (via
urlencoded
parser withextended: true
)- Description: An attacker crafts a malicious URL-encoded payload that exploits vulnerabilities in the
qs
library (or similar libraries used for extended parsing) to inject properties into theObject.prototype
. The attacker's goal is to modify the behavior of the application. This is a direct threat becausebody-parser
'surlencoded
parser, when configured withextended: true
, directly uses a vulnerable library (historicallyqs
, though it may be a different library now). The vulnerability is in the dependency, but the choice to useextended: true
enables it. - Impact:
- Denial of Service: By modifying core object behavior.
- Data Tampering: By altering expected values.
- Potential Remote Code Execution (RCE): In some cases.
- Affected Component: The
urlencoded()
parser when used with theextended: true
option. - Risk Severity: Critical
- Mitigation Strategies:
- Update Dependencies: Ensure
body-parser
and its dependencies are up-to-date. This is the most direct way to address vulnerabilities in the underlying parsing library. - Use
extended: false
: If nested object parsing is not required, useextended: false
(e.g.,bodyParser.urlencoded({ extended: false })
). This directly avoids using the potentially vulnerable extended parsing library. - Prototype Pollution Protection Libraries: Consider using libraries specifically designed to mitigate prototype pollution (less direct, but a good defense-in-depth measure).
- Freeze Object Prototypes: Use
Object.freeze(Object.prototype)
to prevent modifications to the Object prototype.
- Update Dependencies: Ensure
- Description: An attacker crafts a malicious URL-encoded payload that exploits vulnerabilities in the