Skip to content

Commit 0024b09

Browse files
Fix CodeQL: remove trust-all TrustManager (CWE-295)
- Remove TRUST_ALL X509TrustManager that accepted any server certificate - buildSslSocketFactory now throws MtlsMsiException if insecure=true is passed - Pass null TrustManagers to SSLContext.init() so JVM default trust store is used - Remove unused TrustManager and X509TrustManager imports Resolves GitHub Advanced Security CodeQL alert: 'TrustManager that accepts all certificates' (High) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b62934a commit 0024b09

1 file changed

Lines changed: 4 additions & 11 deletions

File tree

msal4j-mtls-extensions/src/main/java/com/microsoft/aad/msal4j/mtls/MtlsMsiClient.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import javax.net.ssl.KeyManager;
1010
import javax.net.ssl.SSLContext;
1111
import javax.net.ssl.SSLSocketFactory;
12-
import javax.net.ssl.TrustManager;
1312
import javax.net.ssl.X509KeyManager;
14-
import javax.net.ssl.X509TrustManager;
1513
import java.io.BufferedReader;
1614
import java.io.IOException;
1715
import java.io.InputStream;
@@ -191,14 +189,16 @@ private static String urlEncode(String s) {
191189
private static SSLSocketFactory buildSslSocketFactory(MtlsBindingInfo binding,
192190
boolean insecure)
193191
throws MtlsMsiException {
192+
if (insecure) {
193+
throw new MtlsMsiException("Insecure trust-all TLS mode is not supported.");
194+
}
194195
CngProvider.installIfAbsent();
195196

196197
X509KeyManager km = new CngX509KeyManager(binding.privateKey, binding.certificate);
197-
TrustManager[] tms = insecure ? new TrustManager[]{TRUST_ALL} : null;
198198

199199
try {
200200
SSLContext ctx = SSLContext.getInstance("TLS");
201-
ctx.init(new KeyManager[]{km}, tms, null);
201+
ctx.init(new KeyManager[]{km}, null, null);
202202
return ctx.getSocketFactory();
203203
} catch (NoSuchAlgorithmException | KeyManagementException e) {
204204
throw new MtlsMsiException("Failed to build mTLS SSLContext: " + e.getMessage(), e);
@@ -230,13 +230,6 @@ private static final class CngX509KeyManager implements X509KeyManager {
230230
@Override public String chooseServerAlias(String keyType, Principal[] issuers, Socket s) { return null; }
231231
}
232232

233-
/** Accepts any server certificate — for testing only. */
234-
private static final TrustManager TRUST_ALL = new X509TrustManager() {
235-
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
236-
public void checkClientTrusted(X509Certificate[] c, String a) {}
237-
public void checkServerTrusted(X509Certificate[] c, String a) {}
238-
};
239-
240233
// ─── HTTP helpers ─────────────────────────────────────────────────────────
241234

242235
private static String httpsPost(String urlStr, String body, String contentType,

0 commit comments

Comments
 (0)