diff options
author | Rosa Gutierrez <rosa.ge@gmail.com> | 2018-01-04 19:35:54 +0100 |
---|---|---|
committer | Rosa Gutierrez <rosa.ge@gmail.com> | 2018-01-05 16:32:32 +0100 |
commit | d40284b1a44773b03d78ca67a888b94fd330d1b1 (patch) | |
tree | ac9cada273b492f231bf1b48d215e36ff78fde05 /activestorage/app/models | |
parent | 5a5014688873f1d6e1b66075eea8a4356b5a4d07 (diff) | |
download | rails-d40284b1a44773b03d78ca67a888b94fd330d1b1.tar.gz rails-d40284b1a44773b03d78ca67a888b94fd330d1b1.tar.bz2 rails-d40284b1a44773b03d78ca67a888b94fd330d1b1.zip |
Force content disposition to attachment for specific content types
In this way we avoid HTML, XML, SVG and other files that can be rendered
by the browser to be served inline by default. Depending on the origin
from where these files are served, this might lead to XSS
vulnerabilities, and in the best case, to more realistic phishing
attacks and open redirects.
We force it rather than falling back to it when other disposition is not
provided. Otherwise it would be possible for someone to force inline
just by passing `disposition=inline` in the URL.
The list of content types to be served as attachments is configurable.
Diffstat (limited to 'activestorage/app/models')
-rw-r--r-- | activestorage/app/models/active_storage/blob.rb | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/activestorage/app/models/active_storage/blob.rb b/activestorage/app/models/active_storage/blob.rb index 3b48ee72af..8fb53fa787 100644 --- a/activestorage/app/models/active_storage/blob.rb +++ b/activestorage/app/models/active_storage/blob.rb @@ -201,8 +201,8 @@ class ActiveStorage::Blob < ActiveRecord::Base # with users. Instead, the +service_url+ should only be exposed as a redirect from a stable, possibly authenticated URL. # Hiding the +service_url+ behind a redirect also gives you the power to change services without updating all URLs. And # it allows permanent URLs that redirect to the +service_url+ to be cached in the view. - def service_url(expires_in: service.url_expires_in, disposition: "inline") - service.url key, expires_in: expires_in, disposition: disposition, filename: filename, content_type: content_type + def service_url(expires_in: service.url_expires_in, disposition: :inline) + service.url key, expires_in: expires_in, disposition: forcibly_serve_as_binary? ? :attachment : disposition, filename: filename, content_type: content_type end # Returns a URL that can be used to directly upload a file for this blob on the service. This URL is intended to be @@ -325,4 +325,9 @@ class ActiveStorage::Blob < ActiveRecord::Base def analyzer_class ActiveStorage.analyzers.detect { |klass| klass.accept?(self) } || ActiveStorage::Analyzer::NullAnalyzer end + + + def forcibly_serve_as_binary? + ActiveStorage.content_types_to_serve_as_binary.include?(content_type) + end end |