From 6f0f0ea347a6b62d1cec1ff6147943367c99be5c Mon Sep 17 00:00:00 2001 From: vendethiel Date: Mon, 5 Oct 2020 15:27:19 +0200 Subject: [PATCH 1/2] Catch URI parse errors during routing Fixes #99 --- lib/Cro/HTTP/Router.pm6 | 6 ++++++ t/http-router.t | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/lib/Cro/HTTP/Router.pm6 b/lib/Cro/HTTP/Router.pm6 index f66dda7b..1653e9bb 100644 --- a/lib/Cro/HTTP/Router.pm6 +++ b/lib/Cro/HTTP/Router.pm6 @@ -231,6 +231,12 @@ module Cro::HTTP::Router { my @*BIND-FAILS; my $log-timeline-task = $request.annotations; my $routing-outcome = Cro::HTTP::LogTimeline::Route.log: $log-timeline-task, -> { + CATCH { + when X::Cro::Uri::ParseError { + emit Cro::HTTP::Response.new(:500status, :$request); + next; + } + } $request.path ~~ $!path-matcher } with $routing-outcome { diff --git a/t/http-router.t b/t/http-router.t index 3020c3f6..41078966 100644 --- a/t/http-router.t +++ b/t/http-router.t @@ -91,6 +91,12 @@ throws-like { bad-request }, X::Cro::HTTP::Router::OnlyInHandler, what => 'bad-r ok $r ~~ Cro::HTTP::Response, 'No matching route gets a HTTP response'; is $r.status, '404', 'Status code when no matching route is 404'; } + + $source.emit(Cro::HTTP::Request.new(:method, :target)); + given $responses.receive -> $r { + ok $r ~~ Cro::HTTP::Response, 'No matching route gets a HTTP response'; + is $r.status, '500', 'Status code uri is invalid is 500'; + } } { From 63ba5120cc318e44e3f1769c18baf7269f0874be Mon Sep 17 00:00:00 2001 From: vendethiel Date: Sun, 7 Mar 2021 22:20:57 +0100 Subject: [PATCH 2/2] Emit a 400 request when a client sends an invalid URI --- lib/Cro/HTTP/Router.pm6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cro/HTTP/Router.pm6 b/lib/Cro/HTTP/Router.pm6 index 1653e9bb..958a7937 100644 --- a/lib/Cro/HTTP/Router.pm6 +++ b/lib/Cro/HTTP/Router.pm6 @@ -233,7 +233,7 @@ module Cro::HTTP::Router { my $routing-outcome = Cro::HTTP::LogTimeline::Route.log: $log-timeline-task, -> { CATCH { when X::Cro::Uri::ParseError { - emit Cro::HTTP::Response.new(:500status, :$request); + emit Cro::HTTP::Response.new(:400status, :$request); next; } }