Skip to content

Commit 2057666

Browse files
authored
Align TLS fragments size with negotiated TDS packet size (#1438)
* Align TLS fragments size with negotiated TDS packet size See #1433 When TLS fragments are bigger than the negotiated TDS packet size, the server may close the connection abruptly. On TLS fragments see https://datatracker.ietf.org/doc/html/rfc5246#section-6.2.1 Signed-off-by: Thomas Segismont <tsegismont@gmail.com> * Remove redundant packetSize field from MSSQLSocketConnection The desired packet size is already stored in connect options. Signed-off-by: Thomas Segismont <tsegismont@gmail.com> --------- Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent 35720d5 commit 2057666

6 files changed

Lines changed: 53 additions & 20 deletions

File tree

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/MSSQLConnectionFactory.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2021 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2024 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -52,10 +52,9 @@ private Future<Connection> connectOrRedirect(MSSQLConnectOptions options, Contex
5252
}
5353
SocketAddress server = options.getSocketAddress();
5454
boolean clientSslConfig = options.isSsl();
55-
int desiredPacketSize = options.getPacketSize();
5655
// Always start unencrypted, the connection will be upgraded if client and server agree
5756
return client.connect(server)
58-
.map(so -> createSocketConnection(so, options, desiredPacketSize, context))
57+
.map(so -> createSocketConnection(so, options, context))
5958
.compose(conn -> conn.sendPreLoginMessage(clientSslConfig)
6059
.compose(encryptionLevel -> login(conn, options, encryptionLevel, context))
6160
)
@@ -76,10 +75,10 @@ private Future<Connection> connectOrRedirect(MSSQLConnectOptions options, Contex
7675
});
7776
}
7877

