Skip to content

Commit

Permalink
🔒 Disable certificate validations in the Java HTTP Client.
Browse files Browse the repository at this point in the history
  • Loading branch information
721806280 committed Feb 26, 2024
1 parent b70c144 commit 5a0eef8
Showing 1 changed file with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@
import io.jenkins.plugins.lark.notice.sdk.MessageSender;
import io.jenkins.plugins.lark.notice.sdk.model.SendResult;
import io.jenkins.plugins.lark.notice.tools.JsonUtils;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509ExtendedTrustManager;
import java.net.Socket;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.Optional;

Expand All @@ -30,6 +38,40 @@
@Slf4j
public abstract class AbstractMessageSender implements MessageSender {

/**
* Define a mock TrustManager to ignore certificate validation
*/
private static final TrustManager MOCK_TRUST_MANAGER = new X509ExtendedTrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) {
}

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
}
};

/**
* Retrieves the robot configuration information.
*
Expand All @@ -49,8 +91,11 @@ protected SendResult sendMessage(String body, String... headers) {
RobotConfigModel robotConfig = getRobotConfig();
String webhook = robotConfig.getWebhook();

HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(webhook))
.header(CONTENT_TYPE, APPLICATION_JSON_VALUE).timeout(Duration.ofMinutes(3))
// Create HttpRequest.Builder
HttpRequest.Builder builder = HttpRequest.newBuilder()
.uri(URI.create(webhook))
.header(CONTENT_TYPE, APPLICATION_JSON_VALUE)
.timeout(Duration.ofMinutes(3))
.POST(HttpRequest.BodyPublishers.ofString(StringUtils.defaultString(body)));

if (ArrayUtils.isNotEmpty(headers)) {
Expand All @@ -61,10 +106,9 @@ protected SendResult sendMessage(String body, String... headers) {
builder.headers(headers);
}

HttpResponse<String> response = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.NORMAL).proxy(robotConfig.getProxySelector()).build()
.send(builder.build(), HttpResponse.BodyHandlers.ofString());

// Create HttpClient and send the request
@Cleanup HttpClient httpClient = createHttpClient(robotConfig);
HttpResponse<String> response = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofString());
sendResult = JsonUtils.readValue(response.body(), SendResult.class);
} catch (Exception e) {
log.error("Failed to send Lark message", e);
Expand All @@ -74,4 +118,22 @@ protected SendResult sendMessage(String body, String... headers) {
return sendResult;
}

/**
* Create HttpClient.
*
* @param robotConfig Robot configuration information.
* @return HttpClient instance.
* @throws Exception Exception during HttpClient creation.
*/
private HttpClient createHttpClient(RobotConfigModel robotConfig) throws Exception {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{MOCK_TRUST_MANAGER}, new SecureRandom());
return HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.NORMAL)
.proxy(robotConfig.getProxySelector())
.sslContext(sslContext)
.build();
}

}

0 comments on commit 5a0eef8

Please sign in to comment.