diff --git a/.changeset/grumpy-dolphins-explain.md b/.changeset/grumpy-dolphins-explain.md new file mode 100644 index 000000000000..48c2440cd225 --- /dev/null +++ b/.changeset/grumpy-dolphins-explain.md @@ -0,0 +1,7 @@ +--- +"wrangler": patch +--- + +fix: trim trailing whitespace from the secrets before uploading + +resolves #993 diff --git a/packages/wrangler/src/__tests__/secret.test.ts b/packages/wrangler/src/__tests__/secret.test.ts index fd4409149265..81b867cd1de2 100644 --- a/packages/wrangler/src/__tests__/secret.test.ts +++ b/packages/wrangler/src/__tests__/secret.test.ts @@ -60,6 +60,22 @@ describe("wrangler secret", () => { describe("interactive", () => { useMockStdin({ isTTY: true }); + it("should trim stdin secret value", async () => { + mockPrompt({ + text: "Enter a secret value:", + type: "password", + result: `hunter2 + `, + }); + + mockPutRequest({ name: `secret-name`, text: `hunter2` }); + await runWrangler("secret put secret-name --name script-name"); + expect(std.out).toMatchInlineSnapshot(` + "🌀 Creating the secret for script script-name + ✨ Success! Uploaded secret secret-name" + `); + }); + it("should create a secret", async () => { mockPrompt({ text: "Enter a secret value:", @@ -146,6 +162,25 @@ describe("wrangler secret", () => { describe("non-interactive", () => { const mockStdIn = useMockStdin({ isTTY: false }); + it("should trim stdin secret value, from piped input", async () => { + mockPutRequest({ name: "the-key", text: "the-secret" }); + // Pipe the secret in as three chunks to test that we reconstitute it correctly. + mockStdIn.send( + `the`, + `-`, + `secret + ` // whitespace & newline being removed + ); + await runWrangler("secret put the-key --name script-name"); + + expect(std.out).toMatchInlineSnapshot(` + "🌀 Creating the secret for script script-name + ✨ Success! Uploaded secret the-key" + `); + expect(std.warn).toMatchInlineSnapshot(`""`); + expect(std.err).toMatchInlineSnapshot(`""`); + }); + it("should create a secret, from piped input", async () => { mockPutRequest({ name: "the-key", text: "the-secret" }); // Pipe the secret in as three chunks to test that we reconstitute it correctly. diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index 9cc29d816ac2..28a347b37d57 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -206,6 +206,14 @@ function demandOneOfOption(...options: string[]) { }; } +/** + * Remove trailing white space from inputs. + * Matching Wrangler legacy behavior with handling inputs + */ +function trimTrailingWhitespace(str: string) { + return str.trimEnd(); +} + class CommandLineArgsError extends Error {} function createCLIParser(argv: string[]) { @@ -1719,9 +1727,11 @@ function createCLIParser(argv: string[]) { const accountId = await requireAuth(config); const isInteractive = process.stdin.isTTY; - const secretValue = isInteractive - ? await prompt("Enter a secret value:", "password") - : await readFromStdin(); + const secretValue = trimTrailingWhitespace( + isInteractive + ? await prompt("Enter a secret value:", "password") + : await readFromStdin() + ); logger.log( `🌀 Creating the secret for script ${scriptName} ${