OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
make external avatar URLs valid for 24h
Markus Kahlbfe1e2003f40
diff --git a/app/models/attachment.rb b/app/models/attachment.rbindex e658b76c26f..e714b58789b 100644--- a/app/models/attachment.rb+++ b/app/models/attachment.rb@@ -59,8 +59,8 @@ class Attachment < ActiveRecord::Base### Returns an URL if the attachment is stored in an external (fog) attachment storage# or nil otherwise.- def external_url- url = URI.parse file.download_url(content_disposition: content_disposition) # returns a path if local+ def external_url(expires_in: nil)+ url = URI.parse file.download_url(content_disposition: content_disposition, expires_in: expires_in) # returns a path if localurl if url.hostrescue URI::InvalidURIErrordiff --git a/app/uploaders/fog_file_uploader.rb b/app/uploaders/fog_file_uploader.rbindex b4727341895..9dc8113617a 100644--- a/app/uploaders/fog_file_uploader.rb+++ b/app/uploaders/fog_file_uploader.rb@@ -27,6 +27,7 @@#++require 'carrierwave/storage/fog'+require 'open_project/patches/carrierwave'class FogFileUploader < CarrierWave::Uploader::Baseinclude FileUploader@@ -57,18 +58,20 @@ class FogFileUploader < CarrierWave::Uploader::Basesuperend+ ##+ # Generates a download URL for this file.+ #+ # @param options [Hash] Options hash.+ # @option options [String] :content_disposition Pass this content disposition to S3 so that it serves the file with it.+ # @option options [DateTime] :expires_at Date at which the link should expire (default: now + 5 minutes)+ # @option options [ActiveSupport::Duration] :expires_in Duration in which the link should expire.+ #+ # @return [String] The URL to download the file from.def download_url(options = {})url_options = {}- if options[:content_disposition].present?- url_options[:query] = {- # Passing this option to S3 will make it serve the file with the- # respective content disposition. Without it no content disposition- # header is sent. This only works for S3 but we don't support- # anything else anyway (see carrierwave.rb).- "response-content-disposition" => options[:content_disposition]- }- end+ set_content_disposition! url_options, options: options+ set_expires_at! url_options, options: optionsremote_file.url url_optionsend@@ -85,4 +88,28 @@ class FogFileUploader < CarrierWave::Uploader::Baserescue Excon::Errors::Forbiddenfalseend++ private++ def set_content_disposition!(url_options, options:)+ if options[:content_disposition].present?+ url_options[:query] = {+ # Passing this option to S3 will make it serve the file with the+ # respective content disposition. Without it no content disposition+ # header is sent. This only works for S3 but we don't support+ # anything else anyway (see carrierwave.rb).+ "response-content-disposition" => options[:content_disposition]+ }+ end+ end++ def set_expires_at!(url_options, options:)+ if options[:expires_in].present?+ url_options[:expire_at] = ::Fog::Time.now + options[:expires_in]+ end++ if options[:expires_at].present?+ url_options[:expire_at] = ::Fog::Time.at options[:expires_at] - ::Fog::Time.offset+ end+ endenddiff --git a/lib/api/helpers/attachment_renderer.rb b/lib/api/helpers/attachment_renderer.rbindex 49fbe4d4ff6..5cde04ca8cf 100644--- a/lib/api/helpers/attachment_renderer.rb+++ b/lib/api/helpers/attachment_renderer.rb@@ -38,9 +38,13 @@ module API# to the external storage,## or by directly rendering the file- def respond_with_attachment(attachment)+ #+ # @param attachment [Attachment] Attachment to be responded with.+ # @param external_link_expires_in [ActiveSupport::Duration] Time after which link expires. Default is 5 minutes.+ # Only applicable in case of external storage.+ def respond_with_attachment(attachment, external_link_expires_in: nil)if attachment.external_storage?- redirect attachment.external_url.to_s+ redirect attachment.external_url(expires_in: external_link_expires_in).to_selsecontent_type attachment.content_typeheader['Content-Disposition'] = "#{attachment.content_disposition}; filename=#{attachment.filename}"@@ -48,6 +52,20 @@ module APIattachment.diskfile.readendend++ def external_avatar_link_expires_in+ seconds = external_avatar_link_expiry_seconds++ if seconds == 0+ nil+ else+ seconds.seconds+ end+ end++ def external_avatar_link_expiry_seconds+ @external_avatar_link_expiry_seconds ||= OpenProject::Configuration.external_avatar_link_expiry_seconds.to_i+ endendendenddiff --git a/lib/open_project/configuration.rb b/lib/open_project/configuration.rbindex 7148a841b1e..36594d7ad57 100644--- a/lib/open_project/configuration.rb+++ b/lib/open_project/configuration.rb@@ -133,6 +133,8 @@ module OpenProject# Allow in-context translations to be loaded with CSP'crowdin_in_context_translations' => true,+ 'external_avatar_link_expiry_seconds' => 24.hours.to_i,+# Default gravatar image, set to something other than 404# to ensure a default is returned'gravatar_fallback_image' => '404',diff --git a/lib/open_project/patches/carrierwave.rb b/lib/open_project/patches/carrierwave.rbnew file mode 100644index 00000000000..3b95ce54f00--- /dev/null+++ b/lib/open_project/patches/carrierwave.rb@@ -0,0 +1,33 @@+require 'carrierwave'++##+# Code copied straight from the CarrierWave source.+# All we did is add `options[:expire_at]`.+#+# @todo Upgrade to CarrierWave 2.0.2 to make this patch obsolete.+class CarrierWave::Storage::Fog::File+ def authenticated_url(options = {})+ if ['AWS', 'Google', 'Rackspace', 'OpenStack'].include?(@uploader.fog_credentials[:provider])+ # avoid a get by using local references+ local_directory = connection.directories.new(:key => @uploader.fog_directory)+ local_file = local_directory.files.new(:key => path)+ expire_at = options[:expire_at] || ::Fog::Time.now + @uploader.fog_authenticated_url_expiration+ case @uploader.fog_credentials[:provider]+ when 'AWS', 'Google'+ # Older versions of fog-google do not support options as a parameter+ if url_options_supported?(local_file)+ local_file.url(expire_at, options)+ else+ warn "Options hash not supported in #{local_file.class}. You may need to upgrade your Fog provider."+ local_file.url(expire_at)+ end+ when 'Rackspace'+ connection.get_object_https_url(@uploader.fog_directory, path, expire_at, options)+ when 'OpenStack'+ connection.get_object_https_url(@uploader.fog_directory, path, expire_at)+ else+ local_file.url(expire_at)+ end+ end+ end+enddiff --git a/modules/avatars/lib/api/v3/users/user_avatar_api.rb b/modules/avatars/lib/api/v3/users/user_avatar_api.rbindex 3ac4e95734b..3ead718a53c 100644--- a/modules/avatars/lib/api/v3/users/user_avatar_api.rb+++ b/modules/avatars/lib/api/v3/users/user_avatar_api.rb@@ -36,7 +36,7 @@ module APIget '/avatar' doif local_avatar = local_avatar?(@user)- respond_with_attachment(local_avatar)+ respond_with_attachment(local_avatar, external_link_expires_in: external_avatar_link_expires_in)elsif avatar_manager.gravatar_enabled?redirect build_gravatar_image_url(@user)elsediff --git a/modules/avatars/spec/requests/user_avatar_api_spec.rb b/modules/avatars/spec/requests/user_avatar_api_spec.rbindex 27c3079b40e..d74dda1f341 100644--- a/modules/avatars/spec/requests/user_avatar_api_spec.rb+++ b/modules/avatars/spec/requests/user_avatar_api_spec.rb@@ -37,6 +37,8 @@ describe 'API v3 User avatar resource', type: :request, content_type: :json dosubject(:response) { last_response }+ let(:setup) { nil }+before dologin_as userend@@ -47,6 +49,8 @@ describe 'API v3 User avatar resource', type: :request, content_type: :json do.to receive(:plugin_openproject_avatars).and_return(enable_gravatars: gravatars, enable_local_avatars: local_avatars)+ setup+get api_v3_paths.user(user.id) + "/avatar"end@@ -82,6 +86,25 @@ describe 'API v3 User avatar resource', type: :request, content_type: :json doit 'serves the attachment file' doexpect(response.status).to eq 200end++ context 'with external file storage (S3)' do+ let(:setup) do+ allow_any_instance_of(Attachment).to receive(:external_storage?).and_return true++ expect_any_instance_of(Attachment)+ .to receive(:external_url)+ .with(expires_in: 86400)+ .and_return("external URL")+ end++ # we test that Attachment#external_url works in `attachments_spec.rb`+ # so here we just make sue it's called accordingly when the external+ # storage is configured+ it 'redirects to temporary external URL' do+ expect(response.status).to eq 302+ expect(response.location).to eq "external URL"+ end+ endendendenddiff --git a/spec/models/attachment_spec.rb b/spec/models/attachment_spec.rbindex a7e85be1ae5..7bdf27a102e 100644--- a/spec/models/attachment_spec.rb+++ b/spec/models/attachment_spec.rb@@ -268,6 +268,24 @@ describe Attachment, type: :model doCarrierWave::Configuration.configure_fog! credentials: {}, directory: "my-bucket", public: falseend+ shared_examples "it has a temporary download link" do+ let(:url_options) { {} }+ let(:query) { attachment.external_url(url_options).to_s.split("?").last }++ it "should have a default expiry time" do+ expect(query).to include "X-Amz-Expires="+ expect(query).not_to include "X-Amz-Expires=3600"+ end++ context "with a custom expiry time" do+ let(:url_options) { { expires_in: 1.hour } }++ it "should use that time" do+ expect(query).to include "X-Amz-Expires=3600"+ end+ end+ end+describe "for an image file" dobefore { image_attachment.save! }@@ -275,6 +293,11 @@ describe Attachment, type: :model doexpect(image_attachment.content_disposition).to eq "inline"expect(image_attachment.external_url.to_s).to include "response-content-disposition=inline"end++ # this is independent from the type of file uploaded so we just test this for the first one+ it_behaves_like "it has a temporary download link" do+ let(:attachment) { image_attachment }+ endenddescribe "for a text file" do