Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Lint/DeprecatedOpenSSLConstant
cop
- Loading branch information
Showing
7 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# 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) | ||
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) | ||
begin_pos = node.loc.selector.column | ||
end_pos = node.loc.expression.last_column | ||
range_between(begin_pos, 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 { |arg| arg.source.tr(":'", '').split('-') } | ||
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.split('-') | ||
size_and_mode = sanitize_arguments(node.arguments) | ||
"'#{(algorithm_parts + size_and_mode + ['CBC']).take(3).join('-')}'" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
spec/rubocop/cop/lint/deprecated_open_ssl_constant_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::DeprecatedOpenSSLConstant do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
it 'registers an offense with cipher constant and two arguments and corrects' do | ||
expect_offense(<<~RUBY) | ||
OpenSSL::Cipher::AES.new(128, :GCM) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-GCM')` instead of `OpenSSL::Cipher::AES.new(128, :GCM)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
OpenSSL::Cipher.new('AES-128-GCM') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense with cipher constant and one argument and corrects' do | ||
expect_offense(<<~RUBY) | ||
OpenSSL::Cipher::AES.new('128-GCM') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-GCM')` instead of `OpenSSL::Cipher::AES.new('128-GCM')`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
OpenSSL::Cipher.new('AES-128-GCM') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense with AES + blocksize constant and mode argument and corrects' do | ||
expect_offense(<<~RUBY) | ||
OpenSSL::Cipher::AES128.new(:GCM) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-GCM')` instead of `OpenSSL::Cipher::AES128.new(:GCM)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
OpenSSL::Cipher.new('AES-128-GCM') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense with AES + blocksize constant and corrects' do | ||
expect_offense(<<~RUBY) | ||
OpenSSL::Cipher::AES128.new | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-CBC')` instead of `OpenSSL::Cipher::AES128.new`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
OpenSSL::Cipher.new('AES-128-CBC') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using cipher with a string' do | ||
expect_no_offenses(<<~RUBY) | ||
OpenSSL::Cipher.new('AES-128-GCM') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when building an instance using an digest constant and corrects' do | ||
expect_offense(<<~RUBY) | ||
OpenSSL::Digest::SHA256.new | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Digest.new('SHA256')` instead of `OpenSSL::Digest::SHA256.new`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
OpenSSL::Digest.new('SHA256') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using ::Digest class methods on an algorithm constant and corrects' do | ||
expect_offense(<<~RUBY) | ||
OpenSSL::Digest::SHA256.digest('foo') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Digest.digest('SHA256', 'foo')` instead of `OpenSSL::Digest::SHA256.digest('foo')`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
OpenSSL::Digest.digest('SHA256', 'foo') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using an digest constant with chained methods and corrects' do | ||
expect_offense(<<~RUBY) | ||
OpenSSL::Digest::SHA256.new.digest('foo') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Digest.new('SHA256')` instead of `OpenSSL::Digest::SHA256.new`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
OpenSSL::Digest.new('SHA256').digest('foo') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when building digest using an algorithm string' do | ||
expect_no_offenses(<<~RUBY) | ||
OpenSSL::Digest.new('SHA256') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using ::Digest class methods with an algorithm string and value' do | ||
expect_no_offenses(<<~RUBY) | ||
OpenSSL::Digest.digest('SHA256', 'foo') | ||
RUBY | ||
end | ||
end |