Skip to content

feat(Storage)!: Enable full object checksum validation for resumable uploads - #15769

Open
mahendra-google wants to merge 6 commits into
googleapis:mainfrom
mahendra-google:feature/enable-full-object-checksum
Open

feat(Storage)!: Enable full object checksum validation for resumable uploads#15769
mahendra-google wants to merge 6 commits into
googleapis:mainfrom
mahendra-google:feature/enable-full-object-checksum

Conversation

@mahendra-google

Copy link
Copy Markdown
Contributor

This PR transitions upload object checksum integrity validation from client-side to the server-side.

Previously, CRC32C validation occurred after the upload is completed, requiring the client to delete the object (using DeleteAndThrow upload validation mode) or leave a corrupted object in the bucket (using ThrowOnly upload validation mode) upon hash mismatches. By leveraging the new LastRequestExecuting event in google-api-dotnet-client core library, this implementation calculates CRC32C incrementally during streaming and injects the x-goog-hash: crc32c=... header on the final chunk. If a checksum mismatch occurs, the server rejects the upload with an HTTP 400 Bad Request, ensuring invalid objects are never created in the bucket.

@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Jul 20, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request updates the upload validation mechanism in the Google Cloud Storage V1 client. It introduces UploadValidationMode.RejectAndThrow to allow the server to reject mismatched objects before creation, deprecates UploadValidationMode.DeleteAndThrow, and removes UploadValidationMode.ThrowOnly along with the UploadValidationException class. It also introduces a HashingStream within CustomMediaUpload to calculate CRC32C hashes on the fly and inject the x-goog-hash header. The feedback highlights that removing ThrowOnly and UploadValidationException are breaking changes for existing client applications, and recommends restoring them with an [Obsolete] attribute to maintain backward compatibility.

I am having trouble creating individual review comments. Click here to see my feedback.

apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/UploadValidationMode.cs (29-35)

high

Removing ThrowOnly from the UploadValidationMode enum is a breaking change for existing client applications. To maintain backward compatibility, please restore ThrowOnly and mark it as [Obsolete], mapping it to the new validation behavior.

        /// <summary>
        /// Obsolete. Use <see cref="RejectAndThrow"/> instead.
        /// Previously, the hash of the data was computed while uploading, and if the resulting object had a different hash, an exception was thrown, but the object remained present in Storage.
        /// </summary>
        [Obsolete("ThrowOnly is deprecated. Use RejectAndThrow instead, as the server now rejects the object before creation.")]
        ThrowOnly = 1,

        /// <summary>
        /// Obsolete. Use <see cref="RejectAndThrow"/> instead.
        /// Previously, the object was uploaded and then deleted if the hash mismatched. 
        /// The server now rejects mismatched objects automatically before creation.
        /// </summary>
        [Obsolete("DeleteAndThrow is deprecated. Use RejectAndThrow instead, as the server now rejects the object before creation.")]
        DeleteAndThrow = 2,

apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/UploadValidationException.cs (23-26)

high

Removing the public class UploadValidationException is a breaking change for existing client applications that catch this exception.

To maintain backward compatibility, please restore this class and mark it as [Obsolete]. This allows existing code to compile with a warning rather than failing with a compilation error.

@mahendra-google mahendra-google changed the title feat(Storage): Enable full object checksum validation for resumable uploads feat(Storage)!: Enable full object checksum validation for resumable uploads Jul 22, 2026
@mahendra-google
mahendra-google marked this pull request as ready for review July 30, 2026 06:14
@mahendra-google
mahendra-google requested review from a team as code owners July 30, 2026 06:14
@amanda-tarafa

Copy link
Copy Markdown
Contributor

Let's keep this one in draft until we have updated the core libraries to the newest dependency.

@amanda-tarafa
amanda-tarafa marked this pull request as draft August 4, 2026 07:04
@mahendra-google
mahendra-google force-pushed the feature/enable-full-object-checksum branch from 8160401 to 4b0d135 Compare August 21, 2026 06:59
@mahendra-google
mahendra-google marked this pull request as ready for review August 21, 2026 07:04
Comment thread apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/CustomMediaUpload.cs Outdated
-   Add integration tests to verify ArgumentException propagation through
  Google.Apis ResumableUpload when resuming an upload from the intermediate offset.

@amanda-tarafa amanda-tarafa left a comment

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.

