vendredi 27 novembre 2020

Match request URL params with rails routes via recognize_path

I want to check if any incoming request actually exists in routes.rb as an entry via recognize_path like this

def path_exists?(path)
  Rails.application.routes.recognize_path(path)
  true
  rescue
   false
end

This works for most URLs but if the URL has a parameter, then it fails to recognize it.

Example:

In routes.rb, say I have an entry like this

put "foo/bar/:param" => "foo#bar"

and the incoming request URL is of the form /foo/bar/5

Then the function recognize_path doesn't recognize the URL as the value of the parameter is 5 but it is matched with :param

How do I match requests that have parameters to their entries in routes.rb using recognize_path or any other similar function?

Reason

I'm filtering out malicious attempts to hit the server using random URLs like /dump.sql.tar.gz, /conf.xz, etc using the gem rack::attack and throttling requests whose URLs are not a part of routes.rb. Hence the above problem.

Aucun commentaire:

Enregistrer un commentaire