A platform for community discussion. Free, open, simple.
SECURITY: Improve SVG sanitization
David Taylor92a699d89b84
diff --git a/app/controllers/user_avatars_controller.rb b/app/controllers/user_avatars_controller.rbindex d0dc32c054d..4abd6a241e9 100644--- a/app/controllers/user_avatars_controller.rb+++ b/app/controllers/user_avatars_controller.rb@@ -150,7 +150,13 @@ class UserAvatarsController < ApplicationControllerresponse.headers["Last-Modified"] = File.ctime(image).httpdateresponse.headers["Content-Length"] = File.size(image).to_simmutable_for 1.year- send_file image, disposition: nil++ if optimized.is_a?(Upload) && !FileHelper.is_inline_safe?(optimized.original_filename)+ response.headers["Content-Security-Policy"] = "sandbox;"+ send_file image, disposition: "attachment"+ else+ send_file image, disposition: nil+ endelserender_blankend@@ -198,7 +204,13 @@ class UserAvatarsController < ApplicationControllerresponse.headers["Last-Modified"] = last_modified.httpdateresponse.headers["Content-Length"] = File.size(path).to_simmutable_for(1.year)- send_file path, disposition: nil++ if !FileHelper.is_inline_safe?(filename)+ response.headers["Content-Security-Policy"] = "sandbox;"+ send_file path, disposition: "attachment"+ else+ send_file path, disposition: nil+ endrescue Errno::ENOENT, ActionController::MissingFilerender_blankenddiff --git a/config/nginx.sample.conf b/config/nginx.sample.confindex da7648bbd7d..d078019675e 100644--- a/config/nginx.sample.conf+++ b/config/nginx.sample.conf@@ -196,8 +196,11 @@ server {try_files $uri =404;}- # SVG needs an extra header attached+ # force attachment so SVGs can't render as a document on direct navigation (does not affect <img>/CSS rendering)location ~* \.(svg)$ {+ add_header Access-Control-Allow-Origin *;+ add_header Content-Disposition attachment;+ try_files $uri =404;}# thumbnails & optimized imagesdiff --git a/lib/upload_creator.rb b/lib/upload_creator.rbindex c698d349915..1e92e3240cb 100644--- a/lib/upload_creator.rb+++ b/lib/upload_creator.rb@@ -519,8 +519,11 @@ class UploadCreatordef clean_svg!doc = Nokogiri.XML(@file)+ doc.internal_subset&.remove+ doc.external_subset&.removedoc.xpath(svg_allowlist_xpath).removedoc.xpath("//@*[starts-with(name(), 'on')]").remove+ doc.traverse { |node| node.remove if node.type == Nokogiri::XML::Node::ENTITY_REF_NODE }doc.css("use").each do |use_el|diff --git a/spec/requests/user_avatars_controller_spec.rb b/spec/requests/user_avatars_controller_spec.rbindex 85eae3b41ee..87b5bd93fd9 100644--- a/spec/requests/user_avatars_controller_spec.rb+++ b/spec/requests/user_avatars_controller_spec.rb@@ -126,6 +126,35 @@ RSpec.describe UserAvatarsController doendend+ it "serves sanitized SVG avatars without DTD entities" do+ SiteSetting.authorized_extensions = "svg"+ user = Fabricate(:user)+ file = Tempfile.new(%w[avatar .svg])+ file.write(<<~SVG)+ <?xml version="1.0" standalone="yes"?>+ <!DOCTYPE svg [+ <!ENTITY xss "<script xmlns='http://www.w3.org/2000/svg'>alert('XSS')</script>">+ ]>+ <svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">+ <text x="10" y="40" font-size="20">&xss;</text>+ </svg>+ SVG+ file.rewind++ upload = UploadCreator.new(file, "avatar.svg", type: "avatar").create_for(user.id)+ user.create_user_avatar! unless user.user_avatar+ user.user_avatar.update!(custom_upload_id: upload.id)+ user.update!(uploaded_avatar_id: upload.id)++ get "/user_avatar/default/#{user.username}/200/#{upload.id}.png"++ expect(response.status).to eq(200)+ expect(response.body).not_to include("<!DOCTYPE")+ expect(response.body).not_to include("<script")+ expect(response.body).not_to include("&xss;")+ expect(response.headers["Content-Disposition"]).to start_with("attachment;")+ end+it "handles non local content correctly" dosetup_s3SiteSetting.avatar_sizes = "100|98|49"@@ -176,6 +205,33 @@ RSpec.describe UserAvatarsController do)end+ it "serves proxied SVG avatars as sandboxed attachments" do+ setup_s3+ SiteSetting.avatar_sizes = "98"+ SiteSetting.s3_cdn_url = "http://cdn.com"+ set_cdn_url("http://awesome.com/boom")++ stub_request(:get, "http://cdn.com/avatar.svg").to_return(+ body: "<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>",+ )++ upload =+ Fabricate(+ :upload,+ original_filename: "avatar.svg",+ extension: "svg",+ url: "//#{SiteSetting.s3_upload_bucket}.s3.dualstack.us-west-1.amazonaws.com/avatar.svg",+ )++ user = Fabricate(:user, uploaded_avatar_id: upload.id)++ get "/user_avatar/default/#{user.username}/98/#{upload.id}.png"++ expect(response.status).to eq(200)+ expect(response.headers["Content-Disposition"]).to start_with("attachment;")+ expect(response.headers["Content-Security-Policy"]).to eq("sandbox;")+ end+it "redirects to external store when enabled" doglobal_setting :redirect_avatar_requests, truesetup_s3