mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eventbridge): enforce the PutEvents request size limit (#9428)
PutEvents counts each entry's size as the EventBridge User Guide gives it: 14 bytes for an explicit Time, plus the UTF-8 bytes of Source, DetailType, Detail and every Resources entry. A request whose entries total more than 1 MiB is refused with ValidationException "Total size of the entries in the request is over the limit." before any entry is published.
This commit is contained in:
2 files changed
+68
No files matched your search
@@ -52,6 +52,7 @@ import org.jboss.logging.Logger;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
@@ -1124,6 +1125,29 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
* such an entry: the guide's sentence is about the action, and no document says how AWS answers them.
|
||||
*/
|
||||
public PutEventsResult putEventsFromRequest(List<Map<String, Object>> entries, String region) {
|
||||
long size = 0;
|
||||
for (Map<String, Object> entry : entries) {
|
||||
if (entry.get("Time") != null) {
|
||||
size += 14;
|
||||
}
|
||||
for (String field : List.of("Source", "DetailType", "Detail")) {
|
||||
String value = (String) entry.get(field);
|
||||
if (value != null) {
|
||||
size += value.getBytes(StandardCharsets.UTF_8).length;
|
||||
}
|
||||
}
|
||||
if (entry.get("Resources") instanceof ArrayNode resources) {
|
||||
for (JsonNode resource : resources) {
|
||||
if (!resource.isNull()) {
|
||||
size += resource.asText().getBytes(StandardCharsets.UTF_8).length;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (size > 1_048_576) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Total size of the entries in the request is over the limit.", 400);
|
||||
}
|
||||
}
|
||||
return putEvents(entries, region, null, true);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@@ -2346,6 +2347,49 @@ class EventBridgeServiceTest {
|
||||
assertNotNull(entry.get("ErrorMessage"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putEventsAcceptsAnEntryAboveTheOld256KiBLimit() {
|
||||
Map<String, Object> entry = Map.of("Source", "s", "DetailType", "d",
|
||||
"Detail", "{\"value\":\"" + "x".repeat(300 * 1024) + "\"}");
|
||||
assertEquals(0, service.putEventsFromRequest(List.of(entry), REGION).failedCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putEventsRejectsAnOversizedEntryBeforePublishing() {
|
||||
Map<String, Object> entry = Map.of("Source", "s", "DetailType", "d",
|
||||
"Detail", "\"" + "x".repeat(1100 * 1024) + "\"");
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.putEventsFromRequest(List.of(entry), REGION));
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals("Total size of the entries in the request is over the limit.", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putEventsAppliesTheSizeLimitToTheWholeBatch() {
|
||||
Map<String, Object> entry = Map.of("Source", "s", "DetailType", "d",
|
||||
"Detail", "\"" + "x".repeat(600 * 1024) + "\"");
|
||||
assertThrows(AwsException.class, () -> service.putEventsFromRequest(List.of(entry, entry), REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putEventsCountsUtf8BytesInsteadOfCharacters() {
|
||||
Map<String, Object> entry = Map.of("Source", "s", "DetailType", "d",
|
||||
"Detail", "\"" + "界".repeat(400_000) + "\"");
|
||||
assertThrows(AwsException.class, () -> service.putEventsFromRequest(List.of(entry), REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putEventsCountsResourcesAndExplicitTimeAtTheBoundary() {
|
||||
Map<String, Object> entry = new HashMap<>(Map.of("Source", "s", "DetailType", "d",
|
||||
"Detail", "{\"x\":\"" + "x".repeat(1_048_549) + "\"}",
|
||||
"Time", Instant.EPOCH,
|
||||
"Resources", OBJECT_MAPPER.createArrayNode().add("界").addNull()));
|
||||
assertEquals(0, service.putEventsFromRequest(List.of(entry), REGION).failedCount());
|
||||
entry.put("Resources", OBJECT_MAPPER.createArrayNode().add("界x"));
|
||||
assertThrows(AwsException.class, () -> service.putEventsFromRequest(List.of(entry), REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putEventsMalformedDetailDoesNotBlockValidEntriesInSameBatch() {
|
||||
List<Map<String, Object>> entries = List.of(
|
||||
|
||||
Reference in new issue
Block a user