-
Notifications
You must be signed in to change notification settings - Fork 0
Introduced protections against deserialization attacks #3
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
Introduced protections against deserialization attacks #3
Conversation
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
</dependency> | ||
<dependency> |
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 library holds security tools for protecting Java API calls.
License: MIT ✅ | Open source ✅ | More facts
<artifactId>system-lambda</artifactId> | ||
<version>${system-lambda.version}</version> | ||
<scope>test</scope> | ||
</dependency> |
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 library holds security tools for protecting Java API calls.
License: MIT ✅ | Open source ✅ | More facts
<artifactId>junit-jupiter-engine</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> |
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 library holds security tools for protecting Java API calls.
License: MIT ✅ | Open source ✅ | More facts
<dependency> | ||
<groupId>io.github.pixee</groupId> | ||
<artifactId>java-security-toolkit</artifactId> | ||
|
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 library holds security tools for protecting Java API calls.
License: MIT ✅ | Open source ✅ | More facts
You’ve installed Korbit to your Github repository but you haven’t created a Korbit account yet! To create your Korbit account and get your PR scans, please visit https://mentor.korbit.ai/dashboard |
The files' contents are under analysis for test generation. |
Hi there! 👋 Thanks for opening a PR. 🎉 To get the most out of Senior Dev, please sign up in our Web App, connect your GitHub account, and add/join your organization Sowhat999. After that, you will receive code reviews beginning on your next opened PR. 🚀 |
Thanks @pixeebot[bot] for opening this PR! For COLLABORATOR only :
|
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.
@pixeebot[bot]
Thank you for your contribution to this repository! We appreciate your effort in opening pull request.
Happy coding!
👋 Hi there!Everything looks good!
|
Processing PR updates... |
Important Auto Review SkippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
Feedback:
Overall, great work on addressing this critical security vulnerability! Well done 👍🏼. |
PR Details of @pixeebot[bot] in java-design-patterns :
|
Potential issues, bugs, and flaws that can introduce unwanted behavior:
Code suggestions and improvements for better exception handling, logic, standardization, and consistency:
|
ByteArrayInputStream baos = new ByteArrayInputStream(countryBlob.getBytes(1, (int) countryBlob.length())); | ||
ObjectInputStream ois = new ObjectInputStream(baos); | ||
ObjectInputFilters.enableObjectFilterIfUnprotected(ois); | ||
country = (Country) ois.readObject(); | ||
LOGGER.info("Country: " + country); |
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.
The code is vulnerable to Java deserialization attacks because it deserializes objects without validating the source or the content of the serialized data. This can lead to various attacks, including arbitrary code execution if the application is processing data from untrusted sources.
To mitigate this risk, it's recommended to implement a more secure form of serialization or use a library that provides safe deserialization features. Additionally, consider using validation mechanisms such as custom ObjectInputFilter
s to check the classes being deserialized or the size of the object graph being created.
try (var fileIn = new FileInputStream(filename); | ||
var objIn = new ObjectInputStream(fileIn)) { | ||
ObjectInputFilters.enableObjectFilterIfUnprotected(objIn); | ||
map = (Map<String, String>) objIn.readObject(); |
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.
The use of ObjectInputStream
without a proper validation mechanism for deserialized objects can lead to security vulnerabilities, such as arbitrary code execution if the content of the file is malicious. While ObjectInputFilters.enableObjectFilterIfUnprotected(objIn);
is an attempt to mitigate this, it's crucial to ensure that the filter is correctly configured to only allow safe classes to be deserialized. Recommended solution is to explicitly check or restrict the classes that can be deserialized by configuring the ObjectInputFilter
to only allow known safe classes.
try (var fileIn = new FileInputStream(filename); | ||
var objIn = new ObjectInputStream(fileIn)) { | ||
ObjectInputFilters.enableObjectFilterIfUnprotected(objIn); | ||
map = (Map<String, String>) objIn.readObject(); |
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.
Casting the result of objIn.readObject()
directly to (Map<String, String>)
without checking can lead to a ClassCastException
if the object read is not actually a Map<String, String>
. This can be a problem especially when dealing with serialized objects from untrusted sources or when the serialized form might change. It's recommended to perform a type check before casting, or use a safer deserialization method that can handle type mismatches gracefully.
Check out the playback for this Pull Request here. |
I'm confident in this change, but I'm not a maintainer of this project. Do you see any reason not to merge it? If this change was not helpful, or you have suggestions for improvements, please let me know! |
Just a friendly ping to remind you about this change. If there are concerns about it, we'd love to hear about them! |
This change may not be a priority right now, so I'll close it. If there was something I could have done better, please let me know! You can also customize me to make sure I'm working with you in the way you want. |
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.
@pixeebot[bot]
Thank you for your contribution to this repository! We appreciate your effort in closing pull request.
Happy coding!
User description
This change hardens Java deserialization operations against attack. Even a simple operation like an object deserialization is an opportunity to yield control of your system to an attacker. In fact, without specific, non-default protections, any object deserialization call can lead to arbitrary code execution. The JavaDoc now even says:
Let's discuss the attack. In Java, types can customize how they should be deserialized by specifying a
readObject()
method like this real example from an old version of Spring:Reflecting on this code reveals a terrifying conclusion. If an attacker presents this object to be deserialized by your app, the runtime will take a class and a method name from the attacker and then call them. Note that an attacker can provide any serliazed type -- it doesn't have to be the one you're expecting, and it will still deserialize.
Attackers can repurpose the logic of selected types within the Java classpath (called "gadgets") and chain them together to achieve arbitrary remote code execution. There are a limited number of publicly known gadgets that can be used for attack, and our change simply inserts an ObjectInputFilter into the
ObjectInputStream
to prevent them from being used.This is a tough vulnerability class to understand, but it is deadly serious. It offers the highest impact possible (remote code execution), it's a common vulnerability (it's in the OWASP Top 10), and exploitation is easy enough that automated exploitation is possible. It's best to remove deserialization entirely, but our protections is effective against all known exploitation strategies.
More reading
I have additional improvements ready for this repo! If you want to see them, leave the comment:
... and I will open a new PR right away!
Powered by: pixeebot (codemod ID: pixee:java/harden-java-deserialization)
Description
ObjectInputFilters
to enhance security against deserialization attacks inCountrySchemaSql
andRainbowFishSerializer
.java-security-toolkit
as a new dependency in the project's mainpom.xml
and in the specific modulesserialized-entity
andtolerant-reader
.Changes walkthrough
CountrySchemaSql.java
Harden Java Deserialization in CountrySchemaSql
serialized-entity/src/main/java/com/iluwatar/serializedentity/CountrySchemaSql.java
ObjectInputFilters
.ObjectInputFilters.enableObjectFilterIfUnprotected
to harden deserialization.
RainbowFishSerializer.java
Harden Java Deserialization in RainbowFishSerializer
tolerant-reader/src/main/java/com/iluwatar/tolerantreader/RainbowFishSerializer.java
ObjectInputFilters
.ObjectInputFilters.enableObjectFilterIfUnprotected
to harden deserialization.
pom.xml
Add Java Security Toolkit Dependency in pom.xml
pom.xml
java-security-toolkit
version property.java-security-toolkit
as a dependency.pom.xml
Include Java Security Toolkit Dependency
serialized-entity/pom.xml
java-security-toolkit
as a dependency.pom.xml
Include Java Security Toolkit Dependency
tolerant-reader/pom.xml
java-security-toolkit
as a dependency.💡 Usage Guide
Checking Your Pull Request
Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.
Talking to CodeAnt AI
Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Check Your Repository Health
To analyze the health of your code repository, visit our dashboard at app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.