fix(dynamodb): refuse a null or omitted required SearchVectors member (#8044)

This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-11 17:55:09 -04:00
1 parent d8978fddf4
commit 3b4cae7387
10 files changed
+543 -30

No files matched your search

@@ -177,7 +177,7 @@ public class AwsJsonController {
if (targetMatch.jsonOnly()) {
yield dynamoDbJsonHandler.handleLegacy(action, request, region);
}
DynamoDbRequestShapes.check(action, request);
DynamoDbRequestShapes.check(action, request, body);
yield dynamoDbJsonHandler.handle(action, request, region);
}
case "sqs" -> sqsJsonHandler.handle(action, request, region);
@@ -1,11 +1,22 @@
package io.floci.aws.services.dynamodb;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonNode;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RequestShapeCheck;
import io.floci.aws.core.common.RequestShapeCheck.ByPosition;
import io.floci.aws.core.common.RequestShapeModel;
import io.floci.aws.core.common.RequestShapeModel.Shape;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
* Rejects a DynamoDB request member of the wrong JSON type with the SerializationException
@@ -20,6 +31,11 @@ public final class DynamoDbRequestShapes {
static final String RESOURCE_NAME = "aws/dynamodb-request-shapes.json";
private static final String UNKNOWN_ERROR = "UnknownError";
private static final String SEARCH_VECTORS = "SearchVectors";
// How the SearchVectors frontend names the type a null required member should have had.
private static final Map<String, String> SEARCH_VECTORS_TYPE_NAMES = Map.of(
"string", "a string", "list", "a sequence", "integer", "i32");
private static final JsonFactory JSON = new JsonFactory();
private static final ByPosition SCALAR_FOR_CONTAINER = new ByPosition("Unexpected field type", UNKNOWN_ERROR);
private static final RequestShapeCheck.Messages MESSAGES = new RequestShapeCheck.Messages(
@@ -53,4 +69,75 @@ public final class DynamoDbRequestShapes {
RequestShapeCheck.check(model, input, request, MESSAGES);
}
}
/**
* As {@link #check(String, JsonNode)}, given the body as sent. SearchVectors has its own frontend,
* which also refuses a null or omitted required member and names the line and byte column where
* it stopped reading.
*/
public static void check(String action, JsonNode request, String body) {
if (SEARCH_VECTORS.equals(action) && !hasSearchVectorsMembers(Holder.MODEL, request)) {
readSearchVectors(Holder.MODEL, request, body == null ? "" : body, true);
} else {
check(action, request);
}
}
/**
* Refuses a null or omitted required SearchVectors member of a request that reached the operation
* without a JSON body, from AppSync, Step Functions or CBOR. The positions are those of the compact
* JSON an SDK sends.
*/
public static void requireSearchVectorsMembers(JsonNode request) {
if (!hasSearchVectorsMembers(Holder.MODEL, request)) {
readSearchVectors(Holder.MODEL, request, request.toString(), false);
}
}
private static boolean hasSearchVectorsMembers(RequestShapeModel model, JsonNode request) {
return searchVectorsInput(model).required().stream().allMatch(request::hasNonNull);
}
private static Shape searchVectorsInput(RequestShapeModel model) {
return model.shapes().get(model.operations().get(SEARCH_VECTORS));
}
/** Reads {@code body} in order, checking each member's JSON type as well when {@code memberTypes}. */
private static void readSearchVectors(RequestShapeModel model, JsonNode request, String body,
boolean memberTypes) {
if (body.isBlank()) {
throw new AwsException("ValidationException", "EOF while parsing a value at line "
+ (body.chars().filter(c -> c == '\n').count() + 1)
+ " column " + (body.length() - body.lastIndexOf('\n') - 1), 400);
}
Shape input = searchVectorsInput(model);
try (JsonParser parser = JSON.createParser(body.getBytes(StandardCharsets.UTF_8))) {
parser.nextToken();
while (parser.nextToken() == JsonToken.FIELD_NAME) {
String name = parser.currentName();
String memberShape = input.members().get(name);
if (parser.nextToken() == JsonToken.VALUE_NULL && input.required().contains(name)) {
throw new AwsException("SerializationException", "invalid type: null, expected "
+ SEARCH_VECTORS_TYPE_NAMES.get(model.shapes().get(memberShape).type())
+ " at " + position(parser.currentTokenLocation(), "null".length() - 1), 400);
}
parser.skipChildren();
if (memberTypes && memberShape != null) {
RequestShapeCheck.check(model, memberShape, request.get(name), MESSAGES);
}
}
for (String member : new TreeSet<>(input.required())) {
if (!request.has(member)) {
throw new AwsException("ValidationException", "missing field `" + member + "` at "
+ position(parser.currentTokenLocation(), 0), 400);
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private static String position(JsonLocation location, int columnOffset) {
return "line " + location.getLineNr() + " column " + (location.getColumnNr() + columnOffset);
}
}
@@ -1617,8 +1617,7 @@ public class NativeDynamoDbJsonHandler {
private static final int MAX_VECTOR_SEARCH_TOP_K = 100;
private Response handleSearchVectors(JsonNode request, String region) {
requireSearchVectorsMember(request, "SearchVector");
requireSearchVectorsMember(request, "IndexName");
DynamoDbRequestShapes.requireSearchVectorsMembers(request);
String tableName = request.path("TableName").asText();
String indexName = request.get("IndexName").asText();
String rccSearchError = returnConsumedCapacityError(request);
@@ -1627,7 +1626,6 @@ public class NativeDynamoDbJsonHandler {
throw new AwsException("ValidationException", rccSearchError, 400);
}
String rccSearch = request.path("ReturnConsumedCapacity").asText("NONE");
requireSearchVectorsMember(request, "TopK");
int topK = request.get("TopK").asInt();
if (topK < 1) {
// AWS names this member TopK and quotes no value, unlike every other member of this
@@ -1676,19 +1674,6 @@ public class NativeDynamoDbJsonHandler {
return Response.ok(response).build();
}
/**
* Rejects an omitted required member of SearchVectors the way the AWS frontend does: as a
* deserialisation failure quoting the offset of the body's closing brace, which for the
* compact JSON an SDK sends is the body length. Measured in eu-west-2, 2026-09-23.
*/
private static void requireSearchVectorsMember(JsonNode request, String member) {
if (!request.hasNonNull(member)) {
throw new AwsException("ValidationException",
"missing field `" + member + "` at line 1 column "
+ request.toString().length(), 400);
}
}
/**
* The vector index a search may run against.
*