From b63ae11a7be8e4fdf6ab108013d8ad7f8195d3de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Corr=C3=AAa=20de=20Oliveira?= Date: Thu, 2 Oct 2025 00:49:20 +0100 Subject: [PATCH] Catch URI parse errors during routing (Copy from #114 vendethiel++) --- Changes | 1 + lib/Cro/HTTP/Router.rakumod | 6 ++++++ t/http-router.rakutest | 6 ++++++ 3 files changed, 13 insertions(+) diff --git a/Changes b/Changes index a4cd806..c86b074 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,7 @@ Revision history for Cro::HTTP {{NEXT}} - Support link generation - Make http function accept a list of http methods + - Catch URI parse errors during routing 0.8.11 - Avoid sending a 0-byte WINDOW_UPDATE frame. diff --git a/lib/Cro/HTTP/Router.rakumod b/lib/Cro/HTTP/Router.rakumod index 5f90e96..e3fe857 100644 --- a/lib/Cro/HTTP/Router.rakumod +++ b/lib/Cro/HTTP/Router.rakumod @@ -322,6 +322,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(:400status, :$request); + next; + } + } $request.path ~~ $!path-matcher } with $routing-outcome { diff --git a/t/http-router.rakutest b/t/http-router.rakutest index 08a6a26..49d0f00 100644 --- a/t/http-router.rakutest +++ b/t/http-router.rakutest @@ -19,6 +19,12 @@ sub body-text(Cro::HTTP::Response $r) { ok $r ~~ Cro::HTTP::Response, 'Empty route set gives a response'; is $r.status, '404', 'Status code from empty route set 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, '400', 'Status code uri is invalid is 400'; + } } throws-like { request }, X::Cro::HTTP::Router::OnlyInHandler, what => 'request',