fix(uri): keep a trailing slash on the base URL so proxied sub-paths survive - #356
Open
OsamaAnsar wants to merge 1 commit into
Open
Conversation
…survive
`encodeBaseUrl` returned a base URL whose path had no trailing slash
(e.g. `https://host/db/prod`). The Hrana client resolves endpoint paths
against it with `new URL("v2/pipeline", baseUrl)`, and per the WHATWG URL
resolver that replaces the last path segment when the base does not end
in `/`. So a client pointed at a libSQL server behind a reverse proxy on
a sub-path sent every request one segment short
(`https://host/db/v2/pipeline` instead of `https://host/db/prod/v2/pipeline`),
which is why appending a `/` to the URL was the known workaround.
Append a trailing slash to a non-empty path in `encodeBaseUrl` so the
base URL is safe for relative resolution. The root case (`""` / `"/"`)
is unchanged. The `wss:`/`ws:` clients get the same normalized URL; the
Hrana WebSocket handshake is not sensitive to the trailing slash.
Closes tursodatabase#296
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Closes #296.
When a client is pointed at a libSQL server hosted behind a reverse proxy on a
sub-path, the last path segment is silently dropped from every request:
The reporter's workaround — appending a trailing
/to the URL — is the clue tothe cause.
Cause
encodeBaseUrlbuilds the base URL with no trailing slash on the path(
https://example.com/some/sub/path). The Hrana client then resolves eachendpoint against it:
Per the WHATWG URL resolver, a relative reference resolved against a base whose
path does not end in
/replaces the base's last segment. Sopathisdropped and
v2/pipelinetakes its place.Fix
encodeBaseUrlnow appends a trailing slash to a non-empty path, so the base URLis safe for relative resolution.
""and"/"are unchanged (the common Turso Cloud case,libsql://db-org.turso.io, is unaffected).wss:/ws:callers receive the same normalized URL. The Hrana WebSockethandshake connects to the URL directly and isn't sensitive to the trailing
slash, so this is a no-op there; a directory-style base is also the correct
shape for a proxied WS deployment.
Tests
packages/libsql-client/src/__tests__/uri.test.ts:encodeBaseUrl()cases that carry a path to expect thetrailing slash.
v2/pipelineandv3-protobuf/pipelineagainst the encoded base and asserts the full sub-path is preserved, plus the
root case still resolves to
/v2/pipeline.npx jest --runInBand uri config— green (uri 10, config 23).tsc --noEmitclean for
@libsql/coreand@libsql/client.