Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(rds-data): reject result rows larger than 64 KB (#5290)

Matheus Oliveira93cc2ff95711
diff --git a/docs/services/rds-data.md b/docs/services/rds-data.md
index 01a615ae65..39115edb89 100644
--- a/docs/services/rds-data.md
+++ b/docs/services/rds-data.md
@@ -40,6 +40,7 @@ The deprecated `ExecuteSql` operation is recognized at `POST /ExecuteSql` and re
- `includeResultMetadata` returns the full AWS `ColumnMetadata` shape per column: `label`, `name`, `type`, `typeName`, `tableName`, `schemaName`, `nullable`, `precision`, `scale`, `isSigned`, `isCaseSensitive`, `isCurrency`, `isAutoIncrement`, and `arrayBaseColumnType`. `label` is the result-set label (the alias when the query aliases a column) and `name` is the underlying column name, falling back to the label for computed columns — clients that hydrate rows by `label` need it present. `type` is a JDBC type code and `typeName` is the engine's own type name (for example `4` and `int4` for a PostgreSQL integer), both taken from the driver so they match the engine actually serving the query. `arrayBaseColumnType` is always `0` because array columns are not mapped to `arrayValue` fields yet.
- `ExecuteStatement` returns `generatedFields` for statements that report an update count on MySQL and MariaDB, carrying the auto-increment keys the engine generated. As on Aurora PostgreSQL, PostgreSQL resources always return an empty list — use a `RETURNING` clause to read generated values. `generatedFields` is omitted for statements that return a result set.
- `BatchExecuteStatement` runs the statement once per entry in `parameterSets` through a JDBC batch and returns one `updateResults` entry per set, each carrying the `generatedFields` that set produced under the same engine rules as `ExecuteStatement` — a set inserting several rows reports one field per generated key. On MySQL and MariaDB the sets are executed one at a time so each entry owns its own keys, because `getGeneratedKeys()` after a JDBC batch reports every key the batch generated with nothing tying a key back to the set that produced it. It accepts `transactionId`, so a batch can take part in a Data API transaction; without one the batch commits automatically. An absent or empty `parameterSets` runs nothing and returns an empty `updateResults`: AWS runs the statement once per parameter set provided and points a caller who wants a single parameterless execution at one empty set (`[[]]`, which runs once here) or at `ExecuteStatement`. A statement that returns rows rather than an update count — a `SELECT`, or a CTE ending in one — is rejected with `BadRequestException` before any set runs, because a batch reports one `UpdateResult` per set and has nowhere to carry a result set, and AWS documents the operation as taking a DML statement. The check reads the driver's description of the prepared statement, so nothing executes first, and a DML statement that also reports rows through a PostgreSQL `RETURNING` clause is left alone: the batch still performs its writes, and `generatedFields` is empty on PostgreSQL anyway.
+- A result set in which any single row is larger than 64 KB is rejected with `UnsupportedResultException` and the message `Packet for query is too large`, as the Data API troubleshooting guide describes. A row is measured by its values as the driver returns them (the raw length of a binary value, the UTF-8 length of anything else), so JSON escaping and base64 encoding in the response do not count toward it.
- A PostgreSQL result set with a `point`, `box`, `circle`, `line`, `lseg`, `path`, `polygon`, `interval`, or `money` column is rejected with `UnsupportedResultException`, as on Aurora PostgreSQL. Cast the column to text to read it.
- SQL errors are returned as `DatabaseErrorException` so AWS SDK callers can handle database failures with normal AWS error decoding.
- If `secretArn` points to a local Secrets Manager secret with JSON credentials (`username` or `user`, plus `password`), those credentials are used. A missing or unavailable secret returns `SecretsErrorException`; malformed or incomplete credentials return `InvalidSecretException`. Floci does not fall back to the resolved RDS resource's master credentials when a secret ARN was supplied.
diff --git a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
index 6802bd496f..00392a5bcf 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rdsdata/RdsDataService.java
@@ -19,6 +19,7 @@ import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
+import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -49,6 +50,13 @@ public class RdsDataService implements Resettable {
*/
private static final long MAX_RESPONSE_BYTES = 1024L * 1024L;
+ /**
+ * The Data API fails a result set in which any single row the database returns is larger than
+ * 64 KB. A row is measured by its values as the driver returns them, before any JSON escaping or
+ * base64 encoding: see {@link #valueBytes(Object)}.
+ */
+ private static final int MAX_ROW_BYTES = 64 * 1024;
+
private static final Logger LOG = Logger.getLogger(RdsDataService.class);
/** Statement keywords whose result is a write, even when a {@code RETURNING} clause also reports rows. */
@@ -544,11 +552,17 @@ public class RdsDataService implements Resettable {
long responseBytes = 2;
while (rs.next()) {
ArrayNode row = objectMapper.createArrayNode();
+ long rawRowBytes = 0;
for (int i = 1; i <= columnCount; i++) {
- row.add(RdsDataFieldMapper.toField(objectMapper, rs.getObject(i), meta.getColumnType(i)));
+ Object value = rs.getObject(i);
+ rawRowBytes += valueBytes(value);
+ row.add(RdsDataFieldMapper.toField(objectMapper, value, meta.getColumnType(i)));
+ }
+ if (rawRowBytes > MAX_ROW_BYTES) {
+ LOG.debugv("Rejecting a result row of {0} bytes", rawRowBytes);
+ throw new AwsException("UnsupportedResultException", "Packet for query is too large", 400);
}
- responseBytes += row.toString().getBytes(StandardCharsets.UTF_8).length
- + (records.isEmpty() ? 0 : 1);
+ responseBytes += row.toString().getBytes(StandardCharsets.UTF_8).length + (records.isEmpty() ? 0 : 1);
if (responseBytes > MAX_RESPONSE_BYTES) {
throw new AwsException("UnsupportedResultException", "Database response exceeded size limit", 400);
}
@@ -557,6 +571,16 @@ public class RdsDataService implements Resettable {
return records;
}
+ /** A column value's size as the driver returns it: the raw length of binary, the UTF-8 length of anything else's text. */
+ static long valueBytes(Object value) throws SQLException {
+ return switch (value) {
+ case null -> 0;
+ case byte[] bytes -> bytes.length;
+ case Blob blob -> blob.length();
+ default -> value.toString().getBytes(StandardCharsets.UTF_8).length;
+ };
+ }
+
private Credentials credentials(JsonNode request, RdsDataResourceResolver.DatabaseTarget target, String region) {
String secretArn = textOrNull(request, "secretArn");
if (secretArn != null && !secretArn.isBlank()) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java
index 02a23be5c4..27509812c2 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rdsdata/RdsDataServiceTest.java
@@ -74,23 +74,55 @@ class RdsDataServiceTest {
assertEquals(10, response.get("records").size());
}
- /** Records serialize as [[{"stringValue":"..."}]]: 22 bytes around a single string. */
+ /** Records serialize as [[{"stringValue":"..."}]]: 20 bytes around a single string per row. */
@Test
void executeStatementCountsTheWholeRecordsArrayAtTheBoundary() throws Exception {
- int overhead = 22;
- int limit = 1024 * 1024;
+ int rows = 25;
+ int exactLength = 41922;
TestHarness harness = new TestHarness();
ObjectNode exact = harness.service.executeStatement(harness.request(
- "select repeat('x', " + (limit - overhead) + ") as c"), REGION);
- assertEquals(1, exact.get("records").size());
+ "select repeat('x', " + exactLength + ") as c from system_range(1, " + rows + ")"), REGION);
+ assertEquals(rows, exact.get("records").size());
- ObjectNode over = harness.request("select repeat('x', " + (limit - overhead + 1) + ") as c");
+ ObjectNode over = harness.request(
+ "select repeat('x', " + (exactLength + 1) + ") as c from system_range(1, " + rows + ")");
AwsException error = assertThrows(AwsException.class,
() -> harness.service.executeStatement(over, REGION));
assertEquals("UnsupportedResultException", error.getErrorCode());
assertEquals("Database response exceeded size limit", error.getMessage());
}
+ @Test
+ void executeStatementRejectsARowOverSixtyFourKilobytes() throws Exception {
+ int limit = 64 * 1024;
+ TestHarness harness = new TestHarness();
+ ObjectNode exact = harness.service.executeStatement(harness.request(
+ "select repeat('x', " + (limit - 1) + ") as a, 'y' as b"), REGION);
+ assertEquals(1, exact.get("records").size());
+
+ ObjectNode over = harness.request("select repeat('x', " + limit + ") as a, 'y' as b");
+ AwsException error = assertThrows(AwsException.class,
+ () -> harness.service.executeStatement(over, REGION));
+ assertEquals("UnsupportedResultException", error.getErrorCode());
+ assertEquals("Packet for query is too large", error.getMessage());
+ assertEquals(400, error.getHttpStatus());
+ }
+
+ /** The limit applies to the row the database returns, not to its JSON escaping or base64 encoding. */
+ @Test
+ void executeStatementMeasuresARowBeforeItIsSerialized() throws Exception {
+ int size = 60 * 1024;
+ TestHarness harness = new TestHarness();
+ ObjectNode quotes = harness.service.executeStatement(harness.request(
+ "select repeat('\"', " + size + ") as c"), REGION);
+ assertEquals(size, quotes.get("records").get(0).get(0).get("stringValue").asText().length());
+
+ ObjectNode binary = harness.service.executeStatement(harness.request(
+ "select cast(repeat('x', " + size + ") as varbinary) as c"), REGION);
+ assertEquals(1, binary.get("records").size());
+ assertTrue(binary.get("records").get(0).get(0).has("blobValue"));
+ }
+
/**
* Message and error code checked against Aurora PostgreSQL 17.7 through the real
* Data API on 2026-09-13.