fix(s3-presigned-post): apply ${filename} substitution skip-condition to all fields (#7191)#7988
Open
mohanrajvenkatesan23-04 wants to merge 1 commit into
Conversation
… to all fields (aws#7191) Per the S3 presigned-POST spec, any form field value (not just `key`) may contain the `${filename}` variable, which S3 substitutes with the uploaded file's name at request time. The previous code only special- cased the `key` field, emitting a `starts-with` policy condition when `key` ended with `${filename}`. All other fields containing `${filename}` were emitted as exact-match conditions, which S3 then invalidates after substitution, breaking the upload. Extract the existing `${filename}` handling into a small helper `addFieldCondition(name, value)` and apply it uniformly to every entry in `Fields` as well as to `key`. The helper uses `indexOf("\${filename}")` so the placeholder is detected anywhere within the value (not just as a suffix); when present, a `starts-with` policy condition is emitted using the literal prefix that precedes the placeholder. Fixes aws#7191
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
Fixes #7191 — Presigned POST URL
${filename}substitution potential bug.Description
Per the S3 presigned-POST spec, any form field value (not just
key) may contain the${filename}variable, which S3 substitutes with the uploaded file's name at request time. The previous implementation only special-cased thekeyfield — whenkeyended with${filename}it emitted a["starts-with", "$key", <prefix>]policy condition; otherwise an exact-match{key: ...}. Every other field containing${filename}was emitted as an exact-match condition, which S3 then invalidates after substitution, breaking the upload.The fix extracts the existing
${filename}handling into a small helperaddFieldCondition(name, value)inpackages/s3-presigned-post/src/createPresignedPost.tsand applies it uniformly to every entry inFieldsas well as tokey. The helper usesindexOf("${filename}")so the placeholder is detected anywhere within the value (not just as a suffix); when present, astarts-withpolicy condition is emitted using the literal prefix that precedes the placeholder. This generalizes the existing behavior to all fields and keeps signing in sync with the form values S3 will actually receive.Files changed:
packages/s3-presigned-post/src/createPresignedPost.ts— extracted helper, applied to all fieldspackages/s3-presigned-post/src/createPresignedPost.spec.ts— added regression testsgenerated by AI tools, and reviewed by Mohanraj Venkatesan
Testing
Added two regression tests in
packages/s3-presigned-post/src/createPresignedPost.spec.ts:should emit a starts-with condition when ${filename} is used in a non-key field (issue #7191)— setsFields: { success_action_redirect: "https://example.com/success?file=${filename}" }and asserts the policy contains["starts-with", "$success_action_redirect", "https://example.com/success?file="]and does NOT contain the exact-match form.should emit a starts-with condition for arbitrary user-metadata fields containing ${filename}— setsFields: { "x-amz-meta-original-name": "${filename}" }and asserts["starts-with", "$x-amz-meta-original-name", ""].The existing pre-existing test
should generate presigned post with filename(Key = "path/to/${filename}") continues to pass: the new helper produces the same["starts-with", "$key", "path/to/"]output it did before.Local test status (transparency): Local test runs were blocked by two environment issues unrelated to the fix:
C:\Users\Mohanraj Venkatesan\...) on Windows.@aws-sdk/util-endpoints(an internal workspace dep transitively imported via@aws-sdk/client-s3) has nodist-*/entries to resolve.The change is small and matches the pattern of the existing tested case (
Key.endsWith("${filename}")→starts-withcondition). CI on this PR will validate the new tests run green.Checklist