8000 bug #40807 RequestMatcher issue when `_controller` is a closure (Plopix) · wouterj/symfony@d5e8d6e · GitHub
[go: up one dir, main page]

Skip to content

Commit d5e8d6e

Browse files
committed
bug symfony#40807 RequestMatcher issue when _controller is a closure (Plopix)
This PR was merged into the 4.4 branch. Discussion ---------- RequestMatcher issue when `_controller` is a closure | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT ## Description If the `matches` method of `RequestMatcher` is used on an attribute which is a closure it crashes. ## How did we get it On a project that is using FOS HTTP Cache, we have this configuration ```yaml fos_http_cache: cache_control: rules: - match: attributes: { _controller: ^App\\Controller\\.*::.* } headers: overwrite: true cache_control: { public: true, private: false, must_revalidate: true, s_maxage: 3600 } ``` Everything works fine unless you are reaching a controller that is a closure. You get a ```TypeError: preg_match(): Argument #2 ($subject) must be of type string, Closure given``` which is to me logical. ## Proposed solution Just testing the type of attribute value and return false before crashing `preg_match` This PR adds a quick unit test to enforce this. Commits ------- 6649123 Fix issue with RequestMatcher when attribute is a closure
2 parents 236e61b + 6649123 commit d5e8d6e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Symfony/Component/HttpFoundation/RequestMatcher.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ public function matches(Request $request)
167167
}
168168

169169
foreach ($this->attributes as $key => $pattern) {
170-
if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
170+
$requestAttribute = $request->attributes->get($key);
171+
if (!\is_string($requestAttribute)) {
172+
return false;
173+
}
174+
if (!preg_match('{'.$pattern.'}', $requestAttribute)) {
171175
return false;
172176
}
173177
}

src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,17 @@ public function testAttributes()
163163
$matcher->matchAttribute('foo', 'babar');
164164
$this->assertFalse($matcher->matches($request));
165165
}
166+
167+
public function testAttributesWithClosure()
168+
{
169+
$matcher = new RequestMatcher();
170+
171+
$request = Request::create('/admin/foo');
172+
$request->attributes->set('_controller', function () {
173+
return new Response('foo');
174+
});
175+
176+
$matcher->matchAttribute('_controller', 'babar');
177+
$this->assertFalse($matcher->matches($request));
178+
}
166179
}

0 commit comments

Comments
 (0)
0