Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/gel/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ export async function getAuthenticatedFetch(

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

if (!token && config.password != null) {
token = await httpSCRAMAuth(baseUrl, config.user, config.password);
Expand Down
40 changes: 40 additions & 0 deletions packages/gel/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,46 @@ if (getAvailableFeatures().has("binary-over-http")) {
fetchConn.rawParse(Language.EDGEQL, `select 1`, Options.defaults()),
).rejects.toThrow(AuthenticationError);
});

test("getAuthenticatedFetch encodes database path segment", async () => {
const originalFetch = globalThis.fetch;

const fetchMock = jest.fn(async () =>
({
ok: true,
status: 200,
statusText: "OK",
headers: new Headers(),
arrayBuffer: async () => new ArrayBuffer(0),
}) as any,
);

(globalThis as any).fetch = fetchMock;

const config = {
address: ["localhost", 5656],
tlsSecurity: "insecure",
database: "preview/pr-662",
secretKey: "test-secret",
user: "test-user",
} as any;

const authenticatedFetch = await getAuthenticatedFetch(
config,
async () => "token",
);

await authenticatedFetch("", {});

expect(fetchMock).toHaveBeenCalledTimes(1);

const [urlArg] = fetchMock.mock.calls[0];
const url = typeof urlArg === "string" ? new URL(urlArg) : urlArg;

expect(url.pathname).toBe("/db/preview%2Fpr-662/");

(globalThis as any).fetch = originalFetch;
});
}

if (getGelVersion().major >= 5) {
Expand Down