A platform for community discussion. Free, open, simple.
SECURITY: Remove event handlers from SVG files
Dan Ungureanu4e130f1e03b2
diff --git a/lib/upload_creator.rb b/lib/upload_creator.rbindex ab1fa9a4a89..4db456376c5 100644--- a/lib/upload_creator.rb+++ b/lib/upload_creator.rb@@ -277,6 +277,7 @@ class UploadCreatordef whitelist_svg!doc = Nokogiri::XML(@file)doc.xpath(svg_whitelist_xpath).remove+ doc.xpath("//@*[starts-with(name(), 'on')]").removeFile.write(@file.path, doc.to_s)@file.rewindenddiff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rbindex b26d7183071..09a0ddb9c06 100644--- a/spec/lib/upload_creator_spec.rb+++ b/spec/lib/upload_creator_spec.rb@@ -247,4 +247,26 @@ RSpec.describe UploadCreator doendendend++ describe '#whitelist_svg!' do+ let(:file) do+ file = Tempfile.new+ file.write(<<~XML)+ <?xml version="1.0" encoding="UTF-8"?>+ <svg xmlns="http://www.w3.org/2000/svg" width="200px" height="200px" onload="alert(location)">+ </svg>+ XML+ file.rewind+ file+ end++ it 'removes event handlers' do+ begin+ UploadCreator.new(file, 'file.svg').whitelist_svg!+ expect(file.read).not_to include('onload')+ ensure+ file.unlink+ end+ end+ endend