nextjs - 💡(How to fix) Fix [Bug] NEXT_PRIVATE_TEST_HEADERS env var disables all internal header filtering [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#93438Fetched 2026-05-04 04:57:45
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Timeline (top)
closed ×1commented ×1labeled ×1locked ×1

When the environment variable NEXT_PRIVATE_TEST_HEADERS is set, the filterInternalHeaders() call is entirely skipped in the request handler, allowing all internal headers to pass through unfiltered.

Root Cause

When the environment variable NEXT_PRIVATE_TEST_HEADERS is set, the filterInternalHeaders() call is entirely skipped in the request handler, allowing all internal headers to pass through unfiltered.

Code Example

// internal headers should not be honored by the request handler
if (!process.env.NEXT_PRIVATE_TEST_HEADERS) {
  filterInternalHeaders(req.headers)
}
RAW_BUFFERClick to expand / collapse

Description

When the environment variable NEXT_PRIVATE_TEST_HEADERS is set, the filterInternalHeaders() call is entirely skipped in the request handler, allowing all internal headers to pass through unfiltered.

Affected File

packages/next/src/server/lib/router-server.ts (line 230–232)

Code

// internal headers should not be honored by the request handler
if (!process.env.NEXT_PRIVATE_TEST_HEADERS) {
  filterInternalHeaders(req.headers)
}

Risk

If this variable is accidentally set in a production environment (e.g., via a misconfigured deployment pipeline or CI/CD leak), an attacker could forge the following internal headers:

  • x-middleware-rewrite — bypass middleware rewrite logic
  • x-middleware-redirect — trigger arbitrary redirects
  • x-matched-path — control which page is rendered
  • x-middleware-set-cookie — set arbitrary cookies

Suggestion

Consider adding a startup warning when this variable is detected in a production environment (NODE_ENV=production), or documenting clearly that this variable must never be set in production.

extent analysis

TL;DR

To prevent internal headers from being passed through unfiltered, ensure the NEXT_PRIVATE_TEST_HEADERS environment variable is not set in production environments.

Guidance

  • Verify that the NEXT_PRIVATE_TEST_HEADERS environment variable is not set in your production environment to prevent security risks.
  • Consider adding a check at startup to warn when this variable is detected in a production environment (NODE_ENV=production).
  • Review your deployment pipeline and CI/CD configuration to ensure this variable is not accidentally set.
  • Document clearly that NEXT_PRIVATE_TEST_HEADERS must never be set in production to prevent potential security vulnerabilities.

Example

if (process.env.NODE_ENV === 'production' && process.env.NEXT_PRIVATE_TEST_HEADERS) {
  console.warn('NEXT_PRIVATE_TEST_HEADERS is set in production, which may expose your application to security risks.');
}

Notes

This fix assumes that the NEXT_PRIVATE_TEST_HEADERS variable is only used for testing purposes and should never be set in a production environment. If this variable is required for other purposes, additional logic may be needed to handle its presence in production.

Recommendation

Apply a workaround by adding a startup warning when NEXT_PRIVATE_TEST_HEADERS is detected in a production environment, as this will help prevent potential security risks without requiring significant code changes.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

nextjs - 💡(How to fix) Fix [Bug] NEXT_PRIVATE_TEST_HEADERS env var disables all internal header filtering [1 comments, 2 participants]