Commit Graph

102 Commits

Author SHA1 Message Date
Petrik
0920b02c1d Upgrade azure-storage-blob to use at least 2.0.0
azure-storage-blob 1.1.0 supports ruby 1.9.3 to 2.5:
https://github.com/Azure/azure-storage-ruby/blob/v1.1.0-common/common/README.md

azure-storage-blob 2.0.0 supports ruby 2.3 to 2.7:
https://github.com/Azure/azure-storage-ruby/blob/v2.0.0-blob/common/README.md
2021-08-23 22:03:15 +02:00
Rafael Mendonça França
18707ab17f
Standardize nodoc comments 2021-07-29 21:18:07 +00:00
Santiago Bartesaghi
e9accafc84 Fix #41388 by preserving protocol and port when generating routes 2021-07-19 09:44:55 -03:00
Adam P. Regasz-Rethy
dfd19e1a76 Add error handling for metadata server 2021-07-13 16:09:11 -04:00
Adam P. Regasz-Rethy
ae90855375 Optionally use IAM for URL signing instead of :credentials 2021-07-13 15:44:08 -04:00
Dirkjan Bussink
0523532a3c
Always use OpenSSL constants for Digest operations
As also previously discussed in
https://github.com/rails/rails/pull/40770#issuecomment-748347066, this
moves the usage of Digest constants to always use the OpenSSL version of
those Digest implementations.
2021-06-30 13:57:54 +02:00
Jose D. Gomez R
302f708729
Passing extra parameters in ActiveStorage::Blob#url to S3 Client
This allows calls of `ActiveStorage::Blob#url` to have more
interaction with the S3 Presigner, enabling, amongst other options,
custom S3 domain URL Generation.

Closes #42488
2021-06-17 13:34:27 +02:00
Marc-Antoine Leblond
a22710ed0d
Fix indentation 2021-06-16 11:53:49 -04:00
Marc-Antoine Leblond
e3fe39285f
Support default cache_control in GCS Active Storage 2021-06-16 11:53:21 -04:00
Alberto Vena
f971a3f85d
Raise a meaningful error if ActiveStorage::Current.host is blank
It's very hard to understand what happens with the following exception

  URI::InvalidURIError:
    bad URI(is not URI?): nil

that is raised when trying to generate a URL for Disk service without
setting the ActiveStorage::Current.host first.

This can happen when the ActiveStorage::SetCurrent is not included
in a controller, or when testing URL generation outside of the
controllers layer (eg. testing URL generation in a model).

Co-authored-by: elia <elia@schito.me>
Co-authored-by: filippo <dev@mailvore.com>
2021-05-10 17:27:17 +02:00
Alex Ghiculescu
ea43daa886 Add a public writer for ActiveStorage::Service::DiskService#root
Extracted from https://github.com/rails/rails/pull/41543. In that PR, I introduce docs on how to test Active Storage in a variety of scenarios. One specific case is when you're doing parallel tests. To avoid the files uploaded / deleted in your tests from clashing with each other, you should have each test run with a separate storage directory.

Currently `ActiveStorage::Service::DiskService#root` is readonly, which means that to do that you need to change it using `instance_variable_set`. This isn't very elegant. Given parallel tests and Active Storage are both turned on by default in Rails I think it makes sense to have a nice API for this.

