Skip to content

Commit acbe28d

Browse files
committed
Harden SchemaFactory and TransformerFactory with XMLConstants.FEATURE_SECURE_PROCESSING (#3006)
(cherry picked from commit 43c7733)
1 parent cc147a3 commit acbe28d

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

rt/ws/transfer/src/main/java/org/apache/cxf/ws/transfer/validationtransformation/XSDResourceTypeIdentifier.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.cxf.ws.transfer.validationtransformation;
2121

2222
import java.io.IOException;
23+
import java.util.logging.Level;
2324
import java.util.logging.Logger;
2425

2526
import javax.xml.XMLConstants;
@@ -32,6 +33,8 @@
3233
import org.w3c.dom.Node;
3334

3435
import org.xml.sax.SAXException;
36+
import org.xml.sax.SAXNotRecognizedException;
37+
import org.xml.sax.SAXNotSupportedException;
3538

3639
import jakarta.annotation.Resource;
3740
import jakarta.xml.ws.WebServiceContext;
@@ -60,6 +63,14 @@ public XSDResourceTypeIdentifier(Source xsd, ResourceTransformer resourceTransfo
6063
try {
6164
this.resourceTransformer = resourceTransformer;
6265
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
66+
schemaFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
67+
try {
68+
schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
69+
schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
70+
} catch (SAXNotRecognizedException | SAXNotSupportedException e) {
71+
LOG.log(Level.WARNING, "The properties '" + XMLConstants.ACCESS_EXTERNAL_DTD
72+
+ "', '" + XMLConstants.ACCESS_EXTERNAL_SCHEMA + "' are not supported.");
73+
}
6374
Schema schema = schemaFactory.newSchema(xsd);
6475
this.validator = schema.newValidator();
6576
} catch (SAXException ex) {

rt/ws/transfer/src/main/java/org/apache/cxf/ws/transfer/validationtransformation/XSDResourceValidator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.cxf.ws.transfer.validationtransformation;
2121

2222
import java.io.IOException;
23+
import java.util.logging.Level;
2324
import java.util.logging.Logger;
2425

2526
import javax.xml.XMLConstants;
@@ -32,6 +33,8 @@
3233
import org.w3c.dom.Node;
3334

3435
import org.xml.sax.SAXException;
36+
import org.xml.sax.SAXNotRecognizedException;
37+
import org.xml.sax.SAXNotSupportedException;
3538

3639
import jakarta.annotation.Resource;
3740
import jakarta.xml.ws.WebServiceContext;
@@ -57,6 +60,14 @@ public class XSDResourceValidator implements ResourceValidator {
5760
public XSDResourceValidator(Source xsd, ResourceTransformer resourceTransformer) {
5861
try {
5962
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
63+
schemaFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
64+
try {
65+
schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
66+
schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
67+
} catch (SAXNotRecognizedException | SAXNotSupportedException e) {
68+
LOG.log(Level.WARNING, "The properties '" + XMLConstants.ACCESS_EXTERNAL_DTD
69+
+ "', '" + XMLConstants.ACCESS_EXTERNAL_SCHEMA + "' are not supported.");
70+
}
6071
Schema schema = schemaFactory.newSchema(xsd);
6172
this.validator = schema.newValidator();
6273
} catch (SAXException ex) {

rt/ws/transfer/src/main/java/org/apache/cxf/ws/transfer/validationtransformation/XSLTResourceTransformer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.logging.Logger;
2323

24+
import javax.xml.XMLConstants;
2425
import javax.xml.transform.Source;
2526
import javax.xml.transform.Templates;
2627
import javax.xml.transform.TransformerConfigurationException;
@@ -61,7 +62,15 @@ public XSLTResourceTransformer(Source xsl) {
6162
public XSLTResourceTransformer(Source xsl, ResourceValidator validator) {
6263
this.validator = validator;
6364
try {
64-
templates = TransformerFactory.newInstance().newTemplates(xsl);
65+
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
66+
try {
67+
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
68+
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
69+
} catch (IllegalArgumentException ex) {
70+
// ignore
71+
}
72+
transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
73+
templates = transformerFactory.newTemplates(xsl);
6574
} catch (TransformerConfigurationException e) {
6675
LOG.severe(e.getLocalizedMessage());
6776
throw new SoapFault("Internal error", getSoapVersion().getReceiver());

systests/ws-transfer/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@
6969
</dependencies>
7070
<profiles>
7171
<profile>
72-
<id>jdk24</id>
72+
<id>jdk17</id>
7373
<activation>
74-
<jdk>[24,)</jdk>
74+
<jdk>[17,)</jdk>
7575
</activation>
7676
<build>
7777
<plugins>

0 commit comments

Comments
 (0)