forked from rubocop/rubocop
/
deprecated_open_ssl_constant.rb
137 lines (117 loc) · 3.69 KB
/
deprecated_open_ssl_constant.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# Algorithmic constants for `OpenSSL::Cipher` and `OpenSSL::Digest`
# deprecated since OpenSSL version 2.2.0. Prefer passing a string
# instead.
#
# @example
#
# # Example for OpenSSL::Cipher instantiation.
#
# # bad
# OpenSSL::Cipher::AES.new(128, :GCM)
#
# # good
# OpenSSL::Cipher.new('aes-128-gcm')
#
# @example
#
# # Example for OpenSSL::Digest instantiation.
#
# # bad
# OpenSSL::Digest::SHA256.new
#
# # good
# OpenSSL::Digest.new('SHA256')
#
# @example
#
# # Example for ::Digest inherited class methods.
#
# # bad
# OpenSSL::Digest::SHA256.digest('foo')
#
# # good
# OpenSSL::Digest.digest('SHA256', 'foo')
#
class DeprecatedOpenSSLConstant < Cop
include RangeHelp
MSG = 'Use `%<constant>s.%<method>s(%<replacement_args>s)`' \
' instead of `%<original>s`.'
def_node_matcher :algorithm_const, <<~PATTERN
(send
$(const
(const
(const {nil? cbase} :OpenSSL) {:Cipher :Digest})
_)
...)
PATTERN
def on_send(node)
return if node.arguments.any? { |arg| arg.variable? || arg.send_type? || arg.const_type? }
add_offense(node) if algorithm_const(node)
end
def autocorrect(node)
algorithm_constant, = algorithm_const(node)
lambda do |corrector|
corrector.remove(algorithm_constant.loc.double_colon)
corrector.remove(algorithm_constant.loc.name)
corrector.replace(
correction_range(node),
"#{node.loc.selector.source}(#{replacement_args(node)})"
)
end
end
private
def message(node)
algorithm_constant, = algorithm_const(node)
parent_constant = openssl_class(algorithm_constant)
replacement_args = replacement_args(node)
method = node.loc.selector.source
format(
MSG,
constant: parent_constant,
method: method,
replacement_args: replacement_args,
original: node.source
)
end
def correction_range(node)
range_between(node.loc.dot.end_pos, node.loc.expression.end_pos)
end
def openssl_class(node)
node.children.first.source
end
def algorithm_name(node)
name = node.loc.name.source
if openssl_class(node) == 'OpenSSL::Cipher'
name.scan(/.{3}/).join('-')
else
name
end
end
def sanitize_arguments(arguments)
arguments.flat_map do |arg|
argument = arg.str_type? ? arg.value : arg.source
argument.tr(":'", '').split('-')
end
end
def replacement_args(node)
algorithm_constant, = algorithm_const(node)
algorithm_name = algorithm_name(algorithm_constant)
if openssl_class(algorithm_constant) == 'OpenSSL::Cipher'
build_cipher_arguments(node, algorithm_name)
else
(["'#{algorithm_name}'"] + node.arguments.map(&:source)).join(', ')
end
end
def build_cipher_arguments(node, algorithm_name)
algorithm_parts = algorithm_name.downcase.split('-')
size_and_mode = sanitize_arguments(node.arguments).map(&:downcase)
"'#{(algorithm_parts + size_and_mode + ['cbc']).take(3).join('-')}'"
end
end
end
end
end