Omitting filters fix for lowercase methods requests (#647)

This commit is contained in:
Serdar Dogruyol - Sedo セド 2022-09-15 11:52:28 +03:00 committed by GitHub
parent 93521b7120
commit c8f857dff3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 8 deletions

37
spec/filters_spec.cr Normal file
View file

@ -0,0 +1,37 @@
require "./spec_helper"
describe "Kemal::FilterHandler" do
it "handles with upcased 'POST'" do
filter_handler = Kemal::FilterHandler.new
filter_handler._add_route_filter("POST", "*", :before) do |env|
env.set "sensitive", "1"
end
Kemal.config.add_filter_handler(filter_handler)
post "/sensitive_post" do |env|
env.get "sensitive"
end
request = HTTP::Request.new("POST", "/sensitive_post")
client_response = call_request_on_app(request)
client_response.status_code.should eq(200)
client_response.body.should eq("1")
end
it "handles with downcased 'post'" do
filter_handler = Kemal::FilterHandler.new
filter_handler._add_route_filter("POST", "*", :before) do |env|
env.set "sensitive", "1"
end
Kemal.config.add_filter_handler(filter_handler)
post "/sensitive_post" do
"sensitive"
end
request = HTTP::Request.new("post", "/sensitive_post")
client_response = call_request_on_app(request)
client_response.status_code.should eq(200)
client_response.body.should eq("")
end
end

View file

@ -85,6 +85,7 @@ end
Spec.after_each do Spec.after_each do
Kemal.config.clear Kemal.config.clear
Kemal::FilterHandler::INSTANCE.tree = Radix::Tree(Array(Kemal::FilterHandler::FilterBlock)).new
Kemal::RouteHandler::INSTANCE.routes = Radix::Tree(Route).new Kemal::RouteHandler::INSTANCE.routes = Radix::Tree(Route).new
Kemal::RouteHandler::INSTANCE.cached_routes = Hash(String, Radix::Result(Route)).new Kemal::RouteHandler::INSTANCE.cached_routes = Hash(String, Radix::Result(Route)).new
Kemal::WebSocketHandler::INSTANCE.routes = Radix::Tree(WebSocket).new Kemal::WebSocketHandler::INSTANCE.routes = Radix::Tree(WebSocket).new

View file

@ -3,6 +3,7 @@ module Kemal
class FilterHandler class FilterHandler
include HTTP::Handler include HTTP::Handler
INSTANCE = new INSTANCE = new
property tree
# This middleware is lazily instantiated and added to the handlers as soon as a call to `after_X` or `before_X` is made. # This middleware is lazily instantiated and added to the handlers as soon as a call to `after_X` or `before_X` is made.
def initialize def initialize

View file

@ -11,19 +11,17 @@ module Kemal
macro only(paths, method = "GET") macro only(paths, method = "GET")
class_name = {{@type.name}} class_name = {{@type.name}}
method_downcase = {{method.downcase}} class_name_method = "#{class_name}/#{{{method}}}"
class_name_method = "#{class_name}/#{method_downcase}"
({{paths}}).each do |path| ({{paths}}).each do |path|
@@only_routes_tree.add class_name_method + path, '/' + method_downcase + path @@only_routes_tree.add class_name_method + path, '/' + {{method}} + path
end end
end end
macro exclude(paths, method = "GET") macro exclude(paths, method = "GET")
class_name = {{@type.name}} class_name = {{@type.name}}
method_downcase = {{method.downcase}} class_name_method = "#{class_name}/#{{{method}}}"
class_name_method = "#{class_name}/#{method_downcase}"
({{paths}}).each do |path| ({{paths}}).each do |path|
@@exclude_routes_tree.add class_name_method + path, '/' + method_downcase + path @@exclude_routes_tree.add class_name_method + path, '/' + {{method}} + path
end end
end end
@ -74,7 +72,7 @@ module Kemal
end end
private def radix_path(method : String, path : String) private def radix_path(method : String, path : String)
"#{self.class}/#{method.downcase}#{path}" "#{self.class}/#{method}#{path}"
end end
end end
end end

View file

@ -57,7 +57,7 @@ module Kemal
end end
private def radix_path(method, path) private def radix_path(method, path)
'/' + method.downcase + path '/' + method + path
end end
private def add_to_radix_tree(method, path, route) private def add_to_radix_tree(method, path, route)