diff --git a/models/site.rb b/models/site.rb index 6050d012..20f9c254 100644 --- a/models/site.rb +++ b/models/site.rb @@ -509,7 +509,7 @@ class Site < Sequel::Model end if pathname.extname.match EDITABLE_FILE_EXT - open(uploaded.path) {|f| + open(uploaded.path, 'r:binary') {|f| matches = f.grep SPAM_MATCH_REGEX if !matches.empty? @@ -520,10 +520,8 @@ class Site < Sequel::Model subject: "[Neocities SPAM]: #{username}", body: %{ #{username} -
- https://#{self.host}#{relative_path} -
- link + https://#{self.host}/#{relative_path} + Match: #{matches.first.strip} } }) end diff --git a/tests/files/invalidutf8.html b/tests/files/invalidutf8.html new file mode 100644 index 00000000..c15dcc6c --- /dev/null +++ b/tests/files/invalidutf8.html @@ -0,0 +1,5 @@ + + +hi ­ + + \ No newline at end of file diff --git a/tests/site_file_tests.rb b/tests/site_file_tests.rb index 1a478385..fb8c1871 100644 --- a/tests/site_file_tests.rb +++ b/tests/site_file_tests.rb @@ -34,6 +34,11 @@ describe 'site_files' do end describe 'upload' do + it 'manages files with invalid UTF8' do + upload 'files[]' => Rack::Test::UploadedFile.new('./tests/files/invalidutf8.html', 'text/html') + File.exists?(@site.files_path('invalidutf8.html')).must_equal true + end + it 'succeeds with index.html file' do @site.site_changed.must_equal false upload 'files[]' => Rack::Test::UploadedFile.new('./tests/files/index.html', 'text/html') @@ -67,12 +72,12 @@ describe 'site_files' do it 'fails with unsupported file' do upload 'files[]' => Rack::Test::UploadedFile.new('./tests/files/flowercrime.wav', 'audio/x-wav') - last_response.body.must_match /not allowed on this site/i + last_response.body.must_match /only supported by.+supporter account/i File.exists?(@site.files_path('flowercrime.wav')).must_equal false @site.site_changed.must_equal false end - it 'succeeds for usually unsupported file on supported plans' do + it 'succeeds for unwhitelisted file on supporter plans' do no_file_restriction_plans = Site::PLAN_FEATURES.select {|p,v| v[:no_file_restrictions] == true} no_file_restriction_plans.each do |plan_type,hash| @site = Fabricate :site, plan_type: plan_type