Skip to content

GraphQl Cors Support #31484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Controller\Cors\HttpResponseHeaderProvider;

use Magento\Framework\App\Response\HeaderProvider\HeaderProviderInterface;
use Magento\GraphQl\Model\Cors\ConfigurationProviderInterface;
use Magento\GraphQl\Model\Cors\Validator\RequestValidatorInterface;

/**
* Provides value for Access-Control-Allow-Credentials header if CORS is enabled
*/
class AllowCredentialsHeaderProvider implements HeaderProviderInterface
{
/**
* provides the allow credentials header value
*/
public const ALLOW_CREDENTIALS = "true";

/**
* @var string
*/
private $headerName;

/**
* CORS configuration provider
*
* @var ConfigurationProviderInterface
*/
private $corsConfiguration;

/**
* @var RequestValidatorInterface
*/
private $requestValidator;

/**
* @param ConfigurationProviderInterface $corsConfiguration
* @param RequestValidatorInterface $requestValidator
* @param string $headerName
*/
public function __construct(
ConfigurationProviderInterface $corsConfiguration,
RequestValidatorInterface $requestValidator,
string $headerName
) {
$this->corsConfiguration = $corsConfiguration;
$this->headerName = $headerName;
$this->requestValidator = $requestValidator;
}

/**
* Get name of header
*
* @return string
*/
public function getName(): string
{
return $this->headerName;
}

/**
* Check if header can be applied
*
* @return bool
*/
public function canApply(): bool
{
return $this->requestValidator->isOriginAllowed() && $this->corsConfiguration->isCredentialsAllowed();
}

/**
* Get value for header
*
* @return string
*/
public function getValue(): string
{
return self::ALLOW_CREDENTIALS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Controller\Cors\HttpResponseHeaderProvider;

use Magento\Framework\App\Response\HeaderProvider\HeaderProviderInterface;
use Magento\GraphQl\Model\Cors\ConfigurationProviderInterface;
use Magento\GraphQl\Model\Cors\Validator\RequestValidatorInterface;

/**
* Provides value for Access-Control-Allow-Header header if CORS is enabled
*/
class AllowHeadersHeaderProvider implements HeaderProviderInterface
{
/**
* @var string
*/
private $headerName;

/**
* CORS configuration provider
*
* @var ConfigurationProviderInterface
*/
private $corsConfiguration;

/**
* @var RequestValidatorInterface
*/
private $requestValidator;

/**
* @param ConfigurationProviderInterface $corsConfiguration
* @param RequestValidatorInterface $requestValidator
* @param string $headerName
*/
public function __construct(
ConfigurationProviderInterface $corsConfiguration,
RequestValidatorInterface $requestValidator,
string $headerName
) {
$this->corsConfiguration = $corsConfiguration;
$this->headerName = $headerName;
$this->requestValidator = $requestValidator;
}

/**
* Get name of header
*
* @return string
*/
public function getName(): string
{
return $this->headerName;
}

/**
* Check if header can be applied
*
* @return bool
*/
public function canApply(): bool
{
return $this->requestValidator->isOriginAllowed() && $this->getValue();
}

/**
* Get value for header
*
* @return string
*/
public function getValue(): string
{
return $this->corsConfiguration->getAllowedHeaders()
? implode(',', $this->corsConfiguration->getAllowedHeaders())
: '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Controller\Cors\HttpResponseHeaderProvider;

use Magento\Framework\App\Response\HeaderProvider\HeaderProviderInterface;
use Magento\GraphQl\Model\Cors\ConfigurationProviderInterface;
use Magento\GraphQl\Model\Cors\Validator\RequestValidatorInterface;

/**
* Provides value for Access-Control-Allow-Methods header if CORS is enabled
*/
class AllowMethodsHeaderProvider implements HeaderProviderInterface
{
/**
* @var string
*/
private $headerName;

/**
* @var string
*/
public const GRAPHQL_CORS_ALLOWED_METHODS = 'GET,POST,OPTIONS';

/**
* CORS configuration provider
*
* @var ConfigurationProviderInterface
*/
private $corsConfiguration;

/**
* @var RequestValidatorInterface
*/
private $requestValidator;

/**
* @param ConfigurationProviderInterface $corsConfiguration
* @param RequestValidatorInterface $requestValidator
* @param string $headerName
*/
public function __construct(
ConfigurationProviderInterface $corsConfiguration,
RequestValidatorInterface $requestValidator,
string $headerName
) {
$this->corsConfiguration = $corsConfiguration;
$this->headerName = $headerName;
$this->requestValidator = $requestValidator;
}

/**
* Get name of header
*
* @return string
*/
public function getName(): string
{
return $this->headerName;
}

/**
* Check if header can be applied
*
* @return bool
*/
public function canApply(): bool
{
return $this->requestValidator->isOriginAllowed() && $this->getValue();
}

/**
* Get value for header
*
* @return string
*/
public function getValue(): string
{
return $this->corsConfiguration->getAllowedMethods()
? implode(',', $this->corsConfiguration->getAllowedMethods())
: self::GRAPHQL_CORS_ALLOWED_METHODS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Controller\Cors\HttpResponseHeaderProvider;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\HeaderProvider\HeaderProviderInterface;
use Magento\GraphQl\Model\Cors\ConfigurationProviderInterface;
use Magento\GraphQl\Model\Cors\Validator\RequestValidatorInterface;

/**
* Provides value for Access-Control-Allow-Origin header if CORS is enabled
*/
class AllowOriginHeaderProvider implements HeaderProviderInterface
{
/**
* @var string
*/
private $headerName;

/**
* CORS configuration provider
*
* @var ConfigurationProviderInterface
*/
private $corsConfiguration;

/**
* @var RequestInterface
*/
private $request;

/**
* @var RequestValidatorInterface
*/
private $requestValidator;

/**
* @param ConfigurationProviderInterface $corsConfiguration
* @param RequestInterface $request
* @param RequestValidatorInterface $requestValidator
* @param string $headerName
*/
public function __construct(
ConfigurationProviderInterface $corsConfiguration,
RequestInterface $request,
RequestValidatorInterface $requestValidator,
string $headerName
) {
$this->corsConfiguration = $corsConfiguration;
$this->headerName = $headerName;
$this->request = $request;
$this->requestValidator = $requestValidator;
}

/**
* Get name of header
*
* @return string
*/
public function getName(): string
{
return $this->headerName;
}

/**
* Check if header can be applied
*
* @return bool
*/
public function canApply(): bool
{
return $this->requestValidator->isOriginAllowed() && $this->getValue();
}

public function getValue(): string
{
return $this->isAllOriginsAllowed() ? '*' : $this->request->getHeader('Origin');
}

/**
* if '*' is present, allow all origins
*
* @return bool
*/
private function isAllOriginsAllowed(): bool
{
return in_array('*', $this->corsConfiguration->getAllowedOrigins());
}
}
Loading