From a31e0aff224d4047e10fae98b6f9dc9f84b7457a Mon Sep 17 00:00:00 2001 From: Micael Bergeron Date: Tue, 12 Sep 2017 14:07:31 +0000 Subject: Resolve "Error 500 in non-UTF8 branch names" --- .../37025-error-500-in-non-utf8-branch-names.yml | 4 ++++ lib/gitlab/git.rb | 2 +- spec/controllers/projects/branches_controller_spec.rb | 15 +++++++++++++++ spec/lib/gitlab/data_builder/push_spec.rb | 2 +- spec/lib/gitlab/git_spec.rb | 9 +++++++++ 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 changelogs/unreleased/37025-error-500-in-non-utf8-branch-names.yml diff --git a/changelogs/unreleased/37025-error-500-in-non-utf8-branch-names.yml b/changelogs/unreleased/37025-error-500-in-non-utf8-branch-names.yml new file mode 100644 index 00000000000..f3118cf0f2f --- /dev/null +++ b/changelogs/unreleased/37025-error-500-in-non-utf8-branch-names.yml @@ -0,0 +1,4 @@ +--- +title: Fixed non-UTF-8 valid branch names from causing an error. +merge_request: 14090 +type: fixed diff --git a/lib/gitlab/git.rb b/lib/gitlab/git.rb index 8c9acbc9fbe..b4b6326cfdd 100644 --- a/lib/gitlab/git.rb +++ b/lib/gitlab/git.rb @@ -11,7 +11,7 @@ module Gitlab include Gitlab::EncodingHelper def ref_name(ref) - encode! ref.sub(/\Arefs\/(tags|heads|remotes)\//, '') + encode_utf8(ref).sub(/\Arefs\/(tags|heads|remotes)\//, '') end def branch_name(ref) diff --git a/spec/controllers/projects/branches_controller_spec.rb b/spec/controllers/projects/branches_controller_spec.rb index 745d051a5c1..5e0b57e9b2e 100644 --- a/spec/controllers/projects/branches_controller_spec.rb +++ b/spec/controllers/projects/branches_controller_spec.rb @@ -367,5 +367,20 @@ describe Projects::BranchesController do expect(parsed_response.first).to eq 'master' end end + + context 'when branch contains an invalid UTF-8 sequence' do + before do + project.repository.create_branch("wrong-\xE5-utf8-sequence") + end + + it 'return with a status 200' do + get :index, + namespace_id: project.namespace, + project_id: project, + format: :html + + expect(response).to have_http_status(200) + end + end end end diff --git a/spec/lib/gitlab/data_builder/push_spec.rb b/spec/lib/gitlab/data_builder/push_spec.rb index cb430b47463..befdc18d1aa 100644 --- a/spec/lib/gitlab/data_builder/push_spec.rb +++ b/spec/lib/gitlab/data_builder/push_spec.rb @@ -47,7 +47,7 @@ describe Gitlab::DataBuilder::Push do include_examples 'deprecated repository hook data' it 'does not raise an error when given nil commits' do - expect { described_class.build(spy, spy, spy, spy, spy, nil) } + expect { described_class.build(spy, spy, spy, spy, 'refs/tags/v1.1.0', nil) } .not_to raise_error end end diff --git a/spec/lib/gitlab/git_spec.rb b/spec/lib/gitlab/git_spec.rb index 4702a978f19..494dfe0e595 100644 --- a/spec/lib/gitlab/git_spec.rb +++ b/spec/lib/gitlab/git_spec.rb @@ -1,3 +1,4 @@ +# coding: utf-8 require 'spec_helper' describe Gitlab::Git do @@ -29,4 +30,12 @@ describe Gitlab::Git do end end end + + describe '.ref_name' do + it 'ensure ref is a valid UTF-8 string' do + utf8_invalid_ref = Gitlab::Git::BRANCH_REF_PREFIX + "an_invalid_ref_\xE5" + + expect(described_class.ref_name(utf8_invalid_ref)).to eq("an_invalid_ref_å") + end + end end -- cgit v1.2.1