Skip to content

Commit a57149f

Browse files
authored
wrangler: fix: support the deletion of secrets with complex names (#10668)
URL encode the secret names before being sent to the API, should allow users to delete secrets with complex names. Change is still compatible with older Wrangler versions thanks to the optional query param flag `url_encoded`.
1 parent 791b48a commit a57149f

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

.changeset/warm-clowns-visit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Support the deletion of secrets with complex names

packages/wrangler/src/__tests__/secret.test.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,16 +563,15 @@ describe("wrangler secret", () => {
563563
msw.use(
564564
http.delete(
565565
`*/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets/:secretName`,
566-
({ params }) => {
566+
({ request, params }) => {
567567
expect(params.accountId).toEqual("some-account-id");
568568
expect(params.scriptName).toEqual(
569569
legacyEnv && env ? `script-name-${env}` : "script-name"
570570
);
571-
if (!legacyEnv) {
572-
if (env) {
573-
expect(params.secretName).toEqual(input.secretName);
574-
}
575-
}
571+
expect(params.secretName).toEqual(input.secretName);
572+
expect(
573+
new URL(request.url).searchParams.get("url_encoded")
574+
).toEqual("true");
576575
return HttpResponse.json(createFetchResult(null));
577576
},
578577
{ once: true }
@@ -616,6 +615,23 @@ describe("wrangler secret", () => {
616615
expect(std.err).toMatchInlineSnapshot(`""`);
617616
});
618617

618+
it("should delete a secret which name includes special characters", async () => {
619+
mockDeleteRequest({ scriptName: "script-name", secretName: "the/key" });
620+
mockConfirm({
621+
text: "Are you sure you want to permanently delete the secret the/key on the Worker script-name?",
622+
result: true,
623+
});
624+
await runWrangler("secret delete the/key --name script-name");
625+
expect(std.out).toMatchInlineSnapshot(`
626+
"
627+
⛅️ wrangler x.x.x
628+
──────────────────
629+
🌀 Deleting the secret the/key on the Worker script-name
630+
✨ Success! Deleted secret the/key"
631+
`);
632+
expect(std.err).toMatchInlineSnapshot(`""`);
633+
});
634+
619635
it("should delete a secret: legacy envs", async () => {
620636
mockDeleteRequest(
621637
{ scriptName: "script-name", secretName: "the-key" },

packages/wrangler/src/secret/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,14 @@ export const secretDeleteCommand = createCommand({
318318
? `/accounts/${accountId}/workers/scripts/${scriptName}/secrets`
319319
: `/accounts/${accountId}/workers/services/${scriptName}/environments/${args.env}/secrets`;
320320

321-
await fetchResult(config, `${url}/${args.key}`, { method: "DELETE" });
321+
await fetchResult(
322+
config,
323+
`${url}/${encodeURIComponent(args.key)}`,
324+
{ method: "DELETE" },
325+
new URLSearchParams({
326+
url_encoded: "true",
327+
})
328+
);
322329
metrics.sendMetricsEvent("delete encrypted variable", {
323330
sendMetrics: config.send_metrics,
324331
});

0 commit comments

Comments
 (0)