Skip to content

Commit 3379fa1

Browse files
committed
🔒 Disable certificate validations in the Java HTTP Client.
1 parent b70c144 commit 3379fa1

File tree

1 file changed

+66
-5
lines changed

1 file changed

+66
-5
lines changed

src/main/java/io/jenkins/plugins/lark/notice/sdk/impl/AbstractMessageSender.java

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@
1010
import io.jenkins.plugins.lark.notice.tools.JsonUtils;
1111
import lombok.extern.slf4j.Slf4j;
1212

13+
import javax.net.ssl.SSLContext;
14+
import javax.net.ssl.SSLEngine;
15+
import javax.net.ssl.TrustManager;
16+
import javax.net.ssl.X509ExtendedTrustManager;
17+
import java.net.Socket;
1318
import java.net.URI;
1419
import java.net.http.HttpClient;
1520
import java.net.http.HttpRequest;
1621
import java.net.http.HttpResponse;
22+
import java.security.SecureRandom;
23+
import java.security.cert.X509Certificate;
1724
import java.time.Duration;
1825
import java.util.Optional;
1926

@@ -30,6 +37,40 @@
3037
@Slf4j
3138
public abstract class AbstractMessageSender implements MessageSender {
3239

40+
/**
41+
* Define a mock TrustManager to ignore certificate validation
42+
*/
43+
private static final TrustManager MOCK_TRUST_MANAGER = new X509ExtendedTrustManager() {
44+
@Override
45+
public X509Certificate[] getAcceptedIssuers() {
46+
return new X509Certificate[]{};
47+
}
48+
49+
@Override
50+
public void checkClientTrusted(X509Certificate[] chain, String authType) {
51+
}
52+
53+
@Override
54+
public void checkServerTrusted(X509Certificate[] chain, String authType) {
55+
}
56+
57+
@Override
58+
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) {
59+
}
60+
61+
@Override
62+
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) {
63+
}
64+
65+
@Override
66+
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
67+
}
68+
69+
@Override
70+
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
71+
}
72+
};
73+
3374
/**
3475
* Retrieves the robot configuration information.
3576
*
@@ -49,8 +90,11 @@ protected SendResult sendMessage(String body, String... headers) {
4990
RobotConfigModel robotConfig = getRobotConfig();
5091
String webhook = robotConfig.getWebhook();
5192

52-
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(webhook))
53-
.header(CONTENT_TYPE, APPLICATION_JSON_VALUE).timeout(Duration.ofMinutes(3))
93+
// Create HttpRequest.Builder
94+
HttpRequest.Builder builder = HttpRequest.newBuilder()
95+
.uri(URI.create(webhook))
96+
.header(CONTENT_TYPE, APPLICATION_JSON_VALUE)
97+
.timeout(Duration.ofMinutes(3))
5498
.POST(HttpRequest.BodyPublishers.ofString(StringUtils.defaultString(body)));
5599

56100
if (ArrayUtils.isNotEmpty(headers)) {
@@ -61,10 +105,9 @@ protected SendResult sendMessage(String body, String... headers) {
61105
builder.headers(headers);
62106
}
63107

64-
HttpResponse<String> response = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1)
65-
.followRedirects(HttpClient.Redirect.NORMAL).proxy(robotConfig.getProxySelector()).build()
108+
// Create HttpClient and send the request
109+
HttpResponse<String> response = createHttpClient(robotConfig)
66110
.send(builder.build(), HttpResponse.BodyHandlers.ofString());
67-
68111
sendResult = JsonUtils.readValue(response.body(), SendResult.class);
69112
} catch (Exception e) {
70113
log.error("Failed to send Lark message", e);
@@ -74,4 +117,22 @@ protected SendResult sendMessage(String body, String... headers) {
74117
return sendResult;
75118
}
76119

120+
/**
121+
* Create HttpClient.
122+
*
123+
* @param robotConfig Robot configuration information.
124+
* @return HttpClient instance.
125+
* @throws Exception Exception during HttpClient creation.
126+
*/
127+
private HttpClient createHttpClient(RobotConfigModel robotConfig) throws Exception {
128+
SSLContext sslContext = SSLContext.getInstance("TLS");
129+
sslContext.init(null, new TrustManager[]{MOCK_TRUST_MANAGER}, new SecureRandom());
130+
return HttpClient.newBuilder()
131+
.version(HttpClient.Version.HTTP_1_1)
132+
.followRedirects(HttpClient.Redirect.NORMAL)
133+
.proxy(robotConfig.getProxySelector())
134+
.sslContext(sslContext)
135+
.build();
136+
}
137+
77138
}

0 commit comments

Comments
 (0)