Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: secret input trailing spaces #1044

Merged
merged 1 commit into from
May 18, 2022
Merged
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
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