Skip to content

Commit

Permalink
fix: added trimTrailingWhitespace() to secretValue being sent in PU…
Browse files Browse the repository at this point in the history
…T request

Matching Wrangler legacy behavior with handling inputs

resolves #993
  • Loading branch information
JacobMGEvans committed May 18, 2022
1 parent 963e9e0 commit a05478f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/grumpy-dolphins-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: trim trailing whitespace from the secrets before uploading

resolves #993
35 changes: 35 additions & 0 deletions packages/wrangler/src/__tests__/secret.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:",
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 13 additions & 3 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

export async function main(argv: string[]): Promise<void> {
Expand Down Expand Up @@ -1719,9 +1727,11 @@ export async function main(argv: string[]): Promise<void> {
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} ${
Expand Down

0 comments on commit a05478f

Please sign in to comment.