Skip to content

fix: strip fragments and empty queries from client base URLs - #1292

Open
waw666waw666 wants to merge 1 commit into
go-resty:v3from
waw666waw666:fix/strip-baseurl-fragments
Open

waw666waw666 wants to merge 1 commit into
go-resty:v3from
waw666waw666:fix/strip-baseurl-fragments

Conversation

@waw666waw666

Copy link
Copy Markdown

#1272 stripped fragments and empty queries from load balancer base URLs because request URLs are composed by concatenating the base URL with the request path. Client.SetBaseURL stored the URL verbatim, so the same corruption applies to the main path: SetBaseURL("https://host/api#frag") + Get("/users") sends the request to /api (the path is swallowed into the fragment), and SetBaseURL("https://host/api?") + Get("/users") turns the path into the query string.

SetBaseURL now sanitizes the URL the same way extractBaseURL does for load balancer bases — parse, clear Fragment/RawFragment and ForceQuery, store the result (new sanitizeBaseURL helper in client.go). Well-formed base URLs, including a trailing slash, are stored unchanged; an unparseable URL is stored as-is so its error still surfaces at request time.

new baseurl_sanitize_test.go spins up an httptest server and asserts the received path/query for base URLs ending in /api#frag, /api?, /api# and /api, plus stored-value assertions for valid, fragment, empty-query and unparseable base URLs. Commands: go test -run 'TestSetBaseURL' -count=1 -v . and full go test -count=1 . (only the pre-existing timing-sensitive TestTraceInfoOnTimeout* failures, identical before and after).

Load balancer base URLs have stripped fragments and empty queries
since go-resty#1272, because request URLs are composed by concatenating the
base URL with the request path. Client.SetBaseURL stored the URL
verbatim, so the same corruption applied there:

- SetBaseURL("https://host/api#frag") + Get("/users") sent the
  request to /api; "/users" was swallowed into the fragment.
- SetBaseURL("https://host/api?") + Get("/users") turned the path
  into the query string.

Sanitize the base URL in SetBaseURL the same way extractBaseURL
sanitizes load balancer bases: parse it, clear Fragment/RawFragment
and ForceQuery, and store the result. Well-formed base URLs,
including a trailing slash, are stored unchanged, and an unparseable
URL is stored as-is so its error still surfaces at request time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant