Skip to content

refactor(request)!: make x-forwarded-proto trust opt-in#1461

Merged
pi0 merged 1 commit into
mainfrom
feat/opt-in-x-forwarded-proto
Jul 14, 2026
Merged

refactor(request)!: make x-forwarded-proto trust opt-in#1461
pi0 merged 1 commit into
mainfrom
feat/opt-in-x-forwarded-proto

Conversation

@pi0x

@pi0x pi0x commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Why

getRequestProtocol trusts the x-forwarded-proto header by default, while its siblings getRequestHost (xForwardedHost) and getRequestIP (xForwardedFor) default forwarded-header trust to false. A directly-exposed app believes a client-spoofed https — affecting secure-cookie decisions, canonical/absolute URLs, and cache keys — and the inconsistency between the three siblings is surprising.

What

  • getRequestProtocol (and getRequestURL, which composes it) now honor x-forwarded-proto only when explicitly enabled with { xForwardedProto: true }.
  • JSDoc + docs/2.utils/1.request.md updated; MIGRATION.md gains an [!IMPORTANT] note.

BREAKING CHANGE: apps behind a trusted reverse proxy/CDN that rely on x-forwarded-proto must now pass { xForwardedProto: true }.

Tests

New regression test asserts a spoofed x-forwarded-proto: https is ignored by default (failed before the fix); existing honored-header tests flipped to explicit opt-in. Full suite green (1441 passed).

Note: this change was briefly pushed to main by mistake and reverted there (5396256); it lands here for review instead.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Updated request protocol and URL generation to ignore x-forwarded-proto by default.
    • Added explicit opt-in ({ xForwardedProto: true }) for trusted reverse proxy/CDN setups.
    • When enabled, uses the first value from comma-separated x-forwarded-proto entries (including handling values with spaces).
  • Documentation
    • Updated MIGRATION.md and request docs to reflect the new x-forwarded-proto opt-in behavior and security considerations.
  • Tests
    • Expanded unit tests to verify default ignoring and correct opt-in parsing behavior.

@pi0x
pi0x requested a review from pi0 as a code owner July 14, 2026 09:29
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The change makes x-forwarded-proto opt-in for getRequestProtocol and getRequestURL, adds coverage for default and enabled behavior, and updates documentation and migration guidance.

Changes

Request protocol forwarding

Layer / File(s) Summary
Runtime opt-in behavior
src/utils/request.ts, test/unit/request.test.ts
x-forwarded-proto is ignored by default and honored only with xForwardedProto: true; tests cover single and comma-separated values.
Documentation and migration guidance
docs/2.utils/1.request.md, MIGRATION.md
Documentation explains the opt-in behavior, trusted proxy requirement, and forwarded protocol parsing.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • h3js/h3#1413: Both changes cover forwarded protocol handling and comma-separated x-forwarded-proto values.
  • h3js/h3#1455: Both changes update conditional handling of x-forwarded-proto.

Suggested reviewers: pi0

Poem

A bunny checks the headers bright,
“Trust forwarded schemes only by invite.”
The default stays safely plain,
Commas parse in a tidy chain.
Docs and tests hop into sight—
Opt-in protocol, fluffy delight!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the main change: x-forwarded-proto is now opt-in for request handling.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/opt-in-x-forwarded-proto

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

getRequestProtocol trusted the x-forwarded-proto header by default,
while its siblings getRequestHost (xForwardedHost) and getRequestIP
(xForwardedFor) default forwarded-header trust to false. A directly
exposed app would believe a client-spoofed https, affecting
secure-cookie decisions, canonical/absolute URLs, and cache keys.

The header is now honored only with { xForwardedProto: true }.

BREAKING CHANGE: getRequestProtocol/getRequestURL ignore
x-forwarded-proto unless xForwardedProto: true is passed.

Co-Authored-By: Claude Fable 5 <[email protected]>
@pi0x
pi0x force-pushed the feat/opt-in-x-forwarded-proto branch from 28be1cf to b713584 Compare July 14, 2026 09:31

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@MIGRATION.md`:
- Around line 142-144: Update the adjacent alert formatting in MIGRATION.md to
resolve markdownlint MD028 while preserving the separate rendering of the
[!IMPORTANT] and [!NOTE] blocks. Apply a targeted suppression or equivalent
structure around the alert boundary, without merging the alerts or changing
their content.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2f8fcd92-e79c-4636-b5dd-dfc51ee2c6f7

📥 Commits

Reviewing files that changed from the base of the PR and between 28be1cf and b713584.

📒 Files selected for processing (4)
  • MIGRATION.md
  • docs/2.utils/1.request.md
  • src/utils/request.ts
  • test/unit/request.test.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • docs/2.utils/1.request.md
  • test/unit/request.test.ts
  • src/utils/request.ts

Comment thread MIGRATION.md
@pi0 pi0 changed the title feat(request)!: make x-forwarded-proto trust opt-in refactor(request)!: make x-forwarded-proto trust opt-in Jul 14, 2026
@pi0
pi0 merged commit a173e99 into main Jul 14, 2026
8 of 9 checks passed
@pi0
pi0 deleted the feat/opt-in-x-forwarded-proto branch July 14, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants