fix(dsql): refuse a startup packet or client message outside DSQL's limits (#6258)

* fix(dsql): refuse a startup packet or client message outside DSQL's limits

The endpoint took a client message up to 64 MB and dropped the connection
without a word past that; DSQL takes 10 MiB and answers FATAL 08P01
"invalid message length" as soon as a longer, or shorter than 4, length
arrives. Measuring the startup packet alongside showed it one byte off as
well: DSQL takes 9,999 bytes and answers "invalid length of startup packet"
from 10,000, where the endpoint took 10,000 and refused more in silence. Both
are DSQL's now, for the startup inside TLS, the password message and every
message after signing in. What the database sends is not DSQL's to limit, so
it is still read up to 64 MB. PostgresWire.checkedLength throws its own
InvalidLengthException, still an IOException, so a reader can tell such a
packet from a broken connection; the RDS proxy and Redshift's bridge end the
connection on it as before.

* fix(dsql): refuse any packet before TLS as not TLS, whatever its length

The first head capped the packets read before TLS at the startup limit too,
so a 10,000-byte plaintext startup packet, first or after a GSSENCRequest,
closed the connection in silence where it had been refused as not TLS; above
10,000 bytes main closed in silence as well. Measured on DSQL, it reads no
more than such a packet's length and code: a plaintext startup packet of any
length is refused with "SSL is mandatory", and one under 8 bytes ends the
connection without a word. The endpoint now reads the body only when it fits
a startup packet, so the refusal reaches the client in order, and refuses a
longer one unread.
This commit is contained in:
Alessandro Lattao authored and GitHub committed 2026-10-22 16:53:06 -04:00
1 parent 8d73535826
commit 564ae350a1
5 files changed
+205 -19

No files matched your search

