From 5fa223c8eec2182c5301230ad46ea3ccc95f142e Mon Sep 17 00:00:00 2001
From: Quang-Minh Nguyen <qmnguyen@gitlab.com>
Date: Wed, 18 May 2022 12:00:33 +0700
Subject: [PATCH] Set MailRoom's postback request content type to text/plain

Changelog: fixed
Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/362068
---
 config/mail_room.yml                         |  1 +
 lib/api/internal/mail_room.rb                |  4 ++++
 spec/lib/gitlab/mail_room/mail_room_spec.rb  |  2 ++
 spec/requests/api/internal/mail_room_spec.rb | 24 ++++++++++++++++++++
 4 files changed, 31 insertions(+)

diff --git a/config/mail_room.yml b/config/mail_room.yml
index 49cb765ebe623..1e76f5e9875bb 100644
--- a/config/mail_room.yml
+++ b/config/mail_room.yml
@@ -46,6 +46,7 @@
       :delivery_method: postback
       :delivery_options:
         :delivery_url: <%= config[:gitlab_url] %>/api/v4/internal/mail_room/<%= key %>
+        :content_type: text/plain
         :jwt_auth_header: <%= Gitlab::MailRoom::INTERNAL_API_REQUEST_HEADER %>
         :jwt_issuer: <%= Gitlab::MailRoom::INTERNAL_API_REQUEST_JWT_ISSUER %>
         :jwt_algorithm: "HS256"
diff --git a/lib/api/internal/mail_room.rb b/lib/api/internal/mail_room.rb
index 238a83670d684..1e5e8c4c4e2e6 100644
--- a/lib/api/internal/mail_room.rb
+++ b/lib/api/internal/mail_room.rb
@@ -12,6 +12,10 @@ module Internal
     class MailRoom < ::API::Base
       feature_category :service_desk
 
+      format :json
+      content_type :txt, 'text/plain'
+      default_format :txt
+
       before do
         authenticate_gitlab_mailroom_request!
       end
diff --git a/spec/lib/gitlab/mail_room/mail_room_spec.rb b/spec/lib/gitlab/mail_room/mail_room_spec.rb
index 12fb12ebd87c7..06a25be757ed7 100644
--- a/spec/lib/gitlab/mail_room/mail_room_spec.rb
+++ b/spec/lib/gitlab/mail_room/mail_room_spec.rb
@@ -303,6 +303,7 @@
             delivery_method: 'postback',
             delivery_options: {
               delivery_url: "http://gitlab.example/api/v4/internal/mail_room/incoming_email",
+              content_type: "text/plain",
               jwt_auth_header: Gitlab::MailRoom::INTERNAL_API_REQUEST_HEADER,
               jwt_issuer: Gitlab::MailRoom::INTERNAL_API_REQUEST_JWT_ISSUER,
               jwt_algorithm: 'HS256',
@@ -316,6 +317,7 @@
             delivery_method: 'postback',
             delivery_options: {
               delivery_url: "http://gitlab.example/api/v4/internal/mail_room/service_desk_email",
+              content_type: "text/plain",
               jwt_auth_header: Gitlab::MailRoom::INTERNAL_API_REQUEST_HEADER,
               jwt_issuer: Gitlab::MailRoom::INTERNAL_API_REQUEST_JWT_ISSUER,
               jwt_algorithm: 'HS256',
diff --git a/spec/requests/api/internal/mail_room_spec.rb b/spec/requests/api/internal/mail_room_spec.rb
index 2a056f21bc825..a0a9c1f9cb3e7 100644
--- a/spec/requests/api/internal/mail_room_spec.rb
+++ b/spec/requests/api/internal/mail_room_spec.rb
@@ -215,5 +215,29 @@
         expect(job).to match a_hash_including('args' => [encoded_email_content])
       end
     end
+
+    context 'handle text/plain request content type' do
+      let(:auth_headers) do
+        jwt_token = JWT.encode(auth_payload, incoming_email_secret, 'HS256')
+        {
+          Gitlab::MailRoom::INTERNAL_API_REQUEST_HEADER => jwt_token,
+          'Content-Type' => 'text/plain'
+        }
+      end
+
+      it 'schedules a EmailReceiverWorker job with email content encoded to utf-8 forcefully' do
+        Sidekiq::Testing.fake! do
+          expect do
+            post api("/internal/mail_room/incoming_email"), headers: auth_headers, params: email_content
+          end.to change { EmailReceiverWorker.jobs.size }.by(1)
+        end
+
+        expect(response).to have_gitlab_http_status(:ok)
+        expect(response.content_type).to eql('application/json')
+
+        job = EmailReceiverWorker.jobs.last
+        expect(job).to match a_hash_including('args' => [email_content])
+      end
+    end
   end
 end
-- 
GitLab