1 | <?php |
||||
2 | /******************************************************************************* |
||||
3 | * This file is part of the GraphQL Bundle package. |
||||
4 | * |
||||
5 | * (c) YnloUltratech <[email protected]> |
||||
6 | * |
||||
7 | * For the full copyright and license information, please view the LICENSE |
||||
8 | * file that was distributed with this source code. |
||||
9 | ******************************************************************************/ |
||||
10 | |||||
11 | namespace Ynlo\GraphQLBundle\Security; |
||||
12 | |||||
13 | use Symfony\Component\HttpFoundation\Request; |
||||
14 | use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; |
||||
15 | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
||||
16 | use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; |
||||
17 | use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry; |
||||
18 | use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
||||
19 | use Ynlo\GraphQLBundle\Extension\EndpointNotValidException; |
||||
20 | |||||
21 | class EndpointResolver |
||||
22 | { |
||||
23 | /** |
||||
24 | * @var DefinitionRegistry |
||||
25 | */ |
||||
26 | protected $definitionRegistry; |
||||
27 | |||||
28 | /** |
||||
29 | * @var AuthorizationChecker |
||||
30 | */ |
||||
31 | protected $authorizationChecker; |
||||
32 | |||||
33 | /** |
||||
34 | * @var array |
||||
35 | */ |
||||
36 | protected $endpointsConfig = []; |
||||
37 | |||||
38 | /** |
||||
39 | * EndpointResolver constructor. |
||||
40 | * |
||||
41 | * Endpoints config should have the following format |
||||
42 | * |
||||
43 | * [ |
||||
44 | * 'endpoints' => [ |
||||
45 | * 'name' => [ |
||||
46 | * 'roles'=> [], |
||||
47 | * 'host' => '', |
||||
48 | * 'path' => '' |
||||
49 | * ] |
||||
50 | * ] |
||||
51 | * ] |
||||
52 | * |
||||
53 | * @param DefinitionRegistry $definitionRegistry |
||||
54 | * @param AuthorizationCheckerInterface $authorizationChecker |
||||
55 | * @param array $endpointsConfig |
||||
56 | */ |
||||
57 | 5 | public function __construct(DefinitionRegistry $definitionRegistry, AuthorizationCheckerInterface $authorizationChecker, array $endpointsConfig = []) |
|||
58 | { |
||||
59 | 5 | $this->definitionRegistry = $definitionRegistry; |
|||
60 | 5 | $this->authorizationChecker = $authorizationChecker; |
|||
0 ignored issues
–
show
|
|||||
61 | 5 | $this->endpointsConfig = $endpointsConfig['endpoints'] ?? []; |
|||
62 | 5 | } |
|||
63 | |||||
64 | /** |
||||
65 | * @param Request $request |
||||
66 | * |
||||
67 | * @return null|Endpoint |
||||
68 | * |
||||
69 | * @throws EndpointNotValidException |
||||
70 | */ |
||||
71 | 5 | public function resolveEndpoint(Request $request): ?Endpoint |
|||
72 | { |
||||
73 | 5 | if (empty($this->endpointsConfig)) { |
|||
74 | 1 | return $this->definitionRegistry->getEndpoint(); |
|||
75 | } |
||||
76 | |||||
77 | 4 | foreach ($this->endpointsConfig as $endpoint => $config) { |
|||
78 | 4 | if (isset($config['host'])) { |
|||
79 | 2 | $host = $request->getHost(); |
|||
80 | 2 | if (preg_match(sprintf('/%s/', $this->cleanExpression($config['host'])), $host)) { |
|||
0 ignored issues
–
show
It seems like
$this->cleanExpression($config['host']) can also be of type string[] ; however, parameter $args of sprintf() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
81 | 1 | $hostPassed = true; |
|||
82 | } else { |
||||
83 | 2 | $hostPassed = false; |
|||
84 | } |
||||
85 | } else { |
||||
86 | 2 | $hostPassed = true; |
|||
87 | } |
||||
88 | |||||
89 | 4 | if (isset($config['path'])) { |
|||
90 | 1 | $path = $request->getPathInfo(); |
|||
91 | 1 | if (preg_match(sprintf('/%s/', $this->cleanExpression($config['path'])), $path)) { |
|||
92 | 1 | $pathPassed = true; |
|||
93 | } else { |
||||
94 | 1 | $pathPassed = false; |
|||
95 | } |
||||
96 | } else { |
||||
97 | 3 | $pathPassed = true; |
|||
98 | } |
||||
99 | |||||
100 | 4 | if (isset($config['roles'])) { |
|||
101 | try { |
||||
102 | 1 | $rolePassed = $this->authorizationChecker->isGranted($config['roles']); |
|||
103 | } catch (AuthenticationCredentialsNotFoundException $exception) { |
||||
104 | 1 | $rolePassed = false; |
|||
105 | } |
||||
106 | } else { |
||||
107 | 3 | $rolePassed = true; |
|||
108 | } |
||||
109 | |||||
110 | 4 | if ($rolePassed && $hostPassed && $pathPassed) { |
|||
111 | 3 | return $this->definitionRegistry->getEndpoint($endpoint); |
|||
112 | } |
||||
113 | } |
||||
114 | |||||
115 | 1 | return null; |
|||
116 | } |
||||
117 | |||||
118 | /** |
||||
119 | * @param string $exp |
||||
120 | * |
||||
121 | * @return null|string|string[] |
||||
122 | */ |
||||
123 | 3 | private function cleanExpression($exp) |
|||
124 | { |
||||
125 | 3 | return preg_replace('/\//', '\/', $exp); |
|||
126 | } |
||||
127 | } |
||||
128 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.