2017-05-22 11:23:04 -05:00
|
|
|
require "socket"
|
|
|
|
require "ipaddr"
|
|
|
|
require 'excon'
|
2017-05-23 14:03:04 -05:00
|
|
|
require 'rate_limiter'
|
2017-05-22 11:23:04 -05:00
|
|
|
|
|
|
|
# Determine the final endpoint for a Web URI, following redirects
|
|
|
|
class FinalDestination
|
|
|
|
|
2017-09-28 01:35:27 -05:00
|
|
|
attr_reader :status, :cookie, :status_code
|
2017-05-22 11:23:04 -05:00
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def initialize(url, opts = nil)
|
2017-07-29 11:42:04 -05:00
|
|
|
@url = url
|
2017-07-17 19:50:06 -05:00
|
|
|
@uri =
|
|
|
|
begin
|
2017-07-29 11:42:04 -05:00
|
|
|
URI(escape_url) if @url
|
2017-07-17 19:50:06 -05:00
|
|
|
rescue URI::InvalidURIError
|
|
|
|
end
|
|
|
|
|
2017-05-22 11:23:04 -05:00
|
|
|
@opts = opts || {}
|
2017-08-08 04:44:27 -05:00
|
|
|
@force_get_hosts = @opts[:force_get_hosts] || []
|
2017-05-22 11:23:04 -05:00
|
|
|
@opts[:max_redirects] ||= 5
|
|
|
|
@opts[:lookup_ip] ||= lambda do |host|
|
|
|
|
begin
|
|
|
|
IPSocket::getaddress(host)
|
|
|
|
rescue SocketError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2017-06-26 14:38:23 -05:00
|
|
|
@ignored = [Discourse.base_url_no_prefix] + (@opts[:ignore_redirects] || [])
|
2017-05-22 11:23:04 -05:00
|
|
|
@limit = @opts[:max_redirects]
|
|
|
|
@status = :ready
|
2017-08-08 04:44:27 -05:00
|
|
|
@http_verb = @force_get_hosts.any? { |host| hostname_matches?(host) } ? :get : :head
|
2017-06-06 12:53:49 -05:00
|
|
|
@cookie = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.connection_timeout
|
|
|
|
20
|
2017-05-22 11:23:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def redirected?
|
|
|
|
@limit < @opts[:max_redirects]
|
|
|
|
end
|
|
|
|
|
|
|
|
def request_headers
|
2017-06-06 12:53:49 -05:00
|
|
|
result = {
|
|
|
|
"User-Agent" => "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
|
2017-05-22 11:23:04 -05:00
|
|
|
"Accept" => "text/html",
|
2017-06-06 12:53:49 -05:00
|
|
|
"Host" => @uri.hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
result['cookie'] = @cookie if @cookie
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def small_get(headers)
|
|
|
|
Net::HTTP.start(@uri.host, @uri.port, use_ssl: @uri.is_a?(URI::HTTPS)) do |http|
|
|
|
|
http.open_timeout = FinalDestination.connection_timeout
|
|
|
|
http.read_timeout = FinalDestination.connection_timeout
|
|
|
|
|
|
|
|
request = Net::HTTP::Get.new(@uri.request_uri, headers)
|
|
|
|
http.request(request) do |response|
|
|
|
|
return response
|
|
|
|
end
|
|
|
|
end
|
2017-05-22 11:23:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def resolve
|
|
|
|
if @limit < 0
|
|
|
|
@status = :too_many_redirects
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2017-06-26 14:38:23 -05:00
|
|
|
@ignored.each do |host|
|
|
|
|
if hostname_matches?(host)
|
|
|
|
@status = :resolved
|
|
|
|
return @uri
|
|
|
|
end
|
2017-06-12 12:22:29 -05:00
|
|
|
end
|
|
|
|
|
2017-05-22 11:23:04 -05:00
|
|
|
return nil unless validate_uri
|
|
|
|
headers = request_headers
|
2017-08-08 04:44:27 -05:00
|
|
|
response = Excon.public_send(@http_verb,
|
2017-06-06 12:53:49 -05:00
|
|
|
@uri.to_s,
|
|
|
|
read_timeout: FinalDestination.connection_timeout,
|
|
|
|
headers: headers
|
|
|
|
)
|
|
|
|
|
|
|
|
location = nil
|
2017-09-28 01:35:27 -05:00
|
|
|
headers = nil
|
|
|
|
|
|
|
|
response_status = response.status.to_i
|
|
|
|
|
2017-06-06 12:53:49 -05:00
|
|
|
case response.status
|
|
|
|
when 200
|
2017-05-22 11:23:04 -05:00
|
|
|
@status = :resolved
|
|
|
|
return @uri
|
2017-06-15 14:09:59 -05:00
|
|
|
when 405, 409, 501
|
2017-06-06 12:53:49 -05:00
|
|
|
get_response = small_get(headers)
|
|
|
|
|
2017-09-28 01:35:27 -05:00
|
|
|
response_status = get_response.code.to_i
|
|
|
|
if response_status == 200
|
2017-06-06 12:53:49 -05:00
|
|
|
@status = :resolved
|
|
|
|
return @uri
|
|
|
|
end
|
|
|
|
|
2017-09-28 01:35:27 -05:00
|
|
|
headers = {}
|
2017-06-06 12:53:49 -05:00
|
|
|
if cookie_val = get_response.get_fields('set-cookie')
|
2017-09-28 01:35:27 -05:00
|
|
|
headers['set-cookie'] = cookie_val.join
|
2017-06-06 12:53:49 -05:00
|
|
|
end
|
|
|
|
|
2017-09-28 01:35:27 -05:00
|
|
|
# TODO this is confusing why grap location for anything not
|
|
|
|
# between 300-400 ?
|
2017-06-06 12:53:49 -05:00
|
|
|
if location_val = get_response.get_fields('location')
|
2017-09-28 01:35:27 -05:00
|
|
|
headers['location'] = location_val.join
|
2017-06-06 12:53:49 -05:00
|
|
|
end
|
2017-09-28 01:35:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
unless headers
|
|
|
|
headers = {}
|
2017-06-06 12:53:49 -05:00
|
|
|
response.headers.each do |k, v|
|
2017-09-28 01:35:27 -05:00
|
|
|
headers[k.to_s.downcase] = v
|
2017-06-06 12:53:49 -05:00
|
|
|
end
|
2017-05-22 11:23:04 -05:00
|
|
|
end
|
|
|
|
|
2017-09-28 01:35:27 -05:00
|
|
|
if (300..399).include?(response_status)
|
|
|
|
location = headers["location"]
|
|
|
|
end
|
|
|
|
|
|
|
|
if set_cookie = headers["set-cookie"]
|
|
|
|
@cookie = set_cookie
|
|
|
|
end
|
|
|
|
|
2017-05-22 11:23:04 -05:00
|
|
|
if location
|
|
|
|
location = "#{@uri.scheme}://#{@uri.host}#{location}" if location[0] == "/"
|
|
|
|
@uri = URI(location) rescue nil
|
|
|
|
@limit -= 1
|
|
|
|
return resolve
|
|
|
|
end
|
|
|
|
|
2017-09-28 01:35:27 -05:00
|
|
|
# this is weird an exception seems better
|
|
|
|
@status = :failure
|
|
|
|
@status_code = response.status
|
|
|
|
|
2017-09-27 01:52:49 -05:00
|
|
|
nil
|
|
|
|
rescue Excon::Errors::Timeout
|
2017-05-22 11:23:04 -05:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_uri
|
2017-05-23 12:31:20 -05:00
|
|
|
validate_uri_format && is_dest_valid?
|
2017-05-22 11:23:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_uri_format
|
|
|
|
return false unless @uri
|
|
|
|
return false unless ['https', 'http'].include?(@uri.scheme)
|
2017-05-23 12:07:18 -05:00
|
|
|
return false if @uri.scheme == 'http' && @uri.port != 80
|
|
|
|
return false if @uri.scheme == 'https' && @uri.port != 443
|
2017-05-22 11:23:04 -05:00
|
|
|
|
2017-05-23 12:07:18 -05:00
|
|
|
# Disallow IP based crawling
|
|
|
|
(IPAddr.new(@uri.hostname) rescue nil).nil?
|
2017-05-22 11:23:04 -05:00
|
|
|
end
|
|
|
|
|
2017-06-12 12:22:29 -05:00
|
|
|
def hostname_matches?(url)
|
|
|
|
@uri && url.present? && @uri.hostname == (URI(url) rescue nil)&.hostname
|
|
|
|
end
|
2017-05-23 15:32:54 -05:00
|
|
|
|
2017-06-12 12:22:29 -05:00
|
|
|
def is_dest_valid?
|
2017-05-23 15:32:54 -05:00
|
|
|
|
2017-05-22 11:23:04 -05:00
|
|
|
return false unless @uri && @uri.host
|
|
|
|
|
2017-06-12 12:22:29 -05:00
|
|
|
# Whitelisted hosts
|
|
|
|
return true if hostname_matches?(SiteSetting.s3_cdn_url) ||
|
|
|
|
hostname_matches?(GlobalSetting.try(:cdn_url)) ||
|
|
|
|
hostname_matches?(Discourse.base_url_no_prefix)
|
|
|
|
|
2017-06-13 11:59:54 -05:00
|
|
|
if SiteSetting.whitelist_internal_hosts.present?
|
|
|
|
SiteSetting.whitelist_internal_hosts.split('|').each do |h|
|
|
|
|
return true if @uri.hostname.downcase == h.downcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-22 11:23:04 -05:00
|
|
|
address_s = @opts[:lookup_ip].call(@uri.hostname)
|
|
|
|
return false unless address_s
|
|
|
|
|
|
|
|
address = IPAddr.new(address_s)
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
if private_ranges.any? { |r| r === address }
|
2017-05-22 11:23:04 -05:00
|
|
|
@status = :invalid_address
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2017-05-23 14:03:04 -05:00
|
|
|
# Rate limit how often this IP can be crawled
|
2017-05-24 12:46:57 -05:00
|
|
|
unless @opts[:skip_rate_limit]
|
|
|
|
RateLimiter.new(nil, "crawl-destination-ip:#{address_s}", 100, 1.hour).performed!
|
|
|
|
end
|
2017-05-23 14:03:04 -05:00
|
|
|
|
2017-05-22 11:23:04 -05:00
|
|
|
true
|
2017-05-23 14:03:04 -05:00
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
false
|
2017-05-22 11:23:04 -05:00
|
|
|
end
|
|
|
|
|
2017-07-29 11:42:04 -05:00
|
|
|
def escape_url
|
2017-09-26 05:34:54 -05:00
|
|
|
TopicEmbed.escape_uri(
|
|
|
|
CGI.unescapeHTML(@url),
|
|
|
|
Regexp.new("[^#{URI::PATTERN::UNRESERVED}#{URI::PATTERN::RESERVED}#]")
|
|
|
|
)
|
2017-07-29 11:42:04 -05:00
|
|
|
end
|
|
|
|
|
2017-05-23 10:51:23 -05:00
|
|
|
def private_ranges
|
|
|
|
FinalDestination.standard_private_ranges +
|
2017-07-27 20:20:09 -05:00
|
|
|
SiteSetting.blacklist_ip_blocks.split('|').map { |r| IPAddr.new(r) rescue nil }.compact
|
2017-05-23 10:51:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.standard_private_ranges
|
2017-05-22 11:23:04 -05:00
|
|
|
@private_ranges ||= [
|
|
|
|
IPAddr.new('127.0.0.1'),
|
|
|
|
IPAddr.new('172.16.0.0/12'),
|
|
|
|
IPAddr.new('192.168.0.0/16'),
|
|
|
|
IPAddr.new('10.0.0.0/8'),
|
|
|
|
IPAddr.new('fc00::/7')
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.lookup_ip(host)
|
|
|
|
IPSocket::getaddress(host)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|