Commit 038be9ff authored by Stan Hu's avatar Stan Hu

Fix Error 500s due to encoding issues when Wiki hooks fire

Saved Wiki content goes through the GitalyClient::WikiService, which calls
StringIO#set_encoding on the input stream. The problem is that this call
mutates the encoding of the given string object to ASCII-88BIT, which
causes problems for models expecting the data to still be in UTF-8.

Freezing the input disables this behavior:
https://github.com/ruby/ruby/blob/v2_4_4/ext/stringio/stringio.c#L1583

Closes #50590
parent 42523a41
---
title: Fix Error 500s due to encoding issues when Wiki hooks fire
merge_request:
author:
type: fixed
...@@ -75,7 +75,7 @@ module Gitlab ...@@ -75,7 +75,7 @@ module Gitlab
end end
def binary_stringio(str) def binary_stringio(str)
StringIO.new(str || '').tap { |io| io.set_encoding(Encoding::ASCII_8BIT) } StringIO.new(str.freeze || '').tap { |io| io.set_encoding(Encoding::ASCII_8BIT) }
end end
private private
......
# coding: utf-8
require "spec_helper" require "spec_helper"
describe Gitlab::EncodingHelper do describe Gitlab::EncodingHelper do
...@@ -187,4 +188,15 @@ describe Gitlab::EncodingHelper do ...@@ -187,4 +188,15 @@ describe Gitlab::EncodingHelper do
end end
end end
end end
describe '#binary_stringio' do
it 'does not mutate the original string encoding' do
test = 'my-test'
io_stream = ext_class.binary_stringio(test)
expect(io_stream.external_encoding.name).to eq('ASCII-8BIT')
expect(test.encoding.name).to eq('UTF-8')
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment