Commit e8da70e6 authored by Stan Hu's avatar Stan Hu

Fix handling of filenames with hash characters in tree view

Addressable::URI interprets the `#` in a URI as a URI fragment
and does not escape it, but Rails has special helpers that treats
these as bona-fide characters that need to be escaped.

Closes https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/23368
parent deaf3af7
...@@ -37,13 +37,13 @@ module TreeHelper ...@@ -37,13 +37,13 @@ module TreeHelper
# Using Rails `*_path` methods can be slow, especially when generating # Using Rails `*_path` methods can be slow, especially when generating
# many paths, as with a repository tree that has thousands of items. # many paths, as with a repository tree that has thousands of items.
def fast_project_blob_path(project, blob_path) 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) File.join(relative_url_root, project.path_with_namespace, 'blob', blob_path)
) )
end end
def fast_project_tree_path(project, tree_path) 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) File.join(relative_url_root, project.path_with_namespace, 'tree', tree_path)
) )
end end
......
---
title: Fix handling of filenames with hash characters in tree view
merge_request: 23368
author:
type: fixed
...@@ -5,6 +5,16 @@ describe TreeHelper do ...@@ -5,6 +5,16 @@ describe TreeHelper do
let(:repository) { project.repository } let(:repository) { project.repository }
let(:sha) { 'c1c67abbaf91f624347bb3ae96eabe3a1b742478' } 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 describe '.render_tree' do
before do before do
@id = sha @id = sha
...@@ -57,6 +67,15 @@ describe TreeHelper do ...@@ -57,6 +67,15 @@ describe TreeHelper do
expect(fast_path).to start_with('/gitlab/root') expect(fast_path).to start_with('/gitlab/root')
end 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 end
describe '.fast_project_tree_path' do describe '.fast_project_tree_path' do
...@@ -73,6 +92,15 @@ describe TreeHelper do ...@@ -73,6 +92,15 @@ describe TreeHelper do
expect(fast_path).to start_with('/gitlab/root') expect(fast_path).to start_with('/gitlab/root')
end 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 end
describe 'flatten_tree' do describe 'flatten_tree' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment