class RuboCop::Cop::Style::IpAddresses

This cop checks for hardcoded IP addresses, which can make code brittle. IP addresses are likely to need to be changed when code is deployed to a different server or environment, which may break a deployment if forgotten. Prefer setting IP addresses in ENV or other configuration.

@example

# bad
ip_address = '127.59.241.29'

# good
ip_address = ENV['DEPLOYMENT_IP_ADDRESS']

Constants

IPV6_MAX_SIZE
MSG

Public Instance Methods

correct_style_detected() click to toggle source

Dummy implementation of method in ConfigurableEnforcedStyle that is called from StringHelp.

# File lib/rubocop/cop/style/ip_addresses.rb, line 46
def correct_style_detected; end
offense?(node) click to toggle source
# File lib/rubocop/cop/style/ip_addresses.rb, line 27
def offense?(node)
  contents = node.source[1...-1]
  return false if contents.empty?

  return false if whitelist.include?(contents.downcase)

  # To try to avoid doing two regex checks on every string,
  # shortcut out if the string does not look like an IP address
  return false unless could_be_ip?(contents)

  contents =~ ::Resolv::IPv4::Regex || contents =~ ::Resolv::IPv6::Regex
end
opposite_style_detected() click to toggle source

Dummy implementation of method in ConfigurableEnforcedStyle that is called from StringHelp.

# File lib/rubocop/cop/style/ip_addresses.rb, line 42
def opposite_style_detected; end

Private Instance Methods

could_be_ip?(str) click to toggle source
# File lib/rubocop/cop/style/ip_addresses.rb, line 55
def could_be_ip?(str)
  # If the string is too long, it can't be an IP
  return false if too_long?(str)

  # If the string doesn't start with a colon or hexadecimal char,
  # we know it's not an IP address
  starts_with_hex_or_colon?(str)
end
starts_with_hex_or_colon?(str) click to toggle source
# File lib/rubocop/cop/style/ip_addresses.rb, line 68
def starts_with_hex_or_colon?(str)
  first_char = str[0].ord
  (48..58).cover?(first_char) || (65..70).cover?(first_char) ||
    (97..102).cover?(first_char)
end
too_long?(str) click to toggle source
# File lib/rubocop/cop/style/ip_addresses.rb, line 64
def too_long?(str)
  str.size > IPV6_MAX_SIZE
end
whitelist() click to toggle source
# File lib/rubocop/cop/style/ip_addresses.rb, line 50
def whitelist
  whitelist = cop_config['Whitelist']
  Array(whitelist).map(&:downcase)
end