fix(sns): preserve binary message attributes through publish and fan-out (#989)

* fix(sns): preserve binary message attributes through publish and fan-out

Parse BinaryValue on Publish/PublishBatch in both JSON and Query handlers,
and base64-encode binary attributes in the SNS-to-SQS and Lambda JSON
envelopes so binary attributes survive fan-out instead of being dropped.

Closes #988

* fix(sns): return AWS-style 400 for invalid base64 and align DataType default

Address Copilot PR feedback:

- Catch IllegalArgumentException from Base64 decoding in both Publish and
  PublishBatch attribute parsing (JSON + Query handlers) and surface an
  InvalidParameterValue / HTTP 400 error consistent with other SNS errors.
- Default DataType to "Binary" in SnsJsonHandler when only BinaryValue is
  provided, matching SnsQueryHandler behavior.
- Extract a shared parseMessageAttributes helper in each handler to remove
  duplication between Publish and PublishBatch.

* fix(sns): give new tests distinct @Order values and drop Base64 FQN

Address maintainer review feedback:

- Move new binary-attribute tests off @Order(9) (already used by
  publish_fanOutToSqsSubscriber) to @Order(18)/@Order(19) so their
  drainQueue calls don't race with other order-9 tests.
- Import java.util.Base64 in SnsQueryHandler, SnsJsonHandler, and
  SnsService and drop the fully-qualified references.
This commit is contained in:
Stuart Lang authored and GitHub committed 2026-05-22 18:03:32 -05:00
1 parent ba04ec4eee
commit 27d72f119c
4 files changed
+143 -34

No files matched your search

@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.sns;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.sns.model.Subscription;
import io.github.hectorvent.floci.services.sns.model.Topic;
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
@@ -13,6 +14,7 @@ import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -156,15 +158,7 @@ public class SnsJsonHandler {
String message = request.path("Message").asText(null);
String subject = request.path("Subject").asText(null);
Map<String, MessageAttributeValue> attributes = new HashMap<>();
JsonNode attrsNode = request.path("MessageAttributes");
if (attrsNode.isObject()) {
attrsNode.fields().forEachRemaining(entry -> {
String dataType = entry.getValue().path("DataType").asText("String");
String stringValue = entry.getValue().path("StringValue").asText();
attributes.put(entry.getKey(), new MessageAttributeValue(stringValue, dataType));
});
}
Map<String, MessageAttributeValue> attributes = parseMessageAttributes(request.path("MessageAttributes"));
String messageId = snsService.publish(topicArn, targetArn, phoneNumber, message, subject, attributes, region);
ObjectNode response = objectMapper.createObjectNode();
@@ -228,13 +222,7 @@ public class SnsJsonHandler {
JsonNode attrsNode = entryNode.path("MessageAttributes");
if (attrsNode.isObject()) {
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);
entry.put("MessageAttributes", parseMessageAttributes(attrsNode));
}
entries.add(entry);
@@ -302,6 +290,33 @@ public class SnsJsonHandler {
return node;
}
private Map<String, MessageAttributeValue> parseMessageAttributes(JsonNode attrsNode) {
Map<String, MessageAttributeValue> attributes = new HashMap<>();
if (!attrsNode.isObject()) {
return attributes;
}
attrsNode.fields().forEachRemaining(entry -> {
JsonNode valueNode = entry.getValue();
String binaryValueBase64 = valueNode.path("BinaryValue").asText(null);
String defaultDataType = binaryValueBase64 != null ? "Binary" : "String";
String dataType = valueNode.path("DataType").asText(defaultDataType);
if (binaryValueBase64 != null) {
byte[] binaryValue;
try {
binaryValue = Base64.getDecoder().decode(binaryValueBase64);
} catch (IllegalArgumentException e) {
throw new AwsException("InvalidParameterValue",
"Invalid binary value for message attribute '" + entry.getKey() + "': not valid base64.", 400);
}
attributes.put(entry.getKey(), new MessageAttributeValue(binaryValue, dataType));
} else {
String stringValue = valueNode.path("StringValue").asText();
attributes.put(entry.getKey(), new MessageAttributeValue(stringValue, dataType));
}
});
return attributes;
}
private Map<String, String> jsonNodeToMap(JsonNode node) {
Map<String, String> map = new HashMap<>();
if (node != null && node.isObject()) {
@@ -11,6 +11,7 @@ import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -183,13 +184,11 @@ public class SnsQueryHandler {
String messageGroupId = getParam(params, "MessageGroupId");
String messageDeduplicationId = getParam(params, "MessageDeduplicationId");
Map<String, MessageAttributeValue> attributes = new HashMap<>();
for (int i = 1; ; i++) {
String name = params.getFirst("MessageAttributes.entry." + i + ".Name");
if (name == null) break;
String value = params.getFirst("MessageAttributes.entry." + i + ".Value.StringValue");
String dataType = params.getFirst("MessageAttributes.entry." + i + ".Value.DataType");
if (value != null) attributes.put(name, new MessageAttributeValue(value, dataType != null ? dataType : "String"));
Map<String, MessageAttributeValue> attributes;
try {
attributes = parseMessageAttributes(params, "MessageAttributes.entry.");
} catch (AwsException e) {
return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
}
try {
@@ -217,14 +216,11 @@ public class SnsQueryHandler {
entry.put("MessageGroupId", getParam(params, entryPrefix + ".MessageGroupId"));
entry.put("MessageDeduplicationId", getParam(params, entryPrefix + ".MessageDeduplicationId"));
Map<String, MessageAttributeValue> attributes = new HashMap<>();
for (int j = 1; ; j++) {
String attrPrefix = entryPrefix + ".MessageAttributes.entry." + j;
String name = params.getFirst(attrPrefix + ".Name");
if (name == null) break;
String value = params.getFirst(attrPrefix + ".Value.StringValue");
String dataType = params.getFirst(attrPrefix + ".Value.DataType");
if (value != null) attributes.put(name, new MessageAttributeValue(value, dataType != null ? dataType : "String"));
Map<String, MessageAttributeValue> attributes;
try {
attributes = parseMessageAttributes(params, entryPrefix + ".MessageAttributes.entry.");
} catch (AwsException e) {
return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
}
if (!attributes.isEmpty()) entry.put("MessageAttributes", attributes);
entries.add(entry);
@@ -361,6 +357,31 @@ public class SnsQueryHandler {
return params.getFirst(name);
}
private Map<String, MessageAttributeValue> parseMessageAttributes(
MultivaluedMap<String, String> params, String prefix) {
Map<String, MessageAttributeValue> attributes = new HashMap<>();
for (int i = 1; ; i++) {
String name = params.getFirst(prefix + i + ".Name");
if (name == null) break;
String value = params.getFirst(prefix + i + ".Value.StringValue");
String binaryValueBase64 = params.getFirst(prefix + i + ".Value.BinaryValue");
String dataType = params.getFirst(prefix + i + ".Value.DataType");
if (binaryValueBase64 != null) {
byte[] binaryValue;
try {
binaryValue = Base64.getDecoder().decode(binaryValueBase64);
} catch (IllegalArgumentException e) {
throw new AwsException("InvalidParameterValue",
"Invalid binary value for message attribute '" + name + "': not valid base64.", 400);
}
attributes.put(name, new MessageAttributeValue(binaryValue, dataType != null ? dataType : "Binary"));
} else if (value != null) {
attributes.put(name, new MessageAttributeValue(value, dataType != null ? dataType : "String"));
}
}
return attributes;
}
Response xmlErrorResponse(String code, String message, int status) {
return AwsQueryResponse.error(code, message, AwsNamespaces.SNS, status);
}
@@ -32,6 +32,7 @@ import java.time.Duration;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.HexFormat;
import java.util.List;
@@ -955,7 +956,12 @@ public class SnsService {
for (var entry : messageAttributes.entrySet()) {
ObjectNode attr = attrs.putObject(entry.getKey());
attr.put("Type", entry.getValue().getDataType());
attr.put("Value", entry.getValue().getStringValue());
if (entry.getValue().getBinaryValue() != null) {
attr.put("Value", Base64.getEncoder()
.encodeToString(entry.getValue().getBinaryValue()));
} else {
attr.put("Value", entry.getValue().getStringValue());
}
}
}
ObjectNode record = objectMapper.createObjectNode();
@@ -1011,7 +1017,12 @@ public class SnsService {
for (var entry : messageAttributes.entrySet()) {
ObjectNode attr = attrs.putObject(entry.getKey());
attr.put("Type", entry.getValue().getDataType());
attr.put("Value", entry.getValue().getStringValue());
if (entry.getValue().getBinaryValue() != null) {
attr.put("Value", Base64.getEncoder()
.encodeToString(entry.getValue().getBinaryValue()));
} else {
attr.put("Value", entry.getValue().getStringValue());
}
}
}
return objectMapper.writeValueAsString(node);
@@ -1044,7 +1055,7 @@ public class SnsService {
ObjectNode attr = attrs.putObject(entry.getKey());
attr.put("Type", entry.getValue().getDataType());
if (entry.getValue().getBinaryValue() != null) {
attr.put("Value", java.util.Base64.getEncoder()
attr.put("Value", Base64.getEncoder()
.encodeToString(entry.getValue().getBinaryValue()));
} else {
attr.put("Value", entry.getValue().getStringValue());