Skip to content

A platform for community discussion. Free, open, simple.

FIX: Truncate long filenames in S3 Content-Disposition header (#39170)

Gerhard Schlager8d5c0c542689
diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rb
index 8abab7d1882..2c366972344 100644
--- a/lib/file_store/s3_store.rb
+++ b/lib/file_store/s3_store.rb
@@ -105,10 +105,7 @@ module FileStore
# Only serve inline for allowlisted safe file types (non-SVG images and PDFs)
# to prevent XSS via HTML/XML/SVG uploads. All other files force download.
# See https://github.com/discourse/discourse/commit/31e31ef44973dc4daaee2f010d71588ea5873b53
- options[:content_disposition] = ActionDispatch::Http::ContentDisposition.format(
- disposition: FileHelper.is_inline_safe?(filename) ? "inline" : "attachment",
- filename: filename,
- )
+ options[:content_disposition] = self.class.content_disposition_for(filename)
path.prepend(File.join(upload_path, "/")) if Rails.configuration.multisite
@@ -435,6 +432,43 @@ module FileStore
self.class.default_s3_options(secure:)
end
+ # S3 limits total request header/metadata size to 2 KB. Content-Disposition
+ # includes the filename twice (filename= and filename*=UTF-8''), so we
+ # limit the full header to 1600 bytes to stay within that budget.
+ MAX_CONTENT_DISPOSITION_BYTES = 1_600
+
+ def self.content_disposition_for(filename, disposition: nil)
+ disposition ||= FileHelper.is_inline_safe?(filename) ? "inline" : "attachment"
+ return "" if filename.blank?
+
+ header = ActionDispatch::Http::ContentDisposition.format(disposition:, filename:)
+ return header if header.bytesize <= MAX_CONTENT_DISPOSITION_BYTES
+
+ extension = File.extname(filename)
+ basename = File.basename(filename, extension)
+
+ # Binary search for the longest basename that fits within the limit
+ low = 1
+ high = basename.length
+
+ while low < high
+ mid = (low + high + 1) / 2
+ candidate = "#{basename[0...mid]}#{extension}"
+ test_header =
+ ActionDispatch::Http::ContentDisposition.format(disposition:, filename: candidate)
+ if test_header.bytesize <= MAX_CONTENT_DISPOSITION_BYTES
+ low = mid
+ else
+ high = mid - 1
+ end
+ end
+
+ ActionDispatch::Http::ContentDisposition.format(
+ disposition:,
+ filename: "#{basename[0...low]}#{extension}",
+ )
+ end
+
private
def presigned_get_url(
diff --git a/lib/file_store/to_s3_migration.rb b/lib/file_store/to_s3_migration.rb
index f07e5d3fc82..63e84ad7466 100644
--- a/lib/file_store/to_s3_migration.rb
+++ b/lib/file_store/to_s3_migration.rb
@@ -212,12 +212,22 @@ module FileStore
Thread.new do
while obj = queue.pop
opts_with_file = obj[:options].merge(body: File.open(obj[:path], "rb"))
- if s3.put_object(opts_with_file)
- putc "."
- lock.synchronize { synced += 1 }
- else
- putc "X"
- lock.synchronize { failed << obj[:path] }
+ begin
+ if s3.put_object(opts_with_file)
+ putc "."
+ lock.synchronize { synced += 1 }
+ else
+ putc "X"
+ lock.synchronize { failed << obj[:path] }
+ end
+ rescue Aws::S3::Errors::MetadataTooLarge
+ if opts_with_file[:content_disposition].present?
+ opts_with_file.delete(:content_disposition)
+ opts_with_file[:body].rewind if opts_with_file[:body].respond_to?(:rewind)
+ retry
+ else
+ raise
+ end
end
end
end
@@ -243,26 +253,15 @@ module FileStore
key: key,
}.merge(FileStore::S3Store.default_s3_options(secure: false))
- if !FileHelper.is_supported_image?(name)
- upload = Upload.find_by(url: "/#{file}")
+ upload = Upload.find_by(url: "/#{file}")
- if upload&.original_filename
- options[:content_disposition] = ActionDispatch::Http::ContentDisposition.format(
- disposition: "attachment",
- filename: upload.original_filename,
- )
- end
+ options[:content_disposition] = FileStore::S3Store.content_disposition_for(
+ upload&.original_filename || name,
+ )
- if upload&.secure
- options[:acl] = FileStore::S3Store.acl_option_value(secure: true)
- options[:tagging] = FileStore::S3Store.visibility_tagging_option_value(secure: true)
- end
- elsif !FileHelper.is_svg?(name)
- upload = Upload.find_by(url: "/#{file}")
- options[:content_disposition] = ActionDispatch::Http::ContentDisposition.format(
- disposition: "attachment",
- filename: upload&.original_filename || name,
- )
+ if upload&.secure
+ options[:acl] = FileStore::S3Store.acl_option_value(secure: true)
+ options[:tagging] = FileStore::S3Store.visibility_tagging_option_value(secure: true)
end
if @dry_run
diff --git a/lib/s3_helper.rb b/lib/s3_helper.rb
index 9f38b6f366a..27d92d187dd 100644
--- a/lib/s3_helper.rb
+++ b/lib/s3_helper.rb
@@ -81,6 +81,14 @@ class S3Helper
options[:body] = file
obj.put(options).etag
end
+ rescue Aws::S3::Errors::MetadataTooLarge
+ if options[:content_disposition].present?
+ options.delete(:content_disposition)
+ file.rewind if file.respond_to?(:rewind)
+ retry
+ else
+ raise
+ end
end
[path, etag.gsub('"', "")]
diff --git a/spec/lib/file_store/s3_store_spec.rb b/spec/lib/file_store/s3_store_spec.rb
index 1da81e6b725..a4ba3788799 100644
--- a/spec/lib/file_store/s3_store_spec.rb
+++ b/spec/lib/file_store/s3_store_spec.rb
@@ -1108,6 +1108,42 @@ RSpec.describe FileStore::S3Store do
end
end
+ describe ".content_disposition_for" do
+ it "returns a valid header for short filenames" do
+ header = described_class.content_disposition_for("logo.png")
+
+ expect(header).to include("logo.png")
+ expect(header).to start_with("inline")
+ end
+
+ it "returns empty string for blank filenames" do
+ expect(described_class.content_disposition_for("")).to eq("")
+ expect(described_class.content_disposition_for(nil)).to eq("")
+ end
+
+ it "uses the specified disposition" do
+ header = described_class.content_disposition_for("logo.png", disposition: "attachment")
+
+ expect(header).to start_with("attachment")
+ end
+
+ it "truncates a very long ASCII filename while preserving extension" do
+ long_name = "a" * 2000 + ".html"
+ header = described_class.content_disposition_for(long_name)
+
+ expect(header.bytesize).to be <= described_class::MAX_CONTENT_DISPOSITION_BYTES
+ expect(header).to include(".html")
+ end
+
+ it "truncates long non-ASCII filenames that expand when percent-encoded" do
+ long_name = "\u4e2d\u6587" * 400 + ".png"
+ header = described_class.content_disposition_for(long_name)
+
+ expect(header.bytesize).to be <= described_class::MAX_CONTENT_DISPOSITION_BYTES
+ expect(header).to include(".png")
+ end
+ end
+
def prepare_fake_s3(upload_key, upload)
@fake_s3 = FakeS3.create
@fake_s3_bucket = @fake_s3.bucket(SiteSetting.s3_upload_bucket)