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

Increase limits for XML parsing #11126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

cushon
Copy link

@cushon cushon commented Dec 19, 2024

To prepare for https://bugs.openjdk.org/browse/JDK-8343022

See also https://docs.oracle.com/en/java/javase/23/docs/api/java.xml/module-summary.html#Conf for documentation on these properties

#11125

Summary by CodeRabbit

  • New Features

    • Enhanced XML parsing security to mitigate potential entity expansion attacks.
  • Bug Fixes

    • Improved error handling during XML parsing operations.
  • Documentation

    • Updated method signature for clarity and improved understanding.

Copy link
Contributor

coderabbitai bot commented Dec 19, 2024

Walkthrough

The pull request introduces security enhancements for XML parsing in two core LanguageTool classes: PatternRuleLoader and DisambiguationRuleLoader. The modifications focus on mitigating potential XML entity expansion attacks by setting specific XML parsing properties, including jdk.xml.maxGeneralEntitySizeLimit, jdk.xml.totalEntitySizeLimit, and jdk.xml.entityExpansionLimit, to zero. Additionally, the DisambiguationRuleLoader class has an updated method signature for getRules. These changes improve the security of XML processing in the library.

Changes

File Change Summary
languagetool-core/.../PatternRuleLoader.java Enhanced XML parsing security in getRules() method by setting properties to zero to prevent entity expansion attacks.
languagetool-core/.../DisambiguationRuleLoader.java Updated getRules method to enhance XML parsing security by configuring SAX parser properties, with a minor change in method signature.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 81e63c5 and 44d9a49.

