Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocorrect of Lint/DeprecatedOpenSSLConstant breaks code #8012

Closed
geniou opened this issue May 22, 2020 · 3 comments · Fixed by #8014
Closed

Autocorrect of Lint/DeprecatedOpenSSLConstant breaks code #8012

geniou opened this issue May 22, 2020 · 3 comments · Fixed by #8014
Labels

Comments

@geniou
Copy link
Contributor

geniou commented May 22, 2020

When running rubocop -a --only=Lint/DeprecatedOpenSSLConstant its breaking the code.

Output:

config/initializers/xing_paperclip_setup.rb:2:15: E: Lint/Syntax: unexpected token tIDENTIFIER
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
  anew('SHA1')chment.instance.class.to_s.split('::').last.underscore
              ^^^^^^
config/initializers/xing_paperclip_setup.rb:3:1: E: Lint/Syntax: unexpected token kEND
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
end
^^^
config/initializers/xing_paperclip_setup.rb:15:36: W: [Corrected] Lint/DeprecatedOpenSSLConstant: Use OpenSSL::Digest.new('SHA1') instead of OpenSSL::Digest::SHA1.new.
    hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, Rails.application.secrets.secret_key_base, data)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^
spec/config/initializers/xing_paperclip_setup_spec.rb:1:50: E: Lint/Syntax: unexpected token tIDENTIFIER
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
describe Paperclip::Interpolations do new('SHA1')ubocop:disable RSpec/FilePath
                                                 ^^^^^^
spec/config/initializers/xing_paperclip_setup_spec.rb:26:17: W: [Corrected] Lint/DeprecatedOpenSSLConstant: Use OpenSSL::Digest.new('SHA1') instead of OpenSSL::Digest::SHA1.new.
          .with(OpenSSL::Digest::SHA1.new, Rails.application.secrets.secret_key_base, data)
                ^^^^^^^^^^^^^^^^^^^^^^^^^
spec/config/initializers/xing_paperclip_setup_spec.rb:33:1: E: Lint/Syntax: unexpected token kEND
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
end

Broken Changes:

-  attachment.instance.class.to_s.split('::').last.underscore
+  anew('SHA1')chment.instance.class.to_s.split('::').last.underscore

- describe Paperclip::Interpolations do # rubocop:disable RSpec/FilePath
+ describe Paperclip::Interpolations do new('SHA1')ubocop:disable RSpec/FilePath

Expected behavior

Should only correct change, not other places.

Actual behavior

Correcting changes, but also breaking code.

Steps to reproduce the problem

rubocop -a --only=Lint/DeprecatedOpenSSLConstant

RuboCop version

0.84.0 (using Parser 2.7.1.2, rubocop-ast 0.0.3, running on ruby 2.7.1 x86_64-darwin19)

@koic
Copy link
Member

koic commented May 22, 2020

Can you show me the original code before auto-correction?

@geniou
Copy link
Contributor Author

geniou commented May 22, 2020

Sure

Paperclip.interpolates :class do |attachment, _style|
  attachment.instance.class.to_s.split('::').last.underscore
end

# The security team complained, that the URL of (private) events would be
# guessable. To make this harder be added a hash to the path. In order to not
# migrate all existing files we need to distinguish between old and new
# images/documents. Adding a flag would be the more elegant way, but we have
# many models with assets so the more simple solution is the time bomb.
Paperclip.interpolates :xing_id_partition do |attachment, style|
  id = attachment.instance.id || 0
  if attachment.instance.updated_at > Time.zone.parse('2019-08-23 00:00')
    # inspired by https://github.com/thoughtbot/paperclip/blob/90f9121a18df25bc3ef948356450ce19fbe1da83/lib/paperclip/attachment.rb#L325
    data = %i[class id].map { |method| attachment.instance.send(method) }.push(style).join('/')
    hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, Rails.application.secrets.secret_key_base, data)
  end
  format("%03x", id)[-3..].split('').push(id).push(hash).compact.join('/')
end

@koic koic added the bug label May 22, 2020
koic added a commit to koic/rubocop that referenced this issue May 22, 2020
…SSLConstant`

Fixes rubocop#8012.

This PR fixes an incorrect autocorrect for `Lint/DeprecatedOpenSSLConstant`
when deprecated OpenSSL constant is used in a block.
@koic
Copy link
Member

koic commented May 22, 2020

I've opened #8014. Thank you!

bbatsov pushed a commit that referenced this issue May 22, 2020
…tant`

Fixes #8012.

This PR fixes an incorrect autocorrect for `Lint/DeprecatedOpenSSLConstant`
when deprecated OpenSSL constant is used in a block.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants