2020-10-10 14:16:11 +02:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-26 19:41:14 +01:00
|
|
|
require "rubocops/extend/formula"
|
2017-06-01 00:57:24 +05:30
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module FormulaAudit
|
2020-08-26 02:22:23 +02:00
|
|
|
# This cop makes sure that deprecated checksums are not used.
|
|
|
|
#
|
|
|
|
# @api private
|
2017-06-01 00:57:24 +05:30
|
|
|
class Checksum < FormulaCop
|
|
|
|
def audit_formula(_node, _class_node, _parent_class_node, body_node)
|
2017-06-14 15:37:37 +05:30
|
|
|
return if body_node.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2019-04-08 12:47:15 -04:00
|
|
|
problem "MD5 checksums are deprecated, please use SHA-256" if method_called_ever?(body_node, :md5)
|
2017-06-01 00:57:24 +05:30
|
|
|
|
2019-04-08 12:47:15 -04:00
|
|
|
problem "SHA1 checksums are deprecated, please use SHA-256" if method_called_ever?(body_node, :sha1)
|
2017-06-14 15:37:37 +05:30
|
|
|
|
|
|
|
sha256_calls = find_every_method_call_by_name(body_node, :sha256)
|
|
|
|
sha256_calls.each do |sha256_call|
|
|
|
|
sha256_node = get_checksum_node(sha256_call)
|
|
|
|
audit_sha256(sha256_node)
|
2017-06-01 00:57:24 +05:30
|
|
|
end
|
2017-06-14 15:37:37 +05:30
|
|
|
end
|
2017-06-01 00:57:24 +05:30
|
|
|
|
2017-06-14 15:37:37 +05:30
|
|
|
def audit_sha256(checksum)
|
|
|
|
return if checksum.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-06-14 15:37:37 +05:30
|
|
|
if regex_match_group(checksum, /^$/)
|
2017-10-21 03:12:50 +02:00
|
|
|
@offense_source_range = @offensive_node.source_range
|
2017-06-14 15:37:37 +05:30
|
|
|
problem "sha256 is empty"
|
2017-06-01 00:57:24 +05:30
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if string_content(checksum).size != 64 && regex_match_group(checksum, /^\w*$/)
|
2017-06-14 15:37:37 +05:30
|
|
|
problem "sha256 should be 64 characters"
|
2017-06-01 00:57:24 +05:30
|
|
|
end
|
|
|
|
|
2017-06-16 19:44:14 +05:30
|
|
|
return unless regex_match_group(checksum, /[^a-f0-9]+/i)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-06-16 19:44:14 +05:30
|
|
|
problem "sha256 contains invalid characters"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# This cop makes sure that checksum strings are lowercase.
|
2020-08-26 02:22:23 +02:00
|
|
|
#
|
|
|
|
# @api private
|
2017-06-16 19:44:14 +05:30
|
|
|
class ChecksumCase < FormulaCop
|
|
|
|
def audit_formula(_node, _class_node, _parent_class_node, body_node)
|
|
|
|
return if body_node.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-06-16 19:44:14 +05:30
|
|
|
sha256_calls = find_every_method_call_by_name(body_node, :sha256)
|
|
|
|
sha256_calls.each do |sha256_call|
|
|
|
|
checksum = get_checksum_node(sha256_call)
|
|
|
|
next if checksum.nil?
|
|
|
|
next unless regex_match_group(checksum, /[A-F]+/)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-06-16 19:44:14 +05:30
|
|
|
problem "sha256 should be lowercase"
|
2017-06-01 00:57:24 +05:30
|
|
|
end
|
2017-06-16 19:44:14 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def autocorrect(node)
|
|
|
|
lambda do |corrector|
|
|
|
|
correction = node.source.downcase
|
|
|
|
corrector.insert_before(node.source_range, correction)
|
|
|
|
corrector.remove(node.source_range)
|
|
|
|
end
|
2017-06-01 00:57:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|