Skip to content

fix: parse absolute-form request targets in request.URL - #1999

Merged
yowainwright merged 3 commits into
koajs:masterfrom
xia-chao:fix/url-absolute-form
Sep 3, 2026
Merged

yowainwright merged 3 commits into
koajs:masterfrom
xia-chao:fix/url-absolute-form

Conversation

@xia-chao

@xia-chao xia-chao commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Problem

When the request line is HTTP/1.1 absolute-form (GET http://example.com/foo HTTP/1.1), ctx.href is correct but ctx.URL is not:

// IncomingMessage.url === 'http://example.com/foo?q=1'
ctx.href      // 'http://example.com/foo?q=1'
ctx.URL.href  // 'http://127.0.0.1http//example.com/foo?q=1'
ctx.path      // '/foo'  (parseurl is fine)

The same garbage URL is produced even when the Host header already matches example.com (http://example.comhttp//example.com/foo?q=1). Proxies and some health checks send this request form.

Fix

href already special-cases /^https?:\/\//i on originalUrl and returns it as-is. request.URL now uses that same branch and constructs new URL(originalUrl) instead of concatenating protocol://host onto an already-absolute URL. Invalid input still collapses to Object.create(null).

URL continues to follow originalUrl after ctx.path= / ctx.query= rewrites, matching the existing href tests.

Testing

node --test __tests__/request/whatwg-url.test.js

Covers http / https / HTTP:// / port / matching Host / empty Host / path rewrite. New tests fail without the change and pass with it.

npm test — 447 pass, 0 fail.

Summary by Sourcery

Correct request.URL parsing for absolute-form request targets.

Bug Fixes:

  • Parse HTTP/1.1 absolute-form request targets correctly in request.URL, preserving their original scheme, host, port, path, and query instead of generating malformed URLs.

Enhancements:

  • Keep request.URL aligned with href behavior and preserve the existing handling of invalid URLs and rewritten paths.

Tests:

  • Add coverage for HTTP and HTTPS absolute-form targets, case-insensitive schemes, ports, Host header variations, and path rewrites.

Summary by CodeRabbit

  • Bug Fixes
    • Improved request URL parsing for absolute HTTP and HTTPS URLs.
    • Preserved URL details such as schemes, ports, and host headers when processing requests.
    • Ensured rewritten request paths do not overwrite the original absolute URL.

href already treats GET http://host/path as a complete URL.
request.URL concatenated protocol://host onto that string,
producing a garbage WHATWG href.
@coderabbitai

coderabbitai Bot commented Sep 1, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: ba29b90b-2bdf-4cab-91b4-9f968d1280ff

📥 Commits

Reviewing files that changed from the base of the PR and between 768b55e and 0c9866c.

📒 Files selected for processing (1)
  • lib/request.js
💤 Files with no reviewable changes (1)
  • lib/request.js

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The URL getter now parses absolute http:// and https:// request targets directly. Relative request targets keep the existing URL construction path. Tests now obtain request through the shared context helper. New tests cover HTTP and HTTPS URLs, uppercase schemes, explicit ports, Host headers, and URL behavior after path rewrites.

Merge Risk: ⚪ Minimal · up to 0c986

This localized change corrects absolute-form request URL parsing while preserving existing rewrite behavior; no actionable merge-blocking risk remains beyond normal checks and review.

🚥 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 and concisely describes the main change: fixing parsing of absolute-form request targets in request.URL.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
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.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files.


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.

@sourcery-ai

sourcery-ai Bot commented Sep 1, 2026

Copy link
Copy Markdown

Reviewer's Guide

Updates req.URL to parse HTTP/HTTPS absolute-form request targets directly, aligning it with href while retaining invalid-input handling and originalUrl semantics; adds comprehensive regression tests for schemes, hosts, ports, and path rewrites.

Flow diagram for absolute-form request URL parsing

flowchart TD
    A["request.URL accessed"] --> B["Read originalUrl"]
    B --> C{"originalUrl matches /^https?:\\/\\//i"}
    C -->|yes| D["new URL(originalUrl)"]
    C -->|no| E["new URL(protocol://host + originalUrl)"]
    D --> F{"URL construction succeeds"}
    E --> F
    F -->|yes| G["Return memoizedURL"]
    F -->|no| H["Return Object.create(null)"]
Loading

File-Level Changes

Change Details Files
Make WHATWG URL parsing preserve absolute-form HTTP request targets instead of prepending the local protocol and host.
  • Detect case-insensitive http:// and https:// targets before constructing the URL.
  • Parse absolute targets directly while retaining the existing origin-form construction path.
  • Continue returning a null-prototype object for invalid URLs and preserving memoization behavior.
lib/request.js
Add regression coverage for absolute-form URL parsing and rewrite semantics.
  • Cover HTTP, HTTPS, uppercase schemes, ports, matching or empty Host headers, and URL components.
  • Verify URL follows originalUrl rather than rewritten path state, consistent with href.
  • Refactor test-helper usage to access both the context factory and request fixture.
__tests__/request/whatwg-url.test.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Sep 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.90%. Comparing base (1061776) to head (2a1e295).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1999   +/-   ##
=======================================
  Coverage   99.90%   99.90%           
=======================================
  Files           9        9           
  Lines        2125     2127    +2     
=======================================
+ Hits         2123     2125    +2     
  Misses          2        2           

☔ 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.

@sourcery-ai sourcery-ai 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.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@yowainwright yowainwright 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.

LGTM!

Comment thread lib/request.js Outdated
@xia-chao

xia-chao commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

recheck

@yowainwright yowainwright 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.

Good one @bun-unsafe 🚀

@yowainwright
yowainwright merged commit 571938d into koajs:master Sep 3, 2026
8 checks passed
@xia-chao
xia-chao deleted the fix/url-absolute-form branch September 3, 2026 15:43
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