79-
private MSSQLSocketConnection createSocketConnection(NetSocket so, MSSQLConnectOptions options, int desiredPacketSize, ContextInternal context) {
78+
private MSSQLSocketConnection createSocketConnection(NetSocket so, MSSQLConnectOptions options, ContextInternal context) {
8079
VertxMetrics vertxMetrics = vertx.metricsSPI();
8180
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(options.getSocketAddress(), "sql", tcpOptions.getMetricsName()) : null;
82-
MSSQLSocketConnection conn = new MSSQLSocketConnection((NetSocketInternal) so, sslHelper, metrics, options, desiredPacketSize, false, 0, sql -> true, 1, context);
81+
MSSQLSocketConnection conn = new MSSQLSocketConnection((NetSocketInternal) so, sslHelper, metrics, options, false, 0, sql -> true, 1, context);
8382
conn.init();
8483
return conn;
8584
}

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/MSSQLSocketConnection.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2021 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2024 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -21,7 +21,10 @@
2121
import io.vertx.core.impl.future.PromiseInternal;
2222
import io.vertx.core.net.ClientSSLOptions;
2323
import io.vertx.core.net.HostAndPort;
24-
import io.vertx.core.net.impl.*;
24+
import io.vertx.core.net.impl.NetSocketInternal;
25+
import io.vertx.core.net.impl.SSLHelper;
26+
import io.vertx.core.net.impl.SslChannelProvider;
27+
import io.vertx.core.net.impl.SslHandshakeCompletionHandler;
2528
import io.vertx.core.spi.metrics.ClientMetrics;
2629
import io.vertx.mssqlclient.MSSQLConnectOptions;
2730
import io.vertx.mssqlclient.MSSQLInfo;
@@ -45,7 +48,6 @@
4548
public class MSSQLSocketConnection extends SocketConnectionBase {
4649

4750
private final MSSQLConnectOptions connectOptions;
48-
private final int packetSize;
4951
private final SSLHelper sslHelper;
5052

5153
private MSSQLDatabaseMetadata databaseMetadata;
@@ -55,15 +57,13 @@ public class MSSQLSocketConnection extends SocketConnectionBase {
5557
SSLHelper sslHelper,
5658
ClientMetrics clientMetrics,
5759
MSSQLConnectOptions connectOptions,
58-
int packetSize,
5960
boolean cachePreparedStatements,
6061
int preparedStatementCacheSize,
6162
Predicate<String> preparedStatementCacheSqlFilter,
6263
int pipeliningLimit,
6364
ContextInternal context) {
6465
super(socket, clientMetrics, cachePreparedStatements, preparedStatementCacheSize, preparedStatementCacheSqlFilter, pipeliningLimit, context);
6566
this.connectOptions = connectOptions;
66-
this.packetSize = packetSize;
6767
this.sslHelper = sslHelper;
6868
}
6969

@@ -139,7 +139,7 @@ Future<Connection> sendLoginMessage(String username, String password, String dat
139139
@Override
140140
public void init() {
141141
ChannelPipeline pipeline = socket.channelHandlerContext().pipeline();
142-
pipeline.addBefore("handler", "messageCodec", new TdsMessageCodec(packetSize));
142+
pipeline.addBefore("handler", "messageCodec", new TdsMessageCodec(connectOptions.getPacketSize()));
143143
pipeline.addBefore("messageCodec", "packetDecoder", new TdsPacketDecoder());
144144
super.init();
145145
}

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/codec/MSSQLCommandCodec.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2022 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2024 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -12,6 +12,8 @@
1212
package io.vertx.mssqlclient.impl.codec;
1313

1414
import io.netty.buffer.ByteBuf;
15+
import io.netty.channel.ChannelHandler;
16+
import io.netty.handler.ssl.SslHandler;
1517
import io.vertx.mssqlclient.MSSQLException;
1618
import io.vertx.mssqlclient.MSSQLInfo;
1719
import io.vertx.sqlclient.impl.command.CommandBase;
@@ -182,7 +184,7 @@ private void handleEnvChange(ByteBuf payload) {
182184
short type = payload.readUnsignedByte();
183185
switch (type) {
184186
case PACKETSIZE:
185-
tdsMessageCodec.encoder().setPacketSize(Integer.parseInt(readUnsignedByteLengthString(payload)));
187+
handlePacketSizeChange(payload);
186188
break;
187189
case XACT_BEGIN:
188190
case DTC_ENLIST:
@@ -205,6 +207,16 @@ private void handleEnvChange(ByteBuf payload) {
205207
payload.readerIndex(startPos + totalLength);
206208
}
207209

210+
private void handlePacketSizeChange(ByteBuf payload) {
211+
int packetSize = Integer.parseInt(readUnsignedByteLengthString(payload));
212+
ChannelHandler first = tdsMessageCodec.chctx().pipeline().first();
213+
if (first instanceof SslHandler) {
214+
SslHandler sslHandler = (SslHandler) first;
215+
sslHandler.setWrapDataSize(packetSize);
216+
}
217+
tdsMessageCodec.encoder().setPacketSize(packetSize);
218+
}
219+
208220
protected void handleRouting(ByteBuf payload) {
209221
}
210222

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/codec/TdsMessageCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2022 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2024 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -35,9 +35,9 @@ public class TdsMessageCodec extends CombinedChannelDuplexHandler<TdsMessageDeco
3535
private Map<String, CursorData> cursorDataMap;
3636
private Throwable failure;
3737

38-
public TdsMessageCodec(int packetSize) {
38+
public TdsMessageCodec(int desiredPacketSize) {
3939
decoder = new TdsMessageDecoder(this);
40-
encoder = new TdsMessageEncoder(this, packetSize);
40+
encoder = new TdsMessageEncoder(this, desiredPacketSize);
4141
init(decoder, encoder);
4242
}
4343

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/codec/TdsMessageEncoder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2022 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2024 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -30,9 +30,9 @@ public class TdsMessageEncoder extends ChannelOutboundHandlerAdapter {
3030
private ChannelHandlerContext chctx;
3131
private int payloadMaxLength;
3232

33-
public TdsMessageEncoder(TdsMessageCodec tdsMessageCodec, int packetSize) {
33+
public TdsMessageEncoder(TdsMessageCodec tdsMessageCodec, int desiredPacketSize) {
3434
this.tdsMessageCodec = tdsMessageCodec;
35-
setPacketSize(packetSize);
35+
setPacketSize(desiredPacketSize);
3636
}
3737

3838
@Override

vertx-mssql-client/src/test/java/io/vertx/mssqlclient/MSSQLEncryptionTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2022 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2024 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -11,13 +11,17 @@
1111

1212
package io.vertx.mssqlclient;
1313

14+
import io.vertx.core.net.ClientSSLOptions;
1415
import io.vertx.ext.unit.TestContext;
1516
import io.vertx.ext.unit.junit.VertxUnitRunner;
1617
import io.vertx.mssqlclient.junit.MSSQLRule;
18+
import io.vertx.sqlclient.Tuple;
1719
import org.junit.ClassRule;
1820
import org.junit.Test;
1921
import org.junit.runner.RunWith;
2022

23+
import java.util.Arrays;
24+
2125
import static io.vertx.mssqlclient.junit.MSSQLRule.Config.TLS;
2226

2327
/**
@@ -43,4 +47,22 @@ public void testEncryptionLoginOnly(TestContext ctx) {
4347
setOptions(rule.options());
4448
asyncAssertConnectionUnencrypted(ctx);
4549
}
50+
51+
@Test
52+
public void testSmallerPacketSize(TestContext ctx) {
53+
setOptions(rule.options()
54+
.setSsl(true)
55+
.setSslOptions(new ClientSSLOptions().setTrustAll(true))
56+
.setPacketSize(512));
57+
58+
char[] chars = new char[200];
59+
Arrays.fill(chars, 'a');
60+
String str = new String(chars);
61+
62+
connect(ctx.asyncAssertSuccess(conn -> {
63+
conn.query("CREATE TABLE #TestSmallerPacketSize (text NVARCHAR(MAX))").execute().onComplete(ctx.asyncAssertSuccess(v -> {
64+
conn.preparedQuery("INSERT INTO #TestSmallerPacketSize (text) VALUES (@p1)").execute(Tuple.of(str)).onComplete(ctx.asyncAssertSuccess());
65+
}));
66+
}));
67+
}
4668
}

0 commit comments

Comments
 (0)