Skip to content

Commit 88840a7

Browse files
#28561: GraphQL added CORS headers (fixing issues)
1 parent 64b4228 commit 88840a7

File tree

9 files changed

+51
-42
lines changed

9 files changed

+51
-42
lines changed

app/code/Magento/GraphQl/Controller/HttpResponse/Cors/CorsAllowCredentialsHeaderProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(
4444
*
4545
* @return string
4646
*/
47-
public function getName()
47+
public function getName(): string
4848
{
4949
return $this->headerName;
5050
}
@@ -54,7 +54,7 @@ public function getName()
5454
*
5555
* @return string
5656
*/
57-
public function getValue()
57+
public function getValue(): string
5858
{
5959
return "1";
6060
}
@@ -64,7 +64,7 @@ public function getValue()
6464
*
6565
* @return bool
6666
*/
67-
public function canApply() : bool
67+
public function canApply(): bool
6868
{
6969
return $this->corsConfiguration->isEnabled() && $this->corsConfiguration->isCredentialsAllowed();
7070
}

app/code/Magento/GraphQl/Controller/HttpResponse/Cors/CorsAllowHeadersHeaderProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(
4444
*
4545
* @return string
4646
*/
47-
public function getName()
47+
public function getName(): string
4848
{
4949
return $this->headerName;
5050
}
@@ -54,17 +54,17 @@ public function getName()
5454
*
5555
* @return bool
5656
*/
57-
public function canApply() : bool
57+
public function canApply(): bool
5858
{
5959
return $this->corsConfiguration->isEnabled() && $this->getValue();
6060
}
6161

6262
/**
6363
* Get value for header
6464
*
65-
* @return string
65+
* @return string|null
6666
*/
67-
public function getValue()
67+
public function getValue(): ?string
6868
{
6969
return $this->corsConfiguration->getAllowedHeaders();
7070
}

app/code/Magento/GraphQl/Controller/HttpResponse/Cors/CorsAllowMethodsHeaderProvider.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
class CorsAllowMethodsHeaderProvider implements HeaderProviderInterface
1717
{
18+
/**
19+
* @var string
20+
*/
1821
private $headerName;
1922

2023
/**
@@ -41,7 +44,7 @@ public function __construct(
4144
*
4245
* @return string
4346
*/
44-
public function getName()
47+
public function getName(): string
4548
{
4649
return $this->headerName;
4750
}
@@ -51,17 +54,17 @@ public function getName()
5154
*
5255
* @return bool
5356
*/
54-
public function canApply() : bool
57+
public function canApply(): bool
5558
{
5659
return $this->corsConfiguration->isEnabled() && $this->getValue();
5760
}
5861

5962
/**
6063
* Get value for header
6164
*
62-
* @return string
65+
* @return string|null
6366
*/
64-
public function getValue()
67+
public function getValue(): ?string
6568
{
6669
return $this->corsConfiguration->getAllowedMethods();
6770
}

app/code/Magento/GraphQl/Controller/HttpResponse/Cors/CorsAllowOriginHeaderProvider.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
class CorsAllowOriginHeaderProvider implements HeaderProviderInterface
1717
{
18+
/**
19+
* @var string
20+
*/
1821
private $headerName;
1922

2023
/**
@@ -41,7 +44,7 @@ public function __construct(
4144
*
4245
* @return string
4346
*/
44-
public function getName()
47+
public function getName(): string
4548
{
4649
return $this->headerName;
4750
}
@@ -51,17 +54,17 @@ public function getName()
5154
*
5255
* @return bool
5356
*/
54-
public function canApply() : bool
57+
public function canApply(): bool
5558
{
5659
return $this->corsConfiguration->isEnabled() && $this->getValue();
5760
}
5861

5962
/**
6063
* Get value for header
6164
*
62-
* @return string
65+
* @return string|null
6366
*/
64-
public function getValue()
67+
public function getValue(): ?string
6568
{
6669
return $this->corsConfiguration->getAllowedOrigins();
6770
}

app/code/Magento/GraphQl/Controller/HttpResponse/Cors/CorsMaxAgeHeaderProvider.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
class CorsMaxAgeHeaderProvider implements HeaderProviderInterface
1717
{
18+
/**
19+
* @var string
20+
*/
1821
private $headerName;
1922

2023
/**
@@ -41,7 +44,7 @@ public function __construct(
4144
*
4245
* @return string
4346
*/
44-
public function getName()
47+
public function getName(): string
4548
{
4649
return $this->headerName;
4750
}
@@ -51,18 +54,18 @@ public function getName()
5154
*
5255
* @return bool
5356
*/
54-
public function canApply()
57+
public function canApply(): bool
5558
{
5659
return $this->corsConfiguration->isEnabled() && $this->getValue();
5760
}
5861

5962
/**
6063
* Get value for header
6164
*
62-
* @return string
65+
* @return string|null
6366
*/
64-
public function getValue()
67+
public function getValue(): ?string
6568
{
66-
return $this->corsConfiguration->getMaxAge();
69+
return (string) $this->corsConfiguration->getMaxAge();
6770
}
6871
}

app/code/Magento/GraphQl/Model/Cors/Configuration.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
*/
1515
class Configuration implements ConfigurationInterface
1616
{
17-
const XML_PATH_CORS_HEADERS_ENABLED = 'graphql/cors/enabled';
18-
const XML_PATH_CORS_ALLOWED_ORIGINS = 'graphql/cors/allowed_origins';
19-
const XML_PATH_CORS_ALLOWED_HEADERS = 'graphql/cors/allowed_headers';
20-
const XML_PATH_CORS_ALLOWED_METHODS = 'graphql/cors/allowed_methods';
21-
const XML_PATH_CORS_MAX_AGE = 'graphql/cors/max_age';
22-
const XML_PATH_CORS_ALLOW_CREDENTIALS = 'graphql/cors/allow_credentials';
17+
public const XML_PATH_CORS_HEADERS_ENABLED = 'graphql/cors/enabled';
18+
public const XML_PATH_CORS_ALLOWED_ORIGINS = 'graphql/cors/allowed_origins';
19+
public const XML_PATH_CORS_ALLOWED_HEADERS = 'graphql/cors/allowed_headers';
20+
public const XML_PATH_CORS_ALLOWED_METHODS = 'graphql/cors/allowed_methods';
21+
public const XML_PATH_CORS_MAX_AGE = 'graphql/cors/max_age';
22+
public const XML_PATH_CORS_ALLOW_CREDENTIALS = 'graphql/cors/allow_credentials';
2323

2424
/**
2525
* @var ScopeConfigInterface

app/code/Magento/GraphQl/Model/Cors/ConfigurationInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,35 @@ interface ConfigurationInterface
1717
*
1818
* @return bool
1919
*/
20-
public function isEnabled() : bool;
20+
public function isEnabled(): bool;
2121

2222
/**
2323
* Get allowed origins or null if stored configuration is empty
2424
*
2525
* @return string|null
2626
*/
27-
public function getAllowedOrigins() : ?string;
27+
public function getAllowedOrigins(): ?string;
2828

2929
/**
3030
* Get allowed headers or null if stored configuration is empty
3131
*
3232
* @return string|null
3333
*/
34-
public function getAllowedHeaders() : ?string;
34+
public function getAllowedHeaders(): ?string;
3535

3636
/**
3737
* Get allowed methods or null if stored configuration is empty
3838
*
3939
* @return string|null
4040
*/
41-
public function getAllowedMethods() : ?string;
41+
public function getAllowedMethods(): ?string;
4242

4343
/**
4444
* Get max age header value
4545
*
4646
* @return int
4747
*/
48-
public function getMaxAge() : int;
48+
public function getMaxAge(): int;
4949

5050
/**
5151
* Are credentials allowed

app/code/Magento/GraphQl/etc/di.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,27 @@
100100
</type>
101101

102102
<preference for="Magento\GraphQl\Model\Cors\ConfigurationInterface" type="Magento\GraphQl\Model\Cors\Configuration" />
103-
<type name="\Magento\GraphQl\Controller\HttpResponse\Cors\CorsMaxAgeHeaderProvider">
103+
<type name="Magento\GraphQl\Controller\HttpResponse\Cors\CorsMaxAgeHeaderProvider">
104104
<arguments>
105105
<argument name="headerName" xsi:type="string">Access-Control-Max-Age</argument>
106106
</arguments>
107107
</type>
108-
<type name="\Magento\GraphQl\Controller\HttpResponse\Cors\CorsAllowCredentialsHeaderProvider">
108+
<type name="Magento\GraphQl\Controller\HttpResponse\Cors\CorsAllowCredentialsHeaderProvider">
109109
<arguments>
110110
<argument name="headerName" xsi:type="string">Access-Control-Allow-Credentials</argument>
111111
</arguments>
112112
</type>
113-
<type name="\Magento\GraphQl\Controller\HttpResponse\Cors\CorsAllowHeadersHeaderProvider">
113+
<type name="Magento\GraphQl\Controller\HttpResponse\Cors\CorsAllowHeadersHeaderProvider">
114114
<arguments>
115115
<argument name="headerName" xsi:type="string">Access-Control-Allow-Headers</argument>
116116
</arguments>
117117
</type>
118-
<type name="\Magento\GraphQl\Controller\HttpResponse\Cors\CorsAllowMethodsHeaderProvider">
118+
<type name="Magento\GraphQl\Controller\HttpResponse\Cors\CorsAllowMethodsHeaderProvider">
119119
<arguments>
120120
<argument name="headerName" xsi:type="string">Access-Control-Allow-Methods</argument>
121121
</arguments>
122122
</type>
123-
<type name="\Magento\GraphQl\Controller\HttpResponse\Cors\CorsAllowOriginHeaderProvider">
123+
<type name="Magento\GraphQl\Controller\HttpResponse\Cors\CorsAllowOriginHeaderProvider">
124124
<arguments>
125125
<argument name="headerName" xsi:type="string">Access-Control-Allow-Origin</argument>
126126
</arguments>

dev/tests/api-functional/testsuite/Magento/GraphQl/CorsHeadersTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ protected function setUp(): void
4545

4646
protected function tearDown(): void
4747
{
48-
parent::tearDown(); // TODO: Change the autogenerated stub
48+
parent::tearDown();
4949

5050
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_HEADERS_ENABLED, 0);
5151
$this->reinitConfig->reinit();
5252
}
5353

54-
public function testNoCorsHeadersWhenCorsIsDisabled()
54+
public function testNoCorsHeadersWhenCorsIsDisabled(): void
5555
{
5656
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_HEADERS_ENABLED, 0);
5757
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_ALLOWED_HEADERS, 'Origin');
@@ -70,7 +70,7 @@ public function testNoCorsHeadersWhenCorsIsDisabled()
7070
self::assertArrayNotHasKey('Access-Control-Allow-Origin', $headers);
7171
}
7272

73-
public function testCorsHeadersWhenCorsIsEnabled()
73+
public function testCorsHeadersWhenCorsIsEnabled(): void
7474
{
7575
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_HEADERS_ENABLED, 1);
7676
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_ALLOWED_HEADERS, 'Origin');
@@ -89,7 +89,7 @@ public function testCorsHeadersWhenCorsIsEnabled()
8989
self::assertEquals('86400', $headers['Access-Control-Max-Age']);
9090
}
9191

92-
public function testEmptyCorsHeadersWhenCorsIsEnabled()
92+
public function testEmptyCorsHeadersWhenCorsIsEnabled(): void
9393
{
9494
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_HEADERS_ENABLED, 1);
9595
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_ALLOWED_HEADERS, '');
@@ -108,7 +108,7 @@ public function testEmptyCorsHeadersWhenCorsIsEnabled()
108108
self::assertArrayNotHasKey('Access-Control-Allow-Origin', $headers);
109109
}
110110

111-
private function getHeadersFromIntrospectionQuery()
111+
private function getHeadersFromIntrospectionQuery(): array
112112
{
113113
$query
114114
= <<<QUERY
@@ -121,6 +121,6 @@ private function getHeadersFromIntrospectionQuery()
121121
}
122122
QUERY;
123123

124-
return $this->graphQlQueryWithResponseHeaders($query)['headers'];
124+
return $this->graphQlQueryWithResponseHeaders($query)['headers'] ?? [];
125125
}
126126
}

0 commit comments

Comments
 (0)