Skip to content

Commit

Permalink
wrangler: Try to URL decode hyperdrive password and database name bef…
Browse files Browse the repository at this point in the history
…ore sending them to API

Also do the same in miniflare plugin
  • Loading branch information
mtlemilio committed Mar 1, 2024
1 parent bdff159 commit 0e82e17
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/twenty-laws-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"miniflare": minor
"wrangler": minor
---

URL decode components of the Hyperdrive config connection string
4 changes: 2 additions & 2 deletions packages/miniflare/src/plugins/hyperdrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ export const HYPERDRIVE_PLUGIN: Plugin<typeof HyperdriveInputOptionsSchema> = {
designator: {
name: `${HYPERDRIVE_PLUGIN_NAME}:${name}`,
},
database,
database: decodeURIComponent(database),
user: url.username,
password: url.password,
password: decodeURIComponent(url.password),
scheme,
},
};
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/e2e/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ describe("hyperdrive dev tests", () => {
[[hyperdrive]]
binding = "HYPERDRIVE"
id = "hyperdrive_id"
localConnectionString = "postgresql://user:pass@127.0.0.1:${port}/some_db"
localConnectionString = "postgresql://user:%21pass@127.0.0.1:${port}/some_db"
`,
"src/index.ts": dedent`
export default {
Expand Down Expand Up @@ -484,7 +484,7 @@ describe("hyperdrive dev tests", () => {
const url = new URL(text);
expect(url.pathname).toBe("/some_db");
expect(url.username).toBe("user");
expect(url.password).toBe("pass");
expect(url.password).toBe("!pass");
expect(url.host).not.toBe("localhost");
});
});
Expand Down
48 changes: 48 additions & 0 deletions packages/wrangler/src/__tests__/hyperdrive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,54 @@ describe("hyperdrive commands", () => {
`);
});

it("should handle creating a hyperdrive config if the password is URL encoded", async () => {
mockHyperdriveRequest();
await runWrangler(
"hyperdrive create test123 --connection-string='postgresql://test:a%23%3F81n%287@foo.us-east-2.aws.neon.tech/neondb'"
);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"7a040c1eee714e91a30ea6707a2d125c\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"foo.us-east-2.aws.neon.tech\\",
\\"port\\": 5432,
\\"database\\": \\"neondb\\",
\\"user\\": \\"test\\"
},
\\"caching\\": {
\\"disabled\\": false
}
}"
`);
});

it("should handle creating a hyperdrive config if the database name is URL encoded", async () => {
mockHyperdriveRequest();
await runWrangler(
"hyperdrive create test123 --connection-string='postgresql://test:password@foo.us-east-2.aws.neon.tech/%22weird%22%20dbname'"
);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"7a040c1eee714e91a30ea6707a2d125c\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"foo.us-east-2.aws.neon.tech\\",
\\"port\\": 5432,
\\"database\\": \\"/\\"weird/\\" dbname\\",
\\"user\\": \\"test\\"
},
\\"caching\\": {
\\"disabled\\": false
}
}"
`);
});

it("should handle listing configs", async () => {
mockHyperdriveRequest();
await runWrangler("hyperdrive list");
Expand Down
6 changes: 3 additions & 3 deletions packages/wrangler/src/hyperdrive/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ export async function handler(
host: url.hostname,
port: parseInt(url.port),
scheme: url.protocol.replace(":", ""),
database: url.pathname.replace("/", ""),
database: decodeURIComponent(url.pathname.replace("/", "")),
user: url.username,
password: url.password,
password: decodeURIComponent(url.password),
},
caching: {
disabled: args.cachingDisabled,
Expand All @@ -103,4 +103,4 @@ export async function handler(
JSON.stringify(database, null, 2)
);
}
}
}

0 comments on commit 0e82e17

Please sign in to comment.