XML 外部エンティティ インジェクション(XXE)
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
OWASP カテゴリ: MASVS-CODE: コード品質
概要
XML 外部エンティティ インジェクション(XXE)は、XML 入力を解析するアプリケーションに対する攻撃です。XXE 攻撃は、外部エンティティを参照する信頼できない XML 入力が、構成が不十分な XML パーサーによって処理された場合に発生します。この攻撃は、サービス拒否、ファイル システムへのアクセス、データの引き出しなど、複数のインシデントをステージングするために使用できます。
影響
アプリケーションが XML ドキュメントを解析すると、ドキュメント内に含まれる DTD(ドキュメント タイプ定義、外部エンティティとも呼ばれます)を処理できます。攻撃者は、DTD として悪意のあるコードを挿入することで、この動作を悪用できます。このコードは、デバイスのファイル システムの一部(アプリのみがアクセス可能)にアクセスでき、機密データを含む可能性があります。
さらに、この悪意のあるコードはデバイスからリクエストを行う可能性があり、境界のセキュリティ対策が回避される可能性があります。
最後に、アプリケーションが DTD を展開すると、参照されるエンティティが複数回反復され、デバイスのリソースが使い果たされてサービス拒否につながる可能性があります。
リスクの軽減
DTD を無効にする
XXE を防ぐ最も安全な方法は、DTD(外部エンティティ)を常に完全に無効にすることです。使用しているパーサーによっては、このメソッドは XML プル パーサー ライブラリの次の例に似ている場合があります。
Java
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
Kotlin
val factory = XmlPullParserFactory.newInstance()
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true)
DTD を無効にすると、サービス拒否攻撃に対してパーサーのセキュリティも強化されます。DTD を完全に無効にできない場合は、各パーサーに固有の方法で外部エンティティと外部ドキュメント タイプ宣言を無効にする必要があります。
市場には多数の XML 解析エンジンが存在するため、XXE 攻撃を防ぐ方法はエンジンによって異なります。詳しくは、エンジンのドキュメントをご覧ください。
ユーザーが XML ドキュメントのプリアンブルに任意のコードを挿入できないように、アプリケーションを再構成する必要があります。クライアントサイドの制御はバイパスされる可能性があるため、これはサーバーサイドで検証する必要があります。
別のライブラリを使用する
使用するライブラリやメソッドを安全な方法で構成できない場合は、別の方法を検討する必要があります。XML Pull パーサーと SAX パーサーはどちらも、DTD とエンティティを許可しない安全な方法で構成できます。
リソース
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-26 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-26 UTC。"],[],[],null,["# XML External Entities Injections (XXE)\n\n\u003cbr /\u003e\n\n**OWASP category:** [MASVS-CODE: Code Quality](https://mas.owasp.org/MASVS/10-MASVS-CODE)\n\nOverview\n--------\n\nAn XML eXternal Entity injection (XXE) is an attack against applications that\nparse XML input. An XXE attack occurs when untrusted XML input with a reference\nto an external entity is processed by a weakly configured XML parser. This\nattack can be used to stage multiple incidents, including denial of service,\nfile system access, or data exfiltration.\n\nImpact\n------\n\nWhen an application parses an XML document, it can process any DTDs (Document\nType Definitions, also known as external entities) contained within the\ndocument. An attacker can exploit this behavior by injecting malicious code as\nDTDs. This code can then access parts of the file system of the device, only\naccessible to the application and potentially containing sensitive data.\nFurthermore, this malicious code can make requests from the device, potentially\nbypassing perimeter security measures.\n\nLastly, if the application expands DTDs,\nthis can create a situation with multiple iterations of referenced entities,\nexhausting the resources of the device and leading to a denial of service.\n\nMitigations\n-----------\n\n### Disable DTDs\n\nThe safest way to prevent XXE is to always disable DTDs (external entities)\ncompletely. Depending on the parser in use, the method could be similar to the\nfollowing example for the XML Pull Parser library: \n\n### Java\n\n XmlPullParserFactory factory = XmlPullParserFactory.newInstance();\n factory.setFeature(\"http://apache.org/xml/features/disallow-doctype-decl\", true);\n\n### Kotlin\n\n val factory = XmlPullParserFactory.newInstance()\n factory.setFeature(\"http://apache.org/xml/features/disallow-doctype-decl\", true)\n\nDisabling DTDs also makes the parser secure against denial of service attacks.\nIf it is not possible to disable DTDs completely, then external entities and\nexternal document type declarations must be disabled in a way that's specific to\neach parser.\n\nBecause of the large number of XML parsing engines in the market, the ways to\nprevent XXE attacks differ from engine to engine. You may need to refer to your\nengine documentation for more information.\n\n### Perform input sanitisation\n\nThe application should be reconfigured so that it does not allow users to inject\narbitrary code in the XML document's preamble. This has to be verified\nserver-side, as client-side controls can be bypassed.\n\n### Use a different library\n\nIf the library or method used cannot be configured in a secure manner, a\ndifferent one should be considered. [XML Pull Parser](/reference/org/xmlpull/v1/XmlPullParser) and [SAX Parser](/reference/javax/xml/parsers/SAXParser) can\nboth be configured in a secure manner, disallowing DTDs and entities.\n\nResources\n---------\n\n- [OWASP XXE](https://owasp.org/www-community/vulnerabilities/XML_External_Entity_(XXE)_Processing)\n- [OWASP XXE Prevention Cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html)\n- [XML Constants: FEATURE_SECURE_PROCESSING](/reference/javax/xml/XMLConstants#FEATURE_SECURE_PROCESSING)\n- [XML Pull Parser](/reference/org/xmlpull/v1/XmlPullParser)\n- [SAX Parser](/reference/javax/xml/parsers/SAXParser)"]]