Skip to content

Commit

Permalink
feat: Add validation around bytes received vs bytes expected (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-munro authored Jun 21, 2023
1 parent 4ea18d9 commit 45d142a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobSourceOption;
import com.google.cloud.storage.StorageException;
import com.google.common.io.ByteStreams;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
Expand Down Expand Up @@ -64,6 +65,20 @@ public DownloadSegment call() {
rc.limit(endPosition);
wc.position(startPosition);
bytesCopied = ByteStreams.copy(rc, wc);
long bytesExpected = endPosition - startPosition;
if (bytesCopied != bytesExpected) {
return DownloadSegment.newBuilder(originalBlob, TransferStatus.FAILED_TO_FINISH)
.setException(
new StorageException(
0,
"Unexpected end of stream, read "
+ bytesCopied
+ " expected "
+ bytesExpected
+ " from object "
+ originalBlob.getBlobId().toGsUtilUriWithGeneration()))
.build();
}
} catch (Exception e) {
if (bytesCopied == -1) {
return DownloadSegment.newBuilder(originalBlob, TransferStatus.FAILED_TO_START)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobSourceOption;
import com.google.cloud.storage.StorageException;
import com.google.common.io.ByteStreams;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
Expand Down Expand Up @@ -60,6 +61,21 @@ public DownloadResult call() {
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING);
bytesCopied = ByteStreams.copy(rc, wc);
if (originalBlob.getSize() != null) {
if (bytesCopied != originalBlob.getSize()) {
return DownloadResult.newBuilder(originalBlob, TransferStatus.FAILED_TO_FINISH)
.setException(
new StorageException(
0,
"Unexpected end of stream, read "
+ bytesCopied
+ " expected "
+ originalBlob.getSize()
+ " from object "
+ originalBlob.getBlobId().toGsUtilUriWithGeneration()))
.build();
}
}
} catch (Exception e) {
if (bytesCopied == -1) {
return DownloadResult.newBuilder(originalBlob, TransferStatus.FAILED_TO_START)
Expand Down

0 comments on commit 45d142a

Please sign in to comment.