mirror of
https://github.com/floci-io/floci.git
synced 2026-11-25 14:57:54 -05:00
fix(dynamodb): bind every PartiQL parameter type and reject unordered operands (#3288)
* fix(dynamodb): bind every PartiQL parameter type and reject unordered operands PartiQL parameters only bound S, N, BOOL and NULL. Every other type was rejected with "Unsupported parameter type in parameters array", so a statement could not compare against B, SS, NS, BS, L or M. Keep those parameters as their wire node and pass them through unchanged. Binding them makes the ordering operators reachable for types that have no ordering, so <, <=, >, >= and BETWEEN now reject any operand outside S, N and B. The message names the operator as it was written. Takes tier2/partiql in the dynamodb-conformance suite from 70 to 113 of 186, with no regression elsewhere in the suite. * fix(dynamodb): validate PartiQL parameters as AttributeValues before they run A parameter was bound on the first type key it carried. A value with zero or several type keys was accepted, and a binary that is not base64 reached the comparison and failed with a 500. Check every parameter the way AWS reads a request. Base64 is checked first and fails with SerializationException. Then each value must carry exactly one type key, down through lists and maps, or it is a ValidationException with the AWS wording. The binary comparison in ExpressionEvaluator now decodes through the guarded path, so a FilterExpression against a binary that is not base64 is a 400 as well. Checked against real DynamoDB in ap-northeast-1 on 2026-09-10.
This commit is contained in:
1 parent
a501eabbf9
commit
66ca9dc4a6
7 files changed
+550
-17
No files matched your search
+89
@@ -0,0 +1,89 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Checks a wire-format AttributeValue the way AWS does before a request runs.
|
||||
* Base64 is checked while the request is read, so a bad value is a
|
||||
* SerializationException. The one-type rule is a ValidationException.
|
||||
*/
|
||||
final class DynamoDbAttributeValueValidator {
|
||||
|
||||
private static final List<String> TYPES = List.of("S", "N", "B", "BOOL", "NULL", "SS", "NS", "BS", "L", "M");
|
||||
|
||||
private DynamoDbAttributeValueValidator() {}
|
||||
|
||||
static void validate(JsonNode value) {
|
||||
requireBase64(value);
|
||||
requireOneType(value);
|
||||
}
|
||||
|
||||
static String typeOf(JsonNode value) {
|
||||
if (!value.isObject()) {
|
||||
throw serializationEx("Unexpected value type in payload");
|
||||
}
|
||||
var types = TYPES.stream().filter(value::has).toList();
|
||||
if (types.isEmpty()) {
|
||||
throw validationEx("Supplied AttributeValue is empty, must contain exactly one of the supported datatypes");
|
||||
}
|
||||
if (types.size() > 1) {
|
||||
throw validationEx("Supplied AttributeValue has more than one datatypes set, "
|
||||
+ "must contain exactly one of the supported datatypes");
|
||||
}
|
||||
return types.getFirst();
|
||||
}
|
||||
|
||||
private static void requireOneType(JsonNode value) {
|
||||
switch (typeOf(value)) {
|
||||
case "L" -> value.get("L").forEach(DynamoDbAttributeValueValidator::requireOneType);
|
||||
case "M" -> value.get("M").forEach(DynamoDbAttributeValueValidator::requireOneType);
|
||||
default -> { }
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireBase64(JsonNode value) {
|
||||
if (!value.isObject()) {
|
||||
return;
|
||||
}
|
||||
if (value.has("B")) {
|
||||
requireBase64Text(value.get("B"));
|
||||
}
|
||||
if (value.has("BS")) {
|
||||
value.get("BS").forEach(DynamoDbAttributeValueValidator::requireBase64Text);
|
||||
}
|
||||
if (value.has("L")) {
|
||||
value.get("L").forEach(DynamoDbAttributeValueValidator::requireBase64);
|
||||
}
|
||||
if (value.has("M")) {
|
||||
value.get("M").forEach(DynamoDbAttributeValueValidator::requireBase64);
|
||||
}
|
||||
}
|
||||
|
||||
// AWS checks the length before the alphabet, and names only the length.
|
||||
private static void requireBase64Text(JsonNode encoded) {
|
||||
if (!encoded.isTextual()) {
|
||||
throw serializationEx("only base-64-encoded strings are convertible to bytes");
|
||||
}
|
||||
var text = encoded.asText();
|
||||
if (text.length() % 4 != 0) {
|
||||
throw serializationEx("Base64 encoded length is expected a multiple of 4 bytes but found: " + text.length());
|
||||
}
|
||||
try {
|
||||
Base64.getDecoder().decode(text);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw serializationEx("Unexpected value type in payload");
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException validationEx(String message) {
|
||||
return new AwsException("ValidationException", message, 400);
|
||||
}
|
||||
|
||||
private static AwsException serializationEx(String message) {
|
||||
return new AwsException("SerializationException", message, 400);
|
||||
}
|
||||
}
|
||||
+7
-8
@@ -391,14 +391,13 @@ class DynamoDbPartiQLHandler {
|
||||
}
|
||||
|
||||
JsonNode toTypedNode(PVal val) {
|
||||
ObjectNode node = mapper.createObjectNode();
|
||||
switch (val) {
|
||||
case PVal.Str s -> node.put("S", s.v());
|
||||
case PVal.Num n -> node.put("N", DynamoDbNumberUtils.validateAndNormalize(n.v()));
|
||||
case PVal.Bool b -> node.put("BOOL", b.v());
|
||||
case PVal.Null ignored -> node.put("NULL", true);
|
||||
}
|
||||
return node;
|
||||
return switch (val) {
|
||||
case PVal.Str s -> mapper.createObjectNode().put("S", s.v());
|
||||
case PVal.Num n -> mapper.createObjectNode().put("N", DynamoDbNumberUtils.validateAndNormalize(n.v()));
|
||||
case PVal.Bool b -> mapper.createObjectNode().put("BOOL", b.v());
|
||||
case PVal.Null ignored -> mapper.createObjectNode().put("NULL", true);
|
||||
case PVal.Av av -> av.node();
|
||||
};
|
||||
}
|
||||
|
||||
private ObjectNode emptyItemsResponse() {
|
||||
|
||||
+42
-7
@@ -58,11 +58,13 @@ public class DynamoDbPartiQLParser {
|
||||
}
|
||||
}
|
||||
|
||||
sealed interface PVal permits PVal.Str, PVal.Num, PVal.Bool, PVal.Null {
|
||||
sealed interface PVal permits PVal.Str, PVal.Num, PVal.Bool, PVal.Null, PVal.Av {
|
||||
record Str(String v) implements PVal {}
|
||||
record Num(String v) implements PVal {}
|
||||
record Bool(boolean v) implements PVal {}
|
||||
record Null() implements PVal {}
|
||||
// A parameter of a type with no literal syntax, kept as its wire node.
|
||||
record Av(String type, JsonNode node) implements PVal {}
|
||||
}
|
||||
|
||||
sealed interface Cond permits Cond.Eq, Cond.Cmp, Cond.Between, Cond.BeginsWith {
|
||||
@@ -167,6 +169,7 @@ public class DynamoDbPartiQLParser {
|
||||
}
|
||||
|
||||
static Stmt parse(String statement, List<JsonNode> parameters) {
|
||||
parameters.forEach(DynamoDbAttributeValueValidator::validate);
|
||||
return new DynamoDbPartiQLParser(tokenize(statement.trim()), parameters).parseStmt();
|
||||
}
|
||||
|
||||
@@ -257,6 +260,27 @@ public class DynamoDbPartiQLParser {
|
||||
return conds;
|
||||
}
|
||||
|
||||
// S, N and B are the only types DynamoDB gives an ordering.
|
||||
private static final Set<String> ORDERED_TYPES = Set.of("S", "N", "B");
|
||||
|
||||
private static String typeCode(PVal val) {
|
||||
return switch (val) {
|
||||
case PVal.Str ignored -> "S";
|
||||
case PVal.Num ignored -> "N";
|
||||
case PVal.Bool ignored -> "BOOL";
|
||||
case PVal.Null ignored -> "NULL";
|
||||
case PVal.Av av -> av.type();
|
||||
};
|
||||
}
|
||||
|
||||
private static void requireOrdered(String op, PVal val) {
|
||||
String type = typeCode(val);
|
||||
if (!ORDERED_TYPES.contains(type)) {
|
||||
throw validationEx("Incorrect operand type for operator or function; "
|
||||
+ "operator or function: " + op + ", operand type: " + type);
|
||||
}
|
||||
}
|
||||
|
||||
private Cond parseCond() {
|
||||
if (peek().type() == TType.IDENT && "begins_with".equalsIgnoreCase(peek().value())) {
|
||||
advance();
|
||||
@@ -273,11 +297,19 @@ public class DynamoDbPartiQLParser {
|
||||
PVal lo = parseValue();
|
||||
consume(TType.AND);
|
||||
PVal hi = parseValue();
|
||||
requireOrdered("BETWEEN", lo);
|
||||
requireOrdered("BETWEEN", hi);
|
||||
return new Cond.Between(attr, lo, hi);
|
||||
}
|
||||
String op = parseOp();
|
||||
PVal val = parseValue();
|
||||
return "=".equals(op) ? new Cond.Eq(attr, val) : new Cond.Cmp(attr, op, val);
|
||||
if ("=".equals(op)) {
|
||||
return new Cond.Eq(attr, val);
|
||||
}
|
||||
if (!"<>".equals(op)) {
|
||||
requireOrdered(op, val);
|
||||
}
|
||||
return new Cond.Cmp(attr, op, val);
|
||||
}
|
||||
|
||||
private String parseOp() {
|
||||
@@ -313,11 +345,14 @@ public class DynamoDbPartiQLParser {
|
||||
throw validationEx("Not enough parameters supplied for ? placeholders");
|
||||
}
|
||||
JsonNode p = parameters.get(paramIdx++);
|
||||
if (p.has("S")) return new PVal.Str(p.get("S").asText());
|
||||
if (p.has("N")) return new PVal.Num(p.get("N").asText());
|
||||
if (p.has("BOOL")) return new PVal.Bool(p.get("BOOL").asBoolean());
|
||||
if (p.has("NULL")) return new PVal.Null();
|
||||
throw validationEx("Unsupported parameter type in parameters array");
|
||||
var type = DynamoDbAttributeValueValidator.typeOf(p);
|
||||
return switch (type) {
|
||||
case "S" -> new PVal.Str(p.get("S").asText());
|
||||
case "N" -> new PVal.Num(p.get("N").asText());
|
||||
case "BOOL" -> new PVal.Bool(p.get("BOOL").asBoolean());
|
||||
case "NULL" -> new PVal.Null();
|
||||
default -> new PVal.Av(type, p);
|
||||
};
|
||||
}
|
||||
|
||||
private String expectIdent() {
|
||||
|
||||
@@ -1096,8 +1096,8 @@ final class ExpressionEvaluator {
|
||||
}
|
||||
}
|
||||
if (a.has("B") && b.has("B")) {
|
||||
byte[] aBytes = Base64.getDecoder().decode(a.get("B").asText());
|
||||
byte[] bBytes = Base64.getDecoder().decode(b.get("B").asText());
|
||||
var aBytes = decodeBinaryBound(a);
|
||||
var bBytes = decodeBinaryBound(b);
|
||||
int minLen = Math.min(aBytes.length, bBytes.length);
|
||||
for (int i = 0; i < minLen; i++) {
|
||||
int diff = (aBytes[i] & 0xFF) - (bBytes[i] & 0xFF);
|
||||
|
||||
Reference in new issue
Block a user