Skip to content

fix(http): resolve relative Location headers per RFC 3986 without path duplication - #2254

Merged
Vincent Biret (baywet) merged 9 commits into
microsoft:mainfrom
hardikkaurani:fix/redirect-relative-url-resolution
Nov 16, 2026
Merged

Vincent Biret (baywet) merged 9 commits into
microsoft:mainfrom
hardikkaurani:fix/redirect-relative-url-resolution

Conversation

@hardikkaurani

@hardikkaurani Hardik K (hardikkaurani) commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2244

Problem

In RedirectHandler.java, when handling a redirect response with a relative Location header, getRedirect() performed manual URL string concatenation:

if (location.startsWith("/")) {
    if (request.url().toString().endsWith("/")) {
        location = location.substring(1);
    }
    location = request.url() + location;
}

HttpUrl requestUrl = request.url();
HttpUrl locationUrl = request.url().resolve(location);

Per RFC 7231 §7.1.2 and RFC 3986 §5.2, a relative URI reference starting with a single slash is an absolute-path reference that must be resolved against the scheme and authority (host and port) of the base URI. Concatenating request.url() + location causes:

  1. Path Duplication: E.g., https://example.com/v1.0/users with Location: /v2.0/users was concatenated to https://example.com/v1.0/users/v2.0/users.
  2. Query String Corruption: E.g., https://example.com/v1.0/users?top=10 with Location: /v2.0/users was concatenated to https://example.com/v1.0/users?top=10/v2.0/users, corrupting the path into the query parameter value.
  3. Infinite Loop Risk on Unresolvable Redirects: In intercept(), if isRedirected() returned true but getRedirect() returned null (e.g. malformed or blank Location header), the while (shouldRedirect) loop failed to terminate shouldRedirect, resulting in re-executing the identical request repeatedly.

Changes

  1. Remove manual string concatenation in RedirectHandler.getRedirect():
    Allow OkHttp's built-in request.url().resolve(location) to resolve relative references directly according to RFC 3986.
  2. Infinite redirect loop safeguard in intercept():
    When followup == null, set shouldRedirect = false so unresolvable redirects terminate the loop immediately and return the received response to the caller.
  3. Blank Location header handling:
    In isRedirected(), treat empty or whitespace-only Location headers as non-redirects (return false). In getRedirect(), trim the location header and return null if blank.

Tests

Added comprehensive test coverage in RedirectHandlerTests.java:

  • Root-relative path resolution without path duplication (/v2.0/users against /v1.0/users).
  • Root-relative path resolution with trailing slashes in original URL.
  • Relative path resolution against parent directory (details against /v1.0/users/item).
  • Query parameter resolution: dropping base queries and retaining redirect target queries.
  • Blank and whitespace-only Location header handling in isRedirected and getRedirect.
  • Unsupported URI schemes (mailto:, javascript:) returning null.
  • Sensitive header (Authorization, Cookie) retention on same-origin relative redirects.
  • End-to-end MockWebServer redirect execution verifying full pipeline flow through OkHttp interceptors.

Local validation:

  • ./gradlew :components:http:okHttp:check passed cleanly (including tests, Spotless, and SpotBugs).
  • Spotless checked across all modules.

…out string concatenation

Remove manual string concatenation of request URL and Location header in
RedirectHandler.getRedirect(). Relying on HttpUrl.resolve() conforms to
RFC 7231 section 7.1.2 and RFC 3986 section 5.2, correctly resolving
absolute-path and relative-path redirects without path duplication or query
string corruption.

Closes microsoft#2244

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
Add unit tests verifying that root-relative Location headers (e.g.
/v2.0/users) resolve directly against the scheme and authority without
duplicating parent path segments from the original request URL.

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…query strings

Add unit tests verifying that original request query strings do not
corrupt destination path segments during relative redirects, and that
redirect target query parameters are preserved as required by RFC 3986.

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…t be resolved

When isRedirected evaluates to true but getRedirect cannot build a followup
request (e.g. malformed Location, unsupported protocol, or empty URI),
set shouldRedirect to false to terminate the loop cleanly and return the
received response rather than infinitely re-executing the same request.

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
Guard against empty or whitespace-only Location headers in both
isRedirected() and getRedirect(), preventing spurious redirect handling
when servers return blank Location header values.

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…ders

Add unit test coverage asserting that empty and blank Location headers
are not treated as redirects in isRedirected() and return null in
getRedirect(), and that malformed or unsupported URI schemes return null.

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…gin relative redirects

Add unit test asserting that Authorization and Cookie headers are preserved
when following same-origin relative redirects.

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…edirects

Add an end-to-end integration test verifying that a 302 response with
a root-relative Location header (/v2/resource) executes through OkHttpClient
and RedirectHandler middleware pipeline to the expected target path.

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner November 14, 2026 06:27
Copilot AI balanced review requested due to automatic review settings November 14, 2026 06:27

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

Following up on #2244, this PR addresses the relative redirect URL resolution issue in RedirectHandler.

Previously, RedirectHandler.getRedirect() performed manual string concatenation (request.url() + location) whenever a Location header started with /. This caused path duplication (e.g. /v1.0/users + /v2.0/users -> /v1.0/users/v2.0/users) and corrupted existing query strings by appending the new path into query parameter values.

Key changes:

  • Replaced manual string concatenation by delegating relative reference resolution directly to OkHttp's HttpUrl.resolve(), adhering to RFC 7231 §7.1.2 and RFC 3986 §5.2.
  • Added a safeguard in intercept() ensuring shouldRedirect is reset to false when followup == null, preventing infinite request loops on unresolvable redirect targets.
  • Handled blank (empty and whitespace-only) Location headers as non-redirects in both isRedirected() and getRedirect().
  • Added unit and MockWebServer integration tests covering root-relative paths, directory-relative paths, query string preservation, sensitive header retention, and end-to-end middleware execution.

All local checks (./gradlew :components:http:okHttp:check and spotlessCheck) pass cleanly. Whenever you have a chance, please let me know if this looks good to you or if you'd like any adjustments.

Copilot AI 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.

🟡 Changes recommended

The new loop-termination safeguard in intercept() lacks a regression test verifying that an unsupported redirect scheme returns the original readable response without calling chain.proceed() again.

1 open issue

Review effort: Balanced

…schemes

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 16, 2026 14:02

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

I've addressed the review feedback in commit 26fe905b:

  • Added interceptWithUnsupportedRedirectSchemeReturnsOriginalResponseWithoutRetrying to RedirectHandlerTests.
  • The test validates that when a 302 response has an unresolvable or unsupported redirect target (e.g. Location: mailto:user@example.com), RedirectHandler.intercept() terminates the redirect loop immediately and returns the original response with its body intact and readable.
  • It also asserts that chain.proceed() is invoked exactly once, and is configured to fail immediately if any subsequent proceed() call is made to safeguard against loop regressions.

All tests (./gradlew test) and spotlessCheck pass cleanly. Whenever you get a chance, please take another look. Thank you!

Copilot AI 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.

🟢 Approval recommended

No active review issues block approval of the relative-redirect handling fix.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@github-project-automation github-project-automation Bot moved this to In Progress 🚧 in Kiota Nov 16, 2026
@baywet
Vincent Biret (baywet) added this pull request to the merge queue Nov 16, 2026
Merged via the queue into microsoft:main with commit 8ca2791 Nov 16, 2026
9 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress 🚧 to Done ✔️ in Kiota Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done ✔️

Development

Successfully merging this pull request may close these issues.

RedirectHandler corrupts relative Location header URLs by string-concatenating with request URL

3 participants