Skip to content

Commit 5287974

Browse files
authored
Fix some sonarcloud warnings. (aws#2723)
1 parent 5f0e888 commit 5287974

File tree

3 files changed

+93
-12
lines changed
  • release-scripts/src/main/java/software/amazon/awssdk/release
  • test
    • protocol-tests-core/src/main/java/software/amazon/awssdk/protocol/asserts/marshalling
    • sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/utils

3 files changed

+93
-12
lines changed

release-scripts/src/main/java/software/amazon/awssdk/release/PomTransformer.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.amazon.awssdk.release;
1717

1818
import java.nio.file.Path;
19+
import javax.xml.XMLConstants;
1920
import javax.xml.parsers.DocumentBuilder;
2021
import javax.xml.parsers.DocumentBuilderFactory;
2122
import javax.xml.transform.OutputKeys;
@@ -33,7 +34,7 @@
3334

3435
public abstract class PomTransformer {
3536
public final void transform(Path file) throws Exception {
36-
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
37+
DocumentBuilderFactory docFactory = newSecureDocumentBuilderFactory();
3738
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
3839
Document doc = docBuilder.parse(file.toFile());
3940

@@ -50,7 +51,7 @@ public final void transform(Path file) throws Exception {
5051

5152
updateDocument(doc);
5253

53-
TransformerFactory transformerFactory = TransformerFactory.newInstance();
54+
TransformerFactory transformerFactory = newSecureTransformerFactory();
5455
transformerFactory.setAttribute("indent-number", 4);
5556
Transformer transformer = transformerFactory.newTransformer();
5657
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
@@ -102,4 +103,48 @@ protected final Element sdkDependencyElement(Document doc, String artifactId) {
102103

103104
return newDependency;
104105
}
106+
107+
private DocumentBuilderFactory newSecureDocumentBuilderFactory() {
108+
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
109+
docFactory.setXIncludeAware(false);
110+
docFactory.setExpandEntityReferences(false);
111+
trySetFeature(docFactory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
112+
trySetFeature(docFactory, "http://apache.org/xml/features/disallow-doctype-decl", true);
113+
trySetFeature(docFactory, "http://xml.org/sax/features/external-general-entities", false);
114+
trySetFeature(docFactory, "http://xml.org/sax/features/external-parameter-entities", false);
115+
trySetAttribute(docFactory, "http://javax.xml.XMLConstants/property/accessExternalDTD", "");
116+
trySetAttribute(docFactory, "http://javax.xml.XMLConstants/property/accessExternalSchema", "");
117+
return docFactory;
118+
}
119+
120+
private TransformerFactory newSecureTransformerFactory() {
121+
TransformerFactory transformerFactory = TransformerFactory.newInstance();
122+
trySetAttribute(transformerFactory, XMLConstants.ACCESS_EXTERNAL_DTD, "");
123+
trySetAttribute(transformerFactory, XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
124+
return transformerFactory;
125+
}
126+
127+
private void trySetFeature(DocumentBuilderFactory factory, String feature, boolean value) {
128+
try {
129+
factory.setFeature(feature, value);
130+
} catch (Exception e) {
131+
throw new RuntimeException(e);
132+
}
133+
}
134+
135+
private void trySetAttribute(DocumentBuilderFactory factory, String feature, String value) {
136+
try {
137+
factory.setAttribute(feature, value);
138+
} catch (Exception e) {
139+
throw new RuntimeException(e);
140+
}
141+
}
142+
143+
private void trySetAttribute(TransformerFactory factory, String feature, Object value) {
144+
try {
145+
factory.setAttribute(feature, value);
146+
} catch (Exception e) {
147+
throw new RuntimeException(e);
148+
}
149+
}
105150
}

test/protocol-tests-core/src/main/java/software/amazon/awssdk/protocol/asserts/marshalling/XmlAsserts.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import javax.xml.parsers.ParserConfigurationException;
2626
import javax.xml.transform.OutputKeys;
2727
import javax.xml.transform.Transformer;
28-
import javax.xml.transform.TransformerConfigurationException;
2928
import javax.xml.transform.TransformerFactory;
3029
import javax.xml.transform.dom.DOMSource;
3130
import javax.xml.transform.stream.StreamResult;
@@ -42,7 +41,7 @@ private XmlAsserts() {
4241
}
4342

4443
private static DocumentBuilder getDocumentBuilder() {
45-
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
44+
DocumentBuilderFactory dbf = newSecureDocumentBuilderFactory();
4645
try {
4746
return dbf.newDocumentBuilder();
4847
} catch (ParserConfigurationException e) {
@@ -74,7 +73,7 @@ private static String formatXml(String xmlDocumentString) throws Exception {
7473
}
7574

7675
private static String formatXml(Document xmlDocument) throws Exception {
77-
Transformer transformer = transformerFactory().newTransformer();
76+
Transformer transformer = newSecureTransformerFactory().newTransformer();
7877
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
7978
StreamResult result = new StreamResult(new StringWriter());
8079
DOMSource source = new DOMSource(xmlDocument);
@@ -84,12 +83,47 @@ private static String formatXml(Document xmlDocument) throws Exception {
8483
}
8584
}
8685

87-
private static TransformerFactory transformerFactory() throws TransformerConfigurationException {
88-
TransformerFactory factory = TransformerFactory.newInstance();
89-
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
90-
if (factory.getFeature(XMLConstants.ACCESS_EXTERNAL_DTD)) {
91-
factory.setFeature(XMLConstants.ACCESS_EXTERNAL_DTD, false);
86+
private static DocumentBuilderFactory newSecureDocumentBuilderFactory() {
87+
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
88+
docFactory.setXIncludeAware(false);
89+
docFactory.setExpandEntityReferences(false);
90+
trySetFeature(docFactory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
91+
trySetFeature(docFactory, "http://apache.org/xml/features/disallow-doctype-decl", true);
92+
trySetFeature(docFactory, "http://xml.org/sax/features/external-general-entities", false);
93+
trySetFeature(docFactory, "http://xml.org/sax/features/external-parameter-entities", false);
94+
trySetAttribute(docFactory, "http://javax.xml.XMLConstants/property/accessExternalDTD", "");
95+
trySetAttribute(docFactory, "http://javax.xml.XMLConstants/property/accessExternalSchema", "");
96+
return docFactory;
97+
}
98+
99+
private static TransformerFactory newSecureTransformerFactory() {
100+
TransformerFactory transformerFactory = TransformerFactory.newInstance();
101+
trySetAttribute(transformerFactory, XMLConstants.ACCESS_EXTERNAL_DTD, "");
102+
trySetAttribute(transformerFactory, XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
103+
return transformerFactory;
104+
}
105+
106+
private static void trySetFeature(DocumentBuilderFactory factory, String feature, boolean value) {
107+
try {
108+
factory.setFeature(feature, value);
109+
} catch (Exception e) {
110+
throw new RuntimeException(e);
111+
}
112+
}
113+
114+
private static void trySetAttribute(DocumentBuilderFactory factory, String feature, String value) {
115+
try {
116+
factory.setAttribute(feature, value);
117+
} catch (Exception e) {
118+
throw new RuntimeException(e);
119+
}
120+
}
121+
122+
private static void trySetAttribute(TransformerFactory factory, String feature, Object value) {
123+
try {
124+
factory.setAttribute(feature, value);
125+
} catch (Exception e) {
126+
throw new RuntimeException(e);
92127
}
93-
return factory;
94128
}
95129
}

test/sdk-benchmarks/src/main/java/software/amazon/awssdk/benchmark/utils/BenchmarkUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ public static SslProvider getSslProvider(String sslProviderValue) {
7070

7171
public static void awaitCountdownLatchUninterruptibly(CountDownLatch countDownLatch, int timeout, TimeUnit unit) {
7272
try {
73-
countDownLatch.await(timeout, unit);
73+
if (!countDownLatch.await(timeout, unit)) {
74+
throw new RuntimeException("Countdown latch did not successfully return within " + timeout + " " + unit);
75+
}
7476
} catch (InterruptedException e) {
7577
// No need to re-interrupt.
7678
logger.error(() -> "InterruptedException thrown ", e);

0 commit comments

Comments
 (0)