@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.dsql;
import com.github.dockerjava.api.DockerClient;
import io.github.hectorvent.floci.core.common.PostgresWire;
import io.github.hectorvent.floci.services.dsql.dataplane.DsqlEndpoint;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.testing.EnforcementFixtures;
@@ -18,13 +19,19 @@ import org.junit.jupiter.api.extension.RegisterExtension;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLWarning;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@@ -137,6 +144,85 @@ class DsqlDataPlaneIntegrationTest {
assertEquals("55000", otherDatabase.getSQLState());
}
/**
* DSQL's limits (measured): a startup packet of 10,000 bytes or more, or under 8, and a client
* message over 10 MiB or under 4, are refused as soon as their length arrives, before signing in
* and after, and the connection closes.
*/
@Test
void aPacketOutsideDsqlsLimitsIsRefusedAsItsLengthArrives() throws Exception {
try (DsqlWireClient client = DsqlWireClient.tls(endpoint.port(), cluster.host())) {
client.send(startupOfLength(9_999));
PostgresWire.Message password = client.next();
assertEquals('R', password.type());
assertEquals(3, PostgresWire.int32(password.body(), 0));
}
try (DsqlWireClient client = DsqlWireClient.tls(endpoint.port(), cluster.host())) {
client.send(startupOfLength(10_000));
assertRefused(client, "invalid length of startup packet");
}
try (DsqlWireClient client = DsqlWireClient.tls(endpoint.port(), cluster.host())) {
client.send(PostgresWire.int32(7), new byte[3]);
assertRefused(client, "invalid length of startup packet");
}
try (DsqlWireClient client = DsqlWireClient.tls(endpoint.port(), cluster.host())) {
client.send(startupOfLength(100));
assertEquals('R', client.next().type());
client.send(header('p', 10 * 1024 * 1024 + 1));
assertRefused(client, "invalid message length");
}
for (int length : new int[] {10 * 1024 * 1024 + 1, 3}) {
try (DsqlWireClient client = cluster.wireClient("admin", cluster.adminToken())) {
client.send(header('Q', length));
assertRefused(client, "invalid message length");
}
}
}
/**
* Before TLS, DSQL reads no more of a packet than its length and code (measured): a startup
* packet is refused as not TLS whatever length it claims, after a GSSENCRequest too, and one
* under 8 bytes ends the connection without a word.
*/
@Test
void aPacketBeforeTlsIsRefusedAsNotTlsWhateverItsLength() throws Exception {
for (int length : new int[] {9_999, 10_000, 20_000}) {
for (boolean gssFirst : new boolean[] {false, true}) {
try (Socket socket = new Socket("127.0.0.1", endpoint.port())) {
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
if (gssFirst) {
out.write(PostgresWire.int32(8));
out.write(PostgresWire.int32(PostgresWire.GSSENC_REQUEST));
assertEquals('N', in.read());
}
out.write(startupOfLength(length));
PostgresWire.Message error = PostgresWire.read(in, Integer.MAX_VALUE, "message");
Map<Character, String> fields = PostgresWire.parseFields(error.body(), 0);
assertEquals("08P01", fields.get('C'), length + (gssFirst ? " after GSSENCRequest" : ""));
assertEquals("unable to accept connection, SSL is mandatory. AWS Authentication is required.",
fields.get('M'));
}
}
}
try (Socket socket = new Socket("127.0.0.1", endpoint.port())) {
socket.getOutputStream().write(PostgresWire.int32(7));
socket.getOutputStream().write(new byte[3]);
assertEquals(-1, socket.getInputStream().read());
}
}
@Test
void aMessageOfTenMibIsTaken() throws Exception {
try (DsqlWireClient client = cluster.wireClient("admin", cluster.adminToken())) {
String padding = "x".repeat(10 * 1024 * 1024 - 4 - 1 - "SELECT 1 /**/".length());
client.send(query("SELECT 1 /*" + padding + "*/"));
assertEquals(List.of("SELECT 1"), client.untilReady());
client.send(query("SELECT 2"));
assertEquals(List.of("SELECT 1"), client.untilReady());
}
}
@Test
void aRoleLogsInOnlyForTheIamRoleMappedToIt() throws Exception {
String accessKeyId = "ASIA" + Long.toString(System.nanoTime(), 36).toUpperCase();
@@ -627,4 +713,34 @@ class DsqlDataPlaneIntegrationTest {
return EnforcementFixtures.createUser(cleanup, httpPort, EnforcementFixtures.auth(account, "iam"),
"dsql-" + System.nanoTime());
}
/** A startup packet signing in as {@code admin}, its application_name padded to make {@code length} bytes. */
private static byte[] startupOfLength(int length) {
Map<String, String> parameters = new LinkedHashMap<>();
parameters.put("user", "admin");
parameters.put("database", "postgres");
parameters.put("application_name", "");
parameters.put("application_name", "a".repeat(length - PostgresWire.startupMessage(parameters).length));
return PostgresWire.startupMessage(parameters);
}
/** A message's type and the length it announces, without any of its body. */
private static byte[] header(char type, int length) {
byte[] header = new byte[5];
header[0] = (byte) type;
System.arraycopy(PostgresWire.int32(length), 0, header, 1, 4);
return header;
}
/** The endpoint answers DSQL's FATAL with {@code message} and the session's id, then closes. */
private static void assertRefused(DsqlWireClient client, String message) throws IOException {
PostgresWire.Message error = client.next();
assertEquals('E', error.type());
Map<Character, String> fields = PostgresWire.parseFields(error.body(), 0);
assertEquals("FATAL", fields.get('S'));
assertEquals("08P01", fields.get('C'));
assertEquals(message, fields.get('M'));
assertTrue(fields.get('D').startsWith("Session Id: "), fields.get('D'));
assertThrows(IOException.class, client::next);
}
}
@@ -28,13 +28,7 @@ public final class DsqlWireClient implements AutoCloseable {
/** Signs in to the endpoint on {@code port} as {@code user}, naming {@code host} as the server. */
public DsqlWireClient(int port, String host, String user, String password) throws Exception {
Socket plain = new Socket("127.0.0.1", port);
plain.getOutputStream().write(PostgresWire.int32(8));
plain.getOutputStream().write(PostgresWire.int32(PostgresWire.SSL_REQUEST));
assertEquals('S', plain.getInputStream().read());
socket = new NonValidatingFactory(null).createSocket(plain, host, port, true);
in = new BufferedInputStream(socket.getInputStream());
out = socket.getOutputStream();
this(port, host);
Map<String, String> startup = new LinkedHashMap<>();
startup.put("user", user);
startup.put("database", "postgres");
@@ -44,6 +38,26 @@ public final class DsqlWireClient implements AutoCloseable {
untilReady();
}
private DsqlWireClient(int port, String host) throws Exception {
Socket plain = new Socket("127.0.0.1", port);
plain.getOutputStream().write(PostgresWire.int32(8));
plain.getOutputStream().write(PostgresWire.int32(PostgresWire.SSL_REQUEST));
assertEquals('S', plain.getInputStream().read());
socket = new NonValidatingFactory(null).createSocket(plain, host, port, true);
in = new BufferedInputStream(socket.getInputStream());
out = socket.getOutputStream();
}
/** TLS to the endpoint on {@code port}, naming {@code host}, and nothing sent on it yet: no startup packet. */
public static DsqlWireClient tls(int port, String host) throws Exception {
return new DsqlWireClient(port, host);
}
/** The next message the endpoint sends; an {@link IOException} once it has closed the connection. */
public PostgresWire.Message next() throws IOException {
return PostgresWire.read(in, Integer.MAX_VALUE, "message");
}
public void send(byte[]... messages) throws IOException {
for (byte[] message : messages) {
out.write(message);