Skip to content

fix(s3-presigned-post): apply ${filename} substitution skip-condition to all fields (#7191)#7988

Open
mohanrajvenkatesan23-04 wants to merge 1 commit into
aws:mainfrom
mohanrajvenkatesan23-04:fix/issue-7191-presigned-post-filename-substitution
Open

fix(s3-presigned-post): apply ${filename} substitution skip-condition to all fields (#7191)#7988
mohanrajvenkatesan23-04 wants to merge 1 commit into
aws:mainfrom
mohanrajvenkatesan23-04:fix/issue-7191-presigned-post-filename-substitution

Conversation

@mohanrajvenkatesan23-04

Copy link
Copy Markdown

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 the key field — when key ended 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 helper addFieldCondition(name, value) in packages/s3-presigned-post/src/createPresignedPost.ts and applies 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. 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 fields
  • packages/s3-presigned-post/src/createPresignedPost.spec.ts — added regression tests

generated by AI tools, and reviewed by Mohanraj Venkatesan

Testing

Added two regression tests in packages/s3-presigned-post/src/createPresignedPost.spec.ts:

  1. should emit a starts-with condition when ${filename} is used in a non-key field (issue #7191) — sets Fields: { 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.
  2. should emit a starts-with condition for arbitrary user-metadata fields containing ${filename} — sets Fields: { "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:

  • The Yarn 4 corepack shim mishandles spaces in the user profile path (C:\Users\Mohanraj Venkatesan\...) on Windows.
  • Running vitest directly bypasses the turbo-orchestrated workspace build, so @aws-sdk/util-endpoints (an internal workspace dep transitively imported via @aws-sdk/client-s3) has no dist-*/ entries to resolve.

The change is small and matches the pattern of the existing tested case (Key.endsWith("${filename}")starts-with condition). CI on this PR will validate the new tests run green.

Checklist

  • I have read the CONTRIBUTING document
  • I have added tests to cover my changes
  • All new and existing tests pass (pending CI; see Testing section for local environment caveat)
  • I have updated the documentation accordingly (no public API surface changed)
  • I have added a changeset entry if needed (internal bug fix; release notes will reference issue Presigned POST URL ${filename} substitution potential bug #7191)

… 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
@mohanrajvenkatesan23-04 mohanrajvenkatesan23-04 requested a review from a team as a code owner May 6, 2026 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Presigned POST URL ${filename} substitution potential bug

1 participant