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 — Auto-port 4.1: Call `handshakeFailure` for failed TLS completion even… · netty/netty@989fde8

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
+187-5Lines changed: 187 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,40 @@
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;
2223
import io.netty.channel.embedded.EmbeddedChannel;
2324
import io.netty.channel.socket.ChannelInputShutdownEvent;
2425
import io.netty.handler.codec.DecoderException;
26+
import io.netty.util.AsyncMapping;
27+
import io.netty.util.concurrent.Future;
28+
import io.netty.util.concurrent.Promise;
2529
import org.junit.jupiter.api.Test;
2630
import org.junit.jupiter.api.function.Executable;
31+
import org.junit.jupiter.params.ParameterizedTest;
32+
import org.junit.jupiter.params.provider.MethodSource;
33+
import org.junit.jupiter.params.provider.ValueSource;
2734

2835
import javax.net.ssl.SSLContext;
2936
import javax.net.ssl.SSLEngine;
3037
import javax.net.ssl.SSLHandshakeException;
38+
import java.nio.channels.ClosedChannelException;
3139
import java.security.NoSuchAlgorithmException;
40+
import java.util.Arrays;
41+
import java.util.List;
3242
import java.util.concurrent.atomic.AtomicBoolean;
43+
import java.util.concurrent.atomic.AtomicInteger;
3344
import java.util.concurrent.atomic.AtomicReference;
3445

3546
import static io.netty.handler.ssl.CloseNotifyTest.assertCloseNotify;
3647
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3748
import static org.junit.jupiter.api.Assertions.assertEquals;
3849
import static org.junit.jupiter.api.Assertions.assertFalse;
50+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3951
import static org.junit.jupiter.api.Assertions.assertNull;
52+
import static org.junit.jupiter.api.Assertions.assertSame;
4053
import static org.junit.jupiter.api.Assertions.assertThrows;
4154
import static org.junit.jupiter.api.Assertions.assertTrue;
4255
import static org.junit.jupiter.api.Assertions.fail;
@@ -70,24 +83,193 @@ protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
7083
assertFalse(channel.finishAndReleaseAll());
7184
}
7285

73-
@Test
74-
public void testHandshakeFailure() {
86+
@ParameterizedTest
87+
@ValueSource(booleans = {false, true})
88+
public void testHandshakeFailure(final boolean exceptionDuringEvent) {
89+
final AtomicInteger failureCount = new AtomicInteger();
90+
final AtomicInteger failureCountOnExceptionReturn = new AtomicInteger();
91+
final AtomicInteger downstreamExceptionCount = new AtomicInteger();
92+
final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>();
7593
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
7694
@Override
7795
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
7896
fail();
7997
}
98+
99+
@Override
100+
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) {
101+
failureCount.incrementAndGet();
102+
failureCause.set(cause);
103+
}
104+
105+
@Override
106+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
107+
super.exceptionCaught(ctx, cause);
108+
failureCountOnExceptionReturn.set(failureCount.get());
109+
}
80110
};
81111

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

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