Skip to content

Commit b205fb7

Browse files
authored
[wrangler] Validate secret bulk JSON stdin values (#14196)
1 parent 8b3365a commit b205fb7

4 files changed

Lines changed: 47 additions & 8 deletions

File tree

.changeset/tall-secrets-sneeze.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Validate JSON stdin values for `wrangler secret bulk`
6+
7+
JSON input piped through stdin now validates that secret values are strings or null before sending them to the API, matching the existing behavior for file input.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,22 @@ describe("wrangler secret", () => {
12681268
);
12691269
});
12701270

1271+
it("should fail if JSON stdin contains a record with non-string values", async ({
1272+
expect,
1273+
}) => {
1274+
mockReadlineInput(
1275+
JSON.stringify({
1276+
"invalid-secret": 999,
1277+
})
1278+
);
1279+
1280+
await expect(
1281+
runWrangler("secret bulk --name script-name")
1282+
).rejects.toThrowErrorMatchingInlineSnapshot(
1283+
`[Error: The value for "invalid-secret" in "piped input" is not null or a "string" instead it is of type "number"]`
1284+
);
1285+
});
1286+
12711287
it("should count success and network failure on secret bulk", async ({
12721288
expect,
12731289
}) => {

packages/wrangler/src/__tests__/versions/secrets/bulk.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,22 @@ describe("versions secret bulk", () => {
251251
`);
252252
});
253253

254+
test("should error on json stdin with non-string values", async ({
255+
expect,
256+
}) => {
257+
mockReadlineInput(
258+
JSON.stringify({
259+
SECRET_1: 1,
260+
})
261+
);
262+
263+
await expect(
264+
runWrangler(`versions secret bulk --name script-name`)
265+
).rejects.toThrowErrorMatchingInlineSnapshot(
266+
`[Error: The value for "SECRET_1" in "piped input" is not null or a "string" instead it is of type "number"]`
267+
);
268+
});
269+
254270
test("unsafe metadata is provided", async ({ expect }) => {
255271
writeWranglerConfig({
256272
name: "script-name",

packages/wrangler/src/secret/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -652,14 +652,6 @@ export async function parseBulkInputToObject(
652652
});
653653
}
654654
}
655-
validateFileSecrets(content, input);
656-
if (!includeNull) {
657-
content = Object.fromEntries(
658-
Object.entries(content).filter(
659-
(entry): entry is [string, string] => entry[1] != null
660-
)
661-
);
662-
}
663655
} else {
664656
secretSource = "stdin";
665657
try {
@@ -684,5 +676,13 @@ export async function parseBulkInputToObject(
684676
return;
685677
}
686678
}
679+
validateFileSecrets(content, input ?? "piped input");
680+
if (!includeNull) {
681+
content = Object.fromEntries(
682+
Object.entries(content).filter(
683+
(entry): entry is [string, string] => entry[1] != null
684+
)
685+
);
686+
}
687687
return { content, secretSource, secretFormat };
688688
}

0 commit comments

Comments
 (0)