Commit 4cfcc975 authored by Jacob Vosmaer (GitLab)'s avatar Jacob Vosmaer (GitLab) Committed by Rémy Coutable

Fix encoding bugs in Gitlab::Git::User

parent ffa2a980
...@@ -17,6 +17,10 @@ module Gitlab ...@@ -17,6 +17,10 @@ module Gitlab
return nil unless message.respond_to?(:force_encoding) return nil unless message.respond_to?(:force_encoding)
return message if message.encoding == Encoding::UTF_8 && message.valid_encoding? return message if message.encoding == Encoding::UTF_8 && message.valid_encoding?
if message.respond_to?(:frozen?) && message.frozen?
message = message.dup
end
message.force_encoding("UTF-8") message.force_encoding("UTF-8")
return message if message.valid_encoding? return message if message.valid_encoding?
......
...@@ -8,7 +8,12 @@ module Gitlab ...@@ -8,7 +8,12 @@ module Gitlab
end end
def self.from_gitaly(gitaly_user) def self.from_gitaly(gitaly_user)
new(gitaly_user.gl_username, gitaly_user.name, gitaly_user.email, gitaly_user.gl_id) new(
gitaly_user.gl_username,
Gitlab::EncodingHelper.encode!(gitaly_user.name),
Gitlab::EncodingHelper.encode!(gitaly_user.email),
gitaly_user.gl_id
)
end end
def initialize(username, name, email, gl_id) def initialize(username, name, email, gl_id)
...@@ -23,7 +28,7 @@ module Gitlab ...@@ -23,7 +28,7 @@ module Gitlab
end end
def to_gitaly def to_gitaly
Gitaly::User.new(gl_username: username, gl_id: gl_id, name: name, email: email) Gitaly::User.new(gl_username: username, gl_id: gl_id, name: name.b, email: email.b)
end end
end end
end end
......
...@@ -9,6 +9,7 @@ describe Gitlab::EncodingHelper do ...@@ -9,6 +9,7 @@ describe Gitlab::EncodingHelper do
["nil", nil, nil], ["nil", nil, nil],
["empty string", "".encode("ASCII-8BIT"), "".encode("UTF-8")], ["empty string", "".encode("ASCII-8BIT"), "".encode("UTF-8")],
["invalid utf-8 encoded string", "my bad string\xE5".force_encoding("UTF-8"), "my bad string"], ["invalid utf-8 encoded string", "my bad string\xE5".force_encoding("UTF-8"), "my bad string"],
["frozen non-ascii string", "é".force_encoding("ASCII-8BIT").freeze, "é".encode("UTF-8")],
[ [
'leaves ascii only string as is', 'leaves ascii only string as is',
'ascii only string', 'ascii only string',
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Git::User do describe Gitlab::Git::User do
let(:username) { 'janedo' } let(:username) { 'janedoe' }
let(:name) { 'Jane Doe' } let(:name) { 'Jane Doé' }
let(:email) { 'janedoe@example.com' } let(:email) { 'janedoé@example.com' }
let(:gl_id) { 'user-123' } let(:gl_id) { 'user-123' }
let(:user) do let(:user) do
described_class.new(username, name, email, gl_id) described_class.new(username, name, email, gl_id)
...@@ -13,7 +13,7 @@ describe Gitlab::Git::User do ...@@ -13,7 +13,7 @@ describe Gitlab::Git::User do
describe '.from_gitaly' do describe '.from_gitaly' do
let(:gitaly_user) do let(:gitaly_user) do
Gitaly::User.new(gl_username: username, name: name, email: email, gl_id: gl_id) Gitaly::User.new(gl_username: username, name: name.b, email: email.b, gl_id: gl_id)
end end
subject { described_class.from_gitaly(gitaly_user) } subject { described_class.from_gitaly(gitaly_user) }
...@@ -48,8 +48,13 @@ describe Gitlab::Git::User do ...@@ -48,8 +48,13 @@ describe Gitlab::Git::User do
it 'creates a Gitaly::User with the correct data' do it 'creates a Gitaly::User with the correct data' do
expect(subject).to be_a(Gitaly::User) expect(subject).to be_a(Gitaly::User)
expect(subject.gl_username).to eq(username) expect(subject.gl_username).to eq(username)
expect(subject.name).to eq(name)
expect(subject.email).to eq(email) expect(subject.name).to eq(name.b)
expect(subject.name).to be_a_binary_string
expect(subject.email).to eq(email.b)
expect(subject.email).to be_a_binary_string
expect(subject.gl_id).to eq(gl_id) expect(subject.gl_id).to eq(gl_id)
end end
end end
......
RSpec::Matchers.define :be_a_binary_string do |_|
match do |actual|
actual.is_a?(String) && actual.encoding == Encoding.find('ASCII-8BIT')
end
description do
"be a String with binary encoding"
end
end
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