-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(storage): add full object checksum validation for bidi flow #13266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2db4acb
feat(storage): Adding CumulativeHasher wrapper class for Full object …
351729c
Fixing lint and small updates
72af919
feat(storage): add full object checksum validation for grpc flow
91e8f36
fix: fix lint
e67803a
feat(storage): add full object checksum validation for bidi flow
a24e32a
Disabling cumulative hasher for noop
5c3c71a
Merge branch 'main' into add-cumulative-hasher-bidi-flow
Dhriti07 dc264f8
Merge branch 'main' into add-cumulative-hasher-bidi-flow
Dhriti07 4b3c67e
format fixes
ad16370
Merge branch 'main' into add-cumulative-hasher-bidi-flow
Dhriti07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -150,7 +150,10 @@ private AccumulatingRead( | |||||||||||||||||
| IOAutoCloseable onCloseCallback) { | ||||||||||||||||||
| super(rangeSpec, retryContext, onCloseCallback); | ||||||||||||||||||
| this.readId = readId; | ||||||||||||||||||
| this.hasher = hasher; | ||||||||||||||||||
| this.hasher = | ||||||||||||||||||
| (rangeSpec.begin() == 0) | ||||||||||||||||||
| ? new CumulativeHasher(hasher, 0, rangeSpec.maxLength()) | ||||||||||||||||||
| : hasher; | ||||||||||||||||||
| this.complete = SettableApiFuture.create(); | ||||||||||||||||||
| this.childRefs = Collections.synchronizedList(new ArrayList<>()); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
@@ -280,7 +283,10 @@ static class StreamingRead extends BaseObjectReadSessionStreamRead<ScatteringByt | |||||||||||||||||
| IOAutoCloseable onCloseCallback) { | ||||||||||||||||||
| super(rangeSpec, retryContext, onCloseCallback); | ||||||||||||||||||
| this.readId = new AtomicLong(readId); | ||||||||||||||||||
| this.hasher = hasher; | ||||||||||||||||||
| this.hasher = | ||||||||||||||||||
| (rangeSpec.begin() == 0) | ||||||||||||||||||
| ? new CumulativeHasher(hasher, 0, rangeSpec.maxLength()) | ||||||||||||||||||
| : hasher; | ||||||||||||||||||
|
Comment on lines
+286
to
+289
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the
Suggested change
|
||||||||||||||||||
| this.closed = false; | ||||||||||||||||||
| this.failFuture = SettableApiFuture.create(); | ||||||||||||||||||
| this.queue = new ArrayBlockingQueue<>(2); | ||||||||||||||||||
|
|
||||||||||||||||||
159 changes: 159 additions & 0 deletions
159
...storage/google-cloud-storage/src/main/java/com/google/cloud/storage/CumulativeHasher.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.storage; | ||
|
|
||
| import com.google.api.gax.grpc.GrpcStatusCode; | ||
| import com.google.cloud.storage.Crc32cValue.Crc32cLengthKnown; | ||
| import com.google.protobuf.ByteString; | ||
| import com.google.storage.v2.Object; | ||
| import io.grpc.Status.Code; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.Locale; | ||
| import java.util.OptionalLong; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * A wrapper around hasher that accumulates checksums and validates them at the end of the read if | ||
| * it was a full object read. | ||
| */ | ||
| final class CumulativeHasher implements Hasher { | ||
| private final Hasher delegate; | ||
| private final long startOffset; | ||
| private final OptionalLong limit; | ||
| private Crc32cLengthKnown cumulativeHash; | ||
|
|
||
| CumulativeHasher(Hasher delegate, long startOffset, OptionalLong limit) { | ||
| this.delegate = delegate; | ||
| this.startOffset = startOffset; | ||
| this.limit = limit; | ||
| this.cumulativeHash = Crc32cValue.zero(); | ||
| } | ||
|
|
||
| @Override | ||
| public Crc32cLengthKnown hash(ByteBuffer b) { | ||
| return delegate.hash(b); | ||
| } | ||
|
|
||
| @Override | ||
| public Crc32cLengthKnown hash(ByteString byteString) { | ||
| return delegate.hash(byteString); | ||
| } | ||
|
|
||
| @Override | ||
| public void validate(Crc32cValue<?> expected, Supplier<ByteBuffer> b) | ||
| throws ChecksumMismatchException { | ||
| ByteBuffer byteBuffer = b.get(); | ||
| Crc32cLengthKnown actual = delegate.hash(byteBuffer); | ||
| if (actual != null) { | ||
| if (expected != null && !actual.eqValue(expected)) { | ||
| throw new ChecksumMismatchException(expected, actual); | ||
| } | ||
| accumulate(actual); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void validate(Crc32cValue<?> expected, ByteString byteString) | ||
| throws ChecksumMismatchException { | ||
| Crc32cLengthKnown actual = delegate.hash(byteString); | ||
| if (actual != null) { | ||
| if (expected != null && !actual.eqValue(expected)) { | ||
| throw new ChecksumMismatchException(expected, actual); | ||
| } | ||
| accumulate(actual); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void validateUnchecked(Crc32cValue<?> expected, ByteString byteString) | ||
| throws UncheckedChecksumMismatchException { | ||
| Crc32cLengthKnown actual = delegate.hash(byteString); | ||
| if (actual != null) { | ||
| if (expected != null && !actual.eqValue(expected)) { | ||
| throw new UncheckedChecksumMismatchException(expected, actual); | ||
| } | ||
| accumulate(actual); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public <C extends Crc32cValue<?>> C nullSafeConcat(C r1, Crc32cLengthKnown r2) { | ||
| return delegate.nullSafeConcat(r1, r2); | ||
| } | ||
|
|
||
| @Override | ||
| public Crc32cLengthKnown initialValue() { | ||
| return delegate.initialValue(); | ||
| } | ||
|
|
||
| // Checks if it was a full object read. | ||
| boolean qualifiesForVerification(Object metadata) { | ||
| return startOffset == 0 | ||
| && metadata != null | ||
| && metadata.hasChecksums() | ||
| && metadata.getChecksums().hasCrc32C() | ||
| && (!limit.isPresent() || limit.getAsLong() >= metadata.getSize()); | ||
| } | ||
|
|
||
| void validateCumulativeChecksum(Object metadata) | ||
| throws UncheckedCumulativeChecksumMismatchException { | ||
| if (qualifiesForVerification(metadata)) { | ||
| Crc32cValue<?> expected = Crc32cValue.of(metadata.getChecksums().getCrc32C()); | ||
| Crc32cLengthKnown actual = getCumulativeHash(); | ||
| if (!actual.eqValue(expected)) { | ||
| throw new UncheckedCumulativeChecksumMismatchException(expected, actual); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void accumulate(Crc32cLengthKnown actual) { | ||
| cumulativeHash = cumulativeHash.concat(actual); | ||
| } | ||
|
|
||
| Crc32cLengthKnown getCumulativeHash() { | ||
| return cumulativeHash; | ||
| } | ||
| } | ||
|
|
||
| final class UncheckedCumulativeChecksumMismatchException | ||
| extends com.google.api.gax.rpc.DataLossException { | ||
| private static final GrpcStatusCode STATUS_CODE = GrpcStatusCode.of(Code.DATA_LOSS); | ||
| private final Crc32cValue<?> expected; | ||
| private final Crc32cLengthKnown actual; | ||
|
|
||
| UncheckedCumulativeChecksumMismatchException(Crc32cValue<?> expected, Crc32cLengthKnown actual) { | ||
| super( | ||
| String.format( | ||
| Locale.US, | ||
| "Mismatch cumulative checksum value. Expected %s actual %s", | ||
| expected.debugString(), | ||
| actual.debugString()), | ||
| /* cause= */ null, | ||
| STATUS_CODE, | ||
| /* retryable= */ false); | ||
| this.expected = expected; | ||
| this.actual = actual; | ||
| } | ||
|
|
||
| Crc32cValue<?> getExpected() { | ||
| return expected; | ||
| } | ||
|
|
||
| Crc32cLengthKnown getActual() { | ||
| return actual; | ||
| } | ||
| } |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition
rangeSpec.begin() == 0is too broad for full object checksum validation. It will trigger for any partial read that starts at the beginning of the object (e.g., reading only the first 100 bytes). This results in unnecessary overhead and potential validation failures if the hasher attempts to compare a partial range's checksum against the full object's checksum. Consider usingrangeSpec.isAll()to ensure validation is only enabled for full object reads.