From 9d799ccacfbdfa0ac0f3e7c9477d7040d471dd47 Mon Sep 17 00:00:00 2001
From: Ahmad Sherif <me@ahmadsherif.com>
Date: Wed, 9 May 2018 17:27:26 +0200
Subject: [PATCH] Fix finding wiki pages when they have invalidly-encoded
 content

Fixes #43715
---
 GITALY_SERVER_VERSION                              |  2 +-
 .../fix-wiki-find-page-invalid-encoding.yml        |  5 +++++
 config/initializers/gollum.rb                      | 14 ++++++++++++++
 spec/models/project_wiki_spec.rb                   | 11 +++++++++++
 4 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 changelogs/unreleased/fix-wiki-find-page-invalid-encoding.yml

diff --git a/GITALY_SERVER_VERSION b/GITALY_SERVER_VERSION
index 01781720cd4..897e21587ed 100644
--- a/GITALY_SERVER_VERSION
+++ b/GITALY_SERVER_VERSION
@@ -1 +1 @@
-0.99.0
+0.100.0
diff --git a/changelogs/unreleased/fix-wiki-find-page-invalid-encoding.yml b/changelogs/unreleased/fix-wiki-find-page-invalid-encoding.yml
new file mode 100644
index 00000000000..f003bef8671
--- /dev/null
+++ b/changelogs/unreleased/fix-wiki-find-page-invalid-encoding.yml
@@ -0,0 +1,5 @@
+---
+title: Fix finding wiki pages when they have invalidly-encoded content
+merge_request: 18856
+author:
+type: fixed
diff --git a/config/initializers/gollum.rb b/config/initializers/gollum.rb
index 81e0577a7c9..ea9cc151a57 100644
--- a/config/initializers/gollum.rb
+++ b/config/initializers/gollum.rb
@@ -7,6 +7,20 @@ module Gollum
 end
 require "gollum-lib"
 
+module Gollum
+  class Page
+    def text_data(encoding = nil)
+      data = if raw_data.respond_to?(:encoding)
+               raw_data.force_encoding(encoding || Encoding::UTF_8)
+             else
+               raw_data
+             end
+
+      Gitlab::EncodingHelper.encode!(data)
+    end
+  end
+end
+
 Rails.application.configure do
   config.after_initialize do
     Gollum::Page.per_page = Kaminari.config.default_per_page
diff --git a/spec/models/project_wiki_spec.rb b/spec/models/project_wiki_spec.rb
index d6c4031329d..f1142832f1a 100644
--- a/spec/models/project_wiki_spec.rb
+++ b/spec/models/project_wiki_spec.rb
@@ -159,6 +159,17 @@ describe ProjectWiki do
           expect(page.title).to eq("autre pag茅")
         end
       end
+
+      context 'pages with invalidly-encoded content' do
+        before do
+          create_page("encoding is fun", "f\xFCr".b)
+        end
+
+        it "can find the page" do
+          page = subject.find_page("encoding is fun")
+          expect(page.content).to eq("fr")
+        end
+      end
     end
 
     context 'when Gitaly wiki_find_page is enabled' do
-- 
2.30.9