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 JdkSslServerContext when TrustManagerFactory returns nul… by normanmaurer · Pull Request #16700 · netty/netty

Avoid NPE in JdkSslServerContext when TrustManagerFactory returns nul… #16700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
normanmaurer merged 1 commit into 4.2 from trust_null_42
Apr 24, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Avoid NPE in JdkSslServerContext when TrustManagerFactory returns nul… #16700

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
743f8f0
Avoid NPE in JdkSslServerContext when TrustManagerFactory returns nul…
daguimu Apr 24, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Avoid NPE in JdkSslServerContext when TrustManagerFactory returns null (
#16691)

`JdkSslServerContext.newSSLContext` passes
`trustManagerFactory.getTrustManagers()` through
`wrapTrustManagerIfNeeded` before calling `SSLContext.init`. If a caller
supplies a `TrustManagerFactory` whose `getTrustManagers()` legitimately
returns `null` (a valid return value that asks `SSLContext.init` to fall
back to the JDK default trust store), building the server context fails
with:

```
javax.net.ssl.SSLException: failed to initialize the server-side SSL context
Caused by: java.lang.NullPointerException: Cannot read the array length because "trustManagers" is null
    at io.netty.handler.ssl.JdkSslServerContext.wrapTrustManagerIfNeeded(JdkSslServerContext.java:364)
    at io.netty.handler.ssl.JdkSslServerContext.newSSLContext(JdkSslServerContext.java:341)
```

This mirrors the client-side bug reported in #14488 / fixed in PR
`JdkSslServerContext` has routed trust managers through
`wrapTrustManagerIfNeeded` since the `EnhancingX509ExtendedTrustManager`
wrapping was introduced, long before the `ResumptionController` wiring
was added — so the NPE has been latent on the server path for multiple
releases when a user supplies such a factory.

```java
private static TrustManager[] wrapTrustManagerIfNeeded(
        TrustManager[] trustManagers, ResumptionController resumptionController) {
    if (WRAP_TRUST_MANAGER && PlatformDependent.javaVersion() >= 7) {
        for (int i = 0; i < trustManagers.length; i++) {     // NPE when trustManagers is null
            ...
        }
    }
    return trustManagers;
}
```

`WRAP_TRUST_MANAGER` is `true` on Java 7+ (guarded only by a FIPS
feature-check that passes on common JVMs), and `trustManagerFactory`
reaches this helper either as the user-supplied factory or — when the
user passes `null` — as a default-initialized `TrustManagerFactory`. The
user-supplied path is what fails: if the factory's `getTrustManagers()`
returns `null`, `trustManagers.length` dereferences `null`.

Short-circuit `wrapTrustManagerIfNeeded` when `trustManagers` is `null`,
returning `null` so it flows into `SSLContext.init` exactly as it did
before any wrapping was applied. `SSLContext.init(km, null, sr)` is
explicitly documented to fall back to the default trust manager.

```java
if (trustManagers == null) {
    return null;
}
```

Scope is limited to this one helper. Public APIs, the
`EnhancingX509ExtendedTrustManager` wrapping path, and the
`ResumptionController` wrapping path are all unchanged.

| Change point | Test |
|---|---|
| `wrapTrustManagerIfNeeded` null-array guard |
`JdkSslServerContextTest.testTrustManagerFactoryReturningNullDoesNotThrowNpe`
— installs a custom `TrustManagerFactorySpi` whose
`engineGetTrustManagers()` returns `null`, builds the server
`SslContext` via `SslContextBuilder.forServer(cert,
key).sslProvider(JDK).trustManager(tmf)`, and asserts it is returned
without throwing. Verified to reproduce the
`SSLException`/`NullPointerException` on the pre-fix code (`Cannot read
the array length because "trustManagers" is null`) and to pass after the
fix. |

All 36 tests in `JdkSslServerContextTest` pass locally (0 failures, 0
errors, 1 pre-existing skip).

- `TrustManagerFactory` implementations that return `null` from
`getTrustManagers()` (i.e., "use the JDK defaults") now work with the
JDK-backed `SslContextBuilder.forServer()`.
- Purely defensive change inside a private helper. The fix pattern
parallels PR #16690 for `JdkSslClientContext.wrapIfNeeded`.

Paired with #16690 (same bug pattern on the client side).
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@daguimu @normanmaurer
daguimu authored and normanmaurer committed Apr 24, 2026
commit 743f8f0f808d9e76bee3d683553ce316fd69dd4d

There are no files selected for viewing

4 changes: 4 additions & 0 deletions 4 handler/src/main/java/io/netty/handler/ssl/JdkSslServerContext.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ private static SSLContext newSSLContext(Provider sslContextProvider, X509Certifi

private static TrustManager[] wrapTrustManagerIfNeeded(
TrustManager[] trustManagers, ResumptionController resumptionController) {

if (trustManagers == null) {
return null;
}
if (WRAP_TRUST_MANAGER) {
for (int i = 0; i < trustManagers.length; i++) {
TrustManager tm = trustManagers[i];
Expand Down
63 changes: 63 additions & 0 deletions 63 handler/src/test/java/io/netty/handler/ssl/JdkSslServerContextTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@
*/
package io.netty.handler.ssl;

import io.netty.handler.ssl.util.SelfSignedCertificate;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import javax.net.ssl.ManagerFactoryParameters;
import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.TrustManagerFactorySpi;
import java.io.File;
import java.security.KeyStore;
import java.security.Provider;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class JdkSslServerContextTest extends SslContextTest {

Expand All @@ -38,4 +47,58 @@ public void execute() throws Throwable {
}
});
}

// A TrustManagerFactory whose getTrustManagers() legitimately returns null
// (e.g., asking SSLContext.init to fall back to the JDK default trust store)
// previously NPE'd inside wrapTrustManagerIfNeeded. Verify the server context
// now builds without throwing.
@Test
void testTrustManagerFactoryReturningNullDoesNotThrowNpe() throws Exception {
SelfSignedCertificate ssc = new SelfSignedCertificate();
try {
TrustManagerFactory tmf = new TrustManagerFactory(
new NullReturningTrustManagerFactorySpi(),
NullReturningTrustManagerFactorySpi.PROVIDER, "null") {
};
// TrustManagerFactory must be initialized before SslContextBuilder will accept it;
// without this call the builder throws before reaching the code path under test.
tmf.init((KeyStore) null);

SslContext ctx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
.sslProvider(SslProvider.JDK)
.trustManager(tmf)
.build();
// Success is "did not throw". Before the fix this call produced
// SSLException("failed to initialize the server-side SSL context")
// caused by NullPointerException from wrapTrustManagerIfNeeded.
assertNotNull(ctx);
} finally {
ssc.delete();
}
}

private static final class NullReturningTrustManagerFactorySpi extends TrustManagerFactorySpi {
// The Provider(String, double, String) constructor is deprecated since JDK 9 in
// favor of (String, String, String), but the replacement is unavailable on the
// JDK 8 source level this module targets, so we suppress the deprecation warning.
@SuppressWarnings("deprecation")
static final Provider PROVIDER = new Provider("NullReturningProvider", 1.0, "test-only") {
private static final long serialVersionUID = 1L;
};

@Override
protected void engineInit(KeyStore ks) {
// no-op
}

@Override
protected void engineInit(ManagerFactoryParameters spec) {
// no-op
}

@Override
protected TrustManager[] engineGetTrustManagers() {
return null;
}
}
}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.