catch file name too long

This commit is contained in:
Kyle Drake 2025-08-09 18:20:10 -05:00
parent 5571a5913f
commit 3a2cc571e2
2 changed files with 19 additions and 0 deletions

View file

@ -37,6 +37,11 @@ post '/site_files/create' do
redirect redirect_uri
end
if SiteFile.name_too_long?(name)
flash[:error] = "File name is too long (exceeds #{SiteFile::FILE_NAME_CHARACTER_LIMIT} characters)."
redirect redirect_uri
end
extname = File.extname name
unless extname.empty? || extname.match(/^\.#{Site::EDITABLE_FILE_EXT}/i)

View file

@ -36,6 +36,20 @@ describe 'site_files' do
_(PurgeCacheWorker.jobs.length).must_equal 1
_(PurgeCacheWorker.jobs.first['args'].last).must_equal '/test'
end
it 'rejects filenames that exceed the character limit' do
long_filename = 'a' * (SiteFile::FILE_NAME_CHARACTER_LIMIT + 1) + '.html'
post '/site_files/create', {filename: long_filename, csrf_token: 'abcd'}, {'rack.session' => { 'id' => @site.id, '_csrf_token' => 'abcd' }}
_(last_response.status).must_equal 302
_(last_response.headers['Location']).must_match /dashboard/
# Check for error message by following the redirect
get '/dashboard', {}, {'rack.session' => { 'id' => @site.id, '_csrf_token' => 'abcd' }}
_(last_response.body).must_match /file name is too long/i
_(last_response.body).must_match /exceeds #{SiteFile::FILE_NAME_CHARACTER_LIMIT} characters/
end
end
describe 'rename' do