2018-12-04 06:55:49 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Checks
|
|
|
|
class DiffCheck < BaseChecker
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
LOG_MESSAGES = {
|
|
|
|
validate_file_paths: "Validating diffs' file paths...",
|
|
|
|
diff_content_check: "Validating diff contents..."
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def validate!
|
2018-12-20 11:45:06 -05:00
|
|
|
return if deletion?
|
2018-12-04 06:55:49 -05:00
|
|
|
return unless should_run_diff_validations?
|
|
|
|
return if commits.empty?
|
|
|
|
|
|
|
|
file_paths = []
|
|
|
|
|
2020-12-11 13:09:57 -05:00
|
|
|
if ::Feature.enabled?(:diff_check_with_paths_changed_rpc, project, default_enabled: true)
|
2020-12-04 01:09:34 -05:00
|
|
|
paths = project.repository.find_changed_paths(commits.map(&:sha))
|
|
|
|
paths.each do |path|
|
|
|
|
file_paths.concat([path.path])
|
2018-12-28 05:44:25 -05:00
|
|
|
|
2020-12-04 01:09:34 -05:00
|
|
|
validate_diff(path)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
process_commits do |commit|
|
|
|
|
validate_once(commit) do
|
|
|
|
commit.raw_deltas.each do |diff|
|
|
|
|
file_paths.concat([diff.new_path, diff.old_path].compact)
|
|
|
|
|
|
|
|
validate_diff(diff)
|
|
|
|
end
|
2018-12-28 05:44:25 -05:00
|
|
|
end
|
|
|
|
end
|
2018-12-04 06:55:49 -05:00
|
|
|
end
|
|
|
|
|
2020-12-04 01:09:34 -05:00
|
|
|
validate_file_paths(file_paths.uniq)
|
2018-12-04 06:55:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def validate_lfs_file_locks?
|
|
|
|
strong_memoize(:validate_lfs_file_locks) do
|
|
|
|
project.lfs_enabled? && project.any_lfs_file_locks?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-28 05:44:25 -05:00
|
|
|
def should_run_diff_validations?
|
2018-12-04 06:55:49 -05:00
|
|
|
validations_for_diff.present? || path_validations.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_diff(diff)
|
|
|
|
validations_for_diff.each do |validation|
|
|
|
|
if error = validation.call(diff)
|
2020-02-26 13:09:24 -05:00
|
|
|
raise ::Gitlab::GitAccess::ForbiddenError, error
|
2018-12-04 06:55:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Method overwritten in EE to inject custom validations
|
|
|
|
def validations_for_diff
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
def path_validations
|
|
|
|
validate_lfs_file_locks? ? [lfs_file_locks_validation] : []
|
|
|
|
end
|
|
|
|
|
2018-12-28 05:44:25 -05:00
|
|
|
def process_commits
|
2018-12-04 06:55:49 -05:00
|
|
|
logger.log_timed(LOG_MESSAGES[:diff_content_check]) do
|
2019-09-18 10:02:45 -04:00
|
|
|
# n+1: https://gitlab.com/gitlab-org/gitlab/issues/3593
|
2018-12-04 06:55:49 -05:00
|
|
|
::Gitlab::GitalyClient.allow_n_plus_1_calls do
|
|
|
|
commits.each do |commit|
|
|
|
|
logger.check_timeout_reached
|
|
|
|
|
2018-12-28 05:44:25 -05:00
|
|
|
yield(commit)
|
2018-12-04 06:55:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_file_paths(file_paths)
|
|
|
|
logger.log_timed(LOG_MESSAGES[__method__]) do
|
|
|
|
path_validations.each do |validation|
|
|
|
|
if error = validation.call(file_paths)
|
2020-02-26 13:09:24 -05:00
|
|
|
raise ::Gitlab::GitAccess::ForbiddenError, error
|
2018-12-04 06:55:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def lfs_file_locks_validation
|
|
|
|
lambda do |paths|
|
|
|
|
lfs_lock = project.lfs_file_locks.where(path: paths).where.not(user_id: user_access.user.id).take
|
|
|
|
|
|
|
|
if lfs_lock
|
|
|
|
return "The path '#{lfs_lock.path}' is locked in Git LFS by #{lfs_lock.user.name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-28 08:09:44 -04:00
|
|
|
|
|
|
|
Gitlab::Checks::DiffCheck.prepend_if_ee('EE::Gitlab::Checks::DiffCheck')
|