allow text files with no extension (for well-known stuff)

This commit is contained in:
Kyle Drake 2024-11-20 12:23:22 -06:00
parent 5fb1523396
commit 920f25b098
6 changed files with 25 additions and 6 deletions

View file

@ -39,7 +39,7 @@ post '/site_files/create' do
extname = File.extname name extname = File.extname name
unless extname.match /^\.#{Site::EDITABLE_FILE_EXT}/i unless extname.empty? || extname.match(/^\.#{Site::EDITABLE_FILE_EXT}/i)
flash[:error] = "Must be an text editable file type (#{Site::VALID_EDITABLE_EXTENSIONS.join(', ')})." flash[:error] = "Must be an text editable file type (#{Site::VALID_EDITABLE_EXTENSIONS.join(', ')})."
redirect redirect_uri redirect redirect_uri
end end

View file

@ -754,11 +754,12 @@ class Site < Sequel::Model
end end
def self.valid_file_mime_type_and_ext?(mime_type, extname) def self.valid_file_mime_type_and_ext?(mime_type, extname)
unless (Site::VALID_MIME_TYPES.include?(mime_type) || mime_type =~ /text/ || mime_type =~ /inode\/x-empty/) && valid_mime_type = Site::VALID_MIME_TYPES.include?(mime_type) || mime_type =~ /text/ || mime_type =~ /inode\/x-empty/
Site::VALID_EXTENSIONS.include?(extname.sub(/^./, '').downcase) valid_extension = Site::VALID_EXTENSIONS.include?(extname.sub(/^./, '').downcase)
return false unless valid_extension
return true if mime_type =~ /text/ || mime_type == 'application/json'
end end
true valid_mime_type && valid_extension
end end
def self.valid_file_type?(uploaded_file) def self.valid_file_type?(uploaded_file)
@ -1264,7 +1265,7 @@ class Site < Sequel::Model
file[:is_html] = !(extname.match(HTML_REGEX)).nil? file[:is_html] = !(extname.match(HTML_REGEX)).nil?
file[:is_image] = !(file[:ext].match IMAGE_REGEX).nil? file[:is_image] = !(file[:ext].match IMAGE_REGEX).nil?
file[:is_editable] = !(file[:ext].match EDITABLE_FILE_EXT).nil? file[:is_editable] = !(file[:ext].match EDITABLE_FILE_EXT).nil? || file[:ext].empty?
file file
end end

1
tests/files/json-file Normal file
View file

@ -0,0 +1 @@
{"Paul Frazee":"is hereby eternally memorialized in this test, and will forever be known for only this contribution to western civilization"}

BIN
tests/files/testjpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

1
tests/files/text-file Normal file
View file

@ -0,0 +1 @@
This is a text file.

View file

@ -401,6 +401,22 @@ describe 'site_files' do
_(@site.site_changed).must_equal false _(@site.site_changed).must_equal false
end end
it 'allows non-extension filename upload if it is a text or JSON file' do
uploaded_files = [Rack::Test::UploadedFile.new('./tests/files/text-file', 'text/plain'), Rack::Test::UploadedFile.new('./tests/files/json-file', 'application/json')]
uploaded_files.each do |file|
upload file.original_filename => file
_(last_response.body).must_match /successfully uploaded/i
_(File.exists?(@site.files_path(file.original_filename))).must_equal true
username, path = PurgeCacheWorker.jobs.last['args']
_(username).must_equal @site.username
_(path).must_equal '/'+file.original_filename
end
upload 'testjpeg' => Rack::Test::UploadedFile.new('./tests/files/testjpeg', 'image/jpeg')
_(last_response.body).must_match /invalid_file_type/i
end
it 'works with square bracket filename' do it 'works with square bracket filename' do
uploaded_file = Rack::Test::UploadedFile.new('./tests/files/te[s]t.jpg', 'image/jpeg') uploaded_file = Rack::Test::UploadedFile.new('./tests/files/te[s]t.jpg', 'image/jpeg')
upload 'te[s]t.jpg' => uploaded_file upload 'te[s]t.jpg' => uploaded_file