This PR just changes the `attr_reader` to an `attr_accessor`. With this change, the [code sample here](https://github.com/rails/rails/pull/41543/files#diff-e61fcdded4b6112b041ff388609c7032d09b8212ce209d67a7debc32f230d77aR1059-R1061) works.
2021-05-06 16:23:48 -05:00
Matt Muller
4b44d4c0e9
Fix S3 multipart uploads when threshold is larger than file 2020-12-11 16:26:40 -05:00
Paul Blaze
84057dab16
Update aws-sdk-s3 dependency
whitelist_headers support added in 1.48.0:
https://github.com/aws/aws-sdk-ruby/blob/master/gems/aws-sdk-s3/CHANGELOG.md#1480-2019-08-30
2020-05-19 23:09:39 +08:00
Ryuta Kamizono
393df7425f Fix syntax error 2020-05-19 01:04:22 +09:00
Travis Pew
c0ab9a7d29
Include Content-Length in signature for ActiveStorage direct upload
[CVE-2020-8162]
2020-05-15 14:32:19 -07:00
George Claghorn
3a38c07211 Revert "Set a public ACL for files uploaded to a public GCS service"
This reverts commit 43503bdfecb86ed7386eecc54a75ccf3744b5dc2.
2020-04-28 17:16:47 -04:00
Brendan Abbott
43503bdfec
Set a public ACL for files uploaded to a public GCS service 2020-04-28 15:44:17 -04:00
Mikel Kew
cf7c27f2ff
Ensure direct uploads to a public S3 service have a public ACL 2020-04-28 15:42:17 -04:00
Yasuo Honda
dad4026f94 Unlock azure-storage-blob version to use version 2.0
`azure-storage-blob` 2.0.0 has been released.
https://rubygems.org/gems/azure-storage-blob/versions/2.0.0

According to this commit and changelog, `azure-storage-blob` 1.1.0 did not support Ruby 2.6 and higher.
As of right now, Rails master branch should support Ruby 2.5.0, 2.6.0 and 2.7.0
then it should be appropriate to unlock `azure-storage-blob` version.

252e3f06a5

This commit addresses this following error:

```ruby
% cd activestorage
% bundle exec rake test

Traceback (most recent call last):
	14: from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rake-13.0.1/lib/rake/rake_test_loader.rb:5:in `<main>'
	13: from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rake-13.0.1/lib/rake/rake_test_loader.rb:5:in `select'
	12: from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rake-13.0.1/lib/rake/rake_test_loader.rb:17:in `block in <main>'
	11: from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/zeitwerk-2.3.0/lib/zeitwerk/kernel.rb:23:in `require'
	10: from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/zeitwerk-2.3.0/lib/zeitwerk/kernel.rb:23:in `require'
	 9: from /Users/yahonda/src/github.com/rails/rails/activestorage/test/service/azure_storage_service_test.rb:7:in `<top (required)>'
	 8: from /Users/yahonda/src/github.com/rails/rails/activestorage/test/service/azure_storage_service_test.rb:8:in `<class:AzureStorageServiceTest>'
	 7: from /Users/yahonda/src/github.com/rails/rails/activestorage/lib/active_storage/service.rb:51:in `configure'
	 6: from /Users/yahonda/src/github.com/rails/rails/activestorage/lib/active_storage/service/configurator.rb:8:in `build'
	 5: from /Users/yahonda/src/github.com/rails/rails/activestorage/lib/active_storage/service/configurator.rb:17:in `build'
	 4: from /Users/yahonda/src/github.com/rails/rails/activestorage/lib/active_storage/service/configurator.rb:30:in `resolve'
	 3: from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/zeitwerk-2.3.0/lib/zeitwerk/kernel.rb:23:in `require'
	 2: from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/zeitwerk-2.3.0/lib/zeitwerk/kernel.rb:23:in `require'
	 1: from /Users/yahonda/src/github.com/rails/rails/activestorage/lib/active_storage/service/azure_storage_service.rb:3:in `<top (required)>'
/Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/rubygems_integration.rb:346:in `block (2 levels) in replace_gem': can't activate azure-storage-blob (~> 1.1), already activated azure-storage-blob-2.0.0. Make sure all dependencies are added to Gemfile. (Gem::LoadError)
	11: from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rake-13.0.1/lib/rake/rake_test_loader.rb:5:in `<main>'
	10: from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rake-13.0.1/lib/rake/rake_test_loader.rb:5:in `select'
	 9: from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rake-13.0.1/lib/rake/rake_test_loader.rb:17:in `block in <main>'
	 8: from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/zeitwerk-2.3.0/lib/zeitwerk/kernel.rb:23:in `require'
	 7: from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/zeitwerk-2.3.0/lib/zeitwerk/kernel.rb:23:in `require'
	 6: from /Users/yahonda/src/github.com/rails/rails/activestorage/test/service/azure_storage_service_test.rb:7:in `<top (required)>'
	 5: from /Users/yahonda/src/github.com/rails/rails/activestorage/test/service/azure_storage_service_test.rb:8:in `<class:AzureStorageServiceTest>'
	 4: from /Users/yahonda/src/github.com/rails/rails/activestorage/lib/active_storage/service.rb:51:in `configure'
	 3: from /Users/yahonda/src/github.com/rails/rails/activestorage/lib/active_storage/service/configurator.rb:8:in `build'
	 2: from /Users/yahonda/src/github.com/rails/rails/activestorage/lib/active_storage/service/configurator.rb:17:in `build'
	 1: from /Users/yahonda/src/github.com/rails/rails/activestorage/lib/active_storage/service/configurator.rb:29:in `resolve'
/Users/yahonda/src/github.com/rails/rails/activestorage/lib/active_storage/service/configurator.rb:33:in `rescue in resolve': Missing service adapter for "AzureStorage" (RuntimeError)
rake aborted!
```

* Update Gemfile.lock to bump azure-storage-blob version

```
% bundle update --conservative faraday azure-storage-blob google-cloud-storage`
```

- How to find gem name to update by setting `gem "azure-storage-blob", "= 2.0.0"` temporarily

```ruby
% git diff
diff --git a/Gemfile b/Gemfile
index 5fdaceab2f..6be7dccf4b 100644
--- a/Gemfile
+++ b/Gemfile
@@ -84,7 +84,7 @@ end
 group :storage do
   gem "aws-sdk-s3", require: false
   gem "google-cloud-storage", "~> 1.11", require: false
-  gem "azure-storage-blob", require: false
+  gem "azure-storage-blob", "= 2.0.0", require: false # Use 2.0.0 temporarily to find which gems need bump

   gem "image_processing", "~> 1.2"
 end
% bundle install
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies.....
Bundler could not find compatible versions for gem "faraday":
  In snapshot (Gemfile.lock):
    faraday (= 0.17.1)

  In Gemfile:
    azure-storage-blob (= 2.0.0) was resolved to 2.0.0, which depends on
      azure-storage-common (~> 2.0) was resolved to 2.0.1, which depends on
        faraday (~> 1.0)

    google-cloud-storage (~> 1.11) was resolved to 1.25.0, which depends on
      googleauth (~> 0.9) was resolved to 0.10.0, which depends on
        faraday (~> 0.12)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
% git checkout -f Gemfile
% bundle update --conservative faraday azure-storage-blob google-cloud-storage
```
2020-03-17 07:49:57 +09:00
Ryuta Kamizono
797ae91543 Google::Cloud::Storage.new takes keyword arguments 2019-12-29 03:03:51 +09:00
Peter Zhu
2e15092942 Remove query params in DiskService 2019-12-06 16:52:30 -05:00
Peter Zhu
fbb83d78c3 Use DiskController for both public and private files 2019-12-06 16:02:16 -05:00
George Claghorn
709cee9c9a
Defer Active Storage service configuration until use 2019-11-08 15:03:42 -05:00
Jason Lee
5ae3b01e97 Ensure public-read ACL for S3 service with public mode. 2019-10-17 15:24:00 +08:00
Peter Zhu
feab7031b5 Permanent URLs for public storage blobs
Services can be configured in `config/storage.yml` with a new key
`public: true | false` to indicate whether a service holds public
blobs or private blobs. Public services will always return a
permanent URL.

Deprecates `Blob#service_url` in favor of `Blob#url`.
2019-10-11 15:14:43 -04:00
Gannon McGibbon
e6487e84fc Make Active Storage services aware of configuration names 2019-10-03 13:08:30 -04:00
Akira Matsuda
d9933a7f9f Keyword arguments is not a Hash 2019-09-06 18:52:00 +09:00
Peter Zhu
9940c65a78 Implement content disposition in direct upload 2019-09-05 16:40:13 -04:00
Peter Zhu
ebedf0a6c0 Update azure-storage gem to latest version 2019-08-06 16:02:42 -04:00
Gannon McGibbon
388e011b08
Merge pull request #36792 from peterzhu2118/azure-content-disposition
Upload filename and disposition for Azure
2019-07-31 18:44:58 -04:00
Peter Zhu
24ae11daae Upload file with filename and disposition for Azure 2019-07-31 15:44:40 -04:00
Peter Zhu
e6d2e8bf9b Upload file with filename and disposition for S3 2019-07-31 09:54:51 -04:00
Eileen M. Uchitelle
dc7b650e15
Merge pull request #36715 from peterzhu2118/azure-content-type
Add content_type to upload method for Azure
2019-07-24 15:13:23 -04:00
Peter Zhu
094fa9277d Add content_type to upload in Azure 2019-07-24 14:56:20 -04:00
Peter Wagenet
b07ce56a38 Fix host for ActiveStorage DiskService
Previous behavior would only set host, which didn't work correctly
if the default_url_options contained the protocol or the port.
2019-07-09 12:54:35 -07:00
George Claghorn
d5a2f7ec14
Mirror direct uploads 2019-05-22 15:07:35 -04:00
George Claghorn
9c5135ce6a
S3: permit uploading files larger than 5 GB
Use multipart uploads for files larger than 100 MB. Dynamically calculate part size based on total object size and maximum part count.
2019-05-16 10:58:33 -04:00
George Claghorn
61b2f3d336 Halve string allocations in S3 streaming and partial downloads
See 19770d6.
2019-04-26 08:14:06 -04:00
George Claghorn
43fc7b476b GCS service: skip unnecessary bucket lookups 2019-03-14 10:56:41 -04:00
Abhay Nikam
d3f9226190 Delegated path_for to primary in the MirrorService 2019-02-14 23:06:42 +05:30
garytaylor
457dfc0621 Allow configuring the Azure Storage service with extra client options 2019-02-04 09:08:55 -05:00
Simo Leone
c5b71c9bae
include the content type when uploading to S3 2019-01-24 17:05:49 +00:00
Ryuta Kamizono
892e38c78e Enable Style/RedundantBegin cop to avoid newly adding redundant begin block
Currently we sometimes find a redundant begin block in code review
(e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205).

I'd like to enable `Style/RedundantBegin` cop to avoid that, since
rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5
(https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with
that situation than before.
2018-12-21 06:12:42 +09:00
Hiroki Sanpei
4deb88ca26 Fix ArgumentError when uploading to amazon s3 2018-11-28 17:06:29 +09:00
Rosa Gutierrez
06ab7b27ea Prevent content type and disposition bypass in storage service URLs
* Force content-type to binary on service urls for relevant content types

We have a list of content types that must be forcibly served as binary,
but in practice this only means to serve them as attachment always. We
should also set the Content-Type to the configured binary type.

As a bonus: add text/cache-manifest to the list of content types to be
served as binary by default.

* Store content-disposition and content-type in GCS

Forcing these in the service_url when serving the file works fine for S3
and Azure, since these services include params in the signature.
However, GCS specifically excludes response-content-disposition and
response-content-type from the signature, which means an attacker can
modify these and have files that should be served as text/plain attachments
served as inline HTML for example. This makes our attempt to force
specific files to be served as binary and as attachment can be easily
bypassed.

The only way this can be forced in GCS is by storing
content-disposition and content-type in the object metadata.

* Update GCS object metadata after identifying blob

In some cases we create the blob and upload the data before identifying
the content-type, which means we can't store that in GCS right when
uploading. In these, after creating the attachment, we enqueue a job to
identify the blob, and set the content-type.

In other cases, files are uploaded to the storage service via direct
upload link. We create the blob before the direct upload, which happens
independently from the blob creation itself. We then mark the blob as
identified, but we have already the content-type we need without having
put it in the service.

In these two cases, then, we need to update the metadata in the GCS
service.

* Include content-type and disposition in the verified key for disk service

This prevents an attacker from modifying these params in the service
signed URL, which is particularly important when we want to force them
to have specific values for security reasons.

* Allow only a list of specific content types to be served inline

This is different from the content types that must be served as binary
in the sense that any content type not in this list will be always
served as attachment but with its original content type. Only types in
this list are allowed to be served either inline or as attachment.

Apart from forcing this in the service URL, for GCS we need to store the
disposition in the metadata.

Fix CVE-2018-16477.
2018-11-27 15:36:27 -05:00
Cameron Bothner
aae56c3529 Handle only specifically relevant Azure HTTPErrors
The Azure gem uses `Azure::Core::Http::HTTPError` for everything:
checksum mismatch, missing object, network unavailable, and many more.
(https://www.rubydoc.info/github/yaxia/azure-storage-ruby/Azure/Core/Http/HTTPError).
Rescuing that class obscures all sorts of  configuration errors. We
should check the type of error in those rescue  blocks, and reraise when
needed.
2018-08-23 23:36:43 -04:00
George Claghorn
3868648cae Revert "Merge pull request #33667 from cbothner/azure-service-swallowing-all-errors"
This reverts commit b204d167c5cfebd59f771d406178e371811ac43a, reversing
changes made to de6a200f82a3de399fa685d583503bc88dbc5e9f.
2018-08-23 19:07:29 -04:00
Cameron Bothner
6acf2fa363 Handle only specifically relevant Azure HTTPErrors
The Azure gem uses `Azure::Core::Http::HTTPError` for everything:
checksum mismatch, missing object, network unavailable, and many more.
(https://www.rubydoc.info/github/yaxia/azure-storage-ruby/Azure/Core/Http/HTTPError).
Rescuing that class obscures all sorts of  configuration errors. We
should check the type of error in those rescue  blocks, and reraise when
needed.
2018-08-23 17:54:35 -04:00
Cameron Bothner
5cd2d07bdc Translate service-specific missing object exceptions into a generic one
`ActiveStorage::Blob#download` and `ActiveStorage::Blob#open` raise
`ActiveStorage::FileNotFoundError` when the corresponding file is missing
from the storage service. Services translate service-specific missing
object exceptions (e.g. `Google::Cloud::NotFoundError` for the GCS service
and `Errno::ENOENT` for the disk service) into
`ActiveStorage::FileNotFoundError`.
2018-08-21 15:31:14 -04:00
George Claghorn
79573b3aff Camelize instead of classifying
Avoid mangling service names that end in S:

    "GCS".classify # => "GC"
    "GCS".camelize # => "GCS"
2018-08-06 22:23:47 -04:00