1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activestorage/test/models/variant_test.rb
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

177 lines
5.5 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
require "database/setup"
class ActiveStorage::VariantTest < ActiveSupport::TestCase
test "resized variation of JPEG blob" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(resize: "100x100").processed
assert_match(/racecar\.jpg/, variant.service_url)
image = read_image(variant)
assert_equal 100, image.width
assert_equal 67, image.height
end
test "resized and monochrome variation of JPEG blob" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(resize: "100x100", monochrome: true).processed
assert_match(/racecar\.jpg/, variant.service_url)
image = read_image(variant)
assert_equal 100, image.width
assert_equal 67, image.height
assert_match(/Gray/, image.colorspace)
end
test "monochrome with default variant_processor" do
begin
ActiveStorage.variant_processor = nil
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(monochrome: true).processed
image = read_image(variant)
assert_match(/Gray/, image.colorspace)
ensure
ActiveStorage.variant_processor = :mini_magick
end
end
test "disabled variation of JPEG blob" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(resize: "100x100", monochrome: false).processed
assert_match(/racecar\.jpg/, variant.service_url)
image = read_image(variant)
assert_equal 100, image.width
assert_equal 67, image.height
assert_match(/RGB/, image.colorspace)
end
test "disabled variation of JPEG blob with :combine_options" do
blob = create_file_blob(filename: "racecar.jpg")
variant = ActiveSupport::Deprecation.silence do
blob.variant(combine_options: {
resize: "100x100",
monochrome: false
}).processed
end
assert_match(/racecar\.jpg/, variant.service_url)
image = read_image(variant)
assert_equal 100, image.width
assert_equal 67, image.height
assert_match(/RGB/, image.colorspace)
end
test "disabled variation using :combine_options" do
begin
ActiveStorage.variant_processor = nil
blob = create_file_blob(filename: "racecar.jpg")
variant = ActiveSupport::Deprecation.silence do
blob.variant(combine_options: {
crop: "100x100+0+0",
monochrome: false
}).processed
end
assert_match(/racecar\.jpg/, variant.service_url)
image = read_image(variant)
assert_equal 100, image.width
assert_equal 100, image.height
assert_match(/RGB/, image.colorspace)
ensure
ActiveStorage.variant_processor = :mini_magick
end
end
test "center-weighted crop of JPEG blob using :combine_options" do
begin
ActiveStorage.variant_processor = nil
blob = create_file_blob(filename: "racecar.jpg")
variant = ActiveSupport::Deprecation.silence do
blob.variant(combine_options: {
gravity: "center",
resize: "100x100^",
crop: "100x100+0+0",
}).processed
end
assert_match(/racecar\.jpg/, variant.service_url)
image = read_image(variant)
assert_equal 100, image.width
assert_equal 100, image.height
ensure
ActiveStorage.variant_processor = :mini_magick
end
end
test "center-weighted crop of JPEG blob using :resize_to_fill" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(resize_to_fill: [100, 100]).processed
assert_match(/racecar\.jpg/, variant.service_url)
image = read_image(variant)
assert_equal 100, image.width
assert_equal 100, image.height
end
test "resized variation of PSD blob" do
blob = create_file_blob(filename: "icon.psd", content_type: "image/vnd.adobe.photoshop")
variant = blob.variant(resize: "20x20").processed
assert_match(/icon\.png/, variant.service_url)
image = read_image(variant)
assert_equal "PNG", image.type
assert_equal 20, image.width
assert_equal 20, image.height
end
test "resized variation of ICO blob" do
blob = create_file_blob(filename: "favicon.ico", content_type: "image/vnd.microsoft.icon")
variant = blob.variant(resize: "20x20").processed
assert_match(/icon\.png/, variant.service_url)
image = read_image(variant)
assert_equal "PNG", image.type
assert_equal 20, image.width
assert_equal 20, image.height
end
test "optimized variation of GIF blob" do
blob = create_file_blob(filename: "image.gif", content_type: "image/gif")
assert_nothing_raised do
blob.variant(layers: "Optimize").processed
end
end
test "variation of invariable blob" do
assert_raises ActiveStorage::InvariableError do
create_file_blob(filename: "report.pdf", content_type: "application/pdf").variant(resize: "100x100")
end
end
test "service_url doesn't grow in length despite long variant options" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(font: "a" * 10_000).processed
assert_operator variant.service_url.length, :<, 726
end
test "works for vips processor" do
begin
ActiveStorage.variant_processor = :vips
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(thumbnail_image: 100).processed
image = read_image(variant)
assert_equal 100, image.width
assert_equal 67, image.height
rescue LoadError
# libvips not installed
ensure
ActiveStorage.variant_processor = :mini_magick
end
end
end