📒 Files selected for processing (1)
  • languagetool-core/src/main/java/org/languagetool/tagging/disambiguation/rules/DisambiguationRuleLoader.java (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • languagetool-core/src/main/java/org/languagetool/tagging/disambiguation/rules/DisambiguationRuleLoader.java

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
languagetool-core/src/main/java/org/languagetool/rules/patterns/PatternRuleLoader.java (1)

76-78: Consider extracting XML security settings to a shared utility

The XML security settings are duplicated between PatternRuleLoader and DisambiguationRuleLoader. Consider extracting these settings to a shared utility method to ensure consistency and ease maintenance.

Create a new utility class/method like this:

public class XMLSecurityUtil {
    private static final String PROP_MAX_ENTITY_SIZE = "jdk.xml.maxGeneralEntitySizeLimit";
    private static final String PROP_TOTAL_ENTITY_SIZE = "jdk.xml.totalEntitySizeLimit";
    private static final String PROP_ENTITY_EXPANSION = "jdk.xml.entityExpansionLimit";
    private static final String FEATURE_EXTERNAL_DTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd";

    public static void secureXMLReader(XMLReader reader) throws SAXException {
        reader.setFeature(FEATURE_EXTERNAL_DTD, false);
        reader.setProperty(PROP_MAX_ENTITY_SIZE, 0);
        reader.setProperty(PROP_TOTAL_ENTITY_SIZE, 0);
        reader.setProperty(PROP_ENTITY_EXPANSION, 0);
    }
}

Then use it in both classes:

XMLSecurityUtil.secureXMLReader(saxParser.getXMLReader());
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d9f9c11 and 81e63c5.

📒 Files selected for processing (2)
  • languagetool-core/src/main/java/org/languagetool/rules/patterns/PatternRuleLoader.java (1 hunks)
  • languagetool-core/src/main/java/org/languagetool/tagging/disambiguation/rules/DisambiguationRuleLoader.java (1 hunks)
🔇 Additional comments (1)
languagetool-core/src/main/java/org/languagetool/rules/patterns/PatternRuleLoader.java (1)

76-78: Verify the impact of zero limits on valid XML files

While setting these limits to 0 provides maximum security, let's verify that this doesn't affect processing of legitimate XML files in the codebase.

✅ Verification successful

Let me check the actual content of these XML files to understand how they use entities.


Let me check the actual content of these external entity files to ensure they don't enable recursive entity expansion.


Let me check the content of a few representative entity files to understand how they're used.


Based on the analysis of the XML files and entity usage in the codebase, I can now provide a final response:

Setting XML entity limits to 0 is safe and recommended

The codebase only uses simple entity declarations for text substitution patterns (like <!ENTITY verbos_ambiguos "...">) and DTD definitions. There are no recursive entities, external entity references (except for well-defined local files in Portuguese module), or complex entity expansions that would be affected by these security limits:

  • The entities are used for storing grammar rules, word lists, and language patterns
  • No XML bomb patterns or dangerous entity expansion constructs were found
  • External entities are only used for modular organization of language rules
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for XML files that might be affected by these limits
echo "Searching for XML files with entity declarations..."
rg -l "<!ENTITY" --type xml

echo "Searching for XML files with SYSTEM or PUBLIC identifiers..."
rg -l "<!DOCTYPE.*?(SYSTEM|PUBLIC)" --type xml

Length of output: 7065


Script:

#!/bin/bash
# Check content of a few representative XML files
echo "Checking grammar.xml entity usage..."
rg "<!ENTITY" -A 2 "languagetool-language-modules/en/src/main/resources/org/languagetool/rules/en/grammar.xml"

echo -e "\nChecking false-friends.xml DTD..."
cat "languagetool-core/src/main/resources/org/languagetool/rules/false-friends.dtd"

echo -e "\nChecking for any recursive/nested entities..."
rg "<!ENTITY.*%.*SYSTEM" --type xml

Length of output: 46869


Script:

#!/bin/bash
# Check content of external entity files in Portuguese and Spanish modules
echo "Checking Portuguese entities..."
rg -l "<!ENTITY" "languagetool-language-modules/pt/src/main/resources/org/languagetool/resource/pt/entities/" || echo "No Portuguese entity files found"

echo -e "\nChecking Spanish entities..."
rg -l "<!ENTITY" "languagetool-language-modules/es/src/main/resources/org/languagetool/resource/es/entities.ent" || echo "No Spanish entity file found"

echo -e "\nChecking for any nested ENTITY definitions..."
rg "<!ENTITY.*%.*<!ENTITY" --type xml

Length of output: 1730


Script:

#!/bin/bash
# Check content of representative entity files
echo "Checking content of verbs.ent..."
head -n 10 "languagetool-language-modules/pt/src/main/resources/org/languagetool/resource/pt/entities/verbs.ent"

echo -e "\nChecking content of Spanish entities.ent..."
head -n 10 "languagetool-language-modules/es/src/main/resources/org/languagetool/resource/es/entities.ent"

echo -e "\nChecking for any entity expansion limits in XML files..."
rg "entityExpansionLimit|maxGeneralEntitySizeLimit|totalEntitySizeLimit" --type xml

Length of output: 22663

Comment on lines +47 to +49
saxParser.getXMLReader().setProperty("jdk.xml.maxGeneralEntitySizeLimit", 0);
saxParser.getXMLReader().setProperty("jdk.xml.totalEntitySizeLimit", 0);
saxParser.getXMLReader().setProperty("jdk.xml.entityExpansionLimit", 0);
Copy link
Contributor

@coderabbitai coderabbitai bot Dec 19, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add protection against external DTD loading

While the entity expansion limits are good security measures, consider also disabling external DTD loading for consistent security with PatternRuleLoader. Additionally, consider adding error handling for the property settings.

Apply this diff to enhance security and robustness:

+    saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     saxParser.getXMLReader().setProperty("jdk.xml.maxGeneralEntitySizeLimit", 0);
     saxParser.getXMLReader().setProperty("jdk.xml.totalEntitySizeLimit", 0);
     saxParser.getXMLReader().setProperty("jdk.xml.entityExpansionLimit", 0);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
saxParser.getXMLReader().setProperty("jdk.xml.maxGeneralEntitySizeLimit", 0);
saxParser.getXMLReader().setProperty("jdk.xml.totalEntitySizeLimit", 0);
saxParser.getXMLReader().setProperty("jdk.xml.entityExpansionLimit", 0);
saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
saxParser.getXMLReader().setProperty("jdk.xml.maxGeneralEntitySizeLimit", 0);
saxParser.getXMLReader().setProperty("jdk.xml.totalEntitySizeLimit", 0);
saxParser.getXMLReader().setProperty("jdk.xml.entityExpansionLimit", 0);

Copy link
Member

Choose a reason for hiding this comment

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

@cushon Thanks! Does coderabbit's suggestion make sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Author

Choose a reason for hiding this comment

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

While the entity expansion limits are good security measures, consider also disabling external DTD loading

@cushon Thanks! Does coderabbit's suggestion make sense?

I'm not sure--I have context about the JDK change, but less about the requirements of this project.

I can try adding the change to disable external DTD loading while we're here, but it's somewhat unrelated. I think coderabbit's is a bit confused--this isn't setting entity expansion limits, it's actually disabling the limits (setting 0 means 'unlimited').

Are the XML files parsed by this code are trusted, either because they're the ones included in this project, or provided by a user of the library, rather than being arbitrary user input?

Copy link
Member

Choose a reason for hiding this comment

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

Are the XML files parsed by this code are trusted

Generally, yes. While there could be external entities, one (= a user who edits the XML files) would need to add them explicitly.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, I went ahead and added the change to disable external DTD loading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants