mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(dynamodb): size() of a Binary counts its bytes, not its base64 text (#5058)
* fix(dynamodb): size() of a Binary counts its bytes, not its base64 text size() on a B attribute returned the length of the base64 text, so a 3-byte value had size 4 and conditions and filters such as size(b) = :three gave the wrong answer. It now decodes the value first. begins_with on binaries now decodes through the same helper, so a value that is not base64 fails with a 400 SerializationException, as the binary comparisons already did, instead of an uncaught 500. * test(dynamodb): cover begins_with matching a valid binary prefix
This commit is contained in:
1 parent
39ac662165
commit
db2a67f122
2 files changed
+70
-10
No files matched your search
+10
-10
@@ -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();
|
||||
|
||||
+60
@@ -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\"}}");
|
||||
|
||||
Reference in new issue
Block a user