Skip to content

Commit dc4a2ab

Browse files
committed
Disable remote access in XML schema test
The XML test did not access the SPDX schema from the network, but we just want to make sure that this will not happen in the future. Signed-off-by: Piotr P. Karwasz <[email protected]>
1 parent c935467 commit dc4a2ab

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

tools/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
import static org.junit.jupiter.api.Assertions.assertFalse;
1717
import static org.junit.jupiter.api.Assertions.assertTrue;
1818

19-
import java.nio.file.Path;
20-
import java.nio.file.Paths;
2119
import java.util.ArrayList;
2220
import java.util.Collection;
2321
import java.util.List;
2422
import javax.xml.XMLConstants;
25-
import javax.xml.transform.Source;
2623
import javax.xml.transform.stream.StreamSource;
2724
import javax.xml.validation.Schema;
2825
import javax.xml.validation.SchemaFactory;
@@ -45,38 +42,26 @@ public class XmlSchemaVerificationTest extends BaseSchemaVerificationTest {
4542
private static final Schema VERSION_16;
4643

4744
static {
48-
// Surefire sets a `basedir` system property
49-
// Otherwise we assume that the project is in the current working directory (should work in IDEs)
50-
Path toolsPath = Paths.get(System.getProperty("basedir", "."));
51-
Path schemaPath = toolsPath.resolve("../schema");
52-
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
53-
ClassLoader cl = XmlSchemaVerificationTest.class.getClassLoader();
5445
try {
55-
VERSION_10 = factory.newSchema(
56-
new Source[] {spdxSource(), new StreamSource(cl.getResourceAsStream("bom-1.0.xsd"))});
57-
VERSION_11 = factory.newSchema(
58-
new Source[] {spdxSource(), new StreamSource(cl.getResourceAsStream("bom-1.1.xsd"))});
59-
VERSION_12 = factory.newSchema(
60-
new Source[] {spdxSource(), new StreamSource(cl.getResourceAsStream("bom-1.2.xsd"))});
61-
VERSION_13 = factory.newSchema(
62-
new Source[] {spdxSource(), new StreamSource(cl.getResourceAsStream("bom-1.3.xsd"))});
63-
VERSION_14 = factory.newSchema(
64-
new Source[] {spdxSource(), new StreamSource(cl.getResourceAsStream("bom-1.4.xsd"))});
65-
VERSION_15 = factory.newSchema(new Source[] {
66-
spdxSource(), new StreamSource(schemaPath.resolve("bom-1.5.xsd").toFile())
67-
});
68-
VERSION_16 = factory.newSchema(new Source[] {
69-
spdxSource(), new StreamSource(schemaPath.resolve("bom-1.6.xsd").toFile())
70-
});
46+
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
47+
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "file");
48+
ClassLoader cl = XmlSchemaVerificationTest.class.getClassLoader();
49+
// Override the `schemaLocation` property in the file
50+
factory.setProperty(
51+
"http://apache.org/xml/properties/schema/external-schemaLocation",
52+
"http://cyclonedx.org/schema/spdx spdx.xsd");
53+
VERSION_10 = factory.newSchema(cl.getResource("bom-1.0.xsd"));
54+
VERSION_11 = factory.newSchema(cl.getResource("bom-1.1.xsd"));
55+
VERSION_12 = factory.newSchema(cl.getResource("bom-1.2.xsd"));
56+
VERSION_13 = factory.newSchema(cl.getResource("bom-1.3.xsd"));
57+
VERSION_14 = factory.newSchema(cl.getResource("bom-1.4.xsd"));
58+
VERSION_15 = factory.newSchema(cl.getResource("bom-1.5.xsd"));
59+
VERSION_16 = factory.newSchema(cl.getResource("bom-1.6.xsd"));
7160
} catch (SAXException e) {
7261
throw new IllegalStateException(e);
7362
}
7463
}
7564

76-
private static Source spdxSource() {
77-
return new StreamSource(XmlSchemaVerificationTest.class.getClassLoader().getResourceAsStream("spdx.xsd"));
78-
}
79-
8065
/**
8166
* Generates a collection of dynamic tests based on the available XML files.
8267
*

0 commit comments

Comments
 (0)