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
# 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
......
---
title: Fix handling of filenames with hash characters in tree view
merge_request: 23368
author:
type: fixed
......@@ -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
......
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