Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax the validation of Location header when redirecting #5477

Merged
merged 6 commits into from Mar 29, 2024

Conversation

trustin
Copy link
Member

@trustin trustin commented Feb 28, 2024

Reported by @ohadgur at https://discord.com/channels/1087271586832318494/1209914423494311948

Motivation:

If a client is configured to follow redirects with followRedirects(), the client will validate the value of Location header before sending a follow-up request to the given redirect location. RedirectingClient validates and resolves the target location using URI.resolve() which rejects poorly encoded Location header values such as:

  • Location: /foo bar (space should be percent-encoded)
  • Location: /?${} ($, { and } should be percent-encoded.)

Such strict validation might not be suitable in real world scenarios and we could
normalize them just like a client validates and normalizes the initial request target.

Modifications:

  • RedirectingClient now uses RequestTarget.forClient() to parse and normalize the target location so it is more tolerant to poorly encoded Location header values.
  • RedirectingClient now implements its own relative path resolution logic. See
    RedirectingClient.resolveLocation() and `resolveRelativeLocation() for the detail.
  • Added host and port properties to RequestTarget.
  • Added host property to ClientRequestContext.
  • Moved DefaultRequestTarget.findAuthority() to ArmeriaHttpUtil to reuse it in RedirectingClient.
  • Miscellaneous:
    • Fixed a potential bug where RoutingContext.newPath() creates a new RequestTarget whose path is null
    • Fixed a bug where ClientRequestContext.uri() returns a double-encoded URI
      (Special thanks to @ohadgur for reporting this bug and suggesting a fix.)
    • Improved RequestTarget.forClient() to remove the port number in an absolute
      request target when possible, so that http://a:80 is normalized into http://a.

Result:

  • (Bug fix) An Armeria client is now more tolerant to poorly encoded Location header values when following redirects.
  • (New feature) You can now get the host and port part separately from a RequestTarget
    using RequestTarget.host() and RequestTarget.port().
  • (New feature) You can now get the host part from authority of the request URI using
    ClientRequestContext.host().
  • (Improvement) RequestTarget.forClient() now removes a redundant port number from
    the specified URI for simpler request target comparison. For example, https://foo and
    https://foo:443 are considered equal.

@trustin trustin added the defect label Feb 28, 2024
@trustin trustin added this to the 1.28.0 milestone Feb 28, 2024
@trustin
Copy link
Member Author

trustin commented Feb 28, 2024

To-dos:

  • Make resolveLocation() to always generate the full URL to simplify the overall logic and remove buildFullUri().
    • Make RequestTarget remove the port part when the port number is the default value.
  • Support //host/path format in Location

@trustin trustin marked this pull request as draft February 28, 2024 23:17
Copy link

codecov bot commented Feb 28, 2024

Codecov Report

Attention: Patch coverage is 88.59060% with 17 lines in your changes are missing coverage. Please review.

Project coverage is 74.06%. Comparing base (75d5af1) to head (68927bc).
Report is 7 commits behind head on main.

Files Patch % Lines
...com/linecorp/armeria/client/RedirectingClient.java 88.31% 2 Missing and 7 partials ⚠️
...ecorp/armeria/internal/common/ArmeriaHttpUtil.java 66.66% 1 Missing and 2 partials ⚠️
...a/internal/client/DefaultClientRequestContext.java 90.90% 1 Missing and 1 partial ⚠️
...rp/armeria/client/ClientRequestContextWrapper.java 0.00% 1 Missing ⚠️
.../armeria/internal/common/DefaultRequestTarget.java 96.29% 0 Missing and 1 partial ⚠️
...va/com/linecorp/armeria/server/RoutingContext.java 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5477       +/-   ##
===========================================
+ Coverage        0   74.06%   +74.06%     
- Complexity      0    20822    +20822     
===========================================
  Files           0     1801     +1801     
  Lines           0    76656    +76656     
  Branches        0     9783     +9783     
===========================================
+ Hits            0    56777    +56777     
- Misses          0    15253    +15253     
- Partials        0     4626     +4626     

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

Reported by @ohadgur at https://discord.com/channels/1087271586832318494/1209914423494311948

Motivation:

If a client is configured to follow redirects with `followRedirects()`,
the client will validate the value of `Location` header before sending a
follow-up request to the given redirect location. `RedirectingClient`
validates and resolves the target location using `URI.resolve()` which
rejects poorly encoded `Location` header values such as:

- `Location: /foo bar` (space should be percent-encoded)
- `Location: /?${}` (`$`, `{` and `}` should be percent-encoded.)

Modifications:

- `RedirectingClient` now uses `RequestTarget.forClient()` to parse and
  normalize the target location so it is more tolerant to poorly encoded
  `Location` header values.
- `RedirectingClient` now implements its own relative path resolution
  logic. See `RedirectingClient.resolveLocation()` for the detail.
- Added `host` and `port` properties to `RequestTarget`.
- Moved `DefaultRequestTarget.findAuthority()` to `ArmeriaHttpUtil` to
  reuse it in `RedirectingClient`.
- Miscellaneous:
  - Fixed a potential bug where `RoutingContext.newPath()` creates a new
    `RequestTarget` whose `path` is `null`

Result:

- An Armeria client is now more tolerant to poorly encoded `Location`
  header values when following redirects.
@trustin trustin force-pushed the relax_redirect_target_validation branch from f6ab55b to 7d883b9 Compare March 8, 2024 08:15
@trustin trustin marked this pull request as ready for review March 8, 2024 08:42
Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks!

…nt.java

Co-authored-by: minux <songmw725@gmail.com>
Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

Looks solid! 👍

Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

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

Changes make sense 👍 Thanks! 🙇 👍 🙇

@jrhee17 jrhee17 merged commit db3973d into line:main Mar 29, 2024
17 of 18 checks passed
@trustin trustin deleted the relax_redirect_target_validation branch April 1, 2024 08:26
@trustin
Copy link
Member Author

trustin commented Apr 1, 2024

Thanks for reviewing, folks!

@ohadgur
Copy link
Contributor

ohadgur commented Apr 11, 2024

@trustin not urgent, of course, but when is this change expected to be released?

@jrhee17
Copy link
Contributor

jrhee17 commented Apr 11, 2024

We're expecting to release 1.28.0 tomorrow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants