-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Bundler/InsecureProtocolSource
cop (#4720)
- Loading branch information
Showing
7 changed files
with
160 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,64 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Bundler | ||
# The symbol argument `:gemcutter`, `:rubygems` and `:rubyforge` | ||
# are deprecated. So please change your source to URL string that | ||
# 'https://rubygems.org' if possible, or 'http://rubygems.org' if not. | ||
# | ||
# This autocorrect will replace these symbols with 'https://rubygems.org'. | ||
# Because it is secure, HTTPS request is strongly recommended. And in | ||
# most use cases HTTPS will be fine. | ||
# | ||
# However, it don't replace all `sources` of `http://` with `https://`. | ||
# For example, when specifying an internal gem server using HTTP on the | ||
# intranet, a use case where HTTPS can not be specified was considered. | ||
# Consider using HTTP only if you can not use HTTPS. | ||
# | ||
# @example | ||
# # bad | ||
# source :gemcutter | ||
# source :rubygems | ||
# source :rubyforge | ||
# | ||
# # good | ||
# source 'https://rubygems.org' # strongly recommended | ||
# source 'http://rubygems.org' | ||
class InsecureProtocolSource < Cop | ||
MSG = 'The source `:%s` is deprecated because HTTP requests are ' \ | ||
"insecure. Please change your source to 'https://rubygems.org' " \ | ||
"if possible, or 'http://rubygems.org' if not.".freeze | ||
|
||
def_node_matcher :insecure_protocol_source?, <<-PATTERN | ||
(send nil :source | ||
(sym ${:gemcutter :rubygems :rubyforge})) | ||
PATTERN | ||
|
||
def on_send(node) | ||
insecure_protocol_source?(node) do |source| | ||
message = format(MSG, source) | ||
|
||
add_offense( | ||
node, source_range(node.first_argument.loc.expression), message | ||
) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
corrector.replace( | ||
node.first_argument.loc.expression, "'https://rubygems.org'" | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
def source_range(node) | ||
range_between(node.begin_pos, node.end_pos) | ||
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
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,45 @@ | ||
# frozen_string_literal: true | ||
|
||
describe RuboCop::Cop::Bundler::InsecureProtocolSource do | ||
let(:config) { RuboCop::Config.new } | ||
subject(:cop) { described_class.new(config) } | ||
|
||
it 'registers an offense when using `source :gemcutter`' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
source :gemcutter | ||
^^^^^^^^^^ The source `:gemcutter` is deprecated because HTTP requests are insecure. Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `source :rubygems`' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
source :rubygems | ||
^^^^^^^^^ The source `:rubygems` is deprecated because HTTP requests are insecure. Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `source :rubyforge`' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
source :rubyforge | ||
^^^^^^^^^^ The source `:rubyforge` is deprecated because HTTP requests are insecure. Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not. | ||
RUBY | ||
end | ||
|
||
it 'autocorrects `source :gemcutter`' do | ||
new_source = autocorrect_source('source :gemcutter') | ||
|
||
expect(new_source).to eq "source 'https://rubygems.org'" | ||
end | ||
|
||
it 'autocorrects `source :rubygems`' do | ||
new_source = autocorrect_source('source :rubygems') | ||
|
||
expect(new_source).to eq "source 'https://rubygems.org'" | ||
end | ||
|
||
it 'autocorrects `source :rubyforge`' do | ||
new_source = autocorrect_source('source :rubyforge') | ||
|
||
expect(new_source).to eq "source 'https://rubygems.org'" | ||
end | ||
end |