Skip to content

Commit

Permalink
Replace %2F in args with /
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jul 4, 2024
1 parent 5655ecb commit 20e2cd0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function handleRequest(Request $request): Response

$method = $request->getMethod();

$path = \str_ireplace('%2F', '%252F', $request->getUri()->getPath());
$path = \str_ireplace('%2F', '%252F', $request->getUri()->getPath(), $replaceCount);
$path = \rawurldecode($path);

$toMatch = "{$method}\0{$path}";
Expand All @@ -92,6 +92,11 @@ public function handleRequest(Request $request): Response
* @var array<string, string> $routeArgs
*/
[, $requestHandler, $routeArgs] = $match;

if ($replaceCount > 0) {
$routeArgs = \array_map(fn (string $arg) => \str_replace('%2F', '/', $arg), $routeArgs);
}

$request->setAttribute(self::class, $routeArgs);

return $requestHandler->handleRequest($request);
Expand Down
9 changes: 6 additions & 3 deletions test/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,19 @@ public function testMerge(): void

public function testPathIsMatchedDecoded(): void
{
$requestHandler = new ClosureRequestHandler(function () {
$requestHandler = new ClosureRequestHandler(function (Request $request) {
$routeArgs = $request->getAttribute(Router::class);
self::assertSame(['p1' => 'ba/Ω', 'p2' => '/?'], $routeArgs);

return new Response(HttpStatus::OK);
});

$router = new Router($this->server, $this->testLogger, $this->errorHandler);
$router->addRoute("GET", "/fo+%2Fö bar", $requestHandler);
$router->addRoute("GET", "/fo+%2Fö bar/{p1}/{p2}", $requestHandler);

$this->server->start($router, $this->errorHandler);

$uri = "/fo+%2F" . \rawurlencode("ö ") . 'bar';
$uri = "/fo+" . \rawurlencode("/ö ") . 'bar/ba' . \rawurlencode('') . '/' . \rawurlencode('/?');

$request = new Request($this->createMock(Client::class), "GET", Uri\Http::createFromString($uri));
$response = $router->handleRequest($request);
Expand Down

0 comments on commit 20e2cd0

Please sign in to comment.