diff options
author | Stan Hu <stanhu@gmail.com> | 2019-03-14 12:40:10 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2019-03-14 12:55:45 -0700 |
commit | 6552197eecfd355041ecb99f48a48efb93c5ffab (patch) | |
tree | cbeeec8e63d377f0a8c5f675fb347a1e10501e2d /app | |
parent | 1715622c054434abc752c7be11ec4daec9231d5a (diff) | |
download | gitlab-ce-6552197eecfd355041ecb99f48a48efb93c5ffab.tar.gz |
Fix error creating a merge request when diff includes a null byte
If a diff happened to include a single null byte anywhere, insertion
into the database would fail with an Error 500 since the column is text
and not a byte array. To fix this, we mark the diff as binary if we
detect a single null byte and Base64-encode it.
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/57710
Diffstat (limited to 'app')
-rw-r--r-- | app/models/merge_request_diff.rb | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/app/models/merge_request_diff.rb b/app/models/merge_request_diff.rb index e53e2c8fc43..98db1bf7de7 100644 --- a/app/models/merge_request_diff.rb +++ b/app/models/merge_request_diff.rb @@ -296,6 +296,11 @@ class MergeRequestDiff < ActiveRecord::Base private + def encode_in_base64?(diff_text) + (diff_text.encoding == Encoding::BINARY && !diff_text.ascii_only?) || + diff_text.include?("\0") + end + def create_merge_request_diff_files(diffs) rows = if has_attribute?(:external_diff) && Gitlab.config.external_diffs.enabled @@ -348,7 +353,7 @@ class MergeRequestDiff < ActiveRecord::Base diff_hash.tap do |hash| diff_text = hash[:diff] - if diff_text.encoding == Encoding::BINARY && !diff_text.ascii_only? + if encode_in_base64?(diff_text) hash[:binary] = true hash[:diff] = [diff_text].pack('m0') end |