From 636244c287d58ce83f2536de06afcbd6e952c3ad Mon Sep 17 00:00:00 2001 From: roy Date: Mon, 26 Apr 2021 16:32:10 +0800 Subject: [PATCH] adjust validation logic when websocket server check starts with '/' (#11191) Motivation: When create a WebSocketServerProtocolConfig to check URI path starts from '/', only '/' or '//subPath' can be passed by the checker,but '/subPath' should be passed as well Modifications: in `WebSocketServerProtocolHandshakeHandler.isWebSocketPath()` treat '/' a special case Result: '/subPath' can be passed --- ...bSocketServerProtocolHandshakeHandler.java | 2 +- .../WebSocketServerProtocolHandlerTest.java | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java index e205d0d91c..15bc472f6b 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java @@ -112,7 +112,7 @@ class WebSocketServerProtocolHandshakeHandler implements ChannelHandler { String websocketPath = serverConfig.websocketPath(); String uri = req.uri(); boolean checkStartUri = uri.startsWith(websocketPath); - boolean checkNextUri = checkNextUri(uri, websocketPath); + boolean checkNextUri = "/".equals(websocketPath) || checkNextUri(uri, websocketPath); return serverConfig.checkStartsWith() ? (checkStartUri && checkNextUri) : uri.equals(websocketPath); } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java index 426bf591f5..6d949f055f 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandlerTest.java @@ -193,6 +193,38 @@ public class WebSocketServerProtocolHandlerTest { assertFalse(ch.finish()); } + @Test + public void testCheckWebSocketPathStartWithSlash() { + WebSocketRequestBuilder builder = new WebSocketRequestBuilder().httpVersion(HTTP_1_1) + .method(HttpMethod.GET) + .key(HttpHeaderNames.SEC_WEBSOCKET_KEY) + .connection("Upgrade") + .upgrade(HttpHeaderValues.WEBSOCKET) + .version13(); + + WebSocketServerProtocolConfig config = WebSocketServerProtocolConfig.newBuilder() + .websocketPath("/") + .checkStartsWith(true) + .build(); + + FullHttpResponse response; + + createChannel(config, null).writeInbound(builder.uri("/test").build()); + response = responses.remove(); + assertEquals(SWITCHING_PROTOCOLS, response.status()); + response.release(); + + createChannel(config, null).writeInbound(builder.uri("/?q=v").build()); + response = responses.remove(); + assertEquals(SWITCHING_PROTOCOLS, response.status()); + response.release(); + + createChannel(config, null).writeInbound(builder.uri("/").build()); + response = responses.remove(); + assertEquals(SWITCHING_PROTOCOLS, response.status()); + response.release(); + } + @Test public void testCheckValidWebSocketPath() { HttpRequest httpRequest = new WebSocketRequestBuilder().httpVersion(HTTP_1_1) @@ -400,6 +432,10 @@ public class WebSocketServerProtocolHandlerTest { .websocketPath("/test") .sendCloseFrame(null) .build(); + return createChannel(serverConfig, handler); + } + + private EmbeddedChannel createChannel(WebSocketServerProtocolConfig serverConfig, ChannelHandler handler) { return new EmbeddedChannel( new WebSocketServerProtocolHandler(serverConfig), new HttpRequestDecoder(),