From b936a1d344d49bce1af09f92826dc152b77129d1 Mon Sep 17 00:00:00 2001
From: Stan Hu <stanhu@gmail.com>
Date: Tue, 20 Feb 2024 21:23:46 -0800
Subject: [PATCH] Ensure errors.css is interpreted as UTF-8 encoding

Since
https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/19096,
we've inlined the `errors.css` by directly including the Sprockets
assets into the HAML. This worked fine as long as the CSS file
remained as a plain ASCII file. However, the switch to the new CSS
pipeline with
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/144479 appears
to have introduced a single UTF-8 character. Since Sprockets loads the
asset via `File.binread`, the contents of this UTF-8 file is forced as
ASCII-8BIT. When this ASCII-8BIT string is combined with other UTF-8
characters, such as Chinese characters, the encoding fails with a 500
error: `incompatible character encodings: ASCII-8BIT and UTF-8
(Encoding::CompatibilityError)`.

To work around this issue, we force the encoding to UTF-8 to ensure
the error page can render.

Relates to
https://gitlab.com/gitlab-com/gl-infra/production/-/issues/17627

Changelog: fixed
---
 app/views/layouts/errors.html.haml      | 2 +-
 app/views/layouts/oauth_error.html.haml | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/app/views/layouts/errors.html.haml b/app/views/layouts/errors.html.haml
index d07daf0aab999..bf48b1caa91c3 100644
--- a/app/views/layouts/errors.html.haml
+++ b/app/views/layouts/errors.html.haml
@@ -4,7 +4,7 @@
     %meta{ :content => "width=device-width, initial-scale=1", :name => "viewport" }
     %title= yield(:title)
     %style
-      = Rails.application.assets_manifest.find_sources('errors.css').first.to_s.html_safe
+      = Rails.application.assets_manifest.find_sources('errors.css').first.to_s.force_encoding('UTF-8').html_safe
   %body
     .page-container
       = yield
diff --git a/app/views/layouts/oauth_error.html.haml b/app/views/layouts/oauth_error.html.haml
index 697bd9b586428..9262d9032cb0e 100644
--- a/app/views/layouts/oauth_error.html.haml
+++ b/app/views/layouts/oauth_error.html.haml
@@ -5,7 +5,7 @@
     %title= yield(:title)
     = stylesheet_link_tag 'application_utilities'
     %style
-      = Rails.application.assets_manifest.find_sources('errors.css').first.to_s.html_safe
+      = Rails.application.assets_manifest.find_sources('errors.css').first.to_s.force_encoding('UTF-8').html_safe
     :css
       svg {
         width: 280px;
-- 
GitLab