diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-11-22 17:35:49 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-11-22 17:35:49 +0300 |
commit | 777b6713bb473d2e09c8340ab9a96373fdbaae50 (patch) | |
tree | 786e78f65c73daef684150844a9aa6054dfdb2d3 /lib/gitlab/utils.rb | |
parent | 8a235c0c05efec1c8ee14c7454982dc2b8ca9464 (diff) |
Ensure that db encryption keys have proper bytesize
Diffstat (limited to 'lib/gitlab/utils.rb')
-rw-r--r-- | lib/gitlab/utils.rb | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/gitlab/utils.rb b/lib/gitlab/utils.rb index 9e59137a2c0..ad2efa6b4e1 100644 --- a/lib/gitlab/utils.rb +++ b/lib/gitlab/utils.rb @@ -16,6 +16,20 @@ module Gitlab str.force_encoding(Encoding::UTF_8) end + def ensure_utf8_size(str, bytes:) + raise ArgumentError if str.empty? || bytes.negative? + + truncated = str.each_char.each_with_object(String.new) do |char, object| + if object.bytesize + char.bytesize > bytes + break object + else + object.concat(char) + end + end + + truncated + ("\0" * (bytes - truncated.bytesize)) + end + # Append path to host, making sure there's one single / in between def append_path(host, path) "#{host.to_s.sub(%r{\/+$}, '')}/#{path.to_s.sub(%r{^\/+}, '')}" |