Skip to content

Conversation

@momito69
Copy link
Contributor

@momito69 momito69 commented Oct 20, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix ##61967
License MIT

This pull request introduces a new feature to the Symfony Serializer component: Automatic sanitization of string and array of string properties during deserialization, using the new SanitizeDenormalizer.

As it is already done in the Form Component, for a property to be sanitized, you must add the sanitize_html option in the #[Context] attribute of that specific property. Without this option, the property will not be sanitized.
On the top of this if you need to sanitize with a particular sanitizer you can use the second option sanitizer to set the sanitizer you want to use, by default we use the default sanitizer.

Copy link
Member

@stof stof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR misses the integration of this feature in FrameworkBundle.

@momito69 momito69 force-pushed the sanitize-denormalizer branch from 2896194 to 133b158 Compare October 25, 2025 07:33
…g and update usage across denormalizer and tests
@momito69 momito69 force-pushed the sanitize-denormalizer branch from 133b158 to 32e14c7 Compare October 25, 2025 07:35
@momito69 momito69 changed the title [Serializer] Add Sanitize attribute and SanitizeDenormalizer for auto… [Serializer] Add SanitizeDenormalizer for automatic sanitization Oct 25, 2025
Copy link
Member

@stof stof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that support for scalar normalizers is currently broken in ObjectNormalizer, so you should probably wait for #58473 to be merged first.

Anyway, this PR will be for 8.1, not for 7.4 (as the feature freeze for 7.4 and 8.0 was beginning of October)

->tag('serializer.normalizer', ['built_in' => true, 'priority' => -915])

->set('serializer.denormalizer.sanitize', SanitizeDenormalizer::class)
->args([service('service_container')])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

injecting the main DI container is a no-go. It should inject a service locator with the html sanitizer services instead (the main DI container won't be able to get those services anyway as they are not public)

->set('serializer.normalizer.number', NumberNormalizer::class)
->tag('serializer.normalizer', ['built_in' => true, 'priority' => -915])

->set('serializer.denormalizer.sanitize', SanitizeDenormalizer::class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this denormalizer needs to be removed from the config when the html-sanitizer component is not enabled (see how we handle other cross-component features in FrameworkExtension).

continue;
}

foreach ($property->getAttributes(Context::class) as $attribute) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not make any sense. You should read $context, not read attributes again on your own (this defeats all the benefits I listed in my previous review related to caching metadata or supporting all metadata formats)

return false;
}

if (!class_exists($type)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my previous review, I asked for this serializer to be a serializer applied on the string, not applied at the object level.


foreach ($reflection->getProperties() as $property) {
$name = $property->getName();
if (!isset($data[$name])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is broken as it forgets about the support of many advanced features of the ObjectNormalizer (name converters, serialized names, etc...).
This is again something that would be solved automatically once you stop processing the object-level data in that normalizer in favor of making it a proper scalar normalizer.

@nicolas-grekas nicolas-grekas modified the milestones: 7.4, 8.1 Nov 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants