Skip to content

Conversation

smilkuri
Copy link
Contributor

@smilkuri smilkuri commented Sep 4, 2025

Issue

#7304

Description

Fixes HeadBucket cross-region redirects for 400 responses for newer regions with x-amz-bucket-region header

Testing

Locally

 RUN  v3.2.4 /local/home/smilkuri/aws-sdk-js-v3/packages/middleware-sdk-s3

 ✓ src/s3-express/classes/S3ExpressIdentityCache.spec.ts (2 tests) 5ms
 ✓ src/s3-express/classes/S3ExpressIdentityCacheEntry.spec.ts (1 test) 13ms
 ✓ src/s3-express/classes/S3ExpressIdentityProviderImpl.spec.ts (2 tests) 7ms
 ✓ src/region-redirect-middleware.spec.ts (4 tests) 5ms
 ✓ src/validate-bucket-name.spec.ts (3 tests) 5ms
 ✓ src/s3-express/classes/SignatureV4S3Express.spec.ts (1 test) 2ms
 ✓ src/check-content-length-header.spec.ts (6 tests) 6ms
 ✓ src/throw-200-exceptions.spec.ts (6 tests) 11ms
 ✓ src/s3-express/functions/s3ExpressMiddleware.spec.ts (1 test) 2ms
 ✓ src/s3Configuration.spec.ts (1 test) 4ms

 Test Files  10 passed (10)
      Tests  27 passed (27)
   Start at  17:20:14
   Duration  568ms (transform 735ms, setup 0ms, collect 1.43s, tests 60ms, environment 2ms, prepare 1.37s)

Checklist

  • [n/a] If the PR is a feature, add integration tests (*.integ.spec.ts).
  • [n/a] If you wrote E2E tests, are they resilient to concurrent I/O?
  • [n/a] If adding new public functions, did you add the @public tag and enable doc generation on the package?

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@smilkuri smilkuri requested a review from a team as a code owner September 4, 2025 17:18
Comment on lines 39 to 46
if (
err?.$metadata?.httpStatusCode === 301 ||
// err.name === "PermanentRedirect" && --> removing the error name check, as that allows for HEAD operations (which have the 301 status code, but not the same error name) to be covered for region redirection as well
(err?.$metadata?.httpStatusCode === 400 && err?.name === "IllegalLocationConstraintException")
(err?.$metadata?.httpStatusCode === 400 && err?.name === "IllegalLocationConstraintException") ||
(err?.$metadata?.httpStatusCode === 400 &&
context.commandName === "HeadBucketCommand" &&
err?.$response?.headers?.["x-amz-bucket-region"])
) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplify this using variable names and combining the two 400 checks?

const statusCode = err?.$metadata?.httpStatusCode;
const isHeadBucket = context.commandName === "HeadBucketCommand";
const hasBucketRegionHeader = err?.$response?.headers?.["x-amz-bucket-region"];

if (
  statusCode === 301 ||
  (statusCode === 400 && (
    err?.name === "IllegalLocationConstraintException" ||
    (isHeadBucket && hasBucketRegionHeader)
  ))
)

(err?.name === "IllegalLocationConstraintException" || (isHeadBucket && hasBucketRegionHeader)))
) {
try {
const actualRegion = err.$response.headers["x-amz-bucket-region"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reuse the value from hasBucketRegionHeader. Since it isn't a boolean it should simply be called bucketRegionHeader.

@smilkuri smilkuri merged commit bb9e455 into main Sep 4, 2025
6 of 8 checks passed
@kuhe kuhe deleted the cross-region-redirect branch September 5, 2025 14:40
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants