Next.js Middleware SSRF via Header Injection
Next.js Middleware uses special headers to control routing, and if an application trusts user-supplied headers such as X-Middleware-Rewrite without validation, an attacker can rewrite requests to an attacker-controlled host. This could let the server be forced to connect to internal hosts or cloud metadata services.
Published on medium.com
A security issue in Next.js Middleware can allow Server-Side Request Forgery (SSRF) if certain headers are not properly validated. Next.js introduced Middleware to intercept requests before they hit the final route.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the application passes client-supplied middleware routing headers into the rewrite logic without validating or stripping them.
Impact
An attacker could access internal services such as localhost or cloud metadata endpoints, steal sensitive information, pivot deeper into the internal network, and potentially take a step towards remote code execution.
What the attacker needed
Application trusts client-supplied middleware headers; attacker can send requests to the target
The fix
Client-supplied headers should never be trusted for middleware rewrites; rewrite and redirect logic should be validated and sanitised, with allowlists for outbound requests and blocks on internal IP ranges.
How it was found
- Send a request to the target with a Host header and a Location header.
- Include an X-Middleware-Rewrite header pointing to an attacker-controlled URL.
- Observe the server making a request to that URL, for example via an out-of-band interaction server.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
Server-Side Request Forgery (SSRF) is when an attacker makes a server send a request to a destination the attacker chooses, often to internal systems the attacker cannot reach directly. Here, a routing header that the framework uses internally is accepted from the client, so the server can be told to fetch an arbitrary address.
What is SSRF?
The server can be made to fetch an address the attacker chooses, reaching internal services, cloud metadata or other customers' data.