Skip to content

Commit bf7eee3

Browse files
committed
Updated method spoofing to be applied on request mapping instead
1 parent 0732c4d commit bf7eee3

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

packages/http/src/Mappers/PsrRequestToGenericRequestMapper.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function map(mixed $from, mixed $to): GenericRequest
5252
);
5353

5454
return map([
55-
'method' => Method::from($from->getMethod()),
55+
'method' => $this->requestMethod($from, $data),
5656
'uri' => (string) $from->getUri(),
5757
'raw' => $raw,
5858
'body' => $data,
@@ -70,4 +70,31 @@ public function map(mixed $from, mixed $to): GenericRequest
7070
])
7171
->to(GenericRequest::class);
7272
}
73+
74+
private function requestMethod(mixed $from, array $data): Method
75+
{
76+
$originalMethod = Method::from($from->getMethod());
77+
if ($originalMethod !== Method::POST) {
78+
return $originalMethod;
79+
}
80+
81+
if (! isset($data['_method'])) {
82+
return $originalMethod;
83+
}
84+
85+
$spoofedMethod = Method::tryFrom(strtoupper($data['_method']));
86+
if ($spoofedMethod === null) {
87+
return $originalMethod;
88+
}
89+
90+
$allowedMethods = [
91+
Method::PUT,
92+
Method::PATCH,
93+
Method::DELETE,
94+
];
95+
96+
return ! in_array($spoofedMethod, $allowedMethods, strict: true)
97+
? $originalMethod
98+
: $spoofedMethod;
99+
}
73100
}

packages/router/tests/MatchRouteMiddlewareTest.php renamed to packages/http/tests/MethodSpoofingTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
declare(strict_types=1);
44

5-
namespace Tempest\Router\Tests;
5+
namespace Tempest\Http\Tests;
66

77
use PHPUnit\Framework\TestCase;
88
use Tempest\Container\Container;
99
use Tempest\Container\GenericContainer;
1010
use Tempest\Http\GenericRequest;
1111
use Tempest\Http\Method;
1212
use Tempest\Http\Request;
13-
use Tempest\Http\Response;
1413
use Tempest\Http\Responses\NotFound;
1514
use Tempest\Http\Responses\Ok;
1615
use Tempest\Router\HttpMiddlewareCallable;
@@ -19,7 +18,7 @@
1918
use Tempest\Router\RouteConfig;
2019
use Tempest\Router\Routing\Matching\GenericRouteMatcher;
2120

22-
final class MatchRouteMiddlewareTest extends TestCase
21+
final class MethodSpoofingTest extends TestCase
2322
{
2423
private Container $container;
2524
private RouteConfig $routeConfig;

packages/router/src/MatchRouteMiddleware.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public function __construct(
2424

2525
public function __invoke(Request $request, HttpMiddlewareCallable $next): Response
2626
{
27-
$request = $this->applyMethodSpoofing($request);
28-
2927
$matchedRoute = $this->routeMatcher->match($request);
3028

3129
if ($matchedRoute === null && $request->method === Method::HEAD && $request instanceof GenericRequest) {
@@ -72,37 +70,4 @@ private function resolveRequest(Request $request, MatchedRoute $matchedRoute): R
7270

7371
return $request;
7472
}
75-
76-
private function applyMethodSpoofing(Request $request): Request
77-
{
78-
if ($request->method !== Method::POST) {
79-
return $request;
80-
}
81-
82-
if (! ($request instanceof GenericRequest)) {
83-
return $request;
84-
}
85-
86-
if (! $request->hasBody('_method')) {
87-
return $request;
88-
}
89-
90-
$spoofedMethod = Method::tryFrom(strtoupper((string) $request->get('_method')));
91-
92-
if ($spoofedMethod === null) {
93-
return $request;
94-
}
95-
96-
$allowedMethods = [
97-
Method::PUT,
98-
Method::PATCH,
99-
Method::DELETE,
100-
];
101-
102-
if (! in_array($spoofedMethod, $allowedMethods, strict: true)) {
103-
return $request;
104-
}
105-
106-
return $request->withMethod($spoofedMethod);
107-
}
10873
}

0 commit comments

Comments
 (0)