Skip to content

Commit

Permalink
Correctly validate the URL suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 21, 2020
1 parent 1f70bdb commit ded7d5c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core-bundle/src/Routing/Candidates/PageCandidates.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private function addRegexQuery(QueryBuilder $queryBuilder, string $pathInfo): bo
}

preg_match_all(
'#^(/'.implode('|/', array_map('preg_quote', $this->urlPrefixes)).')'.implode('|', $paths).'('.implode('|', array_map('preg_quote', $this->urlSuffixes)).')?'.'$#sD',
'#^(/'.implode('|/', array_map('preg_quote', $this->urlPrefixes)).')('.implode('|', $paths).')('.implode('|', array_map('preg_quote', $this->urlSuffixes)).')'.'$#sD',
$pathInfo,
$matches
);
Expand Down
36 changes: 35 additions & 1 deletion core-bundle/tests/Routing/Candidates/CandidatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public function testIncluesPageWithAbsolutePath(): void
->expects($this->once())
->method('getPathRegex')
->willReturn([
'foo' => '#^/bar/[a-z]+\.html$#sD',
'foo' => '#^/bar/[a-z]+$#sD',
'bar' => '#^/bar$#sD',
])
;
Expand All @@ -394,6 +394,40 @@ public function testIncluesPageWithAbsolutePath(): void
$this->assertSame(['bar/baz', 'bar', 15], $candidates->getCandidates($request));
}

public function testIncluesPageWithAbsolutePathAndSuffix(): void
{
$request = $this->mockRequest('/foo/bar/baz.html');

$queryBuilder = $this->createMock(QueryBuilder::class);
$queryBuilder
->expects($this->once())
->method('orWhere')
->with('type IN (:types)')
->willReturnSelf()
;

$queryBuilder
->expects($this->once())
->method('setParameter')
->with('types', ['foo', 'bar'], Connection::PARAM_STR_ARRAY)
->willReturnSelf()
;

$pageRegistry = $this->mockPageRegistry(['foo', 'bar'], ['.html', '']);
$pageRegistry
->expects($this->once())
->method('getPathRegex')
->willReturn([
'foo' => '#^/bar/[a-z]+\.html$#sD',
'bar' => '#^/bar$#sD',
])
;

$candidates = new PageCandidates($this->mockConnection($queryBuilder), $pageRegistry);

$this->assertSame(['bar/baz', 'bar', 'bar/baz.html', 15], $candidates->getCandidates($request));
}

/**
* @return Request&MockObject
*/
Expand Down

0 comments on commit ded7d5c

Please sign in to comment.