📗
Application Security Manual
  • Intro
    • Start Here
  • Vulnerabilities & Code
    • Injection Vulnerabilities
      • SQL Injection
      • Server-Side Template Injection (SSTI)
      • XPath Injection
      • XML External Entity (XXE) Injection
      • HTTP Request Smuggling
      • Insecure Deserialization
    • Input-based Vulnerabilities
      • Cross-Site Scripting (XSS)
      • Content Security Policy (CSP) Misconfiguration
      • HTTP Parameter Pollution
    • Origin-related Vulnerabilities
      • Cross-Site Request Forgery (CSRF)
      • Server-Side Request Forgery (SSRF)
      • Cross-Origin Resource Sharing (CORS) Misconfiguration
    • Access-related Vulnerabilities
      • Insecure Direct Object References (IDOR)
      • Authorization Code Interception
      • Session Hijacking
      • Insufficient Transport Layer Security (TLS)
      • JSON Web Token (JWT) Vulnerabilities
      • Insecure Client-Side Storage of JWTs
    • Logic & Timing Vulnerabilities
      • Business Logic Flaws
      • Type Juggling (Loose Type Comparison)
      • Timing Attack
  • Design Review
    • Product Security Design Review Framework
    • Security Design Review Checklist
  • Concepts & Culture
    • Implementing SAST Through Security-Driven Developer Culture
    • Security as Code: Baking Security into DevOps
Powered by GitBook
On this page
  • Concept
  • Vulnerable Scenario
  • Explanation
  • Prevention
  • Conclusion
  1. Vulnerabilities & Code
  2. Injection Vulnerabilities

HTTP Request Smuggling

Concept

HTTP Request Smuggling is a vulnerability that occurs when a web application or server incorrectly handles requests with ambiguous or malformed HTTP headers. This vulnerability allows attackers to smuggle or inject additional HTTP requests through an existing connection, potentially bypassing security controls, gaining unauthorized access, or interfering with the application's behavior.

Vulnerable Scenario

Consider a web application that uses a front-end proxy server to forward requests to a back-end server. The front-end server and back-end server have different interpretations of how to handle certain HTTP headers, such as Content-Length and Transfer-Encoding.

Example Code (Vulnerable)

from flask import Flask, request

app = Flask(__name__)

@app.route('/api/process', methods=['POST'])
def process_request():
    # Process the incoming request
    data = request.get_data()
    # Forward the request to the back-end server
    response = forward_request_to_backend(data)
    return response

Explanation

In this example, the /api/process route receives a POST request and processes the incoming data. The request is then forwarded to the back-end server for further processing.

An attacker can exploit this vulnerability by crafting a malicious request that includes ambiguous or conflicting HTTP headers. For example:

POST /api/process HTTP/1.1
Host: example.com
Content-Length: 13
Transfer-Encoding: chunked

0

POST /api/exploit HTTP/1.1
Host: example.com
Content-Length: 10

exploit

In this request, the attacker sets both the Content-Length header and the Transfer-Encoding: chunked header. The front-end server may interpret the request based on the Content-Length header and consider the request body to be 0\r\n\r\nPOST /api/exploit HTTP/1.1\r\nHost: example.com\r\nContent-Length: 10\r\n\r\nexploit, while the back-end server may interpret it based on the Transfer-Encoding: chunked header and treat the request as two separate requests.

As a result, the back-end server may process the smuggled request (POST /api/exploit) independently, potentially bypassing security controls or executing unauthorized actions.

Prevention

To prevent HTTP Request Smuggling vulnerabilities, consider the following measures:

  1. Ensure consistency in handling HTTP headers between front-end and back-end servers. Both servers should have the same interpretation and handling of headers like Content-Length and Transfer-Encoding.

  2. Validate and sanitize incoming HTTP headers to reject requests with ambiguous or conflicting headers.

  3. Use a web application firewall (WAF) or intrusion detection system (IDS) that can detect and block suspicious or malformed requests.

  4. Keep web servers, proxies, and frameworks up to date with the latest security patches to mitigate known vulnerabilities.

  5. Implement proper input validation and filtering mechanisms to reject requests with unexpected or malicious content.

Example Code (Secure)

from flask import Flask, request, abort

app = Flask(__name__)

def is_valid_request(request):
    content_length = request.headers.get('Content-Length')
    transfer_encoding = request.headers.get('Transfer-Encoding')
    
    if content_length and transfer_encoding:
        return False
    
    # Additional validation checks
    # ...
    
    return True

@app.route('/api/process', methods=['POST'])
def process_request():
    if not is_valid_request(request):
        abort(400, 'Bad Request')
    
    # Process the incoming request
    data = request.get_data()
    # Forward the request to the back-end server
    response = forward_request_to_backend(data)
    return response

In the secure example, the is_valid_request function is introduced to validate the incoming request headers. It checks if both Content-Length and Transfer-Encoding headers are present, which is an indication of a potential request smuggling attempt. If the request is invalid, a "Bad Request" error is returned.

Additional validation checks can be added to further scrutinize the request headers and content to ensure their validity and integrity.

By consistently handling HTTP headers between front-end and back-end servers, validating incoming requests, and implementing proper security measures, the risk of HTTP Request Smuggling can be mitigated.

Conclusion

HTTP Request Smuggling is a critical vulnerability that can allow attackers to bypass security controls, gain unauthorized access, and interfere with the application's behavior. It arises from inconsistencies in handling HTTP headers between front-end and back-end servers. To prevent this vulnerability, it is essential to ensure consistent header handling, validate incoming requests, use security tools like WAFs and IDS, keep servers and frameworks up to date, and implement proper input validation and filtering. Developers should be aware of the risks associated with HTTP Request Smuggling and follow best practices to secure their web applications.

Semgrep Rule

This Semgrep rule can be used as a starting point to potentially identify this type of vulnerability.

rules:
  - id: http-request-smuggling
    patterns:
      - pattern: |
          @app.route(..., methods=['POST'])
          def $HANDLER(...):
            ...
            data = request.get_data()
            ...
            forward_request_to_backend(data)
            ...
      - pattern-not: |
          @app.route(..., methods=['POST'])
          def $HANDLER(...):
            ...
            if not is_valid_request(request):
              abort(...)
            ...
            data = request.get_data()
            ...
            forward_request_to_backend(data)
            ...
    message: "Potential HTTP Request Smuggling vulnerability. Ensure consistent handling of HTTP headers and validate incoming requests."
    languages:
      - python
    severity: ERROR
PreviousXML External Entity (XXE) InjectionNextInsecure Deserialization

Last updated 1 year ago