Merge pull request #42227 from ghiculescu/as-invalid-defaults

Active Storage: deprecate invalid default content types
This commit is contained in:
Rafael França 2021-09-22 18:04:03 -04:00 committed by GitHub
commit 6aeeb0c6ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 113 additions and 5 deletions

@ -1,3 +1,14 @@
* Invalid default content types are deprecated
Blobs created with content_type `image/jpg`, `image/pjpeg`, `image/bmp`, `text/javascript` will now produce
a deprecation warning, since these are not valid content types.
These content types will be removed from the defaults in Rails 7.1.
You can set `config.active_storage.silence_invalid_content_types_warning = true` to dismiss the warning.
*Alex Ghiculescu*
## Rails 7.0.0.alpha2 (September 15, 2021) ##
* No changes.

@ -308,6 +308,31 @@ def service
services.fetch(service_name)
end
def content_type=(value)
unless ActiveStorage.silence_invalid_content_types_warning
if INVALID_VARIABLE_CONTENT_TYPES_DEPRECATED_IN_RAILS_7.include?(value)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
#{value} is not a valid content type, it should not be used when creating a blob, and support for it will be removed in Rails 7.1.
If you want to keep supporting this content type past Rails 7.1, add it to `config.active_storage.variable_content_types`.
Dismiss this warning by setting `config.active_storage.silence_invalid_content_types_warning = true`.
MSG
end
if INVALID_VARIABLE_CONTENT_TYPES_TO_SERVE_AS_BINARY_DEPRECATED_IN_RAILS_7.include?(value)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
#{value} is not a valid content type, it should not be used when creating a blob, and support for it will be removed in Rails 7.1.
If you want to keep supporting this content type past Rails 7.1, add it to `config.active_storage.content_types_to_serve_as_binary`.
Dismiss this warning by setting `config.active_storage.silence_invalid_content_types_warning = true`.
MSG
end
end
super
end
INVALID_VARIABLE_CONTENT_TYPES_DEPRECATED_IN_RAILS_7 = ["image/jpg", "image/pjpeg", "image/bmp"]
INVALID_VARIABLE_CONTENT_TYPES_TO_SERVE_AS_BINARY_DEPRECATED_IN_RAILS_7 = ["text/javascript"]
private
def compute_checksum_in_chunks(io)
OpenSSL::Digest::MD5.new.tap do |checksum|

@ -71,6 +71,8 @@ module ActiveStorage
mattr_accessor :video_preview_arguments, default: "-y -vframes 1 -f image2"
mattr_accessor :silence_invalid_content_types_warning, default: false
module Transformers
extend ActiveSupport::Autoload

@ -101,6 +101,8 @@ class Engine < Rails::Engine # :nodoc:
ActiveStorage.binary_content_type = app.config.active_storage.binary_content_type || "application/octet-stream"
ActiveStorage.video_preview_arguments = app.config.active_storage.video_preview_arguments || "-y -vframes 1 -f image2"
ActiveStorage.silence_invalid_content_types_warning = app.config.active_storage.silence_invalid_content_types_warning || false
ActiveStorage.replace_on_assign_to_many = app.config.active_storage.replace_on_assign_to_many || false
ActiveStorage.track_variants = app.config.active_storage.track_variants || false
end

@ -0,0 +1,34 @@
# frozen_string_literal: true
require "test_helper"
require "database/setup"
class ActiveStorage::EngineTest < ActiveSupport::TestCase
test "all default content types are recognized by mini_mime" do
exceptions = ActiveStorage::Blob::INVALID_VARIABLE_CONTENT_TYPES_DEPRECATED_IN_RAILS_7 + ActiveStorage::Blob::INVALID_VARIABLE_CONTENT_TYPES_TO_SERVE_AS_BINARY_DEPRECATED_IN_RAILS_7
ActiveStorage.variable_content_types.each do |content_type|
next if exceptions.include?(content_type) # remove this line in Rails 7.1
assert_equal content_type, MiniMime.lookup_by_content_type(content_type).content_type
end
ActiveStorage.web_image_content_types.each do |content_type|
next if exceptions.include?(content_type) # remove this line in Rails 7.1
assert_equal content_type, MiniMime.lookup_by_content_type(content_type).content_type
end
ActiveStorage.content_types_to_serve_as_binary.each do |content_type|
next if exceptions.include?(content_type) # remove this line in Rails 7.1
assert_equal content_type, MiniMime.lookup_by_content_type(content_type).content_type
end
ActiveStorage.content_types_allowed_inline.each do |content_type|
next if exceptions.include?(content_type) # remove this line in Rails 7.1
assert_equal content_type, MiniMime.lookup_by_content_type(content_type).content_type
end
end
end

