From cc892541220ba2f1d8a17a88185ff4913d93777a Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Wed, 18 Oct 2017 13:19:34 +0200 Subject: Remove unavailable characters when encoding page title to ISO-8859-1 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. --- app/controllers/application_controller.rb | 2 +- lib/gitlab/encoding_helper.rb | 5 +++++ spec/lib/gitlab/encoding_helper_spec.rb | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) 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 [ [ -- cgit v1.2.1