Skip to content

Commit 7f8e44f

Browse files
Merge branch '4.1' into 4.2
* 4.1: [Routing] dont redirect routes with greedy trailing vars with no explicit slash skip native serialize among child and parent serializable objects [Routing] backport tests from 4.1 [MonologBridge] Remove unused local variable Remove unreachable code Add PackageNameTest to ConfigurationTest also add in the changelog the corresponding entry to this PR Support use of hyphen in asset package name Remove gendered pronouns Replace gender by eye color in tests [Security] dont do nested calls to serialize()
2 parents c6a8f6c + da122c1 commit 7f8e44f

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

Matcher/Dumper/PhpMatcherTrait.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,16 @@ private function doMatch(string $pathinfo, array &$allow = [], array &$allowSche
130130
continue;
131131
}
132132

133-
if ($trimmedPathinfo === $pathinfo || !$hasTrailingVar) {
134-
// no-op
135-
} elseif (preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
136-
$matches = $n;
137-
} else {
138-
$hasTrailingSlash = true;
139-
}
140-
141-
if ('/' !== $pathinfo && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
133+
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && $hasTrailingVar;
134+
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
142135
if ($supportsRedirections && (!$requiredMethods || isset($requiredMethods['GET']))) {
143136
return $allow = $allowSchemes = [];
144137
}
145-
if ($trimmedPathinfo === $pathinfo || !$hasTrailingVar) {
146-
continue;
147-
}
138+
continue;
139+
}
140+
141+
if ($hasTrailingSlash && $hasTrailingVar && preg_match($regex, $this->matchHost ? $host.'.'.$trimmedPathinfo : $trimmedPathinfo, $n) && $m === (int) $n['MARK']) {
142+
$matches = $n;
148143
}
149144

150145
foreach ($vars as $i => $v) {

Matcher/UrlMatcher.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,18 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
156156
continue;
157157
}
158158

159-
if ($trimmedPathinfo === $pathinfo || !$hasTrailingVar = preg_match('#\{\w+\}/?$#', $route->getPath())) {
160-
// no-op
161-
} elseif (preg_match($regex, $trimmedPathinfo, $m)) {
162-
$matches = $m;
163-
} else {
164-
$hasTrailingSlash = true;
165-
}
159+
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\{\w+\}/?$#', $route->getPath());
166160

167-
if ('/' !== $pathinfo && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
161+
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
168162
if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods))) {
169163
return $this->allow = $this->allowSchemes = [];
170164
}
171-
if ($trimmedPathinfo === $pathinfo || !$hasTrailingVar) {
172-
continue;
173-
}
165+
166+
continue;
167+
}
168+
169+
if ($hasTrailingSlash && $hasTrailingVar && preg_match($regex, $trimmedPathinfo, $m)) {
170+
$matches = $m;
174171
}
175172

176173
$hostMatches = [];

Tests/Matcher/UrlMatcherTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,12 +711,12 @@ public function testSlashWithVerb()
711711
$this->assertSame(['_route' => 'b'], $matcher->match('/bar/'));
712712

713713
$coll = new RouteCollection();
714-
$coll->add('a', new Route('/dav/{foo<.*>?}', [], [], [], '', [], ['GET', 'OPTIONS']));
714+
$coll->add('a', new Route('/dav/{foo}', [], ['foo' => '.*'], [], '', [], ['GET', 'OPTIONS']));
715715

716716
$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'OPTIONS'));
717717
$expected = [
718718
'_route' => 'a',
719-
'foo' => 'files/bar',
719+
'foo' => 'files/bar/',
720720
];
721721
$this->assertEquals($expected, $matcher->match('/dav/files/bar/'));
722722
}
@@ -746,6 +746,17 @@ public function testSlashAndVerbPrecedence()
746746
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
747747
}
748748

749+
public function testGreedyTrailingRequirement()
750+
{
751+
$coll = new RouteCollection();
752+
$coll->add('a', new Route('/{a}', [], ['a' => '.+']));
753+
754+
$matcher = $this->getUrlMatcher($coll);
755+
756+
$this->assertEquals(['_route' => 'a', 'a' => 'foo'], $matcher->match('/foo'));
757+
$this->assertEquals(['_route' => 'a', 'a' => 'foo/'], $matcher->match('/foo/'));
758+
}
759+
749760
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
750761
{
751762
return new UrlMatcher($routes, $context ?: new RequestContext());

0 commit comments

Comments
 (0)