Skip to content

Commit

Permalink
fix(s3-presigned-post): ensure unique conditions in policy (#5184)
Browse files Browse the repository at this point in the history
* fix(s3-presigned-post): ensure unique conditions in policy
  • Loading branch information
RanVaknin committed Sep 6, 2023
1 parent 729798c commit 3f8b581
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions packages/s3-presigned-post/src/createPresignedPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,26 @@ export const createPresignedPost = async (

// Prepare policies.
const expiration = new Date(now.valueOf() + Expires * 1000);
const conditions: PolicyEntry[] = [
...Conditions,
...Object.entries(fields).map(([k, v]) => ({ [k]: v })),
Key.endsWith("${filename}")
? ["starts-with", "$key", Key.substring(0, Key.lastIndexOf("${filename}"))]
: { key: Key },
];

const conditionsSet = new Set<string>();

for (const condition of Conditions) {
const stringifiedCondition = JSON.stringify(condition);
conditionsSet.add(stringifiedCondition);
}

for (const [k,v] of Object.entries(fields)) {
conditionsSet.add(JSON.stringify({ [k]: v }))
}

if (Key.endsWith("${filename}")) {
conditionsSet.add(JSON.stringify(["starts-with", "$key", Key.substring(0, Key.lastIndexOf("${filename}"))]));
} else {
conditionsSet.add(JSON.stringify({ key: Key }));
}

const conditions = Array.from(conditionsSet).map((item) => JSON.parse(item));

const encodedPolicy = base64Encoder(
utf8Decoder(
JSON.stringify({
Expand Down

0 comments on commit 3f8b581

Please sign in to comment.