debian-mirror-gitlab/app/controllers/concerns/internal_redirect.rb

48 lines
1.1 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-10-15 14:42:47 +05:30
module InternalRedirect
extend ActiveSupport::Concern
def safe_redirect_path(path)
return unless path
2019-09-30 21:07:59 +05:30
# Verify that the string starts with a `/` and a known route character.
return unless path =~ %r{^/[-\w].*$}
2018-10-15 14:42:47 +05:30
uri = URI(path)
# Ignore anything path of the redirect except for the path, querystring and,
# fragment, forcing the redirect within the same host.
full_path_for_uri(uri)
rescue URI::InvalidURIError
nil
end
def safe_redirect_path_for_url(url)
return unless url
uri = URI(url)
safe_redirect_path(full_path_for_uri(uri)) if host_allowed?(uri)
rescue URI::InvalidURIError
nil
end
2018-11-08 19:23:39 +05:30
def sanitize_redirect(url_or_path)
safe_redirect_path(url_or_path) || safe_redirect_path_for_url(url_or_path)
end
2018-10-15 14:42:47 +05:30
def host_allowed?(uri)
uri.host == request.host &&
uri.port == request.port
end
def full_path_for_uri(uri)
path_with_query = [uri.path, uri.query].compact.join('?')
[path_with_query, uri.fragment].compact.join("#")
end
2018-12-05 23:21:45 +05:30
def referer_path(request)
return unless request.referer.presence
URI(request.referer).path
end
2018-10-15 14:42:47 +05:30
end