A platform for community discussion. Free, open, simple.
FIX: Enforce content disposition on S3 presigned URLs (#38681)
Alan Guo Xiang Tan7d4dc26291a1
diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rbindex 8db7475f9c1..4b5a7ab728f 100644--- a/lib/file_store/s3_store.rb+++ b/lib/file_store/s3_store.rb@@ -245,12 +245,17 @@ module FileStoredef signed_url_for_path(path,expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds,- force_download: false+ force_download: false,+ filename: nil)key = path.sub(absolute_base_url + "/", "")- filename = force_download ? File.basename(path) : false- presigned_get_url(key, expires_in:, force_download:, filename:)+ presigned_get_url(+ key,+ expires_in:,+ force_download:,+ filename: filename || File.basename(path),+ )enddef signed_request_for_temporary_upload(@@ -439,9 +444,11 @@ module FileStore)opts = { expires_in: expires_in }- if force_download && filename+ if filename+ disposition =+ (force_download || !FileHelper.is_inline_safe?(filename)) ? "attachment" : "inline"opts[:response_content_disposition] = ActionDispatch::Http::ContentDisposition.format(- disposition: "attachment",+ disposition:,filename: filename,)enddiff --git a/spec/lib/file_store/s3_store_spec.rb b/spec/lib/file_store/s3_store_spec.rbindex de3193de000..5e0ee1622ef 100644--- a/spec/lib/file_store/s3_store_spec.rb+++ b/spec/lib/file_store/s3_store_spec.rb@@ -907,7 +907,7 @@ RSpec.describe FileStore::S3Store doenddescribe ".url_for" do- it "returns signed URL with content disposition when requesting to download image" do+ it "returns signed URL with attachment content disposition when force_download is true" dos3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_onces3_bucket.expects(:object)@@ -923,17 +923,106 @@ RSpec.describe FileStore::S3Store doexpect(store.url_for(upload, force_download: true)).not_to eq(upload.url)end++ it "returns signed URL with inline content disposition for secure image" do+ upload.update!(secure: true)+ s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once+ s3_bucket+ .expects(:object)+ .with(regexp_matches(%r{original/\d+X.*/#{upload.sha1}\.png}))+ .returns(s3_object)+ opts = {+ expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds,+ response_content_disposition:+ ActionDispatch::Http::ContentDisposition.format(+ disposition: "inline",+ filename: upload.original_filename,+ ),+ }+ s3_object.expects(:presigned_url).with(:get, opts)+ expect(store.url_for(upload)).not_to eq(upload.url)+ end++ it "returns signed URL with attachment content disposition for non-inline-safe secure upload" do+ SiteSetting.authorized_extensions = "jpg|jpeg|png|gif|html"+ upload = Fabricate(:upload, original_filename: "file.html", extension: "html", secure: true)+ s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once+ s3_bucket+ .expects(:object)+ .with(regexp_matches(%r{original/\d+X.*/#{upload.sha1}\.html}))+ .returns(s3_object)+ opts = {+ expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds,+ response_content_disposition:+ ActionDispatch::Http::ContentDisposition.format(+ disposition: "attachment",+ filename: "file.html",+ ),+ }+ s3_object.expects(:presigned_url).with(:get, opts)+ expect(store.url_for(upload)).not_to eq(upload.url)+ endenddescribe ".signed_url_for_path" do- it "returns signed URL for a given path" do+ it "returns signed URL with inline content disposition for a given path" dos3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_onces3_bucket.expects(:object).with("special/optimized/file.png").returns(s3_object)- opts = { expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds }+ opts = {+ expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds,+ response_content_disposition:+ ActionDispatch::Http::ContentDisposition.format(+ disposition: "inline",+ filename: "file.png",+ ),+ }+ s3_object+ .expects(:presigned_url)+ .with(:get, opts)+ .returns("https://s3.example.com/special/optimized/file.png?signed=true")+ expect(store.signed_url_for_path("special/optimized/file.png")).to eq(+ "https://s3.example.com/special/optimized/file.png?signed=true",+ )+ end- s3_object.expects(:presigned_url).with(:get, opts)+ it "returns signed URL with attachment content disposition for non-inline-safe path" do+ s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once+ s3_bucket.expects(:object).with("special/optimized/file.html").returns(s3_object)+ opts = {+ expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds,+ response_content_disposition:+ ActionDispatch::Http::ContentDisposition.format(+ disposition: "attachment",+ filename: "file.html",+ ),+ }+ s3_object+ .expects(:presigned_url)+ .with(:get, opts)+ .returns("https://s3.example.com/special/optimized/file.html?signed=true")+ expect(store.signed_url_for_path("special/optimized/file.html")).to eq(+ "https://s3.example.com/special/optimized/file.html?signed=true",+ )+ end- expect(store.signed_url_for_path("special/optimized/file.png")).not_to eq(upload.url)+ it "returns signed URL with attachment content disposition when force_download is true" do+ s3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_once+ s3_bucket.expects(:object).with("special/optimized/file.png").returns(s3_object)+ opts = {+ expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds,+ response_content_disposition:+ ActionDispatch::Http::ContentDisposition.format(+ disposition: "attachment",+ filename: "file.png",+ ),+ }+ s3_object+ .expects(:presigned_url)+ .with(:get, opts)+ .returns("https://s3.example.com/special/optimized/file.png?signed=true")+ expect(store.signed_url_for_path("special/optimized/file.png", force_download: true)).to eq(+ "https://s3.example.com/special/optimized/file.png?signed=true",+ )endit "does not prefix the s3_bucket_folder_path onto temporary upload prefixed keys" dodiff --git a/spec/multisite/s3_store_spec.rb b/spec/multisite/s3_store_spec.rbindex aa36527278f..915b169d7a8 100644--- a/spec/multisite/s3_store_spec.rb+++ b/spec/multisite/s3_store_spec.rb@@ -220,7 +220,14 @@ RSpec.describe "Multisite s3 uploads", type: :multisite dos3_bucket.expects(:object).with("#{upload_path}/#{path}").returns(s3_object).at_least_onces3_object.expects(:presigned_url).with(:get,- { expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds },+ {+ expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds,+ response_content_disposition:+ ActionDispatch::Http::ContentDisposition.format(+ disposition: "inline",+ filename: upload.original_filename,+ ),+ },)upload.url = store.store_upload(uploaded_file, upload)diff --git a/spec/requests/uploads_controller_spec.rb b/spec/requests/uploads_controller_spec.rbindex 4aa9e51fb28..fab7ff8b9a7 100644--- a/spec/requests/uploads_controller_spec.rb+++ b/spec/requests/uploads_controller_spec.rb@@ -595,7 +595,10 @@ RSpec.describe UploadsController doget upload.short_pathexpect(response).to redirect_to(- Discourse.store.signed_url_for_path(Discourse.store.get_path_for_upload(upload)),+ Discourse.store.signed_url_for_path(+ Discourse.store.get_path_for_upload(upload),+ filename: upload.original_filename,+ ),)expect(response.header["Location"]).not_to include("response-content-disposition=attachment",