Skip to content

Commit cdfb1c5

Browse files
committed
Fix HTTP client database path encoding
1 parent 760b834 commit cdfb1c5

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

packages/gel/src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ export async function getAuthenticatedFetch(
9898

9999
const protocol = tlsSecurity === "insecure" ? "http" : "https";
100100
const baseUrl = `${protocol}://${address[0]}:${address[1]}`;
101-
const databaseUrl = `${baseUrl}/db/${database}/${basePath ?? ""}`;
101+
const encodedDatabase = encodeURIComponent(database);
102+
const databaseUrl = `${baseUrl}/db/${encodedDatabase}/${basePath ?? ""}`;
102103

103104
if (!token && config.password != null) {
104105
token = await httpSCRAMAuth(baseUrl, config.user, config.password);

packages/gel/test/client.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,6 +2344,46 @@ if (getAvailableFeatures().has("binary-over-http")) {
23442344
fetchConn.rawParse(Language.EDGEQL, `select 1`, Options.defaults()),
23452345
).rejects.toThrow(AuthenticationError);
23462346
});
2347+
2348+
test("getAuthenticatedFetch encodes database path segment", async () => {
2349+
const originalFetch = globalThis.fetch;
2350+
2351+
const fetchMock = jest.fn(async () =>
2352+
({
2353+
ok: true,
2354+
status: 200,
2355+
statusText: "OK",
2356+
headers: new Headers(),
2357+
arrayBuffer: async () => new ArrayBuffer(0),
2358+
}) as any,
2359+
);
2360+
2361+
(globalThis as any).fetch = fetchMock;
2362+
2363+
const config = {
2364+
address: ["localhost", 5656],
2365+
tlsSecurity: "insecure",
2366+
database: "preview/pr-662",
2367+
secretKey: "test-secret",
2368+
user: "test-user",
2369+
} as any;
2370+
2371+
const authenticatedFetch = await getAuthenticatedFetch(
2372+
config,
2373+
async () => "token",
2374+
);
2375+
2376+
await authenticatedFetch("", {});
2377+
2378+
expect(fetchMock).toHaveBeenCalledTimes(1);
2379+
2380+
const [urlArg] = fetchMock.mock.calls[0];
2381+
const url = typeof urlArg === "string" ? new URL(urlArg) : urlArg;
2382+
2383+
expect(url.pathname).toBe("/db/preview%2Fpr-662/");
2384+
2385+
(globalThis as any).fetch = originalFetch;
2386+
});
23472387
}
23482388

23492389
if (getGelVersion().major >= 5) {

0 commit comments

Comments
 (0)