Skip to content

Commit fc5f066

Browse files
authored
Port unification support reject if client not TLS (#15352)
1 parent 2268c29 commit fc5f066

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServerHandler.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.dubbo.common.io.Bytes;
2121
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
2222
import org.apache.dubbo.common.logger.LoggerFactory;
23+
import org.apache.dubbo.common.ssl.AuthPolicy;
2324
import org.apache.dubbo.common.ssl.CertManager;
2425
import org.apache.dubbo.common.ssl.ProviderCert;
2526
import org.apache.dubbo.remoting.ChannelHandler;
@@ -45,6 +46,7 @@
4546
import io.netty.handler.ssl.SslHandshakeCompletionEvent;
4647
import io.netty.util.AttributeKey;
4748

49+
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_SSL_CONNECT_INSECURE;
4850
import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
4951

5052
public class NettyPortUnificationServerHandler extends ByteToMessageDecoder {
@@ -120,8 +122,27 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
120122
ProviderCert providerConnectionConfig =
121123
certManager.getProviderConnectionConfig(url, ctx.channel().remoteAddress());
122124

123-
if (providerConnectionConfig != null && isSsl(in)) {
124-
enableSsl(ctx, providerConnectionConfig);
125+
if (providerConnectionConfig != null && canDetectSsl(in)) {
126+
if (isSsl(in)) {
127+
enableSsl(ctx, providerConnectionConfig);
128+
} else {
129+
// check server should load TLS or not
130+
if (providerConnectionConfig.getAuthPolicy() != AuthPolicy.NONE) {
131+
byte[] preface = new byte[in.readableBytes()];
132+
in.readBytes(preface);
133+
LOGGER.error(
134+
CONFIG_SSL_CONNECT_INSECURE,
135+
"client request server without TLS",
136+
"",
137+
String.format(
138+
"Downstream=%s request without TLS preface, but server require it. " + "preface=%s",
139+
ctx.channel().remoteAddress(), Bytes.bytes2hex(preface)));
140+
141+
// Untrusted connection; discard everything and close the connection.
142+
in.clear();
143+
ctx.close();
144+
}
145+
}
125146
} else {
126147
Set<String> supportedProtocolNames = new HashSet<>(protocols.keySet());
127148
supportedProtocolNames.retainAll(urlMapper.keySet());
@@ -177,6 +198,11 @@ private void enableSsl(ChannelHandlerContext ctx, ProviderCert providerConnectio
177198
p.remove(this);
178199
}
179200

201+
private boolean canDetectSsl(ByteBuf buf) {
202+
// at least 5 bytes to determine if data is encrypted
203+
return detectSsl && buf.readableBytes() >= 5;
204+
}
205+
180206
private boolean isSsl(ByteBuf buf) {
181207
// at least 5 bytes to determine if data is encrypted
182208
if (detectSsl && buf.readableBytes() >= 5) {

0 commit comments

Comments
 (0)