Skip to content
代码片段 群组 项目
未验证 提交 8071be73 编辑于 作者: Igor Drozdov's avatar Igor Drozdov 提交者: GitLab
浏览文件

Merge branch...

Merge branch '473837-invalid-unicode-500-error-when-pushing-an-image-binary-with-gitguardian-enabled' into 'master' 

Check if params data cannot be JSONified

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/160353



Merged-by: default avatarIgor Drozdov <idrozdov@gitlab.com>
Approved-by: default avatarIgor Drozdov <idrozdov@gitlab.com>
Reviewed-by: default avatarVasilii Iakliushin <viakliushin@gitlab.com>
Reviewed-by: default avatarAsh McKenzie <amckenzie@gitlab.com>
Co-authored-by: default avatarAsh McKenzie <amckenzie@gitlab.com>
No related branches found
No related tags found
无相关合并请求
...@@ -36,26 +36,36 @@ def execute(blobs = []) ...@@ -36,26 +36,36 @@ def execute(blobs = [])
def execute_batched_request(blobs_batch) def execute_batched_request(blobs_batch)
Thread.new do Thread.new do
params = blobs_batch.map do |blob| params = blobs_batch.each_with_object([]) do |blob, all|
blob_params = { document: blob.data }
# GitGuardian limits filename field to 256 characters. # GitGuardian limits filename field to 256 characters.
# That is why we only pass file name, which is sufficient for Git Guardian to perform its checks. # That is why we only pass file name, which is sufficient for Git Guardian to perform its checks.
# See: https://api.gitguardian.com/docs#operation/multiple_scan # See: https://api.gitguardian.com/docs#operation/multiple_scan
if blob.path.present? if blob.path.present?
filename = File.basename(blob.path) filename = File.basename(blob.path)
limited_filename = limit_filename(filename) limited_filename = limit_filename(filename)
end
blob_params[:filename] = limited_filename unless can_be_jsonified?(blob.data)
Gitlab::AppJsonLogger.warn(class: self.class.name,
message: "Not processing data with filename '#{limited_filename}' as it cannot be JSONified")
next
end end
blob_params blob_params = { document: blob.data }
blob_params[:filename] = limited_filename if limited_filename
all << blob_params
end end
response = perform_request(params) if params.empty?
policy_breaks = process_response(response, blobs_batch) Gitlab::AppJsonLogger.warn(class: self.class.name, message: "Nothing to process")
nil
else
response = perform_request(params)
policy_breaks = process_response(response, blobs_batch)
policy_breaks.presence policy_breaks.presence
end
end end
end end
...@@ -70,6 +80,13 @@ def limit_filename(filename) ...@@ -70,6 +80,13 @@ def limit_filename(filename)
filename[over_limit..filename_size] filename[over_limit..filename_size]
end end
def can_be_jsonified?(data)
data.to_json
true
rescue JSON::GeneratorError
false
end
def perform_request(params) def perform_request(params)
options = { options = {
headers: headers, headers: headers,
......
...@@ -328,4 +328,26 @@ ...@@ -328,4 +328,26 @@
end end
end end
end end
context 'with a blob containing binary data' do
let(:filename) { 'rails_sample.jpg' }
let(:blobs) do
[
fake_blob(
path: filename,
data: File.read(File.join('spec', 'fixtures', filename)),
binary: true
)
]
end
it 'warns and does not call GitGuardian API' do
expect(::Gitlab::AppJsonLogger).to receive(:warn).with(class: described_class.name,
message: "Not processing data with filename '#{filename}' as it cannot be JSONified")
expect(::Gitlab::AppJsonLogger).to receive(:warn).with(class: described_class.name,
message: "Nothing to process")
expect(client.execute(blobs)).to eq([])
end
end
end end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册