Skip to content

Commit 45d142a

Browse files
authored
feat: Add validation around bytes received vs bytes expected (#2078)
1 parent 4ea18d9 commit 45d142a

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/transfermanager/ChunkedDownloadCallable.java

+15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.cloud.storage.BlobInfo;
2121
import com.google.cloud.storage.Storage;
2222
import com.google.cloud.storage.Storage.BlobSourceOption;
23+
import com.google.cloud.storage.StorageException;
2324
import com.google.common.io.ByteStreams;
2425
import java.nio.channels.FileChannel;
2526
import java.nio.file.Path;
@@ -64,6 +65,20 @@ public DownloadSegment call() {
6465
rc.limit(endPosition);
6566
wc.position(startPosition);
6667
bytesCopied = ByteStreams.copy(rc, wc);
68+
long bytesExpected = endPosition - startPosition;
69+
if (bytesCopied != bytesExpected) {
70+
return DownloadSegment.newBuilder(originalBlob, TransferStatus.FAILED_TO_FINISH)
71+
.setException(
72+
new StorageException(
73+
0,
74+
"Unexpected end of stream, read "
75+
+ bytesCopied
76+
+ " expected "
77+
+ bytesExpected
78+
+ " from object "
79+
+ originalBlob.getBlobId().toGsUtilUriWithGeneration()))
80+
.build();
81+
}
6782
} catch (Exception e) {
6883
if (bytesCopied == -1) {
6984
return DownloadSegment.newBuilder(originalBlob, TransferStatus.FAILED_TO_START)

google-cloud-storage/src/main/java/com/google/cloud/storage/transfermanager/DirectDownloadCallable.java

+16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.cloud.storage.BlobInfo;
2222
import com.google.cloud.storage.Storage;
2323
import com.google.cloud.storage.Storage.BlobSourceOption;
24+
import com.google.cloud.storage.StorageException;
2425
import com.google.common.io.ByteStreams;
2526
import java.nio.channels.FileChannel;
2627
import java.nio.file.Path;
@@ -60,6 +61,21 @@ public DownloadResult call() {
6061
StandardOpenOption.CREATE,
6162
StandardOpenOption.TRUNCATE_EXISTING);
6263
bytesCopied = ByteStreams.copy(rc, wc);
64+
if (originalBlob.getSize() != null) {
65+
if (bytesCopied != originalBlob.getSize()) {
66+
return DownloadResult.newBuilder(originalBlob, TransferStatus.FAILED_TO_FINISH)
67+
.setException(
68+
new StorageException(
69+
0,
70+
"Unexpected end of stream, read "
71+
+ bytesCopied
72+
+ " expected "
73+
+ originalBlob.getSize()
74+
+ " from object "
75+
+ originalBlob.getBlobId().toGsUtilUriWithGeneration()))
76+
.build();
77+
}
78+
}
6379
} catch (Exception e) {
6480
if (bytesCopied == -1) {
6581
return DownloadResult.newBuilder(originalBlob, TransferStatus.FAILED_TO_START)

0 commit comments

Comments
 (0)