Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(dynamodb): size() of a Binary counts its bytes, not its base64 text (#5058)

Niels Kootstradb2a67f12250
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java
index 20e54d4678..5152b4b29d 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java
@@ -508,16 +508,16 @@ final class ExpressionEvaluator {
high.get("S").asText().getBytes(StandardCharsets.UTF_8));
}
if (low.has("B") && high.has("B")) {
- return Arrays.compareUnsigned(decodeBinaryBound(low), decodeBinaryBound(high));
+ return Arrays.compareUnsigned(decodeBinary(low), decodeBinary(high));
}
return compareAttributeValues(low, high);
}
- // A binary value that is not valid base64 never reaches a comparison on AWS: the request
- // fails to deserialize first, with a 400 SerializationException.
- private static byte[] decodeBinaryBound(JsonNode bound) {
+ // A binary value that is not valid base64 never reaches a comparison, begins_with or size()
+ // on AWS: the request fails to deserialize first, with a 400 SerializationException.
+ private static byte[] decodeBinary(JsonNode value) {
try {
- return Base64.getDecoder().decode(bound.get("B").asText());
+ return Base64.getDecoder().decode(value.get("B").asText());
} catch (IllegalArgumentException e) {
throw new AwsException("SerializationException",
"Unexpected value type in payload", 400);
@@ -791,8 +791,8 @@ final class ExpressionEvaluator {
JsonNode prefixNode = resolveAttributeValue(func.args().get(1), item, exprAttrNames, exprAttrValues);
if (attrNode == null || prefixNode == null) yield false;
if (attrNode.has("B") && prefixNode.has("B")) {
- byte[] attrBytes = java.util.Base64.getDecoder().decode(attrNode.get("B").asText());
- byte[] prefixBytes = java.util.Base64.getDecoder().decode(prefixNode.get("B").asText());
+ byte[] attrBytes = decodeBinary(attrNode);
+ byte[] prefixBytes = decodeBinary(prefixNode);
if (prefixBytes.length > attrBytes.length) yield false;
for (int bi = 0; bi < prefixBytes.length; bi++) {
if (attrBytes[bi] != prefixBytes[bi]) yield false;
@@ -1120,8 +1120,8 @@ final class ExpressionEvaluator {
}
}
if (a.has("B") && b.has("B")) {
- byte[] aBytes = decodeBinaryBound(a);
- byte[] bBytes = decodeBinaryBound(b);
+ byte[] aBytes = decodeBinary(a);
+ byte[] bBytes = decodeBinary(b);
int minLen = Math.min(aBytes.length, bBytes.length);
for (int i = 0; i < minLen; i++) {
int diff = (aBytes[i] & 0xFF) - (bBytes[i] & 0xFF);
@@ -1138,7 +1138,7 @@ final class ExpressionEvaluator {
private static int computeSize(JsonNode attrNode) {
if (attrNode.has("S")) return attrNode.get("S").asText().length();
- if (attrNode.has("B")) return attrNode.get("B").asText().length(); // base64 length
+ if (attrNode.has("B")) return decodeBinary(attrNode).length;
if (attrNode.has("L")) return attrNode.get("L").size();
if (attrNode.has("M")) return attrNode.get("M").size();
if (attrNode.has("SS")) return attrNode.get("SS").size();
diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluatorTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluatorTest.java
index e81ba2c692..31d75e766f 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluatorTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluatorTest.java
@@ -268,6 +268,66 @@ class ExpressionEvaluatorTest {
return mapper.readTree(json);
}
+ // DynamoDB's size() of a Binary is its length in bytes; "AQID" encodes the three bytes 1, 2, 3.
+ @Test
+ void sizeOfABinaryCountsItsBytes() throws Exception {
+ JsonNode i = item("{\"b\": {\"B\": \"AQID\"}}");
+ JsonNode v = values("{\":three\": {\"N\": \"3\"}}");
+ assertTrue(ExpressionEvaluator.matches("size(b) = :three", i, null, v));
+ }
+
+ @Test
+ void aBinaryOfThreeBytesIsSmallerThanFour() throws Exception {
+ JsonNode i = item("{\"b\": {\"B\": \"AQID\"}}");
+ JsonNode v = values("{\":four\": {\"N\": \"4\"}}");
+ assertTrue(ExpressionEvaluator.matches("size(b) < :four", i, null, v));
+ }
+
+ @Test
+ void sizeOfAnEmptyBinaryIsZero() throws Exception {
+ JsonNode i = item("{\"b\": {\"B\": \"\"}}");
+ JsonNode v = values("{\":zero\": {\"N\": \"0\"}}");
+ assertTrue(ExpressionEvaluator.matches("size(b) = :zero", i, null, v));
+ }
+
+ // A binary that is not base64 fails with a 400 SerializationException, as the binary
+ // comparisons below do, rather than being sized by its text.
+ @Test
+ void sizeOfABinaryThatIsNotBase64IsRejected() throws Exception {
+ JsonNode i = item("{\"b\": {\"B\": \"not base64!!\"}}");
+ JsonNode v = values("{\":three\": {\"N\": \"3\"}}");
+ AwsException e = assertThrows(AwsException.class,
+ () -> ExpressionEvaluator.matches("size(b) = :three", i, null, v));
+ assertEquals("SerializationException", e.getErrorCode());
+ assertEquals(400, e.getHttpStatus());
+ }
+
+ // begins_with on a Binary compares bytes: "AQ==" (the byte 1) is a prefix of "AQID"
+ // (the bytes 1, 2, 3) even though its base64 text is not.
+ @Test
+ void beginsWithOnABinaryMatchesABytePrefix() throws Exception {
+ JsonNode i = item("{\"b\": {\"B\": \"AQID\"}}");
+ JsonNode v = values("{\":prefix\": {\"B\": \"AQ==\"}}");
+ assertTrue(ExpressionEvaluator.matches("begins_with(b, :prefix)", i, null, v));
+ }
+
+ @Test
+ void beginsWithOnABinaryRejectsADifferentFirstByte() throws Exception {
+ JsonNode i = item("{\"b\": {\"B\": \"AQID\"}}");
+ JsonNode v = values("{\":prefix\": {\"B\": \"Ag==\"}}");
+ assertFalse(ExpressionEvaluator.matches("begins_with(b, :prefix)", i, null, v));
+ }
+
+ @Test
+ void beginsWithOnABinaryThatIsNotBase64IsRejected() throws Exception {
+ JsonNode i = item("{\"b\": {\"B\": \"not base64!!\"}}");
+ JsonNode v = values("{\":prefix\": {\"B\": \"AQ==\"}}");
+ AwsException e = assertThrows(AwsException.class,
+ () -> ExpressionEvaluator.matches("begins_with(b, :prefix)", i, null, v));
+ assertEquals("SerializationException", e.getErrorCode());
+ assertEquals(400, e.getHttpStatus());
+ }
+
@Test
void orderingAgainstAnotherTypeMatchesNothing() throws Exception {
JsonNode i = item("{\"a\": {\"S\": \"5\"}}");