A platform for community discussion. Free, open, simple.
Revert "FIX: Enforce content disposition on S3 presigned URLs (#38681)" (#38720)
Natalie Taya6f904ebb5d8
diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rbindex 4b5a7ab728f..8db7475f9c1 100644--- a/lib/file_store/s3_store.rb+++ b/lib/file_store/s3_store.rb@@ -245,17 +245,12 @@ module FileStoredef signed_url_for_path(path,expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds,- force_download: false,- filename: nil+ force_download: false)key = path.sub(absolute_base_url + "/", "")- presigned_get_url(- key,- expires_in:,- force_download:,- filename: filename || File.basename(path),- )+ filename = force_download ? File.basename(path) : false+ presigned_get_url(key, expires_in:, force_download:, filename:)enddef signed_request_for_temporary_upload(@@ -444,11 +439,9 @@ module FileStore)opts = { expires_in: expires_in }- if filename- disposition =- (force_download || !FileHelper.is_inline_safe?(filename)) ? "attachment" : "inline"+ if force_download && filenameopts[:response_content_disposition] = ActionDispatch::Http::ContentDisposition.format(- disposition:,+ disposition: "attachment",filename: filename,)enddiff --git a/spec/lib/file_store/s3_store_spec.rb b/spec/lib/file_store/s3_store_spec.rbindex 5e0ee1622ef..de3193de000 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 attachment content disposition when force_download is true" do+ it "returns signed URL with content disposition when requesting to download image" dos3_helper.expects(:s3_bucket).returns(s3_bucket).at_least_onces3_bucket.expects(:object)@@ -923,106 +923,17 @@ 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 with inline content disposition for a given path" do+ it "returns signed URL 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,- 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+ opts = { expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds }- 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+ s3_object.expects(:presigned_url).with(:get, opts)- 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",- )+ expect(store.signed_url_for_path("special/optimized/file.png")).not_to eq(upload.url)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 915b169d7a8..aa36527278f 100644--- a/spec/multisite/s3_store_spec.rb+++ b/spec/multisite/s3_store_spec.rb@@ -220,14 +220,7 @@ 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,- response_content_disposition:- ActionDispatch::Http::ContentDisposition.format(- disposition: "inline",- filename: upload.original_filename,- ),- },+ { expires_in: SiteSetting.s3_presigned_get_url_expires_after_seconds },)upload.url = store.store_upload(uploaded_file, upload)diff --git a/spec/requests/uploads_controller_spec.rb b/spec/requests/uploads_controller_spec.rbindex fab7ff8b9a7..4aa9e51fb28 100644--- a/spec/requests/uploads_controller_spec.rb+++ b/spec/requests/uploads_controller_spec.rb@@ -595,10 +595,7 @@ RSpec.describe UploadsController doget upload.short_pathexpect(response).to redirect_to(- Discourse.store.signed_url_for_path(- Discourse.store.get_path_for_upload(upload),- filename: upload.original_filename,- ),+ Discourse.store.signed_url_for_path(Discourse.store.get_path_for_upload(upload)),)expect(response.header["Location"]).not_to include("response-content-disposition=attachment",