Skip to content

Commit 5723d68

Browse files
authored
Merge pull request from GHSA-vr2x-7687-h6qv
1 parent 1c9a025 commit 5723d68

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

features/authorization/deny.feature

+22
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,25 @@ Feature: Authorization checking
181181
Then the response status code should be 200
182182
And the response should contain "ownerOnlyProperty"
183183
And the JSON node "ownerOnlyProperty" should be equal to the string "updated"
184+
185+
Scenario: A user retrieves a resource with an admin only viewable property
186+
When I add "Accept" header equal to "application/json"
187+
And I add "Authorization" header equal to "Basic ZHVuZ2xhczprZXZpbg=="
188+
And I send a "GET" request to "/secured_dummies"
189+
Then the response status code should be 200
190+
And the response should contain "ownerOnlyProperty"
191+
192+
Scenario: A user retrieves a resource with an admin only viewable property
193+
When I add "Accept" header equal to "application/hal+json"
194+
And I add "Authorization" header equal to "Basic ZHVuZ2xhczprZXZpbg=="
195+
And I send a "GET" request to "/secured_dummies"
196+
Then the response status code should be 200
197+
And the response should contain "ownerOnlyProperty"
198+
199+
Scenario: A user retrieves a resource with an admin only viewable property
200+
Given I add "Accept" header equal to "application/vnd.api+json"
201+
And I add "Authorization" header equal to "Basic ZHVuZ2xhczprZXZpbg=="
202+
And I send a "GET" request to "/secured_dummies"
203+
Then the response status code should be 200
204+
And the response should contain "ownerOnlyProperty"
205+

src/Hal/Serializer/ItemNormalizer.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ public function normalize($object, $format = null, array $context = [])
6262
return parent::normalize($object, $format, $context);
6363
}
6464

65-
if (!isset($context['cache_key'])) {
66-
$context['cache_key'] = $this->getCacheKey($format, $context);
67-
}
68-
6965
if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
7066
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null);
7167
}
@@ -75,6 +71,10 @@ public function normalize($object, $format = null, array $context = [])
7571
$context['iri'] = $iri;
7672
$context['api_normalize'] = true;
7773

74+
if (!isset($context['cache_key'])) {
75+
$context['cache_key'] = $this->getCacheKey($format, $context);
76+
}
77+
7878
$data = parent::normalize($object, $format, $context);
7979
if (!\is_array($data)) {
8080
return $data;

src/JsonApi/Serializer/ItemNormalizer.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ public function normalize($object, $format = null, array $context = [])
7878
return parent::normalize($object, $format, $context);
7979
}
8080

81-
if (!isset($context['cache_key'])) {
82-
$context['cache_key'] = $this->getCacheKey($format, $context);
83-
}
84-
85-
if ($isResourceClass = $this->resourceClassResolver->isResourceClass($resourceClass)) {
81+
if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
8682
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null);
8783
}
8884

@@ -91,6 +87,10 @@ public function normalize($object, $format = null, array $context = [])
9187
$context['iri'] = $iri;
9288
$context['api_normalize'] = true;
9389

90+
if (!isset($context['cache_key'])) {
91+
$context['cache_key'] = $this->getCacheKey($format, $context);
92+
}
93+
9494
$data = parent::normalize($object, $format, $context);
9595
if (!\is_array($data)) {
9696
return $data;

src/Serializer/AbstractItemNormalizer.php

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use ApiPlatform\Exception\InvalidValueException;
2828
use ApiPlatform\Exception\ItemNotFoundException;
2929
use ApiPlatform\Metadata\ApiProperty;
30+
use ApiPlatform\Metadata\CollectionOperationInterface;
3031
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
3132
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
3233
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
@@ -207,6 +208,11 @@ public function normalize($object, $format = null, array $context = [])
207208
$context = $this->initContext($resourceClass, $context);
208209
}
209210

211+
if (isset($context['operation']) && $context['operation'] instanceof CollectionOperationInterface) {
212+
unset($context['operation']);
213+
unset($context['iri']);
214+
}
215+
210216
$iri = null;
211217
if (isset($context['iri'])) {
212218
$iri = $context['iri'];

src/Serializer/CacheKeyTrait.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
namespace ApiPlatform\Serializer;
1515

16+
/**
17+
* Used to override Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer::getCacheKey which is private
18+
* We need the cache_key in JsonApi and Hal before it is computed in Symfony.
19+
*
20+
* @see https://github.com/symfony/symfony/blob/49b6ab853d81e941736a1af67845efa3401e7278/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php#L723 which isn't protected
21+
*/
1622
trait CacheKeyTrait
1723
{
1824
/**
@@ -24,11 +30,15 @@ private function getCacheKey(?string $format, array $context)
2430
unset($context[$key]);
2531
}
2632
unset($context[self::EXCLUDE_FROM_CACHE_KEY]);
33+
unset($context[self::OBJECT_TO_POPULATE]);
2734
unset($context['cache_key']); // avoid artificially different keys
2835

2936
try {
30-
return md5($format.serialize($context));
31-
} catch (\Exception $exception) {
37+
return hash('xxh128', $format.serialize([
38+
'context' => $context,
39+
'ignored' => $context[self::IGNORED_ATTRIBUTES] ?? $this->defaultContext[self::IGNORED_ATTRIBUTES],
40+
]));
41+
} catch (\Exception) {
3242
// The context cannot be serialized, skip the cache
3343
return false;
3444
}

0 commit comments

Comments
 (0)