module Rack::AddressMunging::Detection

This module contains email address detection and validation methods. It is meant to be included in any munging strategy.

Constants

REGEXP_EMAIL
REGEXP_MAILTO

Public Instance Methods

email?(string) click to toggle source
# File lib/rack/address_munging/detection.rb, line 15
def email?(string)
  m = ::Mail::Address.new(string)
  return false unless m.address == string
  return false unless valid_local?(m.local)
  return false unless valid_domain?(m.domain)

  true
rescue StandardError
  false
end

Private Instance Methods

hires_image?(domain) click to toggle source

/path/to/image@2x.format should not be matched as a valid email Hashed version of the path shouldn't match either

# File lib/rack/address_munging/detection.rb, line 58
def hires_image?(domain)
  !(domain !~ /^\dx\.(jpe?g|gif|png|webp)$/ && domain !~ /^\dx-[0-9a-f]{32}\.(jpe?g|gif|png|webp)$/)
end
ip_address?(domain) click to toggle source
# File lib/rack/address_munging/detection.rb, line 45
def ip_address?(domain)
  ip = IpAddr.new(domain)
  ip.to_s == domain
rescue StandardError
  false
end
local_domain?(domain) click to toggle source
# File lib/rack/address_munging/detection.rb, line 52
def local_domain?(domain)
  !domain.include?('.') # Must contain at least a .
end
valid_domain?(domain) click to toggle source
# File lib/rack/address_munging/detection.rb, line 36
def valid_domain?(domain)
  return false if ip_address?(domain)
  return false if local_domain?(domain)
  return false if hires_image?(domain)
  return false if domain.include?('..') # Can't contain ..

  true
end
valid_local?(local) click to toggle source
# File lib/rack/address_munging/detection.rb, line 28
def valid_local?(local)
  return false if local.include?('..')      # Can't contain ..
  return false if local.include?('@')       # Can't contain an @
  return false unless local !~ /^"?\s+"?$/  # Can't be blank

  true
end