Use defusedxml in XMLLoader to block entity expansion#4873
Open
biefan wants to merge 1 commit intocrewAIInc:mainfrom
Open
Use defusedxml in XMLLoader to block entity expansion#4873biefan wants to merge 1 commit intocrewAIInc:mainfrom
biefan wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This changes
XMLLoaderto usedefusedxmlinstead of the standard library XML parser so entity expansion is rejected by default.Problem
The current implementation uses
xml.etree.ElementTree, which expands internal entities. A malicious XML payload such as<!DOCTYPE foo [<!ENTITY xxe "EXPANDED">]><root>&xxe;</root>is therefore parsed into normal output instead of being rejected.That means the loader does not currently fail closed for unsafe XML input, which is exactly the class of issue called out in #4865.
Root Cause
XMLLoaderrelied on the standard parser and only handledParseError. Once the parser was switched todefusedxml, dangerous entity payloads raisedDefusedXmlException, which also needed to be handled by the loader's existing parse-error fallback path.Fix
defusedxmlas acrewai-toolsdependencyXMLLoadertodefusedxml.ElementTreeDefusedXmlExceptionalongsideParseErrorValidation
uv run pytest lib/crewai-tools/tests/rag/test_xml_loader_security.py -quv run ruff check lib/crewai-tools/src/crewai_tools/rag/loaders/xml_loader.py lib/crewai-tools/tests/rag/test_xml_loader_security.pyCloses #4865.
Note
Medium Risk
Changes XML parsing behavior by switching to
defusedxml, which may reject previously-accepted XML (e.g., documents with DTD/entity declarations) but is scoped to the RAG XML loader and improves security against XXE/entity expansion.Overview
Hardens XML ingestion in RAG.
XMLLoadernow usesdefusedxml.ElementTreeinstead of the stdlib parser and treatsDefusedXmlExceptionthe same asParseError, falling back to returning the raw content withparse_errormetadata.Adds
defusedxmlas acrewai-toolsdependency (and updatesuv.lock) and includes a regression test ensuring internal entity declarations are rejected rather than expanded.Written by Cursor Bugbot for commit 7880b36. This will update automatically on new commits. Configure here.