Skip to content

Commit 3f8b581

Browse files
authored
fix(s3-presigned-post): ensure unique conditions in policy (#5184)
* fix(s3-presigned-post): ensure unique conditions in policy
1 parent 729798c commit 3f8b581

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

packages/s3-presigned-post/src/createPresignedPost.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,26 @@ export const createPresignedPost = async (
6666

6767
// Prepare policies.
6868
const expiration = new Date(now.valueOf() + Expires * 1000);
69-
const conditions: PolicyEntry[] = [
70-
...Conditions,
71-
...Object.entries(fields).map(([k, v]) => ({ [k]: v })),
72-
Key.endsWith("${filename}")
73-
? ["starts-with", "$key", Key.substring(0, Key.lastIndexOf("${filename}"))]
74-
: { key: Key },
75-
];
69+
70+
const conditionsSet = new Set<string>();
71+
72+
for (const condition of Conditions) {
73+
const stringifiedCondition = JSON.stringify(condition);
74+
conditionsSet.add(stringifiedCondition);
75+
}
76+
77+
for (const [k,v] of Object.entries(fields)) {
78+
conditionsSet.add(JSON.stringify({ [k]: v }))
79+
}
80+
81+
if (Key.endsWith("${filename}")) {
82+
conditionsSet.add(JSON.stringify(["starts-with", "$key", Key.substring(0, Key.lastIndexOf("${filename}"))]));
83+
} else {
84+
conditionsSet.add(JSON.stringify({ key: Key }));
85+
}
86+
87+
const conditions = Array.from(conditionsSet).map((item) => JSON.parse(item));
88+
7689
const encodedPolicy = base64Encoder(
7790
utf8Decoder(
7891
JSON.stringify({

0 commit comments

Comments
 (0)