Skip to content
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 XXE attacks #6

Conversation

pixeebot[bot]
Copy link

@pixeebot pixeebot bot commented May 24, 2024

This change updates all instances of XMLInputFactory to prevent them from resolving external entities, which can protect you from arbitrary code execution, sensitive data exfiltration, and probably a bunch more evil things attackers are still discovering.

Without this protection, attackers can cause your XMLInputFactory parser to retrieve sensitive information with attacks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<book>
    <title>&xxe;</title>
</book>

Yes, it's pretty insane that this is the default behavior. Our change hardens the factories created with the necessary security features to prevent your parser from resolving external entities.

+ import io.github.pixee.security.XMLInputFactorySecurity;
  ...
- XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
+ XMLInputFactory xmlInputFactory = XMLInputFactorySecurity.hardenFactory(XMLInputFactory.newFactory());

You could take our protections one step further by changing our supplied code to prevent the user from supplying a DOCTYPE, which is more aggressive and more secure, but also more likely to affect existing code behavior:

+ import io.github.pixee.security.XMLInputFactorySecurity;
+ import io.github.pixee.security.XMLRestrictions;
  ...
  XMLInputFactory xmlInputFactory = XMLInputFactorySecurity.hardenFactory(XMLInputFactory.newFactory(), XMLRestrictions.DISALLOW_DOCTYPE);
More reading

I have additional improvements ready for this repo! If you want to see them, leave the comment:

@pixeebot next

... and I will open a new PR right away!

🧚🤖 Powered by Pixeebot

💬Feedback | 👥Community | 📚Docs | Codemod ID: pixee:java/harden-xmlinputfactory

@@ -79,5 +79,9 @@
<artifactId>jaxen</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Copy link
Author

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

@@ -86,4 +86,16 @@
</plugin>
</plugins>
</build>
<dependencyManagement>
Copy link
Author

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

Copy link

Unable to locate .performanceTestingBot config file

Copy link

cr-gpt bot commented May 24, 2024

Seems you are using me but didn't get OPENAI_API_KEY seted in Variables/Secrets for this repo. you could follow readme for more information

Micro-Learning Topic: External entity injection (Detected by phrase)

Matched on "XXE"

What is this? (2min video)

An XML External Entity attack is a type of attack against an application that parses XML input. This attack occurs when XML input containing a reference to an external entity is processed by a weakly configured XML parser. This attack may lead to the disclosure of confidential data, denial of service, server-side request forgery, port scanning from the perspective of the machine where the parser is located, and other system impacts.

Try a challenge in Secure Code Warrior

Helpful references

Copy link

codesyncapp bot commented May 24, 2024

Check out the playback for this Pull Request here.

@trafico-bot trafico-bot bot added the 🔍 Ready for Review Pull Request is not reviewed yet label May 24, 2024
@labels-and-badges labels-and-badges bot added NO JIRA This PR does not have a Jira Ticket PR:size/S Denotes a Pull Request that changes 10-29 lines. release This PR is a release labels May 24, 2024
Copy link

coderabbitai bot commented May 24, 2024

Important

Review Skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Comment on lines 115 to 119
protected <T> boolean hasThisXMLRootElement(AttachmentContent content, String rootElementNamespace, String rootElementName, User user, T context) throws TException {
XMLInputFactory xmlif = XMLInputFactory.newFactory();
XMLInputFactory xmlif = hardenFactory(XMLInputFactory.newFactory());
XMLStreamReader xmlStreamReader = null;
InputStream attachmentStream = null;
try {

Choose a reason for hiding this comment

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

The method hasThisXMLRootElement initializes resources such as XMLStreamReader and InputStream within a try block but does not explicitly close these resources, which can lead to resource leaks. It's recommended to use try-with-resources statement for both XMLStreamReader and InputStream to ensure they are closed properly, even if exceptions occur.

Comment on lines 12 to 16
import com.google.common.collect.Sets;
import static io.github.pixee.security.XMLInputFactorySecurity.hardenFactory;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;

Choose a reason for hiding this comment

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

The usage of both org.apache.commons.lang.StringEscapeUtils and org.apache.commons.lang3.StringUtils from different versions of the Apache Commons Lang library could lead to confusion and potential compatibility issues. It's advisable to use a consistent version of the library across the project to avoid such issues. Consider migrating all usages to org.apache.commons.lang3, which is the newer version, to ensure consistency and access to the latest features and bug fixes.

Copy link

sweep-ai bot commented May 24, 2024

Sweep: PR Review

Authors of pull request: @pixeebot[bot]

This pull request enhances the security of XML parsing in the project by integrating the java-security-toolkit library.

The pom.xml file was updated to include a new dependency on java-security-toolkit from io.github.pixee, with version 1.1.3. This was done by adding a <dependencyManagement> section and defining the version in the <properties> section for centralized version control.

In src-licenseinfo/pom.xml, the java-security-toolkit dependency was added to ensure its functionalities are available within the project.

The AbstractCLIParser.java file was modified to use the hardenFactory method from the java-security-toolkit library. This change replaces the standard XMLInputFactory instantiation with a hardened version, enhancing the security of XML input factory instances.

These changes collectively improve the security and robustness of XML parsing operations in the project.


Sweep Found These Issues

backend/src/src-licenseinfo/src/main/java/org/eclipse/sw360/licenseinfo/parsers/AbstractCLIParser.java
  • The hardenFactory method is used to enhance the security of the XMLInputFactory instance, but there is no verification or logging to ensure that the factory is indeed hardened, which could lead to silent failures if the hardening process fails.
  • }
    protected <T> boolean hasThisXMLRootElement(AttachmentContent content, String rootElementNamespace, String rootElementName, User user, T context) throws TException {
    XMLInputFactory xmlif = hardenFactory(XMLInputFactory.newFactory());
    XMLStreamReader xmlStreamReader = null;

    View Diff


Potential Issues

Sweep is unsure if these are issues, but they might be worth checking out.

backend/src/pom.xml
  • The new dependency on java-security-toolkit may introduce compatibility issues if it conflicts with other dependencies in the project.
  • <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>io.github.pixee</groupId>
    <artifactId>java-security-toolkit</artifactId>
    <version>${versions.java-security-toolkit}</version>
    </dependency>
    </dependencies>
    </dependencyManagement>
    <properties>
    <versions.java-security-toolkit>1.1.3</versions.java-security-toolkit>
    </properties>

    View Diff


Copy link
Author

pixeebot bot commented Jun 1, 2024

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!

Copy link
Author

pixeebot bot commented Jun 2, 2024

Unless you're in the microscopic percentage of developers intentionally allowing external entities in XML on purpose, this change should be merged. In that case however, it should be noted that whoever is supplying XML to your code must be completely trusted.

It should also be noted that the parser you think you're using may actually be different than the one you intend, since the Java XML system has conditions that allows libraries to insert themselves as the primary XML provider.

If there are other concerns about this change, I'd love to hear about them!

Copy link
Author

pixeebot bot commented Jun 8, 2024

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.

@pixeebot pixeebot bot closed this Jun 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NO JIRA This PR does not have a Jira Ticket PR:size/S Denotes a Pull Request that changes 10-29 lines. 🔍 Ready for Review Pull Request is not reviewed yet release This PR is a release size/S
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

0 participants