Skip to content

Commit

Permalink
Include Content-Length in signature for ActiveStorage direct upload
Browse files Browse the repository at this point in the history
  • Loading branch information
travisp authored and tenderlove committed May 14, 2020
1 parent b738f19 commit 17507e8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion activestorage/lib/active_storage/service/s3_service.rb
Expand Up @@ -81,7 +81,8 @@ def url(key, expires_in:, filename:, disposition:, content_type:)
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
instrument :url, key: key do |payload|
generated_url = object_for(key).presigned_url :put, expires_in: expires_in.to_i,
content_type: content_type, content_length: content_length, content_md5: checksum
content_type: content_type, content_length: content_length, content_md5: checksum,
whitelist_headers: ['content-length']

payload[:url] = generated_url

Expand Down
23 changes: 23 additions & 0 deletions activestorage/test/service/s3_service_test.rb
Expand Up @@ -30,6 +30,29 @@ class ActiveStorage::Service::S3ServiceTest < ActiveSupport::TestCase
@service.delete key
end

test "directly uploading file larger than the provided content-length does not work" do
key = SecureRandom.base58(24)
data = "Some text that is longer than the specified content length"
checksum = Digest::MD5.base64digest(data)
url = @service.url_for_direct_upload(key, expires_in: 5.minutes, content_type: "text/plain", content_length: data.size - 1, checksum: checksum)

uri = URI.parse url
request = Net::HTTP::Put.new uri.request_uri
request.body = data
request.add_field "Content-Type", "text/plain"
request.add_field "Content-MD5", checksum
upload_result = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
http.request request
end

assert_equal "403", upload_result.code
assert_raises ActiveStorage::FileNotFoundError do
@service.download(key)
end
ensure
@service.delete key
end

test "upload a zero byte file" do
blob = directly_upload_file_blob filename: "empty_file.txt", content_type: nil
user = User.create! name: "DHH", avatar: blob
Expand Down

0 comments on commit 17507e8

Please sign in to comment.