From 7b90034acd5aca95c0195eba58cf5cb47d0e53f3 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Mon, 26 Nov 2018 23:04:22 +0000 Subject: Merge branch 'sh-fix-hash-filename-handling' into 'master' Fix handling of filenames with hash characters in tree view Closes #54473 See merge request gitlab-org/gitlab-ce!23368 (cherry picked from commit 5d82035f623fd910c010c30b97112c46218430d4) e8da70e6 Fix handling of filenames with hash characters in tree view --- app/helpers/tree_helper.rb | 4 ++-- .../unreleased/sh-fix-hash-filename-handling.yml | 5 ++++ spec/helpers/tree_helper_spec.rb | 28 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 changelogs/unreleased/sh-fix-hash-filename-handling.yml diff --git a/app/helpers/tree_helper.rb b/app/helpers/tree_helper.rb index 78a11616d4c..e2879bfdcf1 100644 --- a/app/helpers/tree_helper.rb +++ b/app/helpers/tree_helper.rb @@ -37,13 +37,13 @@ module TreeHelper # Using Rails `*_path` methods can be slow, especially when generating # many paths, as with a repository tree that has thousands of items. def fast_project_blob_path(project, blob_path) - Addressable::URI.escape( + ActionDispatch::Journey::Router::Utils.escape_path( File.join(relative_url_root, project.path_with_namespace, 'blob', blob_path) ) end def fast_project_tree_path(project, tree_path) - Addressable::URI.escape( + ActionDispatch::Journey::Router::Utils.escape_path( File.join(relative_url_root, project.path_with_namespace, 'tree', tree_path) ) end diff --git a/changelogs/unreleased/sh-fix-hash-filename-handling.yml b/changelogs/unreleased/sh-fix-hash-filename-handling.yml new file mode 100644 index 00000000000..cb32051a4ab --- /dev/null +++ b/changelogs/unreleased/sh-fix-hash-filename-handling.yml @@ -0,0 +1,5 @@ +--- +title: Fix handling of filenames with hash characters in tree view +merge_request: 23368 +author: +type: fixed diff --git a/spec/helpers/tree_helper_spec.rb b/spec/helpers/tree_helper_spec.rb index ab4566e261b..4a62e696cd9 100644 --- a/spec/helpers/tree_helper_spec.rb +++ b/spec/helpers/tree_helper_spec.rb @@ -5,6 +5,16 @@ describe TreeHelper do let(:repository) { project.repository } let(:sha) { 'c1c67abbaf91f624347bb3ae96eabe3a1b742478' } + def create_file(filename) + project.repository.create_file( + project.creator, + filename, + 'test this', + message: "Automatically created file #{filename}", + branch_name: 'master' + ) + end + describe '.render_tree' do before do @id = sha @@ -57,6 +67,15 @@ describe TreeHelper do expect(fast_path).to start_with('/gitlab/root') end + + it 'encodes files starting with #' do + filename = '#test-file' + create_file(filename) + + fast_path = fast_project_blob_path(project, filename) + + expect(fast_path).to end_with('%23test-file') + end end describe '.fast_project_tree_path' do @@ -73,6 +92,15 @@ describe TreeHelper do expect(fast_path).to start_with('/gitlab/root') end + + it 'encodes files starting with #' do + filename = '#test-file' + create_file(filename) + + fast_path = fast_project_tree_path(project, filename) + + expect(fast_path).to end_with('%23test-file') + end end describe 'flatten_tree' do -- cgit v1.2.1