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

Backport: Fix BOM validation failing when URL contains encoded [ and ] characters #3866

Merged
merged 2 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
<lib.commons-compress.version>1.26.1</lib.commons-compress.version>
<lib.cvss-calculator.version>1.4.2</lib.cvss-calculator.version>
<lib.owasp-rr-calculator.version>1.0.1</lib.owasp-rr-calculator.version>
<lib.cyclonedx-java.version>9.0.3</lib.cyclonedx-java.version>
<lib.cyclonedx-java.version>9.0.4</lib.cyclonedx-java.version>
<lib.greenmail.version>1.6.15</lib.greenmail.version>
<lib.jackson.version>2.17.1</lib.jackson.version>
<lib.jackson-databind.version>2.17.1</lib.jackson-databind.version>
Expand Down Expand Up @@ -338,19 +338,6 @@
<version>${lib.cloud-sql-connector-jdbc-sqlserver.version}</version>
</dependency>

<!-- Xerces -->
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.12.2</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
Expand Down
73 changes: 2 additions & 71 deletions src/main/java/org/dependencytrack/util/XmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,91 +18,22 @@
*/
package org.dependencytrack.util;

import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.InputStream;

import static org.apache.xerces.jaxp.JAXPConstants.JAXP_SCHEMA_LANGUAGE;

import static org.apache.xerces.jaxp.JAXPConstants.JAXP_SCHEMA_SOURCE;
import static org.apache.xerces.jaxp.JAXPConstants.W3C_XML_SCHEMA;


public final class XmlUtil {

private XmlUtil() { }

/**
* Constructs a validating secure SAX Parser.
*
* @param schemaStream One or more inputStreams with the schema(s) that the
* parser should be able to validate the XML against, one InputStream per
* schema
* @return a SAX Parser
* @throws javax.xml.parsers.ParserConfigurationException is thrown if there
* is a parser configuration exception
* @throws org.xml.sax.SAXNotRecognizedException thrown if there is an
* unrecognized feature
* @throws org.xml.sax.SAXNotSupportedException thrown if there is a
* non-supported feature
* @throws org.xml.sax.SAXException is thrown if there is a
* org.xml.sax.SAXException
*/
public static SAXParser buildSecureSaxParser(InputStream... schemaStream) throws ParserConfigurationException,
SAXNotRecognizedException, SAXNotSupportedException, SAXException {
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
System.setProperty("javax.xml.accessExternalSchema", "file, https");

final SAXParser saxParser = factory.newSAXParser();
saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
saxParser.setProperty(JAXP_SCHEMA_SOURCE, schemaStream);
return saxParser;
}

/**
* Constructs a secure SAX Parser.
*
* @return a SAX Parser
* @throws javax.xml.parsers.ParserConfigurationException thrown if there is
* a parser configuration exception
* @throws org.xml.sax.SAXNotRecognizedException thrown if there is an
* unrecognized feature
* @throws org.xml.sax.SAXNotSupportedException thrown if there is a
* non-supported feature
* @throws org.xml.sax.SAXException is thrown if there is a
* org.xml.sax.SAXException
*/
public static SAXParser buildSecureSaxParser() throws ParserConfigurationException,
SAXNotRecognizedException, SAXNotSupportedException, SAXException {
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
return factory.newSAXParser();
private XmlUtil() {
}

/**
* Constructs a new document builder with security features enabled.
*
* @return a new document builder
* @throws javax.xml.parsers.ParserConfigurationException thrown if there is
* a parser configuration exception
* a parser configuration exception
*/
public static DocumentBuilder buildSecureDocumentBuilder() throws ParserConfigurationException {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,27 @@ public void testValidateWithValidBom(final Path bomFilePath) throws Exception {
assertThatNoException().isThrownBy(() -> validator.validate(bomBytes));
}

@Test // https://github.com/DependencyTrack/dependency-track/issues/3831
public void testValidateJsonWithUrlContainingEncodedBrackets() {
assertThatNoException()
.isThrownBy(() -> validator.validate("""
{
"bomFormat": "CycloneDX",
"specVersion": "1.5",
"components": [
{
"type": "library",
"name": "acme-library",
"externalReferences": [
{
"type": "website",
"url": "https://example.com/foo?bar=%5Bbaz%5D"
}
]
}
]
}
""".getBytes()));
}

}
Loading