Skip to content

Commit e5e1b7c

Browse files
committed
Merge branch 'fix-42459-#-in-branch' into 'master'
Manually escape '#' when building relative uris, closes #42459 Closes #42459 See merge request gitlab-org/gitlab-ce!17679
2 parents 2a2b65c + f97dffd commit e5e1b7c

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Fix relative uri when "#" is in branch name
3+
merge_request:
4+
author: Jan
5+
type: fixed

lib/banzai/filter/relative_link_filter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def rebuild_relative_uri(uri)
8484
relative_url_root,
8585
project.full_path,
8686
uri_type(file_path),
87-
Addressable::URI.escape(ref),
87+
Addressable::URI.escape(ref).gsub('#', '%23'),
8888
Addressable::URI.escape(file_path)
8989
].compact.join('/').squeeze('/').chomp('/')
9090

spec/lib/banzai/filter/relative_link_filter_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,23 @@ def nested(element)
217217
end
218218
end
219219

220+
context 'when ref name contains special chars' do
221+
let(:ref) {'mark#\'@],+;-._/#@!$&()+down'}
222+
223+
it 'correctly escapes the ref' do
224+
# Adressable won't escape the '#', so we do this manually
225+
ref_escaped = 'mark%23\'@%5D,+;-._/%23@!$&()+down'
226+
227+
# Stub this method so the branch doesn't actually need to be in the repo
228+
allow_any_instance_of(described_class).to receive(:uri_type).and_return(:raw)
229+
230+
doc = filter(link('files/images/logo-black.png'))
231+
232+
expect(doc.at_css('a')['href'])
233+
.to eq "/#{project_path}/raw/#{ref_escaped}/files/images/logo-black.png"
234+
end
235+
end
236+
220237
context 'when requested path is a directory with space in the repo' do
221238
let(:ref) { 'master' }
222239
let(:commit) { project.commit('38008cb17ce1466d8fec2dfa6f6ab8dcfe5cf49e') }

0 commit comments

Comments
 (0)