Skip to content

Commit 94b9156

Browse files
committed
chore: fixed some sonar issues
Refs: XRDDEV-2628
1 parent b48c092 commit 94b9156

File tree

6 files changed

+115
-4
lines changed

6 files changed

+115
-4
lines changed

src/client/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ dependencies {
77
api(project(":rest"))
88

99
testImplementation(libs.bundles.testImplementation)
10+
testImplementation(libs.org.wiremock.wiremock)
11+
implementation(libs.org.assertj.assertjCore)
12+
1013
}
1114

1215

src/client/src/main/java/org/niis/xrd4j/client/util/ClientUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private ClientUtil() {
6969
*/
7070
public static void doTrustToCertificates() throws XRd4JException {
7171
// Create a trust manager that does not validate certificate chains
72+
@SuppressWarnings("java:S4830") // Sonar rule: "Server certificates should be verified during SSL/TLS connections"
7273
TrustManager[] trustAllCerts = new TrustManager[]{
7374
new X509TrustManager() {
7475

@@ -91,7 +92,7 @@ public void checkClientTrusted(X509Certificate[] certs, String authType) throws
9192

9293
try {
9394
// Install the all-trusting trust manager
94-
SSLContext sc = SSLContext.getInstance("SSL");
95+
SSLContext sc = SSLContext.getInstance("TLSv1.2");
9596
sc.init(null, trustAllCerts, new SecureRandom());
9697
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
9798
HostnameVerifier hv = (String urlHostName, SSLSession session) -> {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2018 Nordic Institute for Interoperability Solutions (NIIS)
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package org.niis.xrd4j.client.util;
24+
25+
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
26+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
27+
import org.assertj.core.api.Assertions;
28+
import org.junit.jupiter.api.AfterEach;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
31+
32+
import javax.net.ssl.HostnameVerifier;
33+
import javax.net.ssl.HttpsURLConnection;
34+
import javax.net.ssl.SSLHandshakeException;
35+
import javax.net.ssl.SSLSocketFactory;
36+
37+
import java.io.IOException;
38+
import java.net.HttpURLConnection;
39+
import java.net.URL;
40+
41+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
42+
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
43+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
44+
import static jakarta.servlet.http.HttpServletResponse.SC_OK;
45+
import static org.assertj.core.api.Assertions.assertThat;
46+
47+
@WireMockTest(httpsEnabled = true)
48+
class ClientUtilTest {
49+
50+
private SSLSocketFactory defaultSSLSocketFactory;
51+
private HostnameVerifier defaultHostnameVerifier;
52+
53+
@BeforeEach
54+
void setUp() {
55+
defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
56+
defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
57+
}
58+
59+
@AfterEach
60+
void tearDown() {
61+
HttpsURLConnection.setDefaultSSLSocketFactory(defaultSSLSocketFactory);
62+
HttpsURLConnection.setDefaultHostnameVerifier(defaultHostnameVerifier);
63+
}
64+
65+
66+
@Test
67+
void certificateNotTrusted(WireMockRuntimeInfo wm) throws Exception {
68+
stubFor(get("/").willReturn(ok()));
69+
70+
URL url = new URL(wm.getHttpsBaseUrl());
71+
var connection = (HttpURLConnection) url.openConnection();
72+
73+
Assertions.assertThatThrownBy(connection::getResponseCode)
74+
.isInstanceOf(SSLHandshakeException.class)
75+
.hasMessageStartingWith("PKIX path building failed");
76+
77+
}
78+
79+
@Test
80+
void doTrustToCertificates(WireMockRuntimeInfo wm) throws Exception {
81+
stubFor(get("/").willReturn(ok()));
82+
83+
ClientUtil.doTrustToCertificates();
84+
85+
var responseCode = getResponseCode(wm.getHttpsBaseUrl());
86+
87+
assertThat(responseCode).isEqualTo(SC_OK);
88+
}
89+
90+
private int getResponseCode(String url) throws IOException {
91+
var connection = (HttpURLConnection) new URL(url).openConnection();
92+
return connection.getResponseCode();
93+
}
94+
95+
}

src/common/src/main/java/org/niis/xrd4j/common/security/SymmetricEncrypter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public SymmetricEncrypter(Key key, byte[] iv, String transformation) {
8989
* @throws BadPaddingException if there's an error
9090
*/
9191
@Override
92+
@SuppressWarnings("java:S3329")
9293
protected byte[] encrypt(byte[] plaintext) throws NoSuchAlgorithmException, InvalidKeyException,
9394
InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
9495
Cipher cipher = Cipher.getInstance(this.transformation);

src/gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ org-apache-tomcat-embed-core = { group = "org.apache.tomcat.embed", name = "tomc
2525
org-assertj-assertjCore = { module = "org.assertj:assertj-core", version.ref = "assertj" }
2626
org-xmlunit-xmlunitAssertj3 = { module = "org.xmlunit:xmlunit-assertj3", version.ref = "xmlunit" }
2727
org-xmlunit-xmlunitPlaceholders = { module = "org.xmlunit:xmlunit-placeholders", version.ref = "xmlunit" }
28+
org-wiremock-wiremock = { module = "org.wiremock:wiremock", version = "3.9.1" }
2829

2930
licenseGradlePlugin = { module = "gradle.plugin.com.hierynomus.gradle.plugins:license-gradle-plugin", version = "0.16.1" }
3031
dependencyCheckGradlePlugin = { module = "org.owasp:dependency-check-gradle", version = "10.0.4" }

src/server/src/main/java/org/niis/xrd4j/server/AbstractAdapterServlet.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.niis.xrd4j.server.utils.AdapterUtils;
3737

3838
import jakarta.servlet.ServletException;
39+
import jakarta.servlet.ServletInputStream;
3940
import jakarta.servlet.http.HttpServlet;
4041
import jakarta.servlet.http.HttpServletRequest;
4142
import jakarta.servlet.http.HttpServletResponse;
@@ -133,12 +134,12 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
133134
if (contentTypeMatches(requestContentType, Constants.TEXT_XML)) {
134135
// Regular SOAP message without attachments
135136
LOGGER.info("Request's content type is \"{}\".", Constants.TEXT_XML);
136-
soapRequest = SOAPHelper.toSOAP(request.getInputStream());
137+
soapRequest = SOAPHelper.toSOAP(getInputStream(request));
137138
} else if (contentTypeMatches(requestContentType, Constants.MULTIPART_RELATED)) {
138139
// SOAP message with attachments
139140
LOGGER.info("Request's content type is \"{}\".", Constants.MULTIPART_RELATED);
140141
MimeHeaders mh = AdapterUtils.getHeaders(request);
141-
soapRequest = SOAPHelper.toSOAP(request.getInputStream(), mh);
142+
soapRequest = SOAPHelper.toSOAP(getInputStream(request), mh);
142143
LOGGER.trace(AdapterUtils.getAttachmentsInfo(soapRequest));
143144
} else {
144145
// Invalid content type -> message is not processed
@@ -149,7 +150,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
149150
// Conversion has failed if soapRequest is null. Return SOAP Fault.
150151
if (soapRequest == null) {
151152
LOGGER.warn("Unable to deserialize the request to SOAP. SOAP Fault is returned.");
152-
LOGGER.trace("Incoming message : \"{}\"", request.getInputStream().toString());
153+
LOGGER.trace("Incoming message : \"{}\"", getInputStream(request));
153154
ErrorMessage errorMessage = new ErrorMessage(FAULT_CODE_CLIENT, errString, "", "");
154155
soapResponse = this.errorToSOAP(errorMessage, null);
155156
}
@@ -174,6 +175,15 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
174175
writeResponse(soapResponse, response);
175176
}
176177

178+
private ServletInputStream getInputStream(HttpServletRequest request) {
179+
try {
180+
return request.getInputStream();
181+
} catch (IOException e) {
182+
LOGGER.error("Error getting InputStream from request", e);
183+
return null;
184+
}
185+
}
186+
177187
private boolean contentTypeMatches(String contentType, String expected) {
178188
return contentType != null && contentType.toLowerCase().startsWith(expected);
179189
}

0 commit comments

Comments
 (0)