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 — Call `handshakeFailure` for failed TLS completion events (#18323) · netty/netty@ce30707

File tree

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

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/ApplicationProtocolNegotiationHandler.java
+19-7Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public abstract class ApplicationProtocolNegotiationHandler extends ChannelInbou
7373
private final RecyclableArrayList bufferedMessages = RecyclableArrayList.newInstance();
7474
private ChannelHandlerContext ctx;
7575
private boolean sslHandlerChecked;
76+
private boolean handshakeFailed;
7677

7778
/**
7879
* Creates a new instance with the specified fallback protocol name.
@@ -138,16 +139,19 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
138139
String protocol = sslHandler.applicationProtocol();
139140
configurePipeline(ctx, protocol != null ? protocol : fallbackProtocol);
140141
} else {
141-
// if the event is not produced because of an successful handshake we will receive the same
142-
// exception in exceptionCaught(...) and handle it there. This will allow us more fine-grained
143-
// control over which exception we propagate down the ChannelPipeline.
144-
//
145-
// See https://github.com/netty/netty/issues/10342
142+
try {
143+
super.userEventTriggered(ctx, evt);
144+
} finally {
145+
this.notifyHandshakeFailure(ctx, handshakeEvent.cause());
146+
}
147+
148+
return;
146149
}
147150
} catch (Throwable cause) {
148151
exceptionCaught(ctx, cause);
149152
} finally {
150-
// Handshake failures are handled in exceptionCaught(...).
153+
// consume later SSL decoder exceptions to avoid downstream warnings
154+
// see https://github.com/netty/netty/issues/10342; timeouts and early closes may only emit this event
151155
if (handshakeEvent.isSuccess()) {
152156
removeSelfIfPresent(ctx);
153157
}
@@ -192,17 +196,25 @@ protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) thro
192196
ctx.close();
193197
}
194198

199+
private void notifyHandshakeFailure(ChannelHandlerContext ctx, Throwable cause) throws Exception {
200+
if (!this.handshakeFailed) {
201+
this.handshakeFailed = true;
202+
this.handshakeFailure(ctx, cause);
203+
}
204+
}
205+
195206
@Override
196207
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
197208
Throwable wrapped;
198209
if (cause instanceof DecoderException && ((wrapped = cause.getCause()) instanceof SSLException)) {
199210
try {
200-
handshakeFailure(ctx, wrapped);
211+
this.notifyHandshakeFailure(ctx, wrapped);
201212
return;
202213
} finally {
203214
removeSelfIfPresent(ctx);
204215
}
205216
}
217+
206218
logger.warn("{} Failed to select the application-level protocol:", ctx.channel(), cause);
207219
ctx.fireExceptionCaught(cause);
208220
ctx.close();
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/ApplicationProtocolNegotiationHandlerTest.java
+177-3Lines changed: 177 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.netty.handler.ssl;
1717

1818
import io.netty.buffer.ByteBuf;
19+
import io.netty.buffer.Unpooled;
1920
import io.netty.channel.ChannelHandler;
2021
import io.netty.channel.ChannelHandlerContext;
2122
import io.netty.channel.ChannelInboundHandlerAdapter;
@@ -24,19 +25,28 @@
2425
import io.netty.handler.codec.DecoderException;
2526
import org.junit.jupiter.api.Test;
2627
import org.junit.jupiter.api.function.Executable;
28+
import org.junit.jupiter.params.ParameterizedTest;
29+
import org.junit.jupiter.params.provider.Arguments;
30+
import org.junit.jupiter.params.provider.MethodSource;
31+
import org.junit.jupiter.params.provider.ValueSource;
2732

2833
import javax.net.ssl.SSLContext;
2934
import javax.net.ssl.SSLEngine;
3035
import javax.net.ssl.SSLHandshakeException;
36+
import java.nio.channels.ClosedChannelException;
3137
import java.security.NoSuchAlgorithmException;
3238
import java.util.concurrent.atomic.AtomicBoolean;
39+
import java.util.concurrent.atomic.AtomicInteger;
3340
import java.util.concurrent.atomic.AtomicReference;
41+
import java.util.stream.Stream;
3442

3543
import static io.netty.handler.ssl.CloseNotifyTest.assertCloseNotify;
3644
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3745
import static org.junit.jupiter.api.Assertions.assertEquals;
3846
import static org.junit.jupiter.api.Assertions.assertFalse;
47+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3948
import static org.junit.jupiter.api.Assertions.assertNull;
49+
import static org.junit.jupiter.api.Assertions.assertSame;
4050
import static org.junit.jupiter.api.Assertions.assertThrows;
4151
import static org.junit.jupiter.api.Assertions.assertTrue;
4252
import static org.junit.jupiter.api.Assertions.fail;
@@ -70,24 +80,188 @@ protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
7080
assertFalse(channel.finishAndReleaseAll());
7181
}
7282

73-
@Test
74-
public void testHandshakeFailure() {
83+
@ParameterizedTest
84+
@ValueSource(booleans = {false, true})
85+
public void testHandshakeFailure(boolean exceptionDuringEvent) {
86+
AtomicInteger failureCount = new AtomicInteger();
87+
AtomicInteger failureCountOnExceptionReturn = new AtomicInteger();
88+
AtomicInteger downstreamExceptionCount = new AtomicInteger();
89+
AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>();
7590
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
7691
@Override
7792
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
7893
fail();
7994
}
95+
96+
@Override
97+
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) {
98+
failureCount.incrementAndGet();
99+
failureCause.set(cause);
100+
}
101+
102+
@Override
103+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
104+
super.exceptionCaught(ctx, cause);
105+
failureCountOnExceptionReturn.set(failureCount.get());
106+
}
80107
};
81108

82-
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler);
83109
SSLHandshakeException exception = new SSLHandshakeException("error");
84110
SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(exception);
111+
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler, new ChannelInboundHandlerAdapter() {
112+
@Override
113+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
114+
if (exceptionDuringEvent && evt instanceof SslHandshakeCompletionEvent) {
115+
ctx.pipeline().fireExceptionCaught(new DecoderException(exception));
116+
}
117+
118+
ctx.fireUserEventTriggered(evt);
119+
}
120+
}, new ChannelInboundHandlerAdapter() {
121+
@Override
122+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
123+
downstreamExceptionCount.incrementAndGet();
124+
}
125+
});
126+
85127
channel.pipeline().fireUserEventTriggered(completionEvent);
128+
129+
if (!exceptionDuringEvent) {
130+
channel.pipeline().fireExceptionCaught(new DecoderException(exception));
131+
}
132+
133+
assertEquals(1, failureCount.get());
134+
assertEquals(1, failureCountOnExceptionReturn.get());
135+
assertEquals(0, downstreamExceptionCount.get());
136+
assertSame(exception, failureCause.get());
137+
assertNull(channel.pipeline().context(alpnHandler));
138+
assertFalse(channel.finishAndReleaseAll());
139+
}
140+
141+
@ParameterizedTest
142+
@MethodSource("handshakeFailureCauses")
143+
public void testHandshakeFailureEvent(Throwable cause) {
144+
AtomicReference<SslHandshakeCompletionEvent> eventRef =
145+
new AtomicReference<SslHandshakeCompletionEvent>();
146+
AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>();
147+
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
148+
@Override
149+
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
150+
fail();
151+
}
152+
153+
@Override
154+
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) throws Exception {
155+
failureCause.set(cause);
156+
super.handshakeFailure(ctx, cause);
157+
}
158+
};
159+
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler, new ChannelInboundHandlerAdapter() {
160+
@Override
161+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
162+
if (evt instanceof SslHandshakeCompletionEvent) {
163+
eventRef.set((SslHandshakeCompletionEvent) evt);
164+
}
165+
}
166+
});
167+
SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(cause);
168+
169+
channel.pipeline().fireUserEventTriggered(completionEvent);
170+
171+
assertSame(cause, failureCause.get());
172+
assertSame(completionEvent, eventRef.get());
173+
assertFalse(channel.isOpen());
174+
assertFalse(channel.finishAndReleaseAll());
175+
}
176+
177+
private static Stream<Arguments> handshakeFailureCauses() {
178+
return Stream.of(
179+
Arguments.of(new SslHandshakeTimeoutException("timed out")),
180+
Arguments.of(new ClosedChannelException()));
181+
}
182+
183+
@Test
184+
public void testHandshakeFailureExceptionWithoutEvent() {
185+
AtomicInteger failureCount = new AtomicInteger();
186+
AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>();
187+
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
188+
@Override
189+
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
190+
fail();
191+
}
192+
193+
@Override
194+
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) {
195+
failureCount.incrementAndGet();
196+
failureCause.set(cause);
197+
}
198+
};
199+
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler);
200+
SSLHandshakeException exception = new SSLHandshakeException("error");
201+
86202
channel.pipeline().fireExceptionCaught(new DecoderException(exception));
203+
204+
assertEquals(1, failureCount.get());
205+
assertSame(exception, failureCause.get());
87206
assertNull(channel.pipeline().context(alpnHandler));
88207
assertFalse(channel.finishAndReleaseAll());
89208
}
90209

210+
@Test
211+
public void testSniLookupFailure() {
212+
SSLHandshakeException exception = new SSLHandshakeException("SNI lookup failed");
213+
AtomicInteger failureCount = new AtomicInteger();
214+
AtomicInteger handshakeEventCount = new AtomicInteger();
215+
AtomicInteger downstreamExceptionCount = new AtomicInteger();
216+
AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>();
217+
AtomicReference<SniCompletionEvent> sniEvent = new AtomicReference<SniCompletionEvent>();
218+
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
219+
@Override
220+
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
221+
fail();
222+
}
223+
224+
@Override
225+
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) {
226+
failureCount.incrementAndGet();
227+
failureCause.set(cause);
228+
}
229+
};
230+
SniHandler sniHandler = new SniHandler((hostname, promise) -> promise.setFailure(exception));
231+
EmbeddedChannel channel = new EmbeddedChannel(sniHandler, alpnHandler, new ChannelInboundHandlerAdapter() {
232+
@Override
233+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
234+
if (evt instanceof SniCompletionEvent) {
235+
sniEvent.set((SniCompletionEvent) evt);
236+
} else if (evt instanceof SslHandshakeCompletionEvent) {
237+
handshakeEventCount.incrementAndGet();
238+
}
239+
240+
ctx.fireUserEventTriggered(evt);
241+
}
242+
243+
@Override
244+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
245+
downstreamExceptionCount.incrementAndGet();
246+
}
247+
});
248+
249+
try {
250+
channel.writeInbound(Unpooled.buffer(5).writeByte(SslUtils.SSL_CONTENT_TYPE_APPLICATION_DATA)
251+
.writeShort(0x0303).writeShort(0));
252+
253+
assertNotNull(sniEvent.get());
254+
assertSame(exception, sniEvent.get().cause());
255+
assertSame(exception, failureCause.get());
256+
assertEquals(1, failureCount.get());
257+
assertEquals(0, handshakeEventCount.get());
258+
assertEquals(0, downstreamExceptionCount.get());
259+
} finally {
260+
channel.pipeline().remove(sniHandler);
261+
channel.finishAndReleaseAll();
262+
}
263+
}
264+
91265
@Test
92266
public void testHandshakeSuccess() throws NoSuchAlgorithmException {
93267
testHandshakeSuccess0(false);

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.