|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Tempest\Integration\Http; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\TestWith; |
| 6 | +use Tempest\Core\AppConfig; |
| 7 | +use Tempest\Core\Environment; |
| 8 | +use Tempest\Http\Cookie\Cookie; |
| 9 | +use Tempest\Http\GenericRequest; |
| 10 | +use Tempest\Http\Method; |
| 11 | +use Tempest\Http\Session\CsrfTokenDidNotMatch; |
| 12 | +use Tempest\Http\Session\Session; |
| 13 | +use Tempest\Http\Session\VerifyCsrfMiddleware; |
| 14 | +use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
| 15 | + |
| 16 | +final class CsrfTest extends FrameworkIntegrationTestCase |
| 17 | +{ |
| 18 | + public function test_csrf_is_sent_as_cookie(): void |
| 19 | + { |
| 20 | + $this->container->get(AppConfig::class)->environment = Environment::PRODUCTION; |
| 21 | + |
| 22 | + $token = $this->container->get(Session::class)->get(Session::CSRF_TOKEN_KEY); |
| 23 | + |
| 24 | + $this->http |
| 25 | + ->get('/test') |
| 26 | + ->assertHasCookie( |
| 27 | + VerifyCsrfMiddleware::CSRF_COOKIE_KEY, |
| 28 | + fn (Cookie $cookie) => $cookie->value === $token, // @mago-expect security/no-insecure-comparison |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + #[TestWith([Method::POST])] |
| 33 | + #[TestWith([Method::PUT])] |
| 34 | + #[TestWith([Method::PATCH])] |
| 35 | + #[TestWith([Method::DELETE])] |
| 36 | + public function test_throws_when_missing_in_write_verbs(Method $method): void |
| 37 | + { |
| 38 | + $this->expectException(CsrfTokenDidNotMatch::class); |
| 39 | + |
| 40 | + $this->container->get(AppConfig::class)->environment = Environment::PRODUCTION; |
| 41 | + $this->http->sendRequest(new GenericRequest($method, uri: '/test')); |
| 42 | + } |
| 43 | + |
| 44 | + #[TestWith([Method::GET])] |
| 45 | + #[TestWith([Method::OPTIONS])] |
| 46 | + #[TestWith([Method::HEAD])] |
| 47 | + public function test_allows_missing_in_read_verbs(Method $method): void |
| 48 | + { |
| 49 | + $this->container->get(AppConfig::class)->environment = Environment::PRODUCTION; |
| 50 | + |
| 51 | + $this->http |
| 52 | + ->sendRequest(new GenericRequest($method, uri: '/test')) |
| 53 | + ->assertOk(); |
| 54 | + } |
| 55 | + |
| 56 | + public function test_throws_when_mismatch_from_body(): void |
| 57 | + { |
| 58 | + $this->expectException(CsrfTokenDidNotMatch::class); |
| 59 | + |
| 60 | + $this->container->get(AppConfig::class)->environment = Environment::PRODUCTION; |
| 61 | + $this->container->get(Session::class)->set(Session::CSRF_TOKEN_KEY, 'abc'); |
| 62 | + |
| 63 | + $this->http->post('/test', [Session::CSRF_TOKEN_KEY => 'def']); |
| 64 | + } |
| 65 | + |
| 66 | + public function test_throws_when_mismatch_from_header(): void |
| 67 | + { |
| 68 | + $this->expectException(CsrfTokenDidNotMatch::class); |
| 69 | + |
| 70 | + $this->container->get(AppConfig::class)->environment = Environment::PRODUCTION; |
| 71 | + $this->container->get(Session::class)->set(Session::CSRF_TOKEN_KEY, 'abc'); |
| 72 | + |
| 73 | + $this->http->post('/test', [Session::CSRF_TOKEN_KEY => 'def']); |
| 74 | + } |
| 75 | + |
| 76 | + public function test_matches_from_body(): void |
| 77 | + { |
| 78 | + $this->container->get(AppConfig::class)->environment = Environment::PRODUCTION; |
| 79 | + |
| 80 | + $session = $this->container->get(Session::class); |
| 81 | + |
| 82 | + $this->http |
| 83 | + ->post('/test', [Session::CSRF_TOKEN_KEY => $session->token]) |
| 84 | + ->assertOk(); |
| 85 | + } |
| 86 | + |
| 87 | + public function test_matches_from_header(): void |
| 88 | + { |
| 89 | + $this->container->get(AppConfig::class)->environment = Environment::PRODUCTION; |
| 90 | + |
| 91 | + $session = $this->container->get(Session::class); |
| 92 | + |
| 93 | + $this->http |
| 94 | + ->post('/test', headers: [VerifyCsrfMiddleware::CSRF_HEADER_KEY => $session->token]) |
| 95 | + ->assertOk(); |
| 96 | + } |
| 97 | + |
| 98 | + public function test_csrf_component(): void |
| 99 | + { |
| 100 | + $rendered = $this->render(<<<HTML |
| 101 | + <x-csrf-token /> |
| 102 | + HTML); |
| 103 | + |
| 104 | + $session = $this->container->get(Session::class); |
| 105 | + |
| 106 | + $this->assertStringMatchesFormat('<input type="hidden" name="_csrf_token" value="%s">', $rendered); |
| 107 | + $this->assertStringContainsString($session->token, $rendered); |
| 108 | + } |
| 109 | +} |
0 commit comments