|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RuboCop |
| 4 | + module Cop |
| 5 | + module Bundler |
| 6 | + # The symbol argument `:gemcutter`, `:rubygems` and `:rubyforge` |
| 7 | + # are deprecated. So please change your source to URL string that |
| 8 | + # 'https://rubygems.org' if possible, or 'http://rubygems.org' if not. |
| 9 | + # |
| 10 | + # This autocorrect will replace these symbols with 'https://rubygems.org'. |
| 11 | + # Because it is secure, HTTPS request is strongly recommended. And in |
| 12 | + # most use cases HTTPS will be fine. |
| 13 | + # |
| 14 | + # However, it don't replace all `sources` of `http://` with `https://`. |
| 15 | + # For example, when specifying an internal gem server using HTTP on the |
| 16 | + # intranet, a use case where HTTPS can not be specified was considered. |
| 17 | + # Consider using HTTP only if you can not use HTTPS. |
| 18 | + # |
| 19 | + # @example |
| 20 | + # # bad |
| 21 | + # source :gemcutter |
| 22 | + # source :rubygems |
| 23 | + # source :rubyforge |
| 24 | + # |
| 25 | + # # good |
| 26 | + # source 'https://rubygems.org' # strongly recommended |
| 27 | + # source 'http://rubygems.org' |
| 28 | + class InsecureProtocolSource < Cop |
| 29 | + MSG = 'The source `:%s` is deprecated because HTTP requests are ' \ |
| 30 | + "insecure. Please change your source to 'https://rubygems.org' " \ |
| 31 | + "if possible, or 'http://rubygems.org' if not.".freeze |
| 32 | + |
| 33 | + def_node_matcher :insecure_protocol_source?, <<-PATTERN |
| 34 | + (send nil :source |
| 35 | + (sym ${:gemcutter :rubygems :rubyforge})) |
| 36 | + PATTERN |
| 37 | + |
| 38 | + def on_send(node) |
| 39 | + insecure_protocol_source?(node) do |source| |
| 40 | + message = format(MSG, source) |
| 41 | + |
| 42 | + add_offense( |
| 43 | + node, source_range(node.first_argument.loc.expression), message |
| 44 | + ) |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + def autocorrect(node) |
| 49 | + lambda do |corrector| |
| 50 | + corrector.replace( |
| 51 | + node.first_argument.loc.expression, "'https://rubygems.org'" |
| 52 | + ) |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + private |
| 57 | + |
| 58 | + def source_range(node) |
| 59 | + range_between(node.begin_pos, node.end_pos) |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | +end |
0 commit comments