-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Handle invalid mapping type property type #60296
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
base: 7.2
Are you sure you want to change the base?
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
355794e
to
ebad77f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a test case for this?
$normalizedData = $normalizer->denormalize(['foo' => 'foo', 'baz' => 'baz', 'quux' => ['value' => 'quux'], 'type' => $typeValue], AbstractDummy::class); | ||
|
||
if ($shouldFail) { | ||
$this->fail('Expected exception not thrown.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not needed because of the exceptException above
}; | ||
|
||
$discriminatorResolver = new ClassDiscriminatorFromClassMetadata($loaderMock); | ||
$normalizer = new AbstractObjectNormalizerDummy($factory, null, new PhpDocExtractor(), $discriminatorResolver); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure you need the PhpDocExtractor
here
$discriminatorResolver = new ClassDiscriminatorFromClassMetadata($loaderMock); | ||
$normalizer = new AbstractObjectNormalizerDummy($factory, null, new PhpDocExtractor(), $discriminatorResolver); | ||
$serializer = new Serializer([$normalizer]); | ||
$normalizer->setSerializer($serializer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed?
|
||
return [ | ||
[[], true], | ||
[new StdClass(), true], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[new StdClass(), true], | |
[new \stdClass(), true], |
$this->assertInstanceOf(DummyFirstChildQuux::class, $normalizedData->quux); | ||
} | ||
|
||
public function provideInvalidDiscriminatorTypes() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function provideInvalidDiscriminatorTypes() | |
/** | |
* @return iterable<array{0: mixed, 1: bool}> | |
*/ | |
public function provideInvalidDiscriminatorTypes(): iterable |
@@ -1159,6 +1159,10 @@ private function getMappedClass(array $data, string $class, array $context): str | |||
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Type property "%s" not found for the abstract object "%s".', $mapping->getTypeProperty(), $class), null, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), false); | |||
} | |||
|
|||
if (!is_string($type) && (!is_object($type) || !method_exists($type, '__toString'))) { | |||
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type property "%s" for the abstract object "%s" must be a string.', $mapping->getTypeProperty(), $class), $type, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type property "%s" for the abstract object "%s" must be a string.', $mapping->getTypeProperty(), $class), $type, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), false); | |
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type property "%s" for the abstract object "%s" must be a string or a stringable object.', $mapping->getTypeProperty(), $class), $type, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), false); |
maybe?
When using
#[MapRequestPayload]
along with a type that uses a#[DescriminatorMap]
it's possible for a user to craft a payload that triggers aTypeError
by passing the wrong type for the "type" property.For example, a class that has:
and a request comes in with:
will trigger a 500 because
AbstractObjectNormalizer
doesn't validate the field type before passing it to->getClassForType
which typehints for string.This PR adds a conditional that filters anything other than strings or objects that have a __toString method.