mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(sns): enforce message size limits on Publish and PublishBatch (#783)
* fix(sns): enforce message size limits on Publish and PublishBatch Real AWS SNS rejects oversized publish payloads: - Publish: message + Subject + message attributes must not exceed 262144 bytes, otherwise InvalidParameterException with "Invalid parameter: Message too long". - PublishBatch: total bytes across all entries (message + Subject + attributes per entry) must not exceed 262144, otherwise BatchRequestTooLong. Floci was silently accepting both, which broke the .NET conformance tests asserting these limits. Size accounting matches AWS: UTF-8 bytes for message and subject, plus per-attribute (name + type + value) bytes, with the binary-value byte length used directly for Binary attributes. Refs #768. * fix(sns): address review — apply size limit to SMS, fix batch attr cast Move the Publish size check before the SMS short-circuit so oversized SMS payloads are also rejected. Normalize PublishBatch MessageAttributes in SnsJsonHandler to MessageAttributeValue objects so SnsService.publishBatch's cast no longer risks a ClassCastException.
This commit is contained in:
1 parent
02ab9dd1eb
commit
6f9782d093
3 files changed
+89
-4
No files matched your search
@@ -228,10 +228,12 @@ public class SnsJsonHandler {
|
||||
|
||||
JsonNode attrsNode = entryNode.path("MessageAttributes");
|
||||
if (attrsNode.isObject()) {
|
||||
Map<String, String> messageAttributes = new HashMap<>();
|
||||
attrsNode.fields().forEachRemaining(field ->
|
||||
messageAttributes.put(field.getKey(), field.getValue().path("StringValue").asText())
|
||||
);
|
||||
Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
|
||||
attrsNode.fields().forEachRemaining(field -> {
|
||||
String dataType = field.getValue().path("DataType").asText("String");
|
||||
String stringValue = field.getValue().path("StringValue").asText();
|
||||
messageAttributes.put(field.getKey(), new MessageAttributeValue(stringValue, dataType));
|
||||
});
|
||||
entry.put("MessageAttributes", messageAttributes);
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@ public class SnsService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(SnsService.class);
|
||||
private static final Duration FIFO_DEDUP_WINDOW = Duration.ofMinutes(5);
|
||||
private static final int MAX_PUBLISH_SIZE = 262_144;
|
||||
private static final List<String> PENDING_CONFIRMATION_PROTOCOLS =
|
||||
List.of("http", "https", "email", "email-json", "sms");
|
||||
|
||||
@@ -273,6 +274,12 @@ public class SnsService {
|
||||
public String publish(String topicArn, String targetArn, String phoneNumber, String message,
|
||||
String subject, Map<String, MessageAttributeValue> messageAttributes,
|
||||
String messageGroupId, String messageDeduplicationId, String region) {
|
||||
int payloadSize = computePublishSize(message, subject, messageAttributes);
|
||||
if (payloadSize > MAX_PUBLISH_SIZE) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"Invalid parameter: Message too long", 400);
|
||||
}
|
||||
|
||||
// Send SMS
|
||||
if (phoneNumber != null) {
|
||||
return UUID.randomUUID().toString();
|
||||
@@ -355,6 +362,21 @@ public class SnsService {
|
||||
String topicStoreKey = topicKey(region, topicArn);
|
||||
Topic topic = topicStore.get(topicStoreKey)
|
||||
.orElseThrow(() -> new AwsException("NotFound", "Topic does not exist.", 404));
|
||||
|
||||
int batchSize = 0;
|
||||
for (Map<String, Object> entry : entries) {
|
||||
String message = (String) entry.get("Message");
|
||||
String subject = (String) entry.get("Subject");
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, MessageAttributeValue> attrs =
|
||||
(Map<String, MessageAttributeValue>) entry.get("MessageAttributes");
|
||||
batchSize += computePublishSize(message, subject, attrs);
|
||||
}
|
||||
if (batchSize > MAX_PUBLISH_SIZE) {
|
||||
throw new AwsException("BatchRequestTooLong",
|
||||
"Batch requests cannot be longer than " + MAX_PUBLISH_SIZE + " bytes.", 400);
|
||||
}
|
||||
|
||||
boolean isFifo = "true".equals(topic.getAttributes().get("FifoTopic"));
|
||||
List<String[]> successful = new ArrayList<>();
|
||||
List<String[]> failed = new ArrayList<>();
|
||||
@@ -768,6 +790,32 @@ public class SnsService {
|
||||
return AwsArnUtils.arnToQueueUrl(arn, baseUrl);
|
||||
}
|
||||
|
||||
private static int computePublishSize(String message, String subject,
|
||||
Map<String, MessageAttributeValue> attributes) {
|
||||
int total = message == null ? 0 : message.getBytes(StandardCharsets.UTF_8).length;
|
||||
if (subject != null) {
|
||||
total += subject.getBytes(StandardCharsets.UTF_8).length;
|
||||
}
|
||||
if (attributes != null) {
|
||||
for (Map.Entry<String, MessageAttributeValue> entry : attributes.entrySet()) {
|
||||
total += entry.getKey().getBytes(StandardCharsets.UTF_8).length;
|
||||
MessageAttributeValue value = entry.getValue();
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
if (value.getDataType() != null) {
|
||||
total += value.getDataType().getBytes(StandardCharsets.UTF_8).length;
|
||||
}
|
||||
if (value.getBinaryValue() != null) {
|
||||
total += value.getBinaryValue().length;
|
||||
} else if (value.getStringValue() != null) {
|
||||
total += value.getStringValue().getBytes(StandardCharsets.UTF_8).length;
|
||||
}
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
private static String sha256(String message) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
|
||||
Reference in new issue
Block a user