2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-01 17:21:14 -04:00
|
|
|
module Gitlab
|
|
|
|
module EncodingHelper
|
|
|
|
extend self
|
|
|
|
|
|
|
|
# This threshold is carefully tweaked to prevent usage of encodings detected
|
|
|
|
# by CharlockHolmes with low confidence. If CharlockHolmes confidence is low,
|
|
|
|
# we're better off sticking with utf8 encoding.
|
|
|
|
# Reason: git diff can return strings with invalid utf8 byte sequences if it
|
|
|
|
# truncates a diff in the middle of a multibyte character. In this case
|
|
|
|
# CharlockHolmes will try to guess the encoding and will likely suggest an
|
|
|
|
# obscure encoding with low confidence.
|
|
|
|
# There is a lot more info with this merge request:
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab_git/merge_requests/77#note_4754193
|
2017-07-20 09:02:07 -04:00
|
|
|
ENCODING_CONFIDENCE_THRESHOLD = 50
|
2017-06-01 17:21:14 -04:00
|
|
|
|
|
|
|
def encode!(message)
|
2018-01-04 17:27:37 -05:00
|
|
|
message = force_encode_utf8(message)
|
2017-06-01 17:21:14 -04:00
|
|
|
return message if message.valid_encoding?
|
|
|
|
|
|
|
|
# return message if message type is binary
|
2021-05-06 08:10:38 -04:00
|
|
|
detect = detect_encoding(message)
|
2017-09-05 13:16:08 -04:00
|
|
|
return message.force_encoding("BINARY") if detect_binary?(message, detect)
|
2017-06-01 17:21:14 -04:00
|
|
|
|
|
|
|
if detect && detect[:encoding] && detect[:confidence] > ENCODING_CONFIDENCE_THRESHOLD
|
2017-09-03 07:45:44 -04:00
|
|
|
# force detected encoding if we have sufficient confidence.
|
2017-06-01 17:21:14 -04:00
|
|
|
message.force_encoding(detect[:encoding])
|
|
|
|
end
|
|
|
|
|
|
|
|
# encode and clean the bad chars
|
|
|
|
message.replace clean(message)
|
2018-02-09 12:58:29 -05:00
|
|
|
rescue ArgumentError => e
|
|
|
|
return unless e.message.include?('unknown encoding name')
|
|
|
|
|
2017-06-01 17:21:14 -04:00
|
|
|
encoding = detect ? detect[:encoding] : "unknown"
|
|
|
|
"--broken encoding: #{encoding}"
|
|
|
|
end
|
|
|
|
|
2021-05-06 08:10:38 -04:00
|
|
|
def detect_encoding(data, limit: CharlockHolmes::EncodingDetector::DEFAULT_BINARY_SCAN_LEN, cache_key: nil)
|
|
|
|
return if data.nil?
|
|
|
|
|
|
|
|
if Feature.enabled?(:cached_encoding_detection, type: :development, default_enabled: :yaml)
|
|
|
|
return CharlockHolmes::EncodingDetector.new(limit).detect(data) unless cache_key.present?
|
|
|
|
|
|
|
|
Rails.cache.fetch([:detect_binary, CharlockHolmes::VERSION, cache_key], expires_in: 1.week) do
|
|
|
|
CharlockHolmes::EncodingDetector.new(limit).detect(data)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
CharlockHolmes::EncodingDetector.new(limit).detect(data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-05 13:16:08 -04:00
|
|
|
def detect_binary?(data, detect = nil)
|
2021-05-06 08:10:38 -04:00
|
|
|
detect ||= detect_encoding(data)
|
2017-09-05 13:16:08 -04:00
|
|
|
detect && detect[:type] == :binary && detect[:confidence] == 100
|
2017-09-04 13:34:15 -04:00
|
|
|
end
|
|
|
|
|
2021-05-06 08:10:38 -04:00
|
|
|
# EncodingDetector checks the first 1024 * 1024 bytes for NUL byte, libgit2 checks
|
|
|
|
# only the first 8000 (https://github.com/libgit2/libgit2/blob/2ed855a9e8f9af211e7274021c2264e600c0f86b/src/filter.h#L15),
|
|
|
|
# which is what we use below to keep a consistent behavior.
|
|
|
|
def detect_libgit2_binary?(data, cache_key: nil)
|
|
|
|
detect = detect_encoding(data, limit: 8000, cache_key: cache_key)
|
2017-09-05 13:16:08 -04:00
|
|
|
detect && detect[:type] == :binary
|
2017-09-03 07:45:44 -04:00
|
|
|
end
|
|
|
|
|
2020-03-09 11:07:45 -04:00
|
|
|
def encode_utf8(message, replace: "")
|
2018-01-04 17:27:37 -05:00
|
|
|
message = force_encode_utf8(message)
|
|
|
|
return message if message.valid_encoding?
|
2017-06-14 12:11:03 -04:00
|
|
|
|
2021-05-06 08:10:38 -04:00
|
|
|
detect = detect_encoding(message)
|
|
|
|
|
2017-06-06 12:40:07 -04:00
|
|
|
if detect && detect[:encoding]
|
2017-06-01 17:21:14 -04:00
|
|
|
begin
|
|
|
|
CharlockHolmes::Converter.convert(message, detect[:encoding], 'UTF-8')
|
|
|
|
rescue ArgumentError => e
|
2020-09-09 20:08:32 -04:00
|
|
|
Gitlab::AppLogger.warn("Ignoring error converting #{detect[:encoding]} into UTF8: #{e.message}")
|
2017-06-01 17:21:14 -04:00
|
|
|
|
|
|
|
''
|
|
|
|
end
|
|
|
|
else
|
2020-03-09 11:07:45 -04:00
|
|
|
clean(message, replace: replace)
|
2017-06-01 17:21:14 -04:00
|
|
|
end
|
2018-01-04 17:27:37 -05:00
|
|
|
rescue ArgumentError
|
2018-07-02 06:43:06 -04:00
|
|
|
nil
|
2017-06-01 17:21:14 -04:00
|
|
|
end
|
2017-09-04 15:32:57 -04:00
|
|
|
|
2018-07-04 10:02:01 -04:00
|
|
|
def encode_binary(str)
|
|
|
|
return "" if str.nil?
|
2017-12-26 13:53:31 -05:00
|
|
|
|
2018-07-04 10:02:01 -04:00
|
|
|
str.dup.force_encoding(Encoding::ASCII_8BIT)
|
2017-12-26 13:53:31 -05:00
|
|
|
end
|
|
|
|
|
2019-03-28 15:05:27 -04:00
|
|
|
def binary_io(str_or_io)
|
|
|
|
io = str_or_io.to_io.dup if str_or_io.respond_to?(:to_io)
|
|
|
|
io ||= StringIO.new(str_or_io.to_s.freeze)
|
|
|
|
|
|
|
|
io.tap { |io| io.set_encoding(Encoding::ASCII_8BIT) }
|
2017-12-26 13:53:31 -05:00
|
|
|
end
|
|
|
|
|
2017-06-01 17:21:14 -04:00
|
|
|
private
|
|
|
|
|
2018-01-04 17:27:37 -05:00
|
|
|
def force_encode_utf8(message)
|
|
|
|
raise ArgumentError unless message.respond_to?(:force_encoding)
|
|
|
|
return message if message.encoding == Encoding::UTF_8 && message.valid_encoding?
|
|
|
|
|
|
|
|
message = message.dup if message.respond_to?(:frozen?) && message.frozen?
|
|
|
|
|
|
|
|
message.force_encoding("UTF-8")
|
|
|
|
end
|
|
|
|
|
2020-03-09 11:07:45 -04:00
|
|
|
def clean(message, replace: "")
|
|
|
|
message.encode(
|
|
|
|
"UTF-16BE",
|
|
|
|
undef: :replace,
|
|
|
|
invalid: :replace,
|
|
|
|
replace: replace.encode("UTF-16BE")
|
|
|
|
)
|
2017-06-01 17:21:14 -04:00
|
|
|
.encode("UTF-8")
|
|
|
|
.gsub("\0".encode("UTF-8"), "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|