fix(sns): format message Timestamps with three-digit milliseconds (#7513)

AWS gives Timestamp in notifications and subscription confirmations as milliseconds with three digits, such as 2026-11-04T10:54:48.430Z.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-04 12:11:05 -04:00
1 parent 5080093a2a
commit 05eed7a0a9
2 files changed
+13 -3

No files matched your search

@@ -55,6 +55,7 @@ import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Base64;
@@ -81,6 +82,8 @@ public class SnsService implements Resettable, ResourceProvider {
private static final Logger LOG = Logger.getLogger(SnsService.class);
private static final Duration FIFO_DEDUP_WINDOW = Duration.ofMinutes(5);
private static final DateTimeFormatter MESSAGE_TIMESTAMP =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneOffset.UTC);
private static final Set<String> SMS_ATTRIBUTE_NAMES = Set.of("MonthlySpendLimit",
"DeliveryStatusIAMRole", "DeliveryStatusSuccessSamplingRate", "DefaultSenderID",
"DefaultSMSType", "UsageReportS3Bucket");
@@ -1127,7 +1130,7 @@ public class SnsService implements Resettable, ResourceProvider {
String messageId = UUID.randomUUID().toString();
Notification notification = new Notification(messageId,
DateTimeFormatter.ISO_INSTANT.format(Instant.now()), signatureVersion(topic), new HashMap<>());
MESSAGE_TIMESTAMP.format(Instant.now()), signatureVersion(topic), new HashMap<>());
JsonNode parsedBody = null;
boolean bodyParseAttempted = false;
for (Subscription sub : subscriptionsByTopic(effectiveArn, region)) {
@@ -1564,7 +1567,7 @@ public class SnsService implements Resettable, ResourceProvider {
boolean groupScopedDedup = isGroupScopedDeduplication(topic);
List<String[]> successful = new ArrayList<>();
List<String[]> failed = new ArrayList<>();
String timestamp = DateTimeFormatter.ISO_INSTANT.format(Instant.now());
String timestamp = MESSAGE_TIMESTAMP.format(Instant.now());
String version = signatureVersion(topic);
for (Map<String, Object> entry : entries) {
String id = (String) entry.get("Id");
@@ -2868,7 +2871,7 @@ public class SnsService implements Resettable, ResourceProvider {
try {
String token = subscription.getAttributes().get("ConfirmationToken");
String subscribeUrl = baseUrl + "/?Action=ConfirmSubscription&TopicArn=" + topicArn + "&Token=" + token;
String timestamp = DateTimeFormatter.ISO_INSTANT.format(Instant.now());
String timestamp = MESSAGE_TIMESTAMP.format(Instant.now());
ObjectNode node = objectMapper.createObjectNode();
node.put("Type", "SubscriptionConfirmation");
node.put("MessageId", UUID.randomUUID().toString());
@@ -26,7 +26,9 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.matchesPattern;
import static org.junit.jupiter.api.Assertions.*;
/**
@@ -43,6 +45,7 @@ class SnsMessageSignatureIntegrationTest {
List.of("Message", "MessageId", "Subject", "Timestamp", "TopicArn", "Type");
private static final List<String> CONFIRMATION_FIELDS =
List.of("Message", "MessageId", "SubscribeURL", "Timestamp", "Token", "TopicArn", "Type");
private static final String MILLIS_TIMESTAMP = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z";
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final List<String> received = new CopyOnWriteArrayList<>();
@@ -80,6 +83,7 @@ class SnsMessageSignatureIntegrationTest {
JsonNode confirmation = MAPPER.readTree(awaitMessage(0));
assertEquals("SubscriptionConfirmation", confirmation.get("Type").asText());
assertThat(confirmation.get("Timestamp").asText(), matchesPattern(MILLIS_TIMESTAMP));
assertEquals("1", confirmation.get("SignatureVersion").asText());
assertTrue(verify(confirmation, "SigningCertURL"));
@@ -97,6 +101,7 @@ class SnsMessageSignatureIntegrationTest {
JsonNode notification = MAPPER.readTree(awaitMessage(1));
assertEquals("Notification", notification.get("Type").asText());
assertEquals("Signed subject", notification.get("Subject").asText());
assertThat(notification.get("Timestamp").asText(), matchesPattern(MILLIS_TIMESTAMP));
assertEquals("1", notification.get("SignatureVersion").asText());
assertTrue(verify(notification, "SigningCertURL"));
assertEquals(confirmation.get("SigningCertURL").asText(), notification.get("SigningCertURL").asText());
@@ -113,6 +118,7 @@ class SnsMessageSignatureIntegrationTest {
assertEquals("Notification", envelope.get("Type").asText());
assertFalse(envelope.has("Subject"));
assertThat(envelope.get("Timestamp").asText(), matchesPattern(MILLIS_TIMESTAMP));
assertEquals("1", envelope.get("SignatureVersion").asText());
assertTrue(envelope.get("UnsubscribeURL").asText().contains("Action=Unsubscribe"));
assertTrue(verify(envelope, "SigningCertURL"));
@@ -188,6 +194,7 @@ class SnsMessageSignatureIntegrationTest {
JsonNode first = MAPPER.readTree(receiveBody(queueUrl));
JsonNode second = MAPPER.readTree(receiveBody(queueUrl));
assertThat(first.get("Timestamp").asText(), matchesPattern(MILLIS_TIMESTAMP));
assertEquals(first.get("Timestamp").asText(), second.get("Timestamp").asText());
assertNotEquals(first.get("MessageId").asText(), second.get("MessageId").asText());
assertTrue(verify(first, "SigningCertURL"));