A platform for community discussion. Free, open, simple.
FIX: Ensure uploaded watched word CSVs are converted to utf-8. (#32263)
Gary Pendergast05e049190207
diff --git a/app/controllers/admin/watched_words_controller.rb b/app/controllers/admin/watched_words_controller.rbindex 9cf532e63b8..fa019a2ae10 100644--- a/app/controllers/admin/watched_words_controller.rb+++ b/app/controllers/admin/watched_words_controller.rb@@ -56,7 +56,9 @@ class Admin::WatchedWordsController < Admin::StaffControllerScheduler::Defer.later("Upload watched words") dobegin- CSV.foreach(file.tempfile, encoding: "bom|utf-8") do |row|+ content = Encodings.to_utf8(File.read(file.tempfile, mode: "rb"))++ CSV.parse(content) do |row|if row[0].present? && (!has_replacement || row[1].present?)watched_word =WatchedWord.create_or_update_word(diff --git a/spec/requests/admin/watched_words_controller_spec.rb b/spec/requests/admin/watched_words_controller_spec.rbindex dd214f68296..84fc5a2216e 100644--- a/spec/requests/admin/watched_words_controller_spec.rb+++ b/spec/requests/admin/watched_words_controller_spec.rb@@ -232,6 +232,19 @@ RSpec.describe Admin::WatchedWordsController do["test", false],)end++ it "handles files with invalid UTF-8 sequences" do+ content = String.new("h\xE9llo\nworld\x99").force_encoding("Windows-1250")++ post "/admin/customize/watched_words/upload.json",+ params: {+ action_key: "flag",+ file: Rack::Test::UploadedFile.new(file_from_contents(content, "words.csv")),+ }++ expect(response.status).to eq(200)+ expect(WatchedWord.pluck(:word)).to contain_exactly("héllo", "world™")+ endendend