|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Functional\GraphQl; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use Symfony\Component\Config\Loader\LoaderInterface; |
| 18 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 19 | +use Symfony\Component\HttpKernel\Event\RequestEvent; |
| 20 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 21 | +use Twig\Extension\AbstractExtension; |
| 22 | +use Twig\TwigFunction; |
| 23 | + |
| 24 | +final class GraphiqlCspNonceRequestListener |
| 25 | +{ |
| 26 | + public function __construct(private readonly string $nonce) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + public function __invoke(RequestEvent $event): void |
| 31 | + { |
| 32 | + $event->getRequest()->attributes->set('_csp_nonce', $this->nonce); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +final class GraphiqlCspNonceTwigExtension extends AbstractExtension |
| 37 | +{ |
| 38 | + public function getFunctions(): array |
| 39 | + { |
| 40 | + return [ |
| 41 | + new TwigFunction('csp_nonce', static fn (string $directive = 'script'): string => 'function-nonce-'.$directive), |
| 42 | + ]; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +class GraphiqlCspNonceAppKernel extends \AppKernel |
| 47 | +{ |
| 48 | + public static bool $requestNonceEnabled = false; |
| 49 | + public static bool $cspNonceFunctionEnabled = false; |
| 50 | + |
| 51 | + private function suffix(): string |
| 52 | + { |
| 53 | + return (self::$requestNonceEnabled ? 'req_' : 'no_req_').(self::$cspNonceFunctionEnabled ? 'fn' : 'no_fn'); |
| 54 | + } |
| 55 | + |
| 56 | + public function getCacheDir(): string |
| 57 | + { |
| 58 | + return parent::getCacheDir().'/graphiql_csp_'.$this->suffix(); |
| 59 | + } |
| 60 | + |
| 61 | + public function getLogDir(): string |
| 62 | + { |
| 63 | + return parent::getLogDir().'/graphiql_csp_'.$this->suffix(); |
| 64 | + } |
| 65 | + |
| 66 | + protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void |
| 67 | + { |
| 68 | + parent::configureContainer($c, $loader); |
| 69 | + |
| 70 | + $loader->load(static function (ContainerBuilder $container): void { |
| 71 | + $container->loadFromExtension('api_platform', [ |
| 72 | + 'graphql' => ['enabled' => true, 'graphiql' => ['enabled' => true]], |
| 73 | + ]); |
| 74 | + |
| 75 | + if (GraphiqlCspNonceAppKernel::$requestNonceEnabled) { |
| 76 | + $container->register('test.csp_nonce_listener', GraphiqlCspNonceRequestListener::class) |
| 77 | + ->setArguments(['request-nonce-123']) |
| 78 | + ->setPublic(true) |
| 79 | + ->addTag('kernel.event_listener', ['event' => KernelEvents::REQUEST, 'priority' => 256]); |
| 80 | + } |
| 81 | + |
| 82 | + if (GraphiqlCspNonceAppKernel::$cspNonceFunctionEnabled) { |
| 83 | + $container->register('test.csp_nonce_twig_extension', GraphiqlCspNonceTwigExtension::class) |
| 84 | + ->setPublic(true) |
| 85 | + ->addTag('twig.extension'); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +final class GraphiqlCspNonceTest extends ApiTestCase |
| 92 | +{ |
| 93 | + protected static ?bool $alwaysBootKernel = true; |
| 94 | + |
| 95 | + protected static function getKernelClass(): string |
| 96 | + { |
| 97 | + return GraphiqlCspNonceAppKernel::class; |
| 98 | + } |
| 99 | + |
| 100 | + protected function tearDown(): void |
| 101 | + { |
| 102 | + GraphiqlCspNonceAppKernel::$requestNonceEnabled = false; |
| 103 | + GraphiqlCspNonceAppKernel::$cspNonceFunctionEnabled = false; |
| 104 | + |
| 105 | + parent::tearDown(); |
| 106 | + } |
| 107 | + |
| 108 | + public function testRequestAttributeNonceIsEmittedOnScripts(): void |
| 109 | + { |
| 110 | + GraphiqlCspNonceAppKernel::$requestNonceEnabled = true; |
| 111 | + GraphiqlCspNonceAppKernel::$cspNonceFunctionEnabled = false; |
| 112 | + |
| 113 | + $client = self::createClient(); |
| 114 | + $client->request('GET', '/graphql/graphiql', ['headers' => ['Accept' => 'text/html']]); |
| 115 | + |
| 116 | + $this->assertResponseIsSuccessful(); |
| 117 | + $content = $client->getResponse()->getContent(); |
| 118 | + |
| 119 | + $this->assertStringContainsString('<script type="importmap" nonce="request-nonce-123">', $content); |
| 120 | + $this->assertStringContainsString('<script id="graphiql-data" type="application/json" nonce="request-nonce-123">', $content); |
| 121 | + $this->assertStringContainsString('init-graphiql.js" nonce="request-nonce-123"', $content); |
| 122 | + } |
| 123 | + |
| 124 | + public function testCspNonceFunctionIsEmittedOnScripts(): void |
| 125 | + { |
| 126 | + GraphiqlCspNonceAppKernel::$requestNonceEnabled = false; |
| 127 | + GraphiqlCspNonceAppKernel::$cspNonceFunctionEnabled = true; |
| 128 | + |
| 129 | + $client = self::createClient(); |
| 130 | + $client->request('GET', '/graphql/graphiql', ['headers' => ['Accept' => 'text/html']]); |
| 131 | + |
| 132 | + $this->assertResponseIsSuccessful(); |
| 133 | + $content = $client->getResponse()->getContent(); |
| 134 | + |
| 135 | + $this->assertStringContainsString('<script type="importmap" nonce="function-nonce-script">', $content); |
| 136 | + $this->assertStringContainsString('init-graphiql.js" nonce="function-nonce-script"', $content); |
| 137 | + } |
| 138 | + |
| 139 | + public function testRequestAttributeNonceTakesPrecedenceOverFunction(): void |
| 140 | + { |
| 141 | + GraphiqlCspNonceAppKernel::$requestNonceEnabled = true; |
| 142 | + GraphiqlCspNonceAppKernel::$cspNonceFunctionEnabled = true; |
| 143 | + |
| 144 | + $client = self::createClient(); |
| 145 | + $client->request('GET', '/graphql/graphiql', ['headers' => ['Accept' => 'text/html']]); |
| 146 | + |
| 147 | + $this->assertResponseIsSuccessful(); |
| 148 | + $content = $client->getResponse()->getContent(); |
| 149 | + |
| 150 | + $this->assertStringContainsString('nonce="request-nonce-123"', $content); |
| 151 | + $this->assertStringNotContainsString('nonce="function-nonce-script"', $content); |
| 152 | + } |
| 153 | + |
| 154 | + public function testNoNonceIsEmittedWhenNoMechanismAvailable(): void |
| 155 | + { |
| 156 | + GraphiqlCspNonceAppKernel::$requestNonceEnabled = false; |
| 157 | + GraphiqlCspNonceAppKernel::$cspNonceFunctionEnabled = false; |
| 158 | + |
| 159 | + $client = self::createClient(); |
| 160 | + $client->request('GET', '/graphql/graphiql', ['headers' => ['Accept' => 'text/html']]); |
| 161 | + |
| 162 | + $this->assertResponseIsSuccessful(); |
| 163 | + $content = $client->getResponse()->getContent(); |
| 164 | + |
| 165 | + $this->assertStringContainsString('init-graphiql.js', $content); |
| 166 | + $this->assertStringNotContainsString('nonce=', $content); |
| 167 | + } |
| 168 | +} |
0 commit comments