@ -293,6 +293,32 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
end
end
test "warning if blob is created with invalid mime type" do
assert_deprecated do
create_blob(filename: "funky.jpg", content_type: "image/jpg")
end
assert_not_deprecated do
create_blob(filename: "funky.jpg", content_type: "image/jpeg")
end
end
test "warning if blob is created with invalid mime type can be disabled" do
warning_was = ActiveStorage.silence_invalid_content_types_warning
ActiveStorage.silence_invalid_content_types_warning = true
assert_not_deprecated do
create_blob(filename: "funky.jpg", content_type: "image/jpg")
end
assert_not_deprecated do
create_blob(filename: "funky.jpg", content_type: "image/jpeg")
end
ensure
ActiveStorage.silence_invalid_content_types_warning = warning_was
end
private
def expected_url_for(blob, disposition: :attachment, filename: nil, content_type: nil, service_name: :local)
filename ||= blob.filename

@ -104,7 +104,7 @@ class ActiveStorage::VariantTest < ActiveSupport::TestCase
end
test "resized variation of BMP blob" do
blob = create_file_blob(filename: "colors.bmp", content_type: "image/bmp")
blob = create_file_blob(filename: "colors.bmp", content_type: "image/x-bmp")
variant = blob.variant(resize_to_limit: [15, 15]).processed
assert_match(/colors\.png/, variant.url)

@ -1545,7 +1545,7 @@ can transform through ImageMagick.
By default, this is defined as:
```ruby
config.active_storage.variable_content_types = %w(image/png image/gif image/jpg image/jpeg image/pjpeg image/tiff image/bmp image/vnd.adobe.photoshop image/vnd.microsoft.icon image/webp image/avif image/heic image/heif)
config.active_storage.variable_content_types = %w(image/png image/gif image/jpeg image/tiff image/vnd.adobe.photoshop image/vnd.microsoft.icon image/webp image/avif image/heic image/heif)
```
#### `config.active_storage.web_image_content_types`
@ -1557,7 +1557,7 @@ If you want to use `WebP` or `AVIF` variants in your application you can add
By default, this is defined as:
```ruby
config.active_storage.web_image_content_types = %w(image/png image/jpeg image/jpg image/gif)
config.active_storage.web_image_content_types = %w(image/png image/jpeg image/gif)
```
#### `config.active_storage.content_types_to_serve_as_binary`
@ -1566,7 +1566,7 @@ Accepts an array of strings indicating the content types that Active Storage wil
By default, this is defined as:
```ruby
config.active_storage.content_types_to_serve_as_binary = %w(text/html text/javascript image/svg+xml application/postscript application/x-shockwave-flash text/xml application/xml application/xhtml+xml application/mathml+xml text/cache-manifest)
config.active_storage.content_types_to_serve_as_binary = %w(text/html image/svg+xml application/postscript application/x-shockwave-flash text/xml application/xml application/xhtml+xml application/mathml+xml text/cache-manifest)
```
#### `config.active_storage.content_types_allowed_inline`
@ -1575,7 +1575,15 @@ Accepts an array of strings indicating the content types that Active Storage all
By default, this is defined as:
```ruby
config.active_storage.content_types_allowed_inline` = %w(image/png image/gif image/jpg image/jpeg image/tiff image/bmp image/vnd.adobe.photoshop image/vnd.microsoft.icon application/pdf)
config.active_storage.content_types_allowed_inline` = %w(image/png image/gif image/jpeg image/tiff image/vnd.adobe.photoshop image/vnd.microsoft.icon application/pdf)
```
#### `config.active_storage.silence_invalid_content_types_warning`
Since Rails 7, Active Storage will warn if you use an invalid content type that was incorrectly supported in Rails 6. You can use this config to turn the warning off.
```ruby
config.active_storage.silence_invalid_content_types_warning = false
```
#### `config.active_storage.queues.analysis`