2025-02-01 23:54:35 +00:00
|
|
|
# typed: strict
|
2021-04-14 16:08:37 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Homebrew
|
2024-04-26 20:55:51 +02:00
|
|
|
# This cop restricts usage of `IO.read` functions for security reasons.
|
2021-04-14 16:08:37 +01:00
|
|
|
class IORead < Base
|
|
|
|
MSG = "The use of `IO.%<method>s` is a security risk."
|
2024-04-26 20:55:51 +02:00
|
|
|
|
2021-04-14 16:08:37 +01:00
|
|
|
RESTRICT_ON_SEND = [:read, :readlines].freeze
|
|
|
|
|
2025-02-01 23:54:35 +00:00
|
|
|
sig { params(node: RuboCop::AST::SendNode).void }
|
2021-04-14 16:08:37 +01:00
|
|
|
def on_send(node)
|
|
|
|
return if node.receiver != s(:const, nil, :IO)
|
|
|
|
return if safe?(node.arguments.first)
|
|
|
|
|
|
|
|
add_offense(node, message: format(MSG, method: node.method_name))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2025-02-01 23:54:35 +00:00
|
|
|
sig { params(node: RuboCop::AST::Node).returns(T::Boolean) }
|
2021-04-14 16:08:37 +01:00
|
|
|
def safe?(node)
|
|
|
|
if node.str_type?
|
|
|
|
!node.str_content.empty? && !node.str_content.start_with?("|")
|
2025-02-01 23:54:35 +00:00
|
|
|
elsif node.dstr_type? || (node.send_type? && T.cast(node, RuboCop::AST::SendNode).method?(:+))
|
2021-04-14 16:08:37 +01:00
|
|
|
safe?(node.children.first)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|