Replace whitespaces in wiki page attachments file names

This commit is contained in:
Francisco Javier López 2018-09-07 14:10:15 +00:00 committed by Douwe Maan
parent 7f2b287f66
commit f67211f988
3 changed files with 38 additions and 3 deletions

View file

@ -11,7 +11,7 @@ module Wikis
def initialize(*args)
super
@file_name = truncate_file_name(params[:file_name])
@file_name = clean_file_name(params[:file_name])
@file_path = File.join(ATTACHMENT_PATH, SecureRandom.hex, @file_name) if @file_name
@commit_message ||= "Upload attachment #{@file_name}"
@branch_name ||= wiki.default_branch
@ -23,8 +23,16 @@ module Wikis
private
def truncate_file_name(file_name)
def clean_file_name(file_name)
return unless file_name.present?
file_name = truncate_file_name(file_name)
# CommonMark does not allow Urls with whitespaces, so we have to replace them
# Using the same regex Carrierwave use to replace invalid characters
file_name.gsub(CarrierWave::SanitizedFile.sanitize_regexp, '_')
end
def truncate_file_name(file_name)
return file_name if file_name.length <= MAX_FILENAME_LENGTH
extension = File.extname(file_name)

View file

@ -0,0 +1,5 @@
---
title: Replace white spaces in wiki attachments file names
merge_request: 21569
author:
type: fixed

View file

@ -88,8 +88,30 @@ describe Wikis::CreateAttachmentService do
end
end
describe 'validations' do
describe '#parse_file_name' do
context 'when file_name' do
context 'has white spaces' do
let(:file_name) { 'file with spaces' }
it "replaces all of them with '_'" do
result = service.execute
expect(result[:status]).to eq :success
expect(result[:result][:file_name]).to eq 'file_with_spaces'
end
end
context 'has other invalid characters' do
let(:file_name) { "file\twith\tinvalid chars" }
it "replaces all of them with '_'" do
result = service.execute
expect(result[:status]).to eq :success
expect(result[:result][:file_name]).to eq 'file_with_invalid_chars'
end
end
context 'is not present' do
let(:file_name) { nil }