summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToon Claes <toon@iotcl.com>2017-10-18 13:19:34 +0200
committerToon Claes <toon@iotcl.com>2017-10-18 13:19:34 +0200
commitcc892541220ba2f1d8a17a88185ff4913d93777a (patch)
tree0b8c680e56a78d803b93d3dd19aaf4ee28391444
parent1090514ab407b7faa894cbd9b6cd769c376a9b5d (diff)
downloadgitlab-ce-tc-page-title-encoding-fix.tar.gz
Remove unavailable characters when encoding page title to ISO-8859-1tc-page-title-encoding-fix
The Page-Title header has to be ISO-8859-1, so when converting the title to that encoding, remove characters that are not available. Closes gitlab-org/gitlab-ce#39179.
-rw-r--r--app/controllers/application_controller.rb2
-rw-r--r--lib/gitlab/encoding_helper.rb5
-rw-r--r--spec/lib/gitlab/encoding_helper_spec.rb19
3 files changed, 25 insertions, 1 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 967fe39256a..505f8c2f5f6 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -349,6 +349,6 @@ class ApplicationController < ActionController::Base
def set_page_title_header
# Per https://tools.ietf.org/html/rfc5987, headers need to be ISO-8859-1, not UTF-8
- response.headers['Page-Title'] = page_title('GitLab').encode('ISO-8859-1')
+ response.headers['Page-Title'] = Gitlab::EncodingHelper.lossy_encode(page_title('GitLab'), 'ISO-8859-1')
end
end
diff --git a/lib/gitlab/encoding_helper.rb b/lib/gitlab/encoding_helper.rb
index 7b3483a7f96..0efeecd7d4a 100644
--- a/lib/gitlab/encoding_helper.rb
+++ b/lib/gitlab/encoding_helper.rb
@@ -64,6 +64,11 @@ module Gitlab
end
end
+ # Convert all characters to the desired encoding, and drop the characters not available in that encoding
+ def lossy_encode(message, encoding)
+ message.encode(encoding, invalid: :replace, undef: :replace, replace: '')
+ end
+
private
def clean(message)
diff --git a/spec/lib/gitlab/encoding_helper_spec.rb b/spec/lib/gitlab/encoding_helper_spec.rb
index 8b14b227e65..1838b21c5a2 100644
--- a/spec/lib/gitlab/encoding_helper_spec.rb
+++ b/spec/lib/gitlab/encoding_helper_spec.rb
@@ -1,6 +1,8 @@
require "spec_helper"
describe Gitlab::EncodingHelper do
+ using RSpec::Parameterized::TableSyntax
+
let(:ext_class) { Class.new { extend Gitlab::EncodingHelper } }
let(:binary_string) { File.read(Rails.root + "spec/fixtures/dk.png") }
@@ -109,6 +111,23 @@ describe Gitlab::EncodingHelper do
end
end
+ describe '#lossy_encode' do
+ context 'to ISO-8859-1' do
+ where(:language, :input, :output) do
+ 'ASCII' | 'plain ASCII' | 'plain ASCII'
+ 'Portugese' | 'Última Atualização' | 'Última Atualização'
+ 'Russian' | 'Последнее обновление' | ' '
+ 'Esperanto' | 'Lasta ĝisdatigo' | 'Lasta isdatigo'
+ end
+
+ with_them do
+ it 'properly encodes' do
+ expect(ext_class.lossy_encode(input, 'ISO-8859-1').encode('UTF-8')).to eq(output)
+ end
+ end
+ end
+ end
+
describe '#clean' do
[
[