fix(rds-data): reject an ExecuteStatement response over 1 MiB (#5120)

* fix(rds-data): reject an ExecuteStatement response over 1 MiB

* fix(rds-data): count records array brackets and commas in the response size limit

* fix(rds-data): throw UnsupportedResultException for a response over the size limit
This commit is contained in:
Younes Beriane authored and GitHub committed 2026-10-06 14:57:29 -04:00
1 parent 45f45e590e
commit a7e2447a34
2 files changed
+52

No files matched your search

@@ -18,6 +18,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -42,6 +43,12 @@ import java.util.concurrent.TimeUnit;
@ApplicationScoped
public class RdsDataService implements Resettable {
/**
* The Data API fails an ExecuteStatement whose response is larger than 1 MiB. Only the
* serialized records are counted; the small remainder of the body is not.
*/
private static final long MAX_RESPONSE_BYTES = 1024L * 1024L;
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. */
@@ -533,11 +540,18 @@ public class RdsDataService implements Resettable {
private ArrayNode records(ResultSet rs, ResultSetMetaData meta) throws SQLException {
ArrayNode records = objectMapper.createArrayNode();
int columnCount = meta.getColumnCount();
// Serialized size of the records array: its two brackets plus one comma between rows.
long responseBytes = 2;
while (rs.next()) {
ArrayNode row = objectMapper.createArrayNode();
for (int i = 1; i <= columnCount; i++) {
row.add(RdsDataFieldMapper.toField(objectMapper, rs.getObject(i), meta.getColumnType(i)));
}
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);
}
records.add(row);
}
return records;
@@ -53,6 +53,44 @@ class RdsDataServiceTest {
private final ObjectMapper objectMapper = new ObjectMapper();
@Test
void executeStatementRejectsAResponseOverOneMebibyte() throws Exception {
TestHarness harness = new TestHarness();
ObjectNode request = harness.request(
"select repeat('x', 20000) as c from system_range(1, 100)");
AwsException error = assertThrows(AwsException.class,
() -> harness.service.executeStatement(request, REGION));
assertEquals("UnsupportedResultException", error.getErrorCode());
assertEquals("Database response exceeded size limit", error.getMessage());
}
@Test
void executeStatementAllowsAResponseUnderOneMebibyte() throws Exception {
TestHarness harness = new TestHarness();
ObjectNode response = harness.service.executeStatement(harness.request(
"select repeat('x', 20000) as c from system_range(1, 10)"), REGION);
assertEquals(10, response.get("records").size());
}
/** Records serialize as [[{"stringValue":"..."}]]: 22 bytes around a single string. */
@Test
void executeStatementCountsTheWholeRecordsArrayAtTheBoundary() throws Exception {
int overhead = 22;
int limit = 1024 * 1024;
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());
ObjectNode over = harness.request("select repeat('x', " + (limit - overhead + 1) + ") as c");
AwsException error = assertThrows(AwsException.class,
() -> harness.service.executeStatement(over, REGION));
assertEquals("UnsupportedResultException", error.getErrorCode());
assertEquals("Database response exceeded size limit", error.getMessage());
}
/**
* Message and error code checked against Aurora PostgreSQL 17.7 through the real
* Data API on 2026-09-13.