Commit Graph

97 Commits

Author SHA1 Message Date
Shouichi Kamiya
51ac8b9f6f Enable Minitest/LiteralAsActualArgument
There are assertions that expected/actual arguments are passed in the
reversed order by mistake. Enabling the LiteralAsActualArgument rule
prevents this mistake from happening.

The existing tests were auto-corrected by rubocop with a bit of
indentation adjustment.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-09-13 10:09:32 +09:00
Alexandre Ruban
9d6b68446d
ActiveStorage mirror uploads should be asynchronous
Before this commit, when a file was uploaded to a mirror service,
the file upload to each service (primary and mirrors) was happening
synchronously:

```rb
def upload(key, io, checksum: nil, **options)
  each_service.collect do |service|
    io.rewind
    service.upload key, io, checksum: checksum, **options
  end
end
```

In this commit, we change this behavior to only upload the file to the
primary service synchronously and then enqueue a job to upload the
file to the mirrors asynchronously.
2023-08-04 09:08:50 +02:00
Rafael Mendonça França
0591de55af
Remove deprecated ActiveStorage::Current#host and ActiveStorage::Current#host= methods 2023-03-03 00:38:41 +00:00
Jonathan Hefner
07cd0322bd Add ActiveStorage.deprecator
This commit adds `ActiveStorage.deprecator` and replaces all usages of
`ActiveSupport::Deprecation.warn` in `activestorage/lib` and
`activestorage/app` with `ActiveStorage.deprecator`.

Additionally, this commit adds `ActiveStorage.deprecator` to
`Rails.application.deprecators` so that it can be configured via
settings such as `config.active_support.report_deprecations`.
2022-10-31 17:04:48 -05:00
Koichi ITO
819871cc4e Enable Style/MapToHash cop
Ruby 2.6 added block argument processing to `Enumerable#to_h`.
https://bugs.ruby-lang.org/issues/15143

Rails 7 requires Ruby 2.7.0 or higher, so the new feature can use it.
`Style/MapToHash` cop will detect it. And this cop in the `Style` department,
but this seems to improve performance as follows:

```ruby
# map_to_hash.rb
require 'benchmark/ips'

ARRAY = (1..100).to_a
HASH = {foo: 1, bar: 2}

Benchmark.ips do |x|
  x.report('array.map.to_h') { ARRAY.map { |v| [v, v * 2] }.to_h }
  x.report('array.to_h')     { ARRAY.to_h { |v| [v, v * 2] } }

  x.compare!
end

Benchmark.ips do |x|
  x.report('hash.map.to_h') { HASH.map { |k, v| [k.to_s, v * 2] }.to_h }
  x.report('hash.to_h')     { HASH.to_h { |k, v| [k.to_s, v * 2] } }

  x.compare!
end
```

```console
% ruby map_to_hash.rb
Warming up --------------------------------------
      array.map.to_h     9.063k i/100ms
          array.to_h     9.609k i/100ms
Calculating -------------------------------------
      array.map.to_h     89.063k (± 3.9%) i/s -    453.150k in  5.096572s
          array.to_h     96.449k (± 1.7%) i/s -    490.059k in  5.082529s

Comparison:
          array.to_h:    96448.7 i/s
      array.map.to_h:    89063.4 i/s - 1.08x  (± 0.00) slower

Warming up --------------------------------------
       hash.map.to_h   106.284k i/100ms
           hash.to_h   149.354k i/100ms
Calculating -------------------------------------
       hash.map.to_h      1.102M (± 2.2%) i/s -      5.527M in   5.019657s
           hash.to_h      1.490M (± 0.9%) i/s -      7.468M in   5.013264s

Comparison:
           hash.to_h:  1489707.0 i/s
       hash.map.to_h:  1101561.5 i/s - 1.35x  (± 0.00) slower
```

`Style/MapToHash` cop ... https://docs.rubocop.org/rubocop/1.25/cops_style.html#stylemaptohash
2022-02-26 04:31:03 +09:00
Kasper Timm Hansen
1df9b01fe6 Pass an array through our stack, don't bother converting back & forth
Users aren't likely to pass non-array values here anyway.
2021-12-04 02:25:42 +01:00
Gannon McGibbon
79a5e0b759 Add ActiveStorage::Blob.compose 2021-11-25 19:37:57 -05:00
Joshua Sager
e106a4a1d2 Introduce custom metadata 2021-11-23 17:35:33 -05:00
Abhay Nikam
c89f152d8d Slient the warning for ActiveStorage::Current.host logged in test cases 2021-08-10 21:58:01 +05:30
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
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
Marc-Antoine Leblond
25f1eec32a
Fix test failing in GCS service 2021-06-18 11:07:20 -04: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
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
Christian Schmidt
6cd184ff0f
Use image/jpeg instead of non-standard image/jpg 2021-03-05 13:14:11 -05:00
Matt Muller
4b44d4c0e9
Fix S3 multipart uploads when threshold is larger than file 2020-12-11 16:26:40 -05:00
Eugene Kenny
e44b3419d4 Prefix keys in Active Storage service test
Otherwise this test fails when run concurrently.

https://buildkite.com/rails/rails/builds/71999#77923632-f943-4fa0-b478-0af38001c22b/1032-1098
https://buildkite.com/rails/rails/builds/71999#01fdec79-dc1f-4a6a-b730-ac0df134a2e7/970-1062
2020-10-07 21:47:59 +01: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
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
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
Peter Zhu
9940c65a78 Implement content disposition in direct upload 2019-09-05 16:40:13 -04:00
Peter Zhu
d0ed8305ae Add test for content type in direct upload to Azure 2019-08-09 14:54:09 -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
Sharang Dashputre
771973c13d url -> URL where apt except inside actionpack/ 2019-04-01 22:56:35 +05:30
Abhay Nikam
d3f9226190 Delegated path_for to primary in the MirrorService 2019-02-14 23:06:42 +05:30
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
Marcelo Perini Veloso
ce40e85194
add require 'database/setup' in activestorage/test/service/s3_service_test.rb 2018-12-01 15:47:54 -02:00
yuuji.yaginuma
6ca2f2acb2 metadata is not passed to service
Ref: 604fac6d71/activestorage/app/models/active_storage/blob.rb (L256-L264)

This fixes broken `GCSServiceTest`.
https://travis-ci.org/rails/rails/jobs/461868394#L6624-L6626

Follow up to  #34576.
2018-12-01 09:12:55 +09:00
Yannick Schutz
7bd21e8575
Add a test with extra keys to active_storage Service#upload 2018-11-30 15:54:38 +01:00
yuuji.yaginuma
ac721c8552 Fix "warning: ambiguous first argument; put parentheses or a space even after `/' operator" 2018-11-28 11:29:03 +09:00
yuuji.yaginuma
7e7a60bd3f Remove duplicated test
Since 06ab7b27ea1c1ab357085439abacdb464f6742bf,
`GCSServiceTest#test_signed_URL_response_headers` is broken.
https://travis-ci.org/rails/rails/jobs/460454477#L7084-L7087

This seems to be due to lack of `content_type` at upload.
This is solved by specifying `conten_type`.

However, since the same content is also tested with `test_upload_with_content_type`,
it will be duplicated content, so I think that can remove `test_signed_URL_response_headers`.
2018-11-28 10:01:03 +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