Skip to content

Latest commit

 

History

History
62 lines (51 loc) · 10.4 KB

File metadata and controls

62 lines (51 loc) · 10.4 KB

Attack Surface Analysis for expressjs/body-parser

  • Description: Attackers send excessively large request bodies to overwhelm server resources (memory, CPU), leading to service disruption or unavailability.
  • Body-Parser Contribution: body-parser attempts to parse request bodies. Without size limits, it will try to process and store arbitrarily large payloads in memory, directly contributing to resource exhaustion.
  • Example: An attacker sends a POST request with a Content-Length header indicating a multi-gigabyte JSON payload. bodyParser.json() attempts to parse this massive payload, consuming server memory and potentially crashing the application.
  • Impact: Server resource exhaustion (memory, CPU), application slowdown, service unavailability for legitimate users, complete service outage.
  • Risk Severity: High
  • Mitigation Strategies:
    • Implement limit option: Crucially configure the limit option for each parser (json, urlencoded, raw, text) to strictly enforce maximum allowed request body sizes. Choose limits based on expected legitimate data volumes and server capacity. Example: bodyParser.json({ limit: '100kb' }).
    • Web Application Firewall (WAF): Deploy a WAF to filter out requests with excessively large Content-Length headers before they reach the application, providing an initial layer of defense.
  • Description: Attackers send request bodies with deeply nested or highly complex data structures (e.g., deeply nested JSON objects or arrays, numerous URL-encoded parameters). Parsing these structures can consume excessive CPU time, leading to DoS.
  • Body-Parser Contribution: body-parser's json() and urlencoded() parsers are responsible for processing these complex structures. The computational cost of parsing increases significantly with nesting depth and complexity, directly impacting CPU usage.
  • Example: An attacker sends a JSON payload with hundreds of levels of nested objects. bodyParser.json() attempts to parse this deeply nested structure, consuming excessive CPU resources and potentially blocking the server's event loop, causing unresponsiveness.
  • Impact: CPU exhaustion, application slowdown, service unavailability, even with relatively small payload sizes in bytes, potentially leading to complete server freeze or crash.
  • Risk Severity: High
  • Mitigation Strategies:
    • limit option (indirect mitigation): While primarily for size, limit can indirectly help by limiting the overall size of complex payloads. However, it's not a complete solution for CPU-bound complexity attacks.
    • parameterLimit option (for urlencoded()): For bodyParser.urlencoded(), use the parameterLimit option to restrict the maximum number of parameters parsed, mitigating some forms of complexity. Example: bodyParser.urlencoded({ extended: true, parameterLimit: 1000 }).
    • extended: false for urlencoded(): Using extended: false with bodyParser.urlencoded() utilizes the simpler querystring library, which is generally less vulnerable to deep nesting issues compared to the qs library used with extended: true. Choose this option if complex URL-encoded data is not required.
    • Application-level input validation: Implement robust validation logic in your application to explicitly check for and reject overly complex data structures after body-parser processing. Define and enforce acceptable nesting levels and data structure complexity based on application needs.

