mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 17:33:03 -05:00
fix(sqs): reject empty and malformed message attribute values (#8061)
SendMessage and SendMessageBatch on both the JSON and query protocols now answer InvalidParameterValue for a message attribute whose StringValue or BinaryValue is empty or whose BinaryValue is not base64, instead of storing an empty value or failing with a 500 on the decode. In a batch, only the entry carrying the bad attribute fails; the other entries are still sent.
An attribute missing its DataType, or carrying neither a StringValue nor a
BinaryValue, was silently dropped while an empty value was rejected. Both now
fail with InvalidParameterValue, using the wording LocalStack's snapshot tests
record: 'Missing required parameter DataType' and the existing non-empty value
message.
SendMessageBatch now writes the failed entry as soon as its attributes fail
to parse and skips it, rather than carrying the exception to the send loop.
The query handler parses SendMessage and SendMessageBatch attributes through
one helper.
A batch entry with an invalid message attribute was left out of the batch
payload total, so a batch over MaximumMessageSize only because of that
entry was accepted and the bad entry reported as a per-entry failure.
LocalStack and moto both sum every entry before any per-entry check, so the
total now comes from each entry's raw body and attributes, independent of
attribute validation, and such a batch fails whole with BatchRequestTooLong.
The empty-value error derived the base type with split("\\.")[0], which
throws ArrayIndexOutOfBoundsException, surfacing as a 500, for a DataType
of "." or "..". Take the substring before the first '.' instead.
A DataType whose base type (the part before the first period) is not
String, Number or Binary, such as "Strin", "Foo" or ".", was accepted.
SendMessage now rejects it with InvalidParameterValue, and SendMessageBatch
fails only that entry. The match is case-sensitive, and the message follows
LocalStack's check_attributes in localstack-core/localstack/services/sqs/
provider.py, which names the parameter literally as
MessageAttributes.Attribute_name.DataType. LocalStack's f-string drops the
space in "prefixed with"; the space is kept here.
The empty-String-value, missing-DataType and DataType-prefix messages come
from LocalStack's check_attributes. The empty Number and Binary value and
invalid base64 messages are Floci's own wording, unconfirmed against AWS,
so those cases now assert the error code only.
computeMessageSize now counts each attribute through the same helper the batch payload check uses on raw wire fields, so the name, type and value byte rule lives in one place.
Adds JSON and Query batches whose bodies fit the 1 MiB limit but whose invalid entry's attribute pushes the batch over it, plus service tests for UTF-8 byte lengths, decoded binary lengths, the invalid-base64 fallback and computeMessageSize.
Signed-off-by: Tyler Webb <tyler.webb@verkada.com>
This commit is contained in:
1 parent
236e20003f
commit
e41cc9b88d
7 files changed
+505
-83
No files matched your search
@@ -1076,6 +1076,170 @@ class SqsIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedMessageAttributeValuesAreRejected() {
|
||||
String attributeQueueUrl = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateQueue")
|
||||
.formParam("QueueName", "query-attribute-value-queue")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl");
|
||||
|
||||
try {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "SendMessage")
|
||||
.formParam("QueueUrl", attributeQueueUrl)
|
||||
.formParam("MessageBody", "x")
|
||||
.formParam("MessageAttribute.1.Name", "empty")
|
||||
.formParam("MessageAttribute.1.Value.DataType", "String")
|
||||
.formParam("MessageAttribute.1.Value.StringValue", "")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Type", equalTo("Sender"))
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo(
|
||||
"Message (user) attribute 'empty' must contain a non-empty value of type 'String'."));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "SendMessage")
|
||||
.formParam("QueueUrl", attributeQueueUrl)
|
||||
.formParam("MessageBody", "x")
|
||||
.formParam("MessageAttribute.1.Name", "bin")
|
||||
.formParam("MessageAttribute.1.Value.DataType", "Binary")
|
||||
.formParam("MessageAttribute.1.Value.BinaryValue", "not base64!")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "SendMessage")
|
||||
.formParam("QueueUrl", attributeQueueUrl)
|
||||
.formParam("MessageBody", "x")
|
||||
.formParam("MessageAttribute.1.Name", "bin")
|
||||
.formParam("MessageAttribute.1.Value.DataType", "Binary")
|
||||
.formParam("MessageAttribute.1.Value.BinaryValue", "")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "SendMessage")
|
||||
.formParam("QueueUrl", attributeQueueUrl)
|
||||
.formParam("MessageBody", "x")
|
||||
.formParam("MessageAttribute.1.Name", "novalue")
|
||||
.formParam("MessageAttribute.1.Value.DataType", "String")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo(
|
||||
"Message (user) attribute 'novalue' must contain a non-empty value of type 'String'."));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "SendMessage")
|
||||
.formParam("QueueUrl", attributeQueueUrl)
|
||||
.formParam("MessageBody", "x")
|
||||
.formParam("MessageAttribute.1.Name", "notype")
|
||||
.formParam("MessageAttribute.1.Value.StringValue", "v")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("Missing required parameter DataType"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "SendMessage")
|
||||
.formParam("QueueUrl", attributeQueueUrl)
|
||||
.formParam("MessageBody", "x")
|
||||
.formParam("MessageAttribute.1.Name", "odd")
|
||||
.formParam("MessageAttribute.1.Value.DataType", ".")
|
||||
.formParam("MessageAttribute.1.Value.StringValue", "v")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("Type for parameter "
|
||||
+ "MessageAttributes.Attribute_name.DataType must be prefixed with "
|
||||
+ "\"String\", \"Binary\", or \"Number\", but was: ."));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "SendMessageBatch")
|
||||
.formParam("QueueUrl", attributeQueueUrl)
|
||||
.formParam("SendMessageBatchRequestEntry.1.Id", "ok")
|
||||
.formParam("SendMessageBatchRequestEntry.1.MessageBody", "x")
|
||||
.formParam("SendMessageBatchRequestEntry.1.MessageAttribute.1.Name", "bin")
|
||||
.formParam("SendMessageBatchRequestEntry.1.MessageAttribute.1.Value.DataType", "Binary")
|
||||
.formParam("SendMessageBatchRequestEntry.1.MessageAttribute.1.Value.BinaryValue", "AQID")
|
||||
.formParam("SendMessageBatchRequestEntry.2.Id", "empty")
|
||||
.formParam("SendMessageBatchRequestEntry.2.MessageBody", "x")
|
||||
.formParam("SendMessageBatchRequestEntry.2.MessageAttribute.1.Name", "a")
|
||||
.formParam("SendMessageBatchRequestEntry.2.MessageAttribute.1.Value.DataType", "Number")
|
||||
.formParam("SendMessageBatchRequestEntry.2.MessageAttribute.1.Value.StringValue", "")
|
||||
.formParam("SendMessageBatchRequestEntry.3.Id", "bad-binary")
|
||||
.formParam("SendMessageBatchRequestEntry.3.MessageBody", "x")
|
||||
.formParam("SendMessageBatchRequestEntry.3.MessageAttribute.1.Name", "b")
|
||||
.formParam("SendMessageBatchRequestEntry.3.MessageAttribute.1.Value.DataType", "Binary")
|
||||
.formParam("SendMessageBatchRequestEntry.3.MessageAttribute.1.Value.BinaryValue", "not base64!")
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("SendMessageBatchResponse.SendMessageBatchResult.SendMessageBatchResultEntry.Id", equalTo("ok"))
|
||||
.body("SendMessageBatchResponse.SendMessageBatchResult.BatchResultErrorEntry.Id",
|
||||
contains("empty", "bad-binary"))
|
||||
.body("SendMessageBatchResponse.SendMessageBatchResult.BatchResultErrorEntry.Code",
|
||||
everyItem(equalTo("InvalidParameterValue")))
|
||||
.body("SendMessageBatchResponse.SendMessageBatchResult.BatchResultErrorEntry.SenderFault",
|
||||
everyItem(equalTo("true")));
|
||||
|
||||
String bigBody = "x".repeat(400_000);
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "SendMessageBatch")
|
||||
.formParam("QueueUrl", attributeQueueUrl)
|
||||
.formParam("SendMessageBatchRequestEntry.1.Id", "a")
|
||||
.formParam("SendMessageBatchRequestEntry.1.MessageBody", bigBody)
|
||||
.formParam("SendMessageBatchRequestEntry.2.Id", "b")
|
||||
.formParam("SendMessageBatchRequestEntry.2.MessageBody", bigBody)
|
||||
.formParam("SendMessageBatchRequestEntry.3.Id", "c")
|
||||
.formParam("SendMessageBatchRequestEntry.3.MessageBody", bigBody)
|
||||
.formParam("SendMessageBatchRequestEntry.3.MessageAttribute.1.Name", "a")
|
||||
.formParam("SendMessageBatchRequestEntry.3.MessageAttribute.1.Value.DataType", "String")
|
||||
.formParam("SendMessageBatchRequestEntry.3.MessageAttribute.1.Value.StringValue", "")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("BatchRequestTooLong"));
|
||||
|
||||
String fittingBody = "x".repeat(300_000);
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "SendMessageBatch")
|
||||
.formParam("QueueUrl", attributeQueueUrl)
|
||||
.formParam("SendMessageBatchRequestEntry.1.Id", "a")
|
||||
.formParam("SendMessageBatchRequestEntry.1.MessageBody", fittingBody)
|
||||
.formParam("SendMessageBatchRequestEntry.2.Id", "b")
|
||||
.formParam("SendMessageBatchRequestEntry.2.MessageBody", fittingBody)
|
||||
.formParam("SendMessageBatchRequestEntry.3.Id", "c")
|
||||
.formParam("SendMessageBatchRequestEntry.3.MessageBody", fittingBody)
|
||||
.formParam("SendMessageBatchRequestEntry.3.MessageAttribute.1.Name", "big")
|
||||
.formParam("SendMessageBatchRequestEntry.3.MessageAttribute.1.Value.StringValue",
|
||||
"y".repeat(200_000))
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("BatchRequestTooLong"));
|
||||
|
||||
assertEquals("1", allQueueAttributes(attributeQueueUrl).get("ApproximateNumberOfMessages"));
|
||||
} finally {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DeleteQueue")
|
||||
.formParam("QueueUrl", attributeQueueUrl)
|
||||
.when().post("/");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void receiveMessageReturnsPerMessageSenderId() {
|
||||
String testQueueUrl = given()
|
||||
|
||||
@@ -1085,6 +1085,140 @@ class SqsJsonProtocolTest {
|
||||
.body("__type", equalTo("InvalidParameterValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedMessageAttributeValuesAreRejected() {
|
||||
String attributeQueueUrl = given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.CreateQueue")
|
||||
.body("{\"QueueName\":\"json-attribute-value-queue\"}")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().jsonPath().getString("QueueUrl");
|
||||
|
||||
try {
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.SendMessage")
|
||||
.body("{\"QueueUrl\":\"" + attributeQueueUrl + "\",\"MessageBody\":\"x\","
|
||||
+ "\"MessageAttributes\":{\"empty\":{\"DataType\":\"String\",\"StringValue\":\"\"}}}")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValue"))
|
||||
.body("message", equalTo(
|
||||
"Message (user) attribute 'empty' must contain a non-empty value of type 'String'."));
|
||||
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.SendMessage")
|
||||
.body("{\"QueueUrl\":\"" + attributeQueueUrl + "\",\"MessageBody\":\"x\","
|
||||
+ "\"MessageAttributes\":{\"count\":{\"DataType\":\"Number.int\",\"StringValue\":\"\"}}}")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValue"));
|
||||
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.SendMessage")
|
||||
.body("{\"QueueUrl\":\"" + attributeQueueUrl + "\",\"MessageBody\":\"x\","
|
||||
+ "\"MessageAttributes\":{\"bin\":{\"DataType\":\"Binary\",\"BinaryValue\":\"not base64!\"}}}")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValue"));
|
||||
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.SendMessage")
|
||||
.body("{\"QueueUrl\":\"" + attributeQueueUrl + "\",\"MessageBody\":\"x\","
|
||||
+ "\"MessageAttributes\":{\"bin\":{\"DataType\":\"Binary\",\"BinaryValue\":\"\"}}}")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValue"));
|
||||
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.SendMessage")
|
||||
.body("{\"QueueUrl\":\"" + attributeQueueUrl + "\",\"MessageBody\":\"x\","
|
||||
+ "\"MessageAttributes\":{\"novalue\":{\"DataType\":\"String\"}}}")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValue"))
|
||||
.body("message", equalTo(
|
||||
"Message (user) attribute 'novalue' must contain a non-empty value of type 'String'."));
|
||||
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.SendMessage")
|
||||
.body("{\"QueueUrl\":\"" + attributeQueueUrl + "\",\"MessageBody\":\"x\","
|
||||
+ "\"MessageAttributes\":{\"notype\":{\"StringValue\":\"v\"}}}")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValue"))
|
||||
.body("message", equalTo("Missing required parameter DataType"));
|
||||
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.SendMessage")
|
||||
.body("{\"QueueUrl\":\"" + attributeQueueUrl + "\",\"MessageBody\":\"x\","
|
||||
+ "\"MessageAttributes\":{\"odd\":{\"DataType\":\"Strin\",\"StringValue\":\"v\"}}}")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValue"))
|
||||
.body("message", equalTo("Type for parameter MessageAttributes.Attribute_name.DataType must be "
|
||||
+ "prefixed with \"String\", \"Binary\", or \"Number\", but was: Strin"));
|
||||
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.SendMessageBatch")
|
||||
.body("{\"QueueUrl\":\"" + attributeQueueUrl + "\",\"Entries\":["
|
||||
+ "{\"Id\":\"ok\",\"MessageBody\":\"x\",\"MessageAttributes\":"
|
||||
+ "{\"bin\":{\"DataType\":\"Binary\",\"BinaryValue\":\"AQID\"}}},"
|
||||
+ "{\"Id\":\"empty\",\"MessageBody\":\"x\",\"MessageAttributes\":"
|
||||
+ "{\"a\":{\"DataType\":\"Number\",\"StringValue\":\"\"}}},"
|
||||
+ "{\"Id\":\"bad-binary\",\"MessageBody\":\"x\",\"MessageAttributes\":"
|
||||
+ "{\"b\":{\"DataType\":\"Binary\",\"BinaryValue\":\"not base64!\"}}},"
|
||||
+ "{\"Id\":\"plain\",\"MessageBody\":\"x\"}]}")
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body("Successful.Id", containsInAnyOrder("ok", "plain"))
|
||||
.body("Failed.Id", contains("empty", "bad-binary"))
|
||||
.body("Failed.Code", everyItem(equalTo("InvalidParameterValue")))
|
||||
.body("Failed.SenderFault", everyItem(equalTo(true)));
|
||||
|
||||
String bigBody = "x".repeat(400_000);
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.SendMessageBatch")
|
||||
.body("{\"QueueUrl\":\"" + attributeQueueUrl + "\",\"Entries\":["
|
||||
+ "{\"Id\":\"a\",\"MessageBody\":\"" + bigBody + "\"},"
|
||||
+ "{\"Id\":\"b\",\"MessageBody\":\"" + bigBody + "\"},"
|
||||
+ "{\"Id\":\"c\",\"MessageBody\":\"" + bigBody + "\",\"MessageAttributes\":"
|
||||
+ "{\"a\":{\"DataType\":\"String\",\"StringValue\":\"\"}}}]}")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BatchRequestTooLong"));
|
||||
|
||||
String fittingBody = "x".repeat(300_000);
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.SendMessageBatch")
|
||||
.body("{\"QueueUrl\":\"" + attributeQueueUrl + "\",\"Entries\":["
|
||||
+ "{\"Id\":\"a\",\"MessageBody\":\"" + fittingBody + "\"},"
|
||||
+ "{\"Id\":\"b\",\"MessageBody\":\"" + fittingBody + "\"},"
|
||||
+ "{\"Id\":\"c\",\"MessageBody\":\"" + fittingBody + "\",\"MessageAttributes\":"
|
||||
+ "{\"big\":{\"StringValue\":\"" + "y".repeat(200_000) + "\"}}}]}")
|
||||
.when().post("/").then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BatchRequestTooLong"));
|
||||
|
||||
assertMessageCounts(attributeQueueUrl, "2", "0");
|
||||
} finally {
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.DeleteQueue")
|
||||
.body("{\"QueueUrl\":\"" + attributeQueueUrl + "\"}")
|
||||
.when().post("/");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void redrivePolicyReadsBackAsCompactJson() {
|
||||
String dlqUrl = given()
|
||||
|
||||
@@ -1546,6 +1546,35 @@ class SqsServiceTest {
|
||||
() -> sqsService.sendMessage(queue.getQueueUrl(), body, 0, null, null, attrs, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void messageAttributeSize_countsUtf8BytesOfNameTypeAndStringValue() {
|
||||
assertEquals(6 + 6 + 6, SqsService.messageAttributeSize("na\u00efve", "String", "\u20acuro", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void messageAttributeSize_countsDecodedBinaryLength() {
|
||||
assertEquals(3 + 6 + 6, SqsService.messageAttributeSize("bin", "Binary", null, "AAECAwQF"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void messageAttributeSize_countsInvalidBase64AsItsUtf8Length() {
|
||||
assertEquals(3 + 6 + 11, SqsService.messageAttributeSize("bin", "Binary", null, "not base64!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void messageAttributeSize_countsAnAttributeWithNoDataType() {
|
||||
assertEquals(3 + 5, SqsService.messageAttributeSize("big", null, "value", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void computeMessageSize_countsEachAttributeLikeItsWireFields() {
|
||||
Map<String, MessageAttributeValue> attrs = Map.of(
|
||||
"na\u00efve", new MessageAttributeValue("\u20acuro", "String"),
|
||||
"bin", new MessageAttributeValue(new byte[] {0, 1, 2, 3, 4, 5}, "Binary"));
|
||||
|
||||
assertEquals(4 + 18 + 15, SqsService.computeMessageSize("body", attrs));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addPermission_appendsLabelledStatementToPolicy() {
|
||||
Queue queue = sqsService.createQueue("perm-queue", null, "us-east-1");
|
||||
|
||||
Reference in new issue
Block a user