GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid NPE in JdkSslClientContext when TrustManagerFactory returns nul… · netty/netty@a96f448

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/ssl
      • JdkSslClientContext.java
    • test/java/io/netty/handler/ssl
      • JdkSslClientContextTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/JdkSslClientContext.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/JdkSslClientContext.java
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,11 @@ private static SSLContext newSSLContext(Provider sslContextProvider,
318318
}
319319

320320
private static TrustManager[] wrapIfNeeded(TrustManager[] tms, ResumptionController resumptionController) {
321-
if (resumptionController != null) {
322-
for (int i = 0; i < tms.length; i++) {
323-
tms[i] = resumptionController.wrapIfNeeded(tms[i]);
324-
}
321+
if (tms == null || resumptionController == null) {
322+
return tms;
323+
}
324+
for (int i = 0; i < tms.length; i++) {
325+
tms[i] = resumptionController.wrapIfNeeded(tms[i]);
325326
}
326327
return tms;
327328
}
Collapse file

‎handler/src/test/java/io/netty/handler/ssl/JdkSslClientContextTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/JdkSslClientContextTest.java
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,73 @@
1616
package io.netty.handler.ssl;
1717

1818
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
19+
import org.junit.jupiter.api.Test;
1920

21+
import javax.net.ssl.ManagerFactoryParameters;
2022
import javax.net.ssl.SSLException;
23+
import javax.net.ssl.TrustManager;
24+
import javax.net.ssl.TrustManagerFactory;
25+
import javax.net.ssl.TrustManagerFactorySpi;
2126
import java.io.File;
27+
import java.security.KeyStore;
28+
import java.security.Provider;
29+
30+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2231

2332
public class JdkSslClientContextTest extends SslContextTest {
2433
@Override
2534
protected SslContext newSslContext(File crtFile, File keyFile, String pass) throws SSLException {
2635
return new JdkSslClientContext(crtFile, InsecureTrustManagerFactory.INSTANCE, crtFile, keyFile, pass,
2736
null, null, IdentityCipherSuiteFilter.INSTANCE, ApplicationProtocolConfig.DISABLED, 0, 0);
2837
}
38+
39+
// Reproduces https://github.com/netty/netty/issues/14488
40+
// Before the fix, wrapIfNeeded did tms.length without a null check, so a
41+
// TrustManagerFactory whose getTrustManagers() returns null surfaced as an
42+
// opaque NullPointerException wrapped in SSLException. After the fix the
43+
// context is built successfully, matching pre-4.1.114 behavior that relied
44+
// on SSLContext.init accepting a null TrustManager[] to use platform defaults.
45+
@Test
46+
public void testTrustManagerFactoryReturningNullDoesNotThrowNpe() throws Exception {
47+
TrustManagerFactory tmf = new TrustManagerFactory(
48+
new NullReturningTrustManagerFactorySpi(), NullReturningTrustManagerFactorySpi.PROVIDER, "null") {
49+
};
50+
// TrustManagerFactory must be initialized before SslContextBuilder will accept it;
51+
// without this call the builder throws before reaching the code path under test.
52+
tmf.init((KeyStore) null);
53+
54+
SslContext ctx = SslContextBuilder.forClient()
55+
.sslProvider(SslProvider.JDK)
56+
.trustManager(tmf)
57+
.build();
58+
// Success is "did not throw". Before the fix this call produced
59+
// SSLException("failed to initialize the client-side SSL context")
60+
// caused by NullPointerException from wrapIfNeeded.
61+
assertNotNull(ctx);
62+
}
63+
64+
private static final class NullReturningTrustManagerFactorySpi extends TrustManagerFactorySpi {
65+
// The Provider(String, double, String) constructor is deprecated since JDK 9 in
66+
// favor of (String, String, String), but the replacement is unavailable on the
67+
// JDK 8 source level this module targets, so we suppress the deprecation warning.
68+
@SuppressWarnings("deprecation")
69+
static final Provider PROVIDER = new Provider("NullReturningProvider", 1.0, "test-only") {
70+
private static final long serialVersionUID = 1L;
71+
};
72+
73+
@Override
74+
protected void engineInit(KeyStore ks) {
75+
// no-op
76+
}
77+
78+
@Override
79+
protected void engineInit(ManagerFactoryParameters spec) {
80+
// no-op
81+
}
82+
83+
@Override
84+
protected TrustManager[] engineGetTrustManagers() {
85+
return null;
86+
}
87+
}
2988
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.