From 66491238e3a143f00291927dd64d03a5331ef30c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20Se=CC=81bastien?= Date: Tue, 13 Apr 2021 12:53:23 -0700 Subject: [PATCH] Fix issue with RequestMatcher when attribute is a closure --- .../Component/HttpFoundation/RequestMatcher.php | 6 +++++- .../HttpFoundation/Tests/RequestMatcherTest.php | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/RequestMatcher.php b/src/Symfony/Component/HttpFoundation/RequestMatcher.php index 9a4a2a1376..82be2d8681 100644 --- a/src/Symfony/Component/HttpFoundation/RequestMatcher.php +++ b/src/Symfony/Component/HttpFoundation/RequestMatcher.php @@ -167,7 +167,11 @@ class RequestMatcher implements RequestMatcherInterface } foreach ($this->attributes as $key => $pattern) { - if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) { + $requestAttribute = $request->attributes->get($key); + if (!\is_string($requestAttribute)) { + return false; + } + if (!preg_match('{'.$pattern.'}', $requestAttribute)) { return false; } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php index 57e9c3d30f..e913a60c84 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php @@ -163,4 +163,17 @@ class RequestMatcherTest extends TestCase $matcher->matchAttribute('foo', 'babar'); $this->assertFalse($matcher->matches($request)); } + + public function testAttributesWithClosure() + { + $matcher = new RequestMatcher(); + + $request = Request::create('/admin/foo'); + $request->attributes->set('_controller', function () { + return new Response('foo'); + }); + + $matcher->matchAttribute('_controller', 'babar'); + $this->assertFalse($matcher->matches($request)); + } }