Because of the breaking changes you need to update the next version of the library in pipeline-state.json. Add a new property for Storage "nextVersion" with value "5.0.0" and next time we release we'll automatically bump major.

<PackageReference Include="ConfigureAwaitChecker.Analyzer" PrivateAssets="All" />
<PackageReference Include="Google.Apis" VersionOverride="1.76"/>
<PackageReference Include="Google.Api.Gax.Rest" />
<PackageReference Include="Google.Apis.Storage.v1" VersionOverride="[1.74.0.4115, 2.0.0.0)" />

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.

Comment on lines +50 to +53
"Cannot perform hash validation when resuming an upload from a non-zero offset, " +
"as the complete stream contents are required to compute the hash. " +
"To resume this upload, disable validation by setting UploadValidationMode to None.",
nameof(stream));

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.

Two notes on this:

  • If you add this error message here, the upload will execute completely and only at the end the user will know that they cannot resume it with hash validation. That seems like a waste of resources and a poor user experience. Cant' you check this at the beginning of the resume? Or fail as soon as you find gaps on the hash, etc.? I won't block my approval on this but calling it out as a significant improvement.
  • You can still do hash validation on resumes, which are only supported for seekable streams, You still upload from the resume position, but you can calculate the hash for the whole stream rewinding as necessary. This is an improvement to consider for a follow up PR.

ProcessBytes(buffer, offset, bytesRead, startingPos);
if (!_stream.CanSeek)
{
_position += bytesRead;

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.

Consider always maintaining _position up to date, initialized to the _stream.Position if the stream is seekable, otherwise, 0, and then you don't have to use this if everywhere.

Comment on lines +51 to 52
var mediaUpload = new CustomMediaUpload(Service, destination, destination.Bucket, source, destination.ContentType, options);
options?.ModifyMediaUpload(mediaUpload);

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.

This is "weird", the options are meant to modify the mediaUpload object, but now we use them to create the mediaUpload object and then to modify it.

This is non blocker, but for the sake o consistency, can we not modify the CustomMediaUpload constructor? Then add a method in the options WrapSourceStream that wraps the source in a HashingStream. You would then pass that maybe wrapped sourced to the original constructos of CustomMediaUpload which can know if it has to add the last request callback based on if the stream is a HashStream or not.

var maybeHashingSource = options?.WrapStreamSource(source);
var mediaUpload = new CustomMediaUpload(Service, destination, destination.Bucket, maybeHashingSource, destination.ContentType);
options?.ModifyMediaUpload(mediaUpload);

var hashingStream = new CustomMediaUpload.HashingStream(baseStream);
var buffer = new byte[data.Length];

// Simulate resuming an upload from a new process starting at intermediate offset 10

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.

A new process wouldn't use the same hashingStream instance. Move the source stream instead and create a new instance of HashingStream for this test to be really "isolated".

}

[Fact]
public void CustomMediaUpload_ShouldThrowArgumentException_WhenResumingFromIntermediateOffset()

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.

I think this test is redundant with the three above. You tested that the HashingStream does as expected (that should be moved to unit tests) and tested that the CustomMediaUploader does as expected.

This test is just a combination of that.

}

[Fact]
public void CustomMediaUpload_ShouldContainHashHeaderAndCorrectHash_WhenRetriedFromIntermediateOffset()

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.

This test should just be a success test, that is, after resuming, Storage accepts and creates the object. Testing the internals should be left to unit tests, if you need to test that the header is added correctly, then a unit test that has a fake service that intercepts the request and checks its headers is the way to go.


private class BreakDeleteInterceptor : IHttpExecuteInterceptor
[Fact]
public void HashingStream_ShouldHandleRetries_WhenRestartedFromBeginning()

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.

This is a unit test not an integration test. It's not talking to any service.

}

[Fact]
public void HashingStream_ShouldHandleRetries_WhenSeekingBackwardsToIntermediatePoint()

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.

Unit test.

}

[Fact]
public void HashingStream_ShouldDetectGaps_WhenResumingFromIntermediateOffset()

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.

Unit test

…ry in pipeline-state.json

- Assign value 5.0.0 to the nextVersion propery so that next storage release will be major release
…76.0.4250 in apis.json.

- Removed Google.Apis 1.76 version dependency from Google.Cloud.Storage.V1 project file
- Regenerated project file using generateprojects.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants