Spring boot Kafka class deserialization - not in the trusted package
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When developing applications that use messaging systems like Apache Kafka, one of the security aspects that cannot be overlooked is how data is deserialized when it is received by the consumer. In the context of Spring Boot applications using Kafka, careful management of deserialization processes is crucial to prevent potential vulnerabilities such as data tampering or code injection attacks. This article explores the handling of class deserialization in Spring Boot Kafka applications, focusing on the use of non-trusted packages.
Understanding the Issue with Non-Trusted Package Deserialization
The core of the issue lies in how Java deserialization works. Deserialization is the process of converting byte stream back into a Java object. If Java deserializes data from untrusted sources using java standard deserialization mechanisms, there's a risk that the data might contain code intended to harm the application (e.g., remote code execution).
To mitigate such risks, Spring Kafka introduces mechanisms to restrict deserialization to certain trusted packages. The application will only deserialize objects that are from these pre-defined, trusted Java packages. If objects belong to non-trusted packages, it will throw a security exception, thereby preventing potential security risks.
Configuring Trusted Packages in Spring Boot Kafka
To configure trusted packages in Spring Kafka, you need to set the spring.kafka.consumer.properties.spring.deserializer.key.trusted.packages property for keys and spring.kafka.consumer.properties.spring.deserializer.value.trusted.packages for values. These properties can be defined in the application.properties or application.yml of your Spring Boot application.
Here’s an example of how you might configure this in application.yml:
This configuration specifies that only the objects from the com.example.app and org.example.lib packages are trusted for deserialization.
Implementing a Custom Deserializer
While the configuration of trusted packages provides a layer of security, there might be scenarios where you need more control over the deserialization process. In such cases, you can create a custom deserializer. Below is a basic example of a custom deserializer for a Kafka consumer in a Spring Boot application.
To use this deserializer, configure it in your application properties:
Best Practices and Considerations
Implementing security measures around deserialization processes involves more than just restricting the packages. Below are some practices and considerations:
- Logging and Monitoring: Implement comprehensive logging and monitoring around deserialization exceptions and alerts. This way, you’ll be notified of any attempts to breach the system using unexpected serialized data.
- Regular Review: Regularly review and update the list of trusted packages to ensure that it encompasses all necessary libraries while keeping the attack surface as small as possible.
- Security Testing: Include deserialization mechanisms in your security testing processes. Penetration testing should attempt to exploit serialization endpoints.
Summary Table
| Feature | Description | Configuration / Code Example |
| Trusted Package Listing | Limits deserialization to classes from specified packages. | spring.deserializer.trusted.packages: 'com.example.app,org.example.lib' |
| Custom Deserializer | Offers fine-grained control over deserialization logic. | value-deserializer: com.example.app.CustomDeserializer |
| Exception Handling | Essential to manage and alert on deserialization issues. | catch (SerializationException e) |
In conclusion, managing class deserialization in Spring Boot Kafka applications by using trusted packages and potentially implementing custom deserializers can greatly enhance security. These measures, combined with good security practices, ensure that your application remains robust against attacks involving malicious object creation through serialization vectors.
Related reading
- Spring Boot Kafka Commit cannot be completed since the group has already rebalanced
- Spring Boot Kafka Configure DefaultErrorHandler?
- Spring Boot Kafka health indicator
- Spring Boot Kafka Listener vs Consumer
- Spring Boot MSSQL Kerberos Authentication
- Spring Boot Oauth2 client credentials
- Spring Boot Kafka Startup error Connection to node -1 could not be established. Broker may not be available.
- Spring Boot Kafka Unable to start consumer due to NoSuchBeanDefinitionException

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.