Skip to content

Fix first part response validation in parallel presigned URL multipart subscriber#7013

Merged
jencymaryjoseph merged 3 commits into
feature/master/pre-signed-url-getobjectfrom
jencyjos/pre-signed-url-getobject/parallel-subscriber-validation
Jun 9, 2026
Merged

Fix first part response validation in parallel presigned URL multipart subscriber#7013
jencymaryjoseph merged 3 commits into
feature/master/pre-signed-url-getobjectfrom
jencyjos/pre-signed-url-getobject/parallel-subscriber-validation

Conversation

@jencymaryjoseph

@jencymaryjoseph jencymaryjoseph commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Motivation and Context

  1. ParallelPresignedUrlMultipartDownloaderSubscriber did not validate the first part's response metadata (Content-Length, Content-Range alignment). Parts 1+ were validated via validatePartResponse(), but part 0 only checked whether Content-Range was parseable — it never verified that the returned byte range and content length matched expectations.
  2. Additionally, the subscriber had redundant error-forwarding logic, dead code on the success path
  3. PresignedUrlMultipartDownloaderSubscriber had a misleading counter named completedParts that was actually used as a part index assignment counter (incremented before the request, not after completion)
  4. Duplicate validation/request-building logic across both subscribers.
  5. Content-Range validation used string prefix matching (startsWith) which is sensitive to whitespace formatting. RFC 7233 allows variable spacing in the Content-Range header.
  6. forwardExceptionTo(resultFuture, response) was missing from sendFirstRequest, meaning part 0 wouldn't be cancelled if the download was cancelled externally (inconsistent with sendPartRequest and the regular ParallelMultipartDownloaderSubscriber).
  7. parseStartByteFromContentRange was redundant after switching to regex-based validation — replaced with parseContentRange() which returns both start and end bytes for complete range validation.

Modifications

  1. Added validatePartResponse(res, 0) call in sendFirstRequest to validate first part response (Content-Length, Content-Range alignment, start byte offset).
    Also renamed isCompletedExceptionally → downloadFailed for clarity and to avoid confusion with java.util.concurrent.CompletableFuture.isCompletedExceptionally().
  2. Removed dead code: handlePartError(null, 0) on success path when download already failed.
    Separated error != null and downloadFailed.get() checks in sendPartRequest to avoid passing null to handlePartError. Added debug log for late completions.
  3. Added [CompletableFutureUtils.forwardExceptionTo(resultFuture, response)](https://github.com/aws/aws-sdk-java-v2/pull/7013/changes#diff-eb746928a00e747ec260988bb77481373a53af2e2c11bc2918563b05ff11dd5aR143) in sendFirstRequest for consistency with sendPartRequest — ensures part 0 is cancelled if the download is cancelled externally.
  4. Renamed completedParts → nextPartIndex in serial subscriber. Renamed totalComplete to dispatched.
  5. Extracted shared validatePartResponse() and createRangedGetRequest() to PresignedUrlDownloadHelper.
    Moved calculateTotalParts() to MultipartDownloadUtils.
  6. Replaced string prefix matching with regex-based Content-Range validation using MultipartDownloadUtils.parseContentRange().
    Removed parseStartByteFromContentRange (now redundant). Validates both start byte and end byte numerically.
    Improved error message to include part index and actual vs expected byte ranges.
  7. Removed @immutable annotation from serial subscriber (class has mutable volatile fields).

Testing

  1. Added 3 new tests to ParallelPresignedUrlMultipartDownloaderSubscriberTest:
  • firstPartContentLengthMismatch_shouldFail — verifies Content-Length mismatch on part 0 is detected
  • firstPartContentRangeStartByteMismatch_shouldFail — verifies wrong Content-Range start byte on part 0 is detected
  • objectMutatedBetweenParts_shouldFailWith412 — verifies If-Match detects mid-download object mutation
  1. Added PresignedUrlDownloadHelperTest — isolated unit tests for validatePartResponse() and createRangedGetRequest()
  2. Added calculateTotalParts and parseContentRange tests to MultipartDownloadUtilsTest

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@jencymaryjoseph jencymaryjoseph requested a review from a team as a code owner June 3, 2026 19:05
@jencymaryjoseph jencymaryjoseph force-pushed the jencyjos/pre-signed-url-getobject/parallel-subscriber-validation branch 6 times, most recently from a93db33 to d39cda4 Compare June 5, 2026 05:30
if (isCompletedExceptionally.get()) {
handlePartError(error, 0);
if (downloadFailed.get()) {
return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Question: any reason we removed handlePartError(error, 0); from here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, at this point error is null, we return early if error != null. So handlePartError(error, 0); would have been no-op, hence removed it. If downloadFailed is already set, another thread handled the real error, we just return early.

Comment on lines +181 to +182
"Content-Range mismatch for part " + partIndex + ". Expected start byte: " + expectedStartByte
+ ", but got: bytes " + actualStartByte + "-" + actualEndByte));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: can we use String.format for better readability? same for other exception messages

@jencymaryjoseph jencymaryjoseph requested a review from zoewangg June 9, 2026 18:07
@jencymaryjoseph jencymaryjoseph force-pushed the jencyjos/pre-signed-url-getobject/parallel-subscriber-validation branch from ba83697 to 515de9f Compare June 9, 2026 20:16
@jencymaryjoseph jencymaryjoseph force-pushed the jencyjos/pre-signed-url-getobject/parallel-subscriber-validation branch from 515de9f to bf99b11 Compare June 9, 2026 20:34
…pre-signed-url-getobject/parallel-subscriber-validation
@jencymaryjoseph jencymaryjoseph merged commit ba66c7b into feature/master/pre-signed-url-getobject Jun 9, 2026
3 of 4 checks passed
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown

This pull request has been closed and the conversation has been locked. Comments on closed PRs are hard for our team to see. If you need more assistance, please open a new issue that references this one.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jun 9, 2026
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.

2 participants