|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\graphql\Kernel\Framework; |
| 4 | + |
| 5 | +use Drupal\graphql\Routing\QueryRouteEnhancer; |
| 6 | +use Drupal\Tests\graphql\Kernel\GraphQLTestBase; |
| 7 | +use Symfony\Component\HttpFoundation\Request; |
| 8 | + |
| 9 | +/** |
| 10 | + * Test CSRF protection on mutations. |
| 11 | + * |
| 12 | + * @group graphql |
| 13 | + */ |
| 14 | +class CsrfTest extends GraphQLTestBase { |
| 15 | + |
| 16 | + /** |
| 17 | + * Helper state variable that will be flipped when the test mutation executes. |
| 18 | + * |
| 19 | + * @var bool |
| 20 | + */ |
| 21 | + protected $mutationTriggered = FALSE; |
| 22 | + |
| 23 | + /** |
| 24 | + * {@inheritdoc} |
| 25 | + */ |
| 26 | + protected function setUp(): void { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + $schema = <<<GQL |
| 30 | + schema { |
| 31 | + mutation: Mutation |
| 32 | + } |
| 33 | +
|
| 34 | + type Mutation { |
| 35 | + write: Boolean |
| 36 | + } |
| 37 | +GQL; |
| 38 | + $this->setUpSchema($schema); |
| 39 | + $this->mockResolver('Mutation', 'write', |
| 40 | + function () { |
| 41 | + $this->mutationTriggered = TRUE; |
| 42 | + return TRUE; |
| 43 | + } |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Tests that a simple request from an evil origin is not executed. |
| 49 | + * |
| 50 | + * @dataProvider provideSimpleContentTypes |
| 51 | + */ |
| 52 | + public function testEvilOrigin(string $content_type): void { |
| 53 | + $request = Request::create('https://example.com/graphql/test', 'POST', [], [], [], [ |
| 54 | + 'CONTENT_TYPE' => $content_type, |
| 55 | + 'HTTP_ORIGIN' => 'https://evil.example.com', |
| 56 | + ], '{ "query": "mutation { write }" }'); |
| 57 | + |
| 58 | + /** @var \Symfony\Component\HttpFoundation\Response $response */ |
| 59 | + $response = $this->container->get('http_kernel')->handle($request); |
| 60 | + $this->assertFalse($this->mutationTriggered, 'Mutation was triggered'); |
| 61 | + $this->assertSame(400, $response->getStatusCode()); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Data provider for testContentTypeCsrf(). |
| 66 | + */ |
| 67 | + public function provideSimpleContentTypes(): array { |
| 68 | + // Three content types that can be sent with simple no-cors POST requests. |
| 69 | + return [ |
| 70 | + ['text/plain'], |
| 71 | + ['application/x-www-form-urlencoded'], |
| 72 | + ['multipart/form-data'], |
| 73 | + ]; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Tests that a simple multipart form data no-cors request is not executed. |
| 78 | + */ |
| 79 | + public function testMultipartFormDataCsrf(): void { |
| 80 | + $request = Request::create('https://example.com/graphql/test', 'POST', |
| 81 | + [ |
| 82 | + 'operations' => '[{ "query": "mutation { write }" }]', |
| 83 | + ], |
| 84 | + [], |
| 85 | + [], |
| 86 | + [ |
| 87 | + 'CONTENT_TYPE' => 'multipart/form-data', |
| 88 | + 'HTTP_ORIGIN' => 'https://evil.example.com', |
| 89 | + ] |
| 90 | + ); |
| 91 | + |
| 92 | + /** @var \Symfony\Component\HttpFoundation\Response $response */ |
| 93 | + $response = $this->container->get('http_kernel')->handle($request); |
| 94 | + $this->assertFalse($this->mutationTriggered, 'Mutation was triggered'); |
| 95 | + $this->assertSame(400, $response->getStatusCode()); |
| 96 | + $result = json_decode($response->getContent()); |
| 97 | + $this->assertSame("Form requests must include a Apollo-Require-Preflight HTTP header or the Origin HTTP header value needs to be in the allowedOrigins in the CORS settings.", $result->message); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test that the JSON content types always work, cannot be forged with CSRF. |
| 102 | + * |
| 103 | + * @dataProvider provideAllowedJsonHeaders |
| 104 | + */ |
| 105 | + public function testAllowedJsonRequests(array $headers): void { |
| 106 | + $request = Request::create('https://example.com/graphql/test', 'POST', [], [], [], |
| 107 | + $headers, '{ "query": "mutation { write }" }'); |
| 108 | + |
| 109 | + /** @var \Symfony\Component\HttpFoundation\Response $response */ |
| 110 | + $response = $this->container->get('http_kernel')->handle($request); |
| 111 | + $this->assertTrue($this->mutationTriggered, 'Mutation was triggered'); |
| 112 | + $this->assertSame(200, $response->getStatusCode()); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Data provider for testAllowedJsonRequests(). |
| 117 | + */ |
| 118 | + public function provideAllowedJsonHeaders(): array { |
| 119 | + return [ |
| 120 | + [['CONTENT_TYPE' => 'application/json']], |
| 121 | + [['CONTENT_TYPE' => 'application/graphql']], |
| 122 | + ]; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test that a form request with the correct headers against CSRF are allowed. |
| 127 | + * |
| 128 | + * @dataProvider provideAllowedFormRequests |
| 129 | + */ |
| 130 | + public function testAllowedFormRequests(array $headers, array $allowedDomains = []): void { |
| 131 | + $request = Request::create('https://example.com/graphql/test', 'POST', |
| 132 | + [ |
| 133 | + 'operations' => '[{ "query": "mutation { write }" }]', |
| 134 | + ], [], [], $headers); |
| 135 | + |
| 136 | + if (!empty($allowedDomains)) { |
| 137 | + // Replace the QueryRouteEnhancer to inject CORS config we want to test. |
| 138 | + $this->container->set('graphql.route_enhancer.query', new QueryRouteEnhancer([ |
| 139 | + 'enabled' => TRUE, |
| 140 | + 'allowedOrigins' => $allowedDomains, |
| 141 | + ])); |
| 142 | + } |
| 143 | + /** @var \Symfony\Component\HttpFoundation\Response $response */ |
| 144 | + $response = $this->container->get('http_kernel')->handle($request); |
| 145 | + $this->assertTrue($this->mutationTriggered, 'Mutation was triggered'); |
| 146 | + $this->assertSame(200, $response->getStatusCode()); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Data provider for testAllowedFormRequests(). |
| 151 | + */ |
| 152 | + public function provideAllowedFormRequests(): array { |
| 153 | + return [ |
| 154 | + // Omitting the Origin and Apollo-Require-Preflight is allowed. |
| 155 | + [['CONTENT_TYPE' => 'multipart/form-data']], |
| 156 | + // The custom Apollo-Require-Preflight header overrules any evil Origin |
| 157 | + // header. |
| 158 | + [[ |
| 159 | + 'CONTENT_TYPE' => 'multipart/form-data', |
| 160 | + 'HTTP_APOLLO_REQUIRE_PREFLIGHT' => 'test', |
| 161 | + 'HTTP_ORIGIN' => 'https://evil.example.com', |
| 162 | + ]], |
| 163 | + // The Origin header alone with the correct domain is allowed. |
| 164 | + [[ |
| 165 | + 'CONTENT_TYPE' => 'multipart/form-data', |
| 166 | + 'HTTP_ORIGIN' => 'https://example.com', |
| 167 | + ]], |
| 168 | + // The Origin header with an allowed domain. |
| 169 | + [[ |
| 170 | + 'CONTENT_TYPE' => 'multipart/form-data', |
| 171 | + 'HTTP_ORIGIN' => 'https://allowed.example.com', |
| 172 | + ], ['https://allowed.example.com']], |
| 173 | + // The Origin header with any allowed domain. |
| 174 | + [[ |
| 175 | + 'CONTENT_TYPE' => 'multipart/form-data', |
| 176 | + 'HTTP_ORIGIN' => 'https://allowed.example.com', |
| 177 | + ], ['*']], |
| 178 | + ]; |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments