Field NotesApril 2025
A request must not be able to say that middleware already ran: CVE-2025-29927 and in-band control headers
Next.js kept the state of its recursion guard in a request header that arrives through the same door as client traffic. We reconstruct the header from the source of 12.0.0 through 15.2.4 and from the patches on four branches, place it in a weakness class we call in-band control signals, and give a white-box audit record and a fix-verification list.
In this piece
On 21 March 2025 the Next.js project published CVE-2025-29927: if an application performs its authorization check in middleware, the check can be bypassed1. The CVE record gives a CVSS 3.1 score of 9.1 and the weakness type CWE-285, Improper Authorization2. The reporters, Rachid Allam (zhero) and Yasser Allam (inzo_), explained the method in their write-up: send the header x-middleware-subrequest with the right value, and the middleware does not run at all3.
It is easy to read this as one header that was not filtered. We read the source at more than ten version tags between 12.0.0 and 15.2.4, and the patches on four release branches, and we see something different. The header is a recursion record that the framework writes for itself. When middleware calls its own site with fetch, the framework attaches the header. On our reading of the code, that request re-enters as an ordinary HTTP request, at the same entry point as client traffic. The legitimate signal and a forged one enter through the same door, and the door cannot tell them apart.
Our claim is that internal control state on a channel the client can write is a weakness class of its own. We call it the in-band control signal. Stripping a header at the entry point fixes only part of this class. A signal that must legitimately come back in through the public entry can only be made verifiable, moved out of the request, or removed. Next.js took two of those steps in seven days. The claim can be refuted: find one signal that must legitimately arrive through the public entry and that was fixed by entry stripping alone, and the classification needs to change.
01What the header does in the code#
Unless we say otherwise, this section reports what we saw in the source files of vercel/next.js at the tags named. We did not run any version. Step three of the flow, the return through the public entry, is our reading of the code, not a trace.
The writer: every fetch from middleware#
In 12.0.0, sandbox.ts has a function named getFetchHeaders. It reads any x-middleware-subrequest value already on the outgoing request, splits it on colons, appends the name of the current middleware, and writes it back. It also sets user-agent to Next.js Middleware4. Later versions move this code into context.ts, and at 15.2.2 it still appends the name in the same way5. Neither location checks the destination host, so every fetch from middleware carries the header, including calls to third-party APIs. Later versions run Edge-runtime routes in the same sandbox, so their fetch calls get the header too; the test in pull request #60615 is such a route8.
Vercel's post-mortem gives the purpose: middleware runs in a separate process, and when it fetches its own application an internal header tells the routing process that middleware has already run, which detects and prevents recursion14. That is the vendor's statement. The code we read agrees on the purpose. At 12.0.7 and 15.2.2 the server calls the sandbox directly, and we saw no separate process there65.
- 01
Middleware runs
User middleware calls
Normalfetchon its own site - 02
Wrapped fetch
The framework attaches
Normalx-middleware-subrequestwith the middleware name in the value - 03
Public entry
The request returns over the network to the same HTTP entry point as any outside request
Origin unknown - 04
Reader
Reads the header; if the name occurs (five times in later versions) it returns
Trust on presencex-middleware-next - 05
Routing
The request goes to the page or API; the check in middleware never ran
Check skipped
The reader: three forms#
| Tags we read | File and function | Condition that skips middleware |
|---|---|---|
| 12.0.7 | runMiddleware in packages/next/server/next-server.ts | The colon-separated list includes middlewareInfo.name6 |
| 12.0.8, 13.5.6, 14.1.4 | run in server/web/sandbox/sandbox.ts | The list includes params.name71516 |
| 14.2.0, 14.2.24, 15.2.2, 15.2.3 | Same | The number of entries equal to params.name reaches MAX_RECURSION_DEPTH, which is 51712 |
The second form:
const subreq = params.request.headers[`x-middleware-subrequest`]
const subrequests = typeof subreq === 'string' ? subreq.split(':') : []
if (subrequests.includes(params.name)) {
return {
waitUntil: Promise.resolve(),
response: new context.Response(null, {
headers: {
'x-middleware-next': '1',
},
}),
}
}When the condition holds, the function returns an empty response with x-middleware-next, which tells the server to go on. The user's middleware is not called7. To make it hold, a sender needs only the name. The researchers point out that the name is the path of the middleware file, which follows from the framework's file conventions3.
The third form comes from pull request #60615, merged on 6 February 2024. Its description says the aim is to allow 5 recursive calls to match Vercel production8. The same pull request adds code that copies the incoming x-middleware-subrequest value onto the fetch calls that middleware makes, so the framework now passes a client-supplied value down the chain.
Secondary write-ups disagree on when this changed: ProjectDiscovery says a recursion depth limit exists from 13.2.018, and Datadog says the change came in 15.x19. The tags we read place it between 14.1.4 and 14.2.01617.
A lower bound that does not match#
The researchers say that every version from 11.1.4 is affected3. The CVE record starts its affected range at 11.1.4, as did the advisory a week before the dateline220. The record's description says 1.11.4, which we take as a typing error. That description dates from 8 April 2025; the original of 21 March named only 14.2.25 and 15.2.321.
We did not find the header in next-server.ts at the v11.1.4 tag22; the writer is present at 12.0.04. The npm package agrees: the file list of next@11.1.4 has nothing under dist/server/web, and dist/server/next-server.js lacks the string23. The code we read does not support the 11.1.4 bound; we do not know its basis.
02The fix took two steps: authenticate, then remove#
PR #60615 merged: the skip condition becomes five occurrences of the same name8
CodePR #73482 merged: an entry strip list of eight internal headers, without
Codex-middleware-subrequest9The same list is backported to the 14.x branch24
CodeThe researchers report through GitHub private reporting; the report names only 12.x14
ReportThe researchers add that the latest stable releases are affected too3
ReportVercel's security team raises the report to the Next.js team, which confirms it14
TriageThe advisory and the CVE become public1
Disclosure12.3.5 ships14
FixThe advisory for CVE-2025-30218 is published26
Disclosure
Step one: a secret that travels with the signal#
The 15.x fix does three things10:
- At server start it generates 8 random bytes, encodes them as 16 hexadecimal characters, and stores them in a global.
- The middleware
fetchwrapper adds a second header,x-middleware-subrequest-id, with that identifier as its value. filterInternalHeaders, at the entry point, gets one more condition: if a request hasx-middleware-subrequestand its-idvalue is not this process's identifier, deletex-middleware-subrequest.
The reader is unchanged. The commit message is one sentence:
This just adds an additional header to our internal filtering list for middleware.10
The branches enforce the fix in different places. The 13.x branch did not have the eight-name list, so its backport adds the filterInternalHeaders function, the list and the call at the entry together27. The 12.x branch has no entry filter; its patch changes the reader directly, to skip only if the identifier matches and the name occurs28.
We draw two inferences from this code and tested neither. First, each process generates its own identifier, so with several instances a subrequest from instance A that lands on instance B loses the header and runs middleware again. That is safe for authorization and weakens the recursion guard. Second, the fix commit was public on 17 March and contains a test that sends the header; the advisory came on 21 March101. In between, anyone who reads diffs could learn the header name and the form of its value.
Step two: the secret took the same road#
On 2 April Vercel described the problem with the first fix: the identifier is sent on all requests, even when the destination is not the Next.js application's host, so a fetch to a third party from middleware hands the identifier to that party11. The issue is CVE-2025-30218, rated low. The affected versions are exactly the four that had just been patched: 12.3.5, 13.5.9, 14.2.25 and 15.2.326. Vercel says exploitation is unlikely because the attacker must control the third party11.
The four releases of 24 March do not adjust the check again: the diffs for 15.2.4 and 12.3.6 show the writer, the reader and the identifier check removed1225. Vercel says it already planned to remove this recursion protection, because newer Middleware work that supports the Node.js runtime does not support it, and that the disclosure moved the plan forward11.
Cloudflare describes the same events differently: the vulnerability is fully patched in 14.2.26 and 15.2.4, and the earlier interim releases did not fully patch it13. Vercel treats the second issue as a separate low-severity vulnerability.
Why the entry list did not catch it#
Pull request #73482, in December 2024, adds filterInternalHeaders. The commit description says internal request headers are not intended to be used externally, so the change filters them9. The list has eight names, among them x-middleware-rewrite, x-middleware-next and x-now-route-matches. In the last 15.x release before the fix, the list still has those eight29. It reached 14.x 17 days before the report24.
We do not know why x-middleware-subrequest was left out. The code shows that the filter sits at the start of request handling9, and the subrequests that middleware sends to itself come in at the same place. Put the name on the list, and the legitimate recursion signal is deleted too, so the guard stops working. This is an inference; we did not run it. The fix is consistent with it: it deletes conditionally, and its code comment describes the deleted case as a request that did not originate from this session10.
This was not the first such problem in this code base. In January 2025 zhero wrote that some internal parts of the framework were not closed to the outside, and named several internal headers. The same article explains that the cache poisoning of CVE-2024-46982 used x-now-route-matches30. That advisory is dated 17 September 202431, and the name is on the eight-name list. Our reading is that the list answered particular incidents and was not built from the other end, by listing every header the framework reads.
03The response layer: what vendors said, and how often it changed#
From report to fix#
18 days
From the report (2025-02-27 06:03 GMT) to the first fixed release, 14.2.25 (17 March 22:44 GMT), on Vercel's timeline14
Vercel explains the delay this way. The first report named only 12.x, so it had a lower triage priority. The follow-up of 1 March opened a new thread. The reply of 5 March was about a planned LTS policy under which 11.x and 12.x would get no security patches14. The researchers' timeline is compatible and adds reminders on 5 and 11 March3.
The vendor's own accounts differ on one point. The Next.js blog post of 22 March says the 15.x patch was pushed at 19:08Z on 14 March32. The post-mortem says the public pull request was opened at 17:54 GMT on 17 March14. The first may refer to a private branch; neither text says.
The post-mortem lists commitments: document the internal headers or replace them with a proper API, open a partner mailing list, and take reports only through GitHub private reporting. It says the internal header was undocumented14.
Which deployments were affected#
Every row below is a statement by the vendor named. We tested no platform.
| Time (UTC) | Source | Statement about hosting |
|---|---|---|
| 22 March 15:27 | Cloudflare changelog, first version | The WAF rule has been deployed automatically to all sites3435 |
| 22 March 19:49, archived | Next.js blog | Applications hosted on Vercel, Netlify and Cloudflare are not affected32 |
| 22 March 20:26 | Netlify forum | Sites on Netlify are not and have never been affected; end-to-end tests were added to the OpenNext Netlify adapter36 |
| 22 March 22:53, archived | Next.js blog | Not affected: Vercel, Netlify and static exports only. Cloudflare moves under "Affected", worded as a managed WAF rule that can be turned on33 |
| 25 March | Vercel post-mortem | Netlify and Cloudflare Workers were confirmed never to have been affected, for the same reason as Vercel: application routing is decoupled14 |
An application on Cloudflare can be one deployed on Workers, or a self-hosted Next.js server with Cloudflare in front of it. The blog post does not separate the two, and the post-mortem speaks only of the first. Cloudflare's own text says the rule protects Next.js sites on Workers and Pages, and applications hosted elsewhere that use Cloudflare for protection13. It does not say whether an application on Workers has the weakness itself.
According to Vercel, the affected setups are self-hosted applications that use next start and output: 'standalone'; static exports have no middleware. On the Vercel platform the routing logic runs in a separate, globally distributed system, and the post-mortem says this made Vercel "incidentally invulnerable"14. Vercel's changelog of 22 March first said its Firewall protected customers; a capture eight hours later says customers are not affected3738. The post-mortem admits that this changelog reused an earlier template, mentioned the firewall, and was confusing14.
By 3 April the blog URL redirected to the post-mortem; the capture of 2 April still has the original text, which now survives only in the archive39. The official documentation, archived one week before the report, lists authentication and authorization as the first common use of Middleware40, while the post-mortem says Vercel does not recommend Middleware as the sole method of protecting routes14.
The WAF rule: the cost of matching on presence#
By Cloudflare's description, the rule blocks requests that have the x-middleware-subrequest header, regardless of Next.js version, and the custom rule it offers as an alternative tests only whether the header exists13. The page dates the change to opt-in at 16:00 UTC on 22 March, and the matching edit in the documentation repository was merged at 18:46 UTC1335. Cloudflare's stated reason is that sites using auth middleware with third-party auth vendors saw failing requests13. The researchers' write-up has an update that says the rule produced many false positives3.
Datadog's observations of 28 March fit this. It saw a low volume of scans, and most of the payloads it identified were normal activity. Its examples are requests with the Vercel Edge Functions user agent, whose header value looks like a 40-character hexadecimal ID, and recursive middleware with the Next.js Middleware user agent19. CISA's SSVC entry on the CVE record is not a stable signal: Exploitation was none on 21 March, poc from 26 March, and none again on 8 April41.
Cloudflare does not explain the mechanism; a note on the page says only that the rule blocked requests across sites for specific authentication middleware13. Our inference comes from the writer code: the sandbox sends the header on a fetch to any host, so a third-party authentication API receives legitimate requests that carry it. If that API is itself behind Cloudflare, a default-on rule that matches on presence blocks them. One public report describes this for another API: a Next.js issue of 24 March says fetch calls from an Edge-runtime route on 15.2.3 to an API behind Cloudflare carried the header and failed, while the same calls from the Node.js runtime worked42. It is one user's report, filed after the rule became opt-in, and we did not reproduce it. If Cloudflare publishes a different cause, this inference falls.
A writer that ignores the destination puts its signal at other people's entry points, where a presence rule blocks it along with attack traffic. When Cloudflare made the rule opt-in, it added that the rule can first be set to Log to check the impact35.
04A classification of in-band control signals#
The CVE record uses CWE-2852. That describes the result: an authorization check that was not performed correctly. For the cause, we think CWE-807 is closer. Its description reads:
The product uses a protection mechanism that relies on the existence or values of an input, but the input can be modified by an untrusted actor in a way that bypasses the protection mechanism.43
This mapping is our judgement, not the record's. By in-band control signal we mean metadata that travels on the same channel as user data and changes how a component behaves. This article looks only at HTTP request headers.
Who writes, who trusts, who strips#
The table puts this case next to others that have primary documentation. The last column is our classification.
| Signal | Who writes it | Who trusts it, and for what | Who strips it | Basis of trust |
|---|---|---|---|---|
X-Forwarded-For, -Host, -Proto | A reverse proxy | The application framework: client address, host name, whether the request used HTTPS | The documentation requires the last trusted proxy to remove or overwrite them4445 | Entry stripping |
X-Original-URL, X-Rewrite-URL | Symfony calls them legacy IIS headers | Symfony HttpFoundation: the request path | Nobody; fixed in 2018 by removing support46 | Presence |
x-envoy-* | Envoy, or a caller that Envoy classifies as internal (our reading) | Envoy | Envoy sanitizes them depending on whether the request is internal or external47 | Entry stripping |
x-amzn-oidc-data | AWS Application Load Balancer | The backend application: user identity | The documentation requires verification of the signature and the signer field, and recommends that targets accept traffic only from the load balancer48 | Signature |
x-now-route-matches and seven others | Next.js internals | Next.js routing and middleware handling | The entry list, from December 20249 | Entry stripping |
x-middleware-subrequest | The wrapped fetch in Next.js | Next.js: whether to run middleware | Nobody before the fix | Presence, then a co-travelling secret, then removed1012 |
The Symfony advisory credits James Kettle, who reported the issue to the Drupal security team. It says a user could access one URL and have Symfony return a different one, which can bypass restrictions on higher-level caches and web servers46.
The difference lies between the second and fourth columns. For X-Forwarded-*, the writer and the strip point are the same component, on the boundary. For x-middleware-subrequest, the writer stands behind the boundary, but the signal goes around to the front and comes in again. There is no place to put a strip point.
When the signal is absent#
Forging a signal is one direction; making it disappear is the other. Section 7.6.1 of RFC 9110 requires an intermediary to parse the Connection header before it forwards a message and to remove every field that the header names49. In 2019 Nathan Davison showed that a client can use this by naming a proxy-added header in Connection. Some systems forward that list to the next hop unchanged, and a later node then removes the header. One example in the post is X-Forwarded-For: once it is removed, the backend sees a different source address50.
So each signal needs this question: when it is absent, does the system take the stricter path or the looser one?
Five bases of trust#
- 01
Trust on presence
The header exists and its value has the right form, so it is believed.
Forgeablex-middleware-subrequestbefore the fix andX-Original-URLare at this level. - 02
Entry stripping
A boundary component deletes or overwrites the header, and components behind it then trust it. Two conditions: the legitimate signal does not have to come in through the same entry, and the backend cannot be reached around the boundary.
Depends on deployment - 03
Co-travelling secret
A value that only the product knows is attached next to the signal. The secret takes the same road as the signal: where the writer sends one, it sends the other.
Leaks - 04
Signature
The signal itself proves its origin. The receiver checks the key and the signer.
Verifiable - 05
Out of band
The state is not in the request. It is in an in-process call argument, on a separate connection, or recorded by the server itself.
Request cannot write it
The labels are our assessment. The nginx internal directive is an out-of-band example: a location marked with it accepts only internal requests that the server generates, and returns 404 to external ones51. No request field can declare a request internal.
The Next.js fix history maps onto this ladder: level one, level three, then removal. On our reading, level two was not available, for the reason given in the previous section. Level three lasted seven days and left a CVE of its own. Vercel says its platform was not affected because routing runs in a separate system14. We read that as level five.
Our judgement: when you find a level-one signal, first ask whether it can be removed. Symfony and Next.js both ended there. If it cannot, go to level four or five, and do not stop at level three.
05White-box audit: finding this class in a code base#
Three lists: reads, writes, strips#
First build three lists and compare them. Adjust the search strings to the language and framework.
Header reads headers[ headers.get( getHeader( req.header( request.META $_SERVER['HTTP_ Header.Get(
Name hints internal subrequest invoke skip bypass override rewrite original
forwarded real-ip matched trusted authenticated
Writes on outgoing headers.set( setHeader( and wrappers around fetch and HTTP clients
Strip and filter INTERNAL_HEADERS strip sanitize filter delete headers[
Response as command code that treats an upstream response header as a rewrite, redirect or internal-jump instructionEvery name on the read list must be on the strip list, or have a verification step. A name with neither is a candidate. In this case x-middleware-subrequest is on the read and write lists and not on the strip list. Starting from the reads finds it; starting from known incidents and adding names to the strip list does not.
One record per header#
Name
Read point file and function
Decision affected skip a check / select an identity / change the path / change the cache key / other
Writers which components write it legitimately
Path of the legit signal in-process / internal connection / public entry
Strip point which component and function; write "none" if there is none
Basis of trust presence / entry stripping / co-travelling secret / signature / out of band
Behaviour when absent stricter / looser
Outbound scope does the writer limit the destination
Maintained branches identical or not; if not, one record per branchThen decide:
| The record shows | Verdict | Next step |
|---|---|---|
| A security decision, and the basis of trust is presence | Vulnerability | Remove the signal if possible; otherwise move to a signature or out of band |
| A strip point exists, but the legitimate signal enters through the public entry | Stripping does not hold | Same as above; do not only add the name to a list |
| The basis is entry stripping, and the backend can be reached directly | Vulnerable in some deployments | Restrict the sources of the backend, or verify at the backend |
| The basis is a co-travelling secret | More checks needed | Check the outbound scope, the lifetime of the secret and the behaviour with several instances; a failed check must take the stricter path |
| Behaviour is looser when the header is absent | Possibly exposed to removal techniques | Test what happens when Connection names the header; reject when it is missing |
Verifying a fix#
- Run a negative test. Set a response header in middleware as a marker, send a request from outside with the internal header, and confirm that the marker is still there. The self-check that Netlify published works this way36. Test every historical form of the reader; this case has three.
- Read the reader after the patch. In 15.2.3 the reader is unchanged and safety depends on the conditional delete at the entry. In 15.2.4 the reader is gone1012. Both pass the same negative test, with different residual risk.
- Run a leak test. Make middleware send a request to an external endpoint that you control, and inspect the received headers for internal names or identifiers. CVE-2025-30218 is the kind of problem this test finds11.
- Verify each branch separately. Here the 12.x patch and the patches for 13.x and later are in different places2827.
- Log before you block at the boundary. Legitimate traffic with this header exists1913.
- Confirm that authorization is not only in middleware. For each protected route, find a second check in the route handler or in the data access layer.