Attack Surface: Prototype Pollution

  • Description: Attackers inject properties into the Object.prototype or other global prototypes by exploiting vulnerabilities in parsing logic. This can lead to widespread unexpected application behavior, security bypasses, or potentially more severe exploits.
  • Body-Parser Contribution: Older versions of body-parser and specific configurations (especially urlencoded with extended: true using the qs library) have been vulnerable to prototype pollution due to insecure object merging and property assignment during parsing. body-parser directly handles the parsing process that can be exploited.
  • Example: An attacker sends a crafted JSON payload like {"__proto__":{"polluted":"true"}}. In vulnerable versions or configurations, bodyParser.json() parsing could modify the Object.prototype, globally setting the polluted property to true across the application, potentially disrupting functionality or creating security holes.
  • Impact: Widespread unexpected application behavior, potential security bypasses (e.g., authentication or authorization bypass), denial of service through application crashes or unpredictable behavior, and in some scenarios, could be chained with other vulnerabilities for Remote Code Execution (RCE).
  • Risk Severity: High
  • Mitigation Strategies:
    • Upgrade body-parser: Immediately upgrade to the latest version of body-parser. Prototype pollution vulnerabilities have been actively addressed in recent releases. This is the most critical mitigation.
    • Use extended: false for urlencoded(): When using bodyParser.urlencoded(), strongly consider setting extended: false. This uses the built-in querystring library, which is significantly less prone to prototype pollution issues than the qs library used with extended: true. Only use extended: true if absolutely necessary for complex URL-encoded data and with extreme caution.
    • Input Validation and Sanitization: Always rigorously validate and sanitize data received from request bodies after body-parser processing, regardless of body-parser version. Never directly use user-provided data to set object properties without strict validation and sanitization to prevent exploitation of any residual or future prototype pollution or injection vulnerabilities.
  • Description: Relying on default configurations of body-parser without explicitly setting security-relevant options leaves the application vulnerable to attacks that could be easily mitigated by proper configuration.
  • Body-Parser Contribution: body-parser's default settings, while convenient for quick setup, are often not secure defaults for production environments. For example, no default limit is set, and urlencoded defaults might use extended: true, increasing prototype pollution risks. Using body-parser without explicit configuration directly inherits these insecure defaults.
  • Example: An application uses body-parser without setting any limit options. This directly exposes the application to large payload DoS attacks, as body-parser will attempt to process and store arbitrarily large request bodies by default.
  • Impact: Increased vulnerability to high severity attack surfaces like DoS and Prototype Pollution due to missing essential security configurations, potentially leading to service outage or critical security breaches.
  • Risk Severity: High
  • Mitigation Strategies:
    • Explicit Configuration is Mandatory: Never rely on default configurations in production. Always explicitly configure body-parser middleware with options tailored to the application's specific needs and security requirements. Treat explicit configuration as a fundamental security best practice.
    • Security Hardening Checklist: Implement a mandatory security hardening checklist for application setup that requires explicit configuration of body-parser and other security-sensitive middleware with appropriate security settings before deployment.
  • Description: Incorrectly configuring body-parser options, even when attempting to set them, can introduce vulnerabilities or negate intended security measures, making the application vulnerable.
  • Body-Parser Contribution: body-parser provides numerous configuration options (limit, parameterLimit, inflate, extended, type, etc.). Misunderstanding or incorrectly setting these options directly undermines security efforts and can create exploitable weaknesses.
  • Example: A developer attempts to set a limit option but mistakenly sets it to an excessively high value (e.g., limit: '100MB' when 100KB is sufficient). This misconfiguration effectively negates the intended protection against large payload DoS attacks, leaving the application vulnerable. Similarly, using extended: true without understanding the prototype pollution risks is a misconfiguration.
  • Impact: Weakened security posture, creation of exploitable vulnerabilities (e.g., DoS, Prototype Pollution), unexpected application behavior, potentially leading to service outage or security breaches.
  • Risk Severity: High
  • Mitigation Strategies:
    • Thorough Documentation Review and Understanding: Carefully and completely read and understand the body-parser documentation for every configuration option being used. Pay close attention to the intended purpose, security implications, and potential side effects of each setting.
    • Principle of Least Privilege and Minimal Configuration: Configure options with the principle of least privilege in mind. Set limits and restrictions as tightly as possible while still meeting the essential functional requirements of the application. Avoid over-permissive settings.
    • Security-Focused Code Reviews: Conduct mandatory security-focused code reviews specifically to scrutinize body-parser configurations and ensure options are correctly and securely set. Reviewers should have a strong understanding of body-parser security implications.
    • Comprehensive Security Testing: Implement comprehensive security testing, including penetration testing and vulnerability scanning, to validate that body-parser configurations are effective and do not introduce vulnerabilities. Test with various payload sizes, complexities, and content types to ensure robustness.