From 8a650540c9b5489dc4113d837107637052cb73ea Mon Sep 17 00:00:00 2001 From: Olaoluwa Oluro Date: Tue, 2 Jul 2024 21:17:31 +0100 Subject: [PATCH] Fix Changelog error on Non-encoded Commit Trailers **Problem** When a commit message is made with non-Ascii characters in the trailers, the changelog API returns a 500 when retrieved. **Solution** Ensure non-Ascii characters in commit trailers are encoded Changelog: fixed --- lib/gitlab/git/commit.rb | 4 ++-- spec/lib/gitlab/git/commit_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/git/commit.rb b/lib/gitlab/git/commit.rb index d899ed3ba25f4e..49d3eaa576feaa 100644 --- a/lib/gitlab/git/commit.rb +++ b/lib/gitlab/git/commit.rb @@ -432,7 +432,7 @@ def init_from_gitaly(commit) @committer_name = commit.committer.name.dup @committer_email = commit.committer.email.dup @parent_ids = Array(commit.parent_ids) - @trailers = commit.trailers.to_h { |t| [t.key, t.value] } + @trailers = commit.trailers.to_h { |t| [t.key, encode!(t.value)] } @extended_trailers = parse_commit_trailers(commit.trailers) @referenced_by = Array(commit.referenced_by) end @@ -440,7 +440,7 @@ def init_from_gitaly(commit) # Turn the commit trailers into a hash of key: [value, value] arrays def parse_commit_trailers(trailers) trailers.each_with_object({}) do |trailer, hash| - (hash[trailer.key] ||= []) << trailer.value + (hash[trailer.key] ||= []) << encode!(trailer.value) end end diff --git a/spec/lib/gitlab/git/commit_spec.rb b/spec/lib/gitlab/git/commit_spec.rb index a924137b8ec74a..767ca360b96750 100644 --- a/spec/lib/gitlab/git/commit_spec.rb +++ b/spec/lib/gitlab/git/commit_spec.rb @@ -51,6 +51,30 @@ ) end + context 'non-ASCII content' do + let(:body) do + body = +<<~BODY + Äpfel + + Changelog: Äpfel + BODY + + [subject, "\n", body.force_encoding("ASCII-8BIT")].join + end + + it "parses non-ASCII commit trailers" do + expect(commit.trailers).to eq( + { 'Changelog' => 'Äpfel' } + ) + end + + it "parses non-ASCII extended commit trailers" do + expect(commit.extended_trailers).to eq( + { 'Changelog' => ['Äpfel'] } + ) + end + end + context 'non-UTC dates' do let(:seconds) { Time.now.to_i } -- GitLab