Skip to content

fix: retain relative protocol in redirect location header - #156

Merged
pi0 merged 4 commits into
unjs:mainfrom
SukkaW:relative-url-protocol-location
Jul 9, 2026
Merged

pi0 merged 4 commits into
unjs:mainfrom
SukkaW:relative-url-protocol-location

Conversation

@SukkaW

@SukkaW SukkaW commented Jul 3, 2026 •

Copy link
Copy Markdown
Member

Part of #2.

The original bug http-party/node-http-proxy#1298 does not exist in httpxy. They use Node.js legacy URL parsing, but we already use WHATWG URL. So, unlike node-http-proxy, we httpxy do not break anything here.

However, the test case exposes a different nit: because we are using WHATWG URL, after we serialize the destination (u.toString()), we produce http://ext-manual.com/ instead of retaining the relative protocol //ext-manual.com/.

The PR fixes this nit.

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes
    • Improved redirect rewriting for protocol-relative Location headers (//host/path) so they remain protocol-relative after applying rewrite options.
    • Ensured protocol rewriting has no effect on protocol-relative redirects, and redirects are only rewritten when appropriate.
  • Tests
    • Added coverage for protocol-relative redirect behavior under different rewrite configuration combinations.
  • Documentation
    • Updated guidance in the README and agent documentation to clarify protocol-relative redirect semantics and cross-origin redirect handling.

@SukkaW
SukkaW requested a review from pi0 July 3, 2026 15:17
@coderabbitai

coderabbitai Bot commented Jul 3, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@pi0x, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 46 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 788f26c7-6fa1-4f89-8ac9-ad7290ccc04f

📥 Commits

Reviewing files that changed from the base of the PR and between 11ed2d4 and 851265e.

📒 Files selected for processing (4)
  • AGENTS.md
  • README.md
  • src/middleware/web-outgoing.ts
  • test/middleware/web-outgoing.test.ts
📝 Walkthrough

Walkthrough

Modifies setRedirectHostRewrite to preserve protocol-relative Location values (//...) when rewriting redirects. Adds tests for hostRewrite, protocolRewrite, and autoRewrite combinations, plus matching documentation updates.

Changes

Protocol-relative redirect rewriting

Layer / File(s) Summary
Detect and preserve protocol-relative Location
src/middleware/web-outgoing.ts
Adds isProtocolRelative detection on the original Location value and conditionally strips the protocol from the rewritten u.href so //... stays scheme-less.
Test and docs coverage
test/middleware/web-outgoing.test.ts, AGENTS.md, README.md
Adds protocol-relative redirect tests for hostRewrite, protocolRewrite, protocolRewrite + hostRewrite, and protocolRewrite + autoRewrite, and documents the preserved protocol-relative behavior in the guide and README.

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

Possibly related PRs

  • unjs/httpxy#104: Also changes setRedirectHostRewrite redirect URL rewriting and adds closely related tests.

Suggested reviewers: pi0

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main fix: preserving protocol-relative redirect Location headers.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@codecov

codecov Bot commented Jul 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.96%. Comparing base (c48bf65) to head (851265e).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #156   +/-   ##
=======================================
  Coverage   94.95%   94.96%           
=======================================
  Files           8        8           
  Lines         793      794    +1     
  Branches      328      330    +2     
=======================================
+ Hits          753      754    +1     
  Misses         35       35           
  Partials        5        5           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

pi0 and others added 2 commits July 9, 2026 10:25
Document that redirect rewriting only applies to same-host Location
headers and that protocol-relative Location values stay protocol-relative
after rewriting, making protocolRewrite a no-op for them. Add a
regression test covering the protocolRewrite + protocol-relative case.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
A protocol-relative `Location` (`//host`) stays protocol-relative only when
the host alone is rewritten (hostRewrite/autoRewrite). When the operator
explicitly sets `protocolRewrite`, that is a deliberate opt-in to a concrete
scheme (often a downgrade-prevention control), so it now absolutizes the URL
to the chosen protocol instead of being silently ignored.

Updates the ported node-http-proxy#1298 tests accordingly and documents the
interaction in README and AGENTS.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

@pi0 pi0 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thnx!

@pi0
pi0 merged commit fb95a19 into unjs:main Jul 9, 2026
4 checks passed
@SukkaW SukkaW mentioned this pull request Jul 9, 2026
21 of 58 tasks
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