aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib
diff options
context:
space:
mode:
authorGeorge Claghorn <george@basecamp.com>2018-05-28 16:28:46 -0400
committerGeorge Claghorn <george@basecamp.com>2018-05-28 16:28:46 -0400
commit1bdaccc0b8cf8aac0dc833d6db14b9a902593ff5 (patch)
tree47c7fe84ded16a785feb86f42faa225175d4a8c4 /activestorage/lib
parent34cc301f03aea2e579d6687a9ea9782afc1089a0 (diff)
downloadrails-1bdaccc0b8cf8aac0dc833d6db14b9a902593ff5.tar.gz
rails-1bdaccc0b8cf8aac0dc833d6db14b9a902593ff5.tar.bz2
rails-1bdaccc0b8cf8aac0dc833d6db14b9a902593ff5.zip
Verify integrity after chunked download
Diffstat (limited to 'activestorage/lib')
-rw-r--r--activestorage/lib/active_storage/downloader.rb7
-rw-r--r--activestorage/lib/active_storage/errors.rb4
-rw-r--r--activestorage/lib/active_storage/service.rb2
3 files changed, 11 insertions, 2 deletions
diff --git a/activestorage/lib/active_storage/downloader.rb b/activestorage/lib/active_storage/downloader.rb
index 0e7039e104..5953fd08fb 100644
--- a/activestorage/lib/active_storage/downloader.rb
+++ b/activestorage/lib/active_storage/downloader.rb
@@ -10,6 +10,7 @@ module ActiveStorage
def download_blob_to_tempfile
open_tempfile do |file|
download_blob_to file
+ verify_integrity_of file
yield file
end
end
@@ -34,6 +35,12 @@ module ActiveStorage
file.rewind
end
+ def verify_integrity_of(file)
+ unless Digest::MD5.file(file).base64digest == checksum
+ raise ActiveStorage::IntegrityError
+ end
+ end
+
def tempfile_extension_with_delimiter
blob.filename.extension_with_delimiter
end
diff --git a/activestorage/lib/active_storage/errors.rb b/activestorage/lib/active_storage/errors.rb
index f099b13f5b..bedcd080c4 100644
--- a/activestorage/lib/active_storage/errors.rb
+++ b/activestorage/lib/active_storage/errors.rb
@@ -4,4 +4,8 @@ module ActiveStorage
class InvariableError < StandardError; end
class UnpreviewableError < StandardError; end
class UnrepresentableError < StandardError; end
+
+ # Raised when uploaded or downloaded data does not match a precomputed checksum.
+ # Indicates that a network error or a software bug caused data corruption.
+ class IntegrityError < StandardError; end
end
diff --git a/activestorage/lib/active_storage/service.rb b/activestorage/lib/active_storage/service.rb
index 949969fc95..da1af4f745 100644
--- a/activestorage/lib/active_storage/service.rb
+++ b/activestorage/lib/active_storage/service.rb
@@ -3,8 +3,6 @@
require "active_storage/log_subscriber"
module ActiveStorage
- class IntegrityError < StandardError; end
-
# Abstract class serving as an interface for concrete services.
#
# The available services are: