Skip to content

Remote Header Injection via X-Forwarded-For Manipulation

Low
rennf93 published GHSA-77q8-qmj7-x7pp May 5, 2025

Package

fastapi (Python (FastAPI))

Affected versions

All versions that trust the X-Forwarded-For header without validation.

Patched versions

2.0.0

Description

Description:
An HTTP header injection vulnerability has been identified in the FastAPI application. By manipulating the X-Forwarded-For header, an attacker can potentially inject arbitrary IP addresses into the request, which may lead to bypassing security measures that rely on IP-based access controls or logging.

Summary
A vulnerable endpoint in the FastAPI application allows an attacker to inject custom values into the X-Forwarded-For header. This vulnerability can be exploited to manipulate the server’s trust in the originating IP address. The severity of this vulnerability depends on how the application relies on this header for IP-based access control or logging.

Details
The vulnerability lies in the FastAPI application's handling of the X-Forwarded-For header. The X-Forwarded-For header is often used by reverse proxies to identify the original IP address of a client making a request. However, since it is a header sent by the client, it is possible to manipulate it and inject arbitrary IP addresses into the request. This could lead to false attribution of the client IP address, allowing attackers to bypass IP-based security mechanisms or mislead logging systems.

The issue is particularly impactful if the application relies on this header for access control decisions or IP-based logging. In this case, an attacker could spoof their IP address to impersonate trusted users or obfuscate their own identity.
Example code snippet:

from fastapi import FastAPI, Request

app = FastAPI()

@app.get("/")
async def read_root(request: Request):
    client_ip = request.headers.get('X-Forwarded-For', "IP not found")
    return {"message": f"Hello, Secure World! Your IP is {client_ip}"}

The application directly trusts the X-Forwarded-For header without validation, allowing manipulation.

PoC
To reproduce the vulnerability, follow these steps:

Run the FastAPI application locally:
uvicorn main:app --host 0.0.0.0 --port 8000

Send a crafted request with a manipulated X-Forwarded-For header:
curl -i http://127.0.0.1:8000/ -H "X-Forwarded-For: 8.8.8.8"

The server will respond with a message reflecting the manipulated IP address:
{"message": "Hello, Secure World! Your IP is 8.8.8.8"}

Impact
This vulnerability can allow attackers to bypass IP-based access controls, mislead logging systems, and impersonate trusted clients. It is especially impactful when the application relies on the X-Forwarded-For header for IP-based authorization or authentication.

Severity

Low

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Network
Attack complexity
High
Privileges required
None
User interaction
Required
Scope
Changed
Confidentiality
Low
Integrity
None
Availability
None

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.1/AV:N/AC:H/PR:N/UI:R/S:C/C:L/I:N/A:N

CVE ID

CVE-2025-46814

Weaknesses

No CWEs

Credits