mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(dynamodb): check every number in an item request first and read its exponent as a long (#7203)
* fix(dynamodb): check every number in an item request first and read its exponent as a long * fix(dynamodb): read only the members an item operation has, each at the stage DynamoDB reads it * chore(dynamodb): pass each request member to the number check and walk the values with plain loops
This commit is contained in:
1 parent
e19e1cff92
commit
49edfcbd5f
3 files changed
+233
-50
No files matched your search
+44
-39
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* DynamoDB number validation and normalization.
|
||||
@@ -121,57 +122,61 @@ final class DynamoDbNumberUtils {
|
||||
return toNormalizedString(stripped);
|
||||
}
|
||||
|
||||
// AWS checks every number in a request before the table lookup. Single-item write
|
||||
// APIs wrap the error in the validation envelope, Query and Scan do not.
|
||||
static void requireStorable(JsonNode attributes, boolean inValidationEnvelope) {
|
||||
if (attributes == null || !attributes.isObject()) {
|
||||
return;
|
||||
}
|
||||
for (JsonNode value : attributes) {
|
||||
requireStorableValue(value, inValidationEnvelope ? "1 validation error detected: " : "");
|
||||
static void forEachNumber(JsonNode values, Consumer<String> check) {
|
||||
for (JsonNode value : values) {
|
||||
JsonNode number = value.get("N");
|
||||
if (number != null && number.isTextual()) {
|
||||
check.accept(number.asText());
|
||||
}
|
||||
for (JsonNode member : value.path("NS")) {
|
||||
if (member.isTextual()) {
|
||||
check.accept(member.asText());
|
||||
}
|
||||
}
|
||||
forEachNumber(value.path("L"), check);
|
||||
forEachNumber(value.path("M"), check);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireStorableValue(JsonNode value, String prefix) {
|
||||
if (!value.isObject()) {
|
||||
return;
|
||||
}
|
||||
if (value.has("N")) {
|
||||
requireStorableNumber(value.get("N").asText(), prefix);
|
||||
}
|
||||
for (JsonNode n : value.path("NS")) {
|
||||
requireStorableNumber(n.asText(), prefix);
|
||||
}
|
||||
for (JsonNode element : value.path("L")) {
|
||||
requireStorableValue(element, prefix);
|
||||
}
|
||||
for (JsonNode entry : value.path("M")) {
|
||||
requireStorableValue(entry, prefix);
|
||||
static void requireItemNumber(String text, String prefix) {
|
||||
String problem = itemNumberProblem(text);
|
||||
if (problem != null) {
|
||||
throw new AwsException("ValidationException", prefix + problem, 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireStorableNumber(String numStr, String prefix) {
|
||||
BigDecimal bd;
|
||||
// An item call reads the exponent as a long and checks the range without wrapping around,
|
||||
// unlike a PartiQL parameter. It counts the digits before it checks the range.
|
||||
private static String itemNumberProblem(String text) {
|
||||
int marker = exponentMarker(text);
|
||||
BigDecimal mantissa;
|
||||
long exponent;
|
||||
try {
|
||||
bd = new BigDecimal(numStr);
|
||||
mantissa = new BigDecimal(marker < 0 ? text : text.substring(0, marker));
|
||||
exponent = marker < 0 ? 0 : Long.parseLong(text.substring(marker + 1));
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("ValidationException",
|
||||
"The parameter cannot be converted to a numeric value: " + numStr, 400);
|
||||
return PARAMETER_NOT_NUMERIC + ": " + text;
|
||||
}
|
||||
BigDecimal stripped = bd.stripTrailingZeros();
|
||||
if (stripped.precision() > 38) {
|
||||
throw new AwsException("ValidationException",
|
||||
prefix + TOO_MANY_DIGITS, 400);
|
||||
if (38 < mantissa.stripTrailingZeros().precision()) {
|
||||
return TOO_MANY_DIGITS;
|
||||
}
|
||||
BigDecimal abs = stripped.abs();
|
||||
if (abs.compareTo(MAX_ABS) >= 0) {
|
||||
throw new AwsException("ValidationException",
|
||||
prefix + NUMBER_OVERFLOW, 400);
|
||||
long magnitude = (long) mantissa.precision() - mantissa.scale() + 1;
|
||||
if (127 - magnitude < exponent) {
|
||||
return NUMBER_OVERFLOW;
|
||||
}
|
||||
if (abs.signum() > 0 && abs.compareTo(MIN_ABS_NONZERO) < 0) {
|
||||
throw new AwsException("ValidationException",
|
||||
prefix + NUMBER_UNDERFLOW, 400);
|
||||
if (exponent < -128 - magnitude) {
|
||||
return NUMBER_UNDERFLOW;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static int exponentMarker(String text) {
|
||||
int lower = text.indexOf('e');
|
||||
int upper = text.indexOf('E');
|
||||
if (lower < 0 || upper < 0) {
|
||||
return Math.max(lower, upper);
|
||||
}
|
||||
return Math.min(lower, upper);
|
||||
}
|
||||
|
||||
// AWS answers with the overflow wording for any arithmetic result it cannot store,
|
||||
|
||||
+57
-11
@@ -599,10 +599,10 @@ public class NativeDynamoDbJsonHandler {
|
||||
if (!validationErrors.isEmpty()) {
|
||||
throw SmithyValidationErrors.validationException(validationErrors);
|
||||
}
|
||||
requireReadableValues(item, VALIDATION_ENVELOPE);
|
||||
DynamoDbExpressionSize.checkWrite(conditionExpression, "ConditionExpression");
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(item);
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues);
|
||||
DynamoDbNumberUtils.requireStorable(exprAttrValues, true);
|
||||
|
||||
JsonNode expected = request.has("Expected") ? request.get("Expected") : null;
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(expected));
|
||||
@@ -614,6 +614,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
"Can not use both expression and non-expression parameters in the same request: "
|
||||
+ "Non-expression parameters: {Expected} Expression parameters: {ConditionExpression}", 400);
|
||||
}
|
||||
requireReadableValues(exprAttrValues, VALIDATION_ENVELOPE);
|
||||
|
||||
// EAN/EAV with no expression to reference them: AWS reports "can only be specified
|
||||
// when using expressions", not the "unused in expressions" wording (#2893).
|
||||
@@ -654,15 +655,14 @@ public class NativeDynamoDbJsonHandler {
|
||||
JsonNode attributesToGet = request.has("AttributesToGet") ? request.get("AttributesToGet") : null;
|
||||
|
||||
DynamoDbAccessPathValidator.validateAttributesToGet(attributesToGet);
|
||||
requireValidReturnConsumedCapacity(request);
|
||||
requireReadableValues(key, "");
|
||||
if (attributesToGet != null && projectionExpression != null) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Can not use both expression and non-expression parameters in the same request: "
|
||||
+ "Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}", 400);
|
||||
}
|
||||
|
||||
// Validate enums before table lookup
|
||||
requireValidReturnConsumedCapacity(request);
|
||||
|
||||
// Validate ProjectionExpression syntax and reserved words before item lookup
|
||||
if (projectionExpression != null) {
|
||||
DynamoDbExpressionSize.checkRead(projectionExpression, "ProjectionExpression");
|
||||
@@ -717,9 +717,9 @@ public class NativeDynamoDbJsonHandler {
|
||||
if (!delValidationErrors.isEmpty()) {
|
||||
throw SmithyValidationErrors.validationException(delValidationErrors);
|
||||
}
|
||||
requireReadableValues(key, VALIDATION_ENVELOPE);
|
||||
DynamoDbExpressionSize.checkWrite(conditionExpression, "ConditionExpression");
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues);
|
||||
DynamoDbNumberUtils.requireStorable(exprAttrValues, true);
|
||||
JsonNode expectedDel = request.has("Expected") ? request.get("Expected") : null;
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(expectedDel));
|
||||
|
||||
@@ -728,6 +728,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
"Can not use both expression and non-expression parameters in the same request: "
|
||||
+ "Non-expression parameters: {Expected} Expression parameters: {ConditionExpression}", 400);
|
||||
}
|
||||
requireReadableValues(exprAttrValues, VALIDATION_ENVELOPE);
|
||||
|
||||
// EAN/EAV with no expression to reference them: AWS reports "can only be specified
|
||||
// when using expressions", not the "unused in expressions" wording (#2893).
|
||||
@@ -783,13 +784,12 @@ public class NativeDynamoDbJsonHandler {
|
||||
+ "Member must satisfy enum value set: [NONE, ALL_OLD, ALL_NEW, UPDATED_OLD, UPDATED_NEW]");
|
||||
}
|
||||
requireValidReturnConsumedCapacity(request);
|
||||
requireReadableValues(key, VALIDATION_ENVELOPE);
|
||||
DynamoDbExpressionSize.checkWrite(updateExpression, "UpdateExpression");
|
||||
DynamoDbExpressionSize.checkWrite(conditionExpression, "ConditionExpression");
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues);
|
||||
DynamoDbNumberUtils.requireStorable(exprAttrValues, true);
|
||||
ObjectNode attributeUpdateValues = legacyValues(attributeUpdates);
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(attributeUpdateValues);
|
||||
DynamoDbNumberUtils.requireStorable(attributeUpdateValues, true);
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(
|
||||
legacyValues(request.has("Expected") ? request.get("Expected") : null));
|
||||
|
||||
@@ -803,11 +803,13 @@ public class NativeDynamoDbJsonHandler {
|
||||
"Can not use both expression and non-expression parameters in the same request: "
|
||||
+ "Non-expression parameters: {AttributeUpdates} Expression parameters: {UpdateExpression}", 400);
|
||||
}
|
||||
requireReadableValues(exprAttrValues, VALIDATION_ENVELOPE);
|
||||
|
||||
// EAN/EAV with no expression to reference them, mirroring the PutItem guard.
|
||||
rejectExprAttrsWithoutExpression(exprAttrNames, exprAttrValues,
|
||||
updateExpression != null || conditionExpression != null,
|
||||
"UpdateExpression is null, ConditionExpression is null");
|
||||
requireReadableValues(attributeUpdateValues, VALIDATION_ENVELOPE);
|
||||
|
||||
ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression");
|
||||
|
||||
@@ -888,6 +890,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
}
|
||||
dynamoDbService.requireItemCallKey(tableName, keyOrItem, isKeyArg, region);
|
||||
validateLegacyExpected(expected);
|
||||
requireReadableValues(legacyValues(expected), VALIDATION_ENVELOPE);
|
||||
return existing -> evaluateLegacyExpected(existing, expected, conditionalOperator,
|
||||
returnValuesOnConditionCheckFailure);
|
||||
}
|
||||
@@ -1068,6 +1071,15 @@ public class NativeDynamoDbJsonHandler {
|
||||
return values;
|
||||
}
|
||||
|
||||
private static final String VALIDATION_ENVELOPE = "1 validation error detected: ";
|
||||
|
||||
// DynamoDB reads the numbers of each request member at its own stage.
|
||||
private static void requireReadableValues(JsonNode values, String prefix) {
|
||||
if (values != null) {
|
||||
DynamoDbNumberUtils.forEachNumber(values, text -> DynamoDbNumberUtils.requireItemNumber(text, prefix));
|
||||
}
|
||||
}
|
||||
|
||||
private static final Set<String> VALID_SELECT = Set.of(
|
||||
"ALL_ATTRIBUTES", "ALL_PROJECTED_ATTRIBUTES", "SPECIFIC_ATTRIBUTES", "COUNT");
|
||||
|
||||
@@ -1117,6 +1129,10 @@ public class NativeDynamoDbJsonHandler {
|
||||
? List.of("AttributesToGet", "QueryFilter", "ConditionalOperator")
|
||||
: List.of("AttributesToGet", "QueryFilter", "ConditionalOperator", "KeyConditions"),
|
||||
List.of("ProjectionExpression", "FilterExpression", "KeyConditionExpression"));
|
||||
requireReadableValues(exprAttrValues, "");
|
||||
requireReadableValues(exclusiveStartKey, "");
|
||||
requireReadableValues(legacyValues(keyConditions), "");
|
||||
requireReadableValues(legacyValues(queryFilter), "");
|
||||
|
||||
if (keyConditionExpr == null && keyConditions == null) {
|
||||
throw new AwsException("ValidationException",
|
||||
@@ -1133,7 +1149,6 @@ public class NativeDynamoDbJsonHandler {
|
||||
DynamoDbExpressionSize.checkRead(filterExpr, "FilterExpression");
|
||||
DynamoDbExpressionSize.checkRead(projectionExpression, "ProjectionExpression");
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues);
|
||||
DynamoDbNumberUtils.requireStorable(exprAttrValues, false);
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(keyConditions), false);
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(legacyValues(queryFilter), false);
|
||||
ExpressionEvaluator.validateSyntax(keyConditionExpr, "KeyConditionExpression");
|
||||
@@ -1282,11 +1297,13 @@ public class NativeDynamoDbJsonHandler {
|
||||
rejectMixedParameters(request,
|
||||
List.of("AttributesToGet", "ScanFilter", "ConditionalOperator"),
|
||||
List.of("ProjectionExpression", "FilterExpression"));
|
||||
requireReadableValues(exprAttrValues, "");
|
||||
requireReadableValues(exclusiveStartKey, "");
|
||||
requireReadableValues(legacyValues(scanFilter), "");
|
||||
|
||||
DynamoDbExpressionSize.checkReadWithSize(filterExpr, "FilterExpression");
|
||||
DynamoDbExpressionSize.checkRead(projectionExpressionScan, "ProjectionExpression");
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues);
|
||||
DynamoDbNumberUtils.requireStorable(exprAttrValues, false);
|
||||
ExpressionEvaluator.validateSyntax(filterExpr, "FilterExpression");
|
||||
// DynamoDB reports filter errors before projection errors.
|
||||
requireDefinedTokens(filterExpr, "FilterExpression", exprAttrNames, exprAttrValues);
|
||||
@@ -1513,6 +1530,17 @@ public class NativeDynamoDbJsonHandler {
|
||||
}
|
||||
}
|
||||
|
||||
requireValidReturnConsumedCapacity(request);
|
||||
// BatchWriteItem still reads a number the way a PartiQL parameter is read.
|
||||
for (List<JsonNode> writes : items.values()) {
|
||||
for (JsonNode writeReq : writes) {
|
||||
DynamoDbNumberUtils.forEachNumber(writeReq.path("PutRequest").path("Item"),
|
||||
DynamoDbNumberUtils::requirePartiQLParameterNumber);
|
||||
DynamoDbNumberUtils.forEachNumber(writeReq.path("DeleteRequest").path("Key"),
|
||||
DynamoDbNumberUtils::requirePartiQLParameterNumber);
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, List<JsonNode>> entry : items.entrySet()) {
|
||||
TableDefinition bwTable = dynamoDbService.requireTableForItemCall(entry.getKey(), region);
|
||||
Set<String> seen = new HashSet<>();
|
||||
@@ -1531,7 +1559,6 @@ public class NativeDynamoDbJsonHandler {
|
||||
|
||||
// The per-table write cost needs the stored images from before the batch runs.
|
||||
String returnCCBatch = request.path("ReturnConsumedCapacity").asText("NONE");
|
||||
requireValidReturnConsumedCapacity(request);
|
||||
Map<String, DynamoDbWriteCapacity.Cost> costs = null;
|
||||
if (!"NONE".equals(returnCCBatch)) {
|
||||
costs = new LinkedHashMap<>();
|
||||
@@ -1619,6 +1646,18 @@ public class NativeDynamoDbJsonHandler {
|
||||
}
|
||||
}
|
||||
|
||||
// DynamoDB compares the keys as sent before it reads their numbers.
|
||||
for (JsonNode tableRequest : items.values()) {
|
||||
Set<JsonNode> sent = new HashSet<>();
|
||||
for (JsonNode key : tableRequest.path("Keys")) {
|
||||
if (!sent.add(key)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Provided list of item keys contains duplicates", 400);
|
||||
}
|
||||
}
|
||||
tableRequest.path("Keys").forEach(key -> requireReadableValues(key, ""));
|
||||
}
|
||||
|
||||
for (Map.Entry<String, JsonNode> entry : items.entrySet()) {
|
||||
TableDefinition bgTable = dynamoDbService.requireTableForItemCall(entry.getKey(), region);
|
||||
JsonNode keys = entry.getValue().get("Keys");
|
||||
@@ -2376,7 +2415,8 @@ public class NativeDynamoDbJsonHandler {
|
||||
JsonNode transactItemsNode = requireValidTransactRequest(request);
|
||||
|
||||
for (JsonNode txItem : transactItemsNode) {
|
||||
for (JsonNode op : txItem) {
|
||||
for (Map.Entry<String, JsonNode> action : txItem.properties()) {
|
||||
JsonNode op = action.getValue();
|
||||
String conditionExpression = op.has("ConditionExpression")
|
||||
? op.get("ConditionExpression").asText() : null;
|
||||
String updateExpression = op.has("UpdateExpression")
|
||||
@@ -2388,6 +2428,11 @@ public class NativeDynamoDbJsonHandler {
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("Item"), false);
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("Key"), false);
|
||||
DynamoDbAttributeValueValidator.requireNestingWithinLimit(opExprAttrValues, false);
|
||||
requireReadableValues(op.get("Put".equals(action.getKey()) ? "Item" : "Key"), "");
|
||||
// An action without an expression ignores its ExpressionAttributeValues.
|
||||
if (conditionExpression != null || updateExpression != null) {
|
||||
requireReadableValues(opExprAttrValues, "");
|
||||
}
|
||||
DynamoDbExpressionSize.checkRead(op.path("UpdateExpression").textValue(), "UpdateExpression");
|
||||
DynamoDbExpressionSize.checkReadWithSize(conditionExpression, "ConditionExpression");
|
||||
ExpressionEvaluator.validateSyntax(conditionExpression, "ConditionExpression");
|
||||
@@ -2535,6 +2580,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
|
||||
private Response handleTransactGetItems(JsonNode request, String region) {
|
||||
JsonNode transactItemsNode = requireValidTransactRequest(request);
|
||||
transactItemsNode.forEach(txItem -> requireReadableValues(txItem.path("Get").path("Key"), ""));
|
||||
|
||||
for (JsonNode txItem : transactItemsNode) {
|
||||
DynamoDbExpressionSize.checkRead(txItem.path("Get").path("ProjectionExpression").textValue(),
|
||||
|
||||
Reference in new issue
Block a user