fix(dynamodb): refuse unconvertible and out-of-range PartiQL number parameters before parsing the statement (#7179)

* fix(dynamodb): refuse unconvertible and out-of-range PartiQL number parameters before parsing the statement

* fix(dynamodb): decode binary PartiQL parameters first, and count types while reading each parameter

* fix(dynamodb): take a PartiQL parameter member set to null as absent

* fix(dynamodb): drop null parameter members before IAM reads the PartiQL attributes
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-01 07:50:48 -04:00
1 parent 28bde18804
commit 664db0cef3
7 files changed
+228 -23

No files matched your search

@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.dynamodb;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import java.util.Base64;
@@ -55,8 +56,8 @@ final class DynamoDbAttributeValueValidator {
static final String NESTING_EXCEEDED = "Nesting Levels have exceeded supported limits";
static void requireParameterNestingWithinLimit(JsonNode value) {
if (depthOf(value) > MAX_NESTING_LEVELS + 1) {
static void requireParameterLevelWithinLimit(int level) {
if (MAX_NESTING_LEVELS + 1 < level) {
throw validationEx(NESTING_EXCEEDED + ": Attributes in the item have nested levels beyond supported limit");
}
}
@@ -100,7 +101,17 @@ final class DynamoDbAttributeValueValidator {
}
}
private static void requireBase64(JsonNode value) {
// A PartiQL parameter takes a member set to null as absent.
static JsonNode dropNullMembers(JsonNode value) {
if (value instanceof ObjectNode object) {
object.properties().removeIf(member -> member.getValue().isNull());
object.path("M").forEach(DynamoDbAttributeValueValidator::dropNullMembers);
object.path("L").forEach(DynamoDbAttributeValueValidator::dropNullMembers);
}
return value;
}
static void requireBase64(JsonNode value) {
if (!value.isObject()) {
return;
}
@@ -244,7 +244,8 @@ public final class DynamoDbConditionKeys {
}
try {
List<JsonNode> parameters = new ArrayList<>();
body.path("Parameters").forEach(parameters::add);
body.path("Parameters").forEach(parameter ->
parameters.add(DynamoDbAttributeValueValidator.dropNullMembers(parameter.deepCopy())));
return DynamoDbPartiQLParser.parse(statement, parameters)
instanceof DynamoDbPartiQLParser.Stmt.Select select ? select : null;
} catch (RuntimeException ignored) {
@@ -7,6 +7,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import java.math.BigDecimal;
import java.util.Set;
import java.util.TreeSet;
/**
* DynamoDB number validation and normalization.
@@ -22,9 +24,59 @@ final class DynamoDbNumberUtils {
static final String NUMBER_UNDERFLOW =
"Number underflow. Attempting to store a number with magnitude smaller than supported range";
static final String TOO_MANY_DIGITS = "Attempting to store more than 38 significant digits in a Number";
static final String PARAMETER_NOT_NUMERIC = "The parameter cannot be converted to a numeric value";
private DynamoDbNumberUtils() {}
// DynamoDB measures a PartiQL number in 32-bit arithmetic, so an exponent near the limit wraps
// around and reads as an underflow.
static String partiQLNumberProblem(BigDecimal number) {
int magnitude = number.precision() - number.scale() + 1;
if (127 < magnitude) {
return NUMBER_OVERFLOW;
}
if (magnitude < -128) {
return NUMBER_UNDERFLOW;
}
if (38 < number.stripTrailingZeros().precision()) {
return TOO_MANY_DIGITS;
}
return null;
}
// DynamoDB reads a PartiQL parameter as BigDecimal does, but only with an exponent that fits in
// an int. Unlike a literal, the parameter is not rounded to 38 digits.
static BigDecimal requirePartiQLParameterNumber(String text) {
if (text.isEmpty()) {
throw new AwsException("ValidationException", PARAMETER_NOT_NUMERIC, 400);
}
BigDecimal number;
try {
int exponent = Math.max(text.indexOf('e'), text.indexOf('E'));
if (0 <= exponent) {
Integer.parseInt(text.substring(exponent + 1));
}
number = new BigDecimal(text);
} catch (NumberFormatException e) {
throw new AwsException("ValidationException", PARAMETER_NOT_NUMERIC + ": " + text, 400);
}
String problem = partiQLNumberProblem(number);
if (problem != null) {
throw new AwsException("ValidationException", problem, 400);
}
return number;
}
// Members are equal by value here, so 1 and 1.0 are duplicates.
static void requirePartiQLParameterNumberSet(JsonNode members) {
Set<BigDecimal> seen = new TreeSet<>();
for (JsonNode member : members) {
if (!seen.add(requirePartiQLParameterNumber(member.asText()))) {
throw new AwsException("ValidationException", "Input collection contains duplicates", 400);
}
}
}
/**
* Validates and normalizes a DynamoDB number string.
* Throws ValidationException if the number is out of range or has too many significant digits.
@@ -1639,29 +1639,13 @@ public class DynamoDbPartiQLParser {
value = value.round(DECIMAL_LITERAL);
number = new PVal.Num(value.toString());
}
String problem = numberLiteralProblem(value);
String problem = DynamoDbNumberUtils.partiQLNumberProblem(value);
if (problem != null) {
defer(problem + " under " + literalPath);
}
return number;
}
// DynamoDB measures a literal in 32-bit arithmetic, so an exponent near the limit wraps around
// and reads as an underflow.
private static String numberLiteralProblem(BigDecimal number) {
int magnitude = number.precision() - number.scale() + 1;
if (127 < magnitude) {
return DynamoDbNumberUtils.NUMBER_OVERFLOW;
}
if (magnitude < -128) {
return DynamoDbNumberUtils.NUMBER_UNDERFLOW;
}
if (38 < number.stripTrailingZeros().precision()) {
return DynamoDbNumberUtils.TOO_MANY_DIGITS;
}
return null;
}
private PVal.Num parseSignedNumber(Token sign) {
Token operand = advance();
PVal.Num number = switch (operand.type()) {
@@ -3613,6 +3613,7 @@ public class NativeDynamoDbJsonHandler {
}
private Response handleExecuteStatement(JsonNode request, String region) {
decodePartiQLParameters(request.path("Parameters"));
List<String> validationErrors = new ArrayList<>();
addPartiQLReturnValuesError(request, "returnValuesOnConditionCheckFailure", validationErrors);
addLengthError(request, "NextToken", "nextToken", MAX_NEXT_TOKEN_LENGTH, validationErrors);
@@ -3642,6 +3643,7 @@ public class NativeDynamoDbJsonHandler {
private Response handleExecuteTransaction(JsonNode request, String region) {
JsonNode stmts = request.path("TransactStatements");
stmts.forEach(s -> decodePartiQLParameters(s.path("Parameters")));
List<String> validationErrors = new ArrayList<>();
addLengthError(request, "ClientRequestToken", "clientRequestToken", MAX_CLIENT_REQUEST_TOKEN_LENGTH,
validationErrors);
@@ -3771,6 +3773,7 @@ public class NativeDynamoDbJsonHandler {
private Response handleBatchExecuteStatement(JsonNode request, String region) {
JsonNode stmts = request.path("Statements");
stmts.forEach(s -> decodePartiQLParameters(s.path("Parameters")));
List<String> validationErrors = new ArrayList<>();
addReturnConsumedCapacityError(request, validationErrors);
addStatementListErrors(stmts, "statements", validationErrors);
@@ -3867,10 +3870,43 @@ public class NativeDynamoDbJsonHandler {
}
List<JsonNode> params = new ArrayList<>();
for (JsonNode parameter : node) {
DynamoDbAttributeValueValidator.requireParameterNestingWithinLimit(parameter);
checkAttrSets(parameter);
requirePartiQLParameter(parameter, 1);
params.add(parameter);
}
return params;
}
// DynamoDB decodes the binary values of every statement before it checks anything else.
private static void decodePartiQLParameters(JsonNode parameters) {
for (JsonNode parameter : parameters) {
DynamoDbAttributeValueValidator.dropNullMembers(parameter);
DynamoDbAttributeValueValidator.requireBase64(parameter);
}
}
// DynamoDB reads the parameters in order and refuses the first value it cannot take. It reads the
// members of a map in the order of a HashMap sized for them, and a number before the type count.
private void requirePartiQLParameter(JsonNode value, int level) {
DynamoDbAttributeValueValidator.requireParameterLevelWithinLimit(level);
if (value.has("N")) {
DynamoDbNumberUtils.requirePartiQLParameterNumber(value.get("N").asText());
} else if (value.has("NS")) {
DynamoDbNumberUtils.requirePartiQLParameterNumberSet(value.get("NS"));
}
switch (DynamoDbAttributeValueValidator.typeOf(value)) {
case "M" -> {
Map<String, JsonNode> members = HashMap.newHashMap(value.get("M").size());
value.get("M").properties().forEach(member -> members.put(member.getKey(), member.getValue()));
for (JsonNode member : members.values()) {
requirePartiQLParameter(member, level + 1);
}
}
case "L" -> {
for (JsonNode member : value.get("L")) {
requirePartiQLParameter(member, level + 1);
}
}
default -> checkAttrSets(value);
}
}
}