Skip to content

Instantly share code, notes, and snippets.

@IFilonov
Last active July 26, 2023 05:15
Show Gist options
  • Select an option

  • Save IFilonov/7d817a0aa1f1d0a8755135aacf01f72c to your computer and use it in GitHub Desktop.

Select an option

Save IFilonov/7d817a0aa1f1d0a8755135aacf01f72c to your computer and use it in GitHub Desktop.
Scan routes rake
# frozen_string_literal: true
namespace :routes do
desc 'Поиск неприкрытых авторизацией роутов'
task list_all_routes: :environment do
controllers_actions = []
controllers_actions << { controller: 'Контроллер',
action: 'Метод',
verb: 'Глагол',
path: 'Путь',
error: 'Ошибка',
status: 'Статус',
location: 'Редирект'
}
Rails.application.routes.routes.each do |route|
new_route = ActionDispatch::Routing::RouteWrapper.new route
next if new_route.internal?
next unless new_route.action
next if new_route.controller.camelize.match?('ActiveStorage')
controllers_actions << { controller: "#{new_route.controller}_controller".camelize,
action: new_route.action,
verb: new_route.verb,
path: new_route.path}
end
controllers_actions.each do |ca|
next if ca[:controller].match?('Контроллер')
begin
controller_class = ca[:controller].constantize
rescue NameError => e
ca[:error] = "Отсутствует контроллер! NameError! #{e.backtrace.first}"
next
end
resp = controller_class.action(ca[:action]).call("REQUEST_METHOD" => ca[:verb],
"SERVER_NAME"=>"example.org",
"SERVER_PORT"=>"80",
"HTTPS"=>"off",
"HTTP_HOST"=>"test.host",
"REMOTE_ADDR"=>"0.0.0.0",
"HTTP_COOKIE"=>"",
#"PATH_INFO"=>"/api/admin/orders",
"HTTP_USER_AGENT"=>"Rails Testing",
"QUERY_STRING"=>"",
'ORIGINAL_URL' => 'http://www.example.com',
"rack.url_scheme"=>"http",
"rack.input" => -> {},
"action_dispatch.request.parameters" => { "id" => 1,
"reseller_id" => 1,
"format" => "json" }
)
ca[:status] = resp.first
ca[:location] = resp.second['Location']
rescue AbstractController::ActionNotFound => e
error = "Отсутствует метод в контроллере! AbstractController::ActionNotFound! #{e.backtrace.first}"
ca[:error] = error
rescue NoMethodError => e
error = "NoMethodError! #{e.backtrace.first}"
ca[:error] = error
ca[:status] = 200
rescue ActionView::Template::Error => e
error = "ActionView::Template::Error! #{e.backtrace.first}"
ca[:error] = error
ca[:status] = 200
rescue ActionController::ParameterMissing => e
error = "ActionController::ParameterMissing! #{e.backtrace.first}"
ca[:error] = error
ca[:status] = 200
rescue ActiveRecord::RecordNotFound => e
error = "ActiveRecord::RecordNotFound! #{e.backtrace.first}"
ca[:error] = error
ca[:status] = 200
rescue ActionController::UnknownFormat => e
error = "ActionController::UnknownFormat! #{e.backtrace.first}"
ca[:error] = error
ca[:status] = 200
end
file_name = "routes_cas_#{Time.now.strftime('%Y%m%d_%H%M')}.csv"
File.open(file_name, 'w', invalid: :replace, undef: :replace) do |file|
controllers_actions.each do |ca|
values = [ca[:controller], ca[:verb], ca[:action], ca[:path], ca[:status], ca[:location], ca[:error]]
row = CSV.generate(col_sep: ';') { |csv| csv << values }
file.write(row)
end
end
end
end
@BubuntuClu
Copy link

тут параметры в URL'e не надо передавать. нужно правильно передать параметры после Content-length

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment