fix(cloudwatch): reject invalid log event tokens (#2249)

This commit is contained in:
Langning Zhang authored and GitHub committed 2026-08-14 10:26:32 -04:00
1 parent 2d003b52f2
commit 12cf6e2fd8
2 files changed
+71 -2

No files matched your search

@@ -387,6 +387,8 @@ public class CloudWatchLogsService {
int end = parseTokenIndex(nextToken, 2);
pageEnd = Math.min(end, total);
pageStart = Math.max(pageEnd - maxEvents, 0);
} else if (nextToken != null) {
throw invalidNextToken();
} else if (!startFromHead) {
pageEnd = total;
pageStart = Math.max(total - maxEvents, 0);
@@ -400,11 +402,20 @@ public class CloudWatchLogsService {
}
private int parseTokenIndex(String token, int prefixLen) {
int index;
try {
return Integer.parseInt(token.substring(prefixLen));
index = Integer.parseInt(token.substring(prefixLen));
} catch (NumberFormatException e) {
return 0;
throw invalidNextToken();
}
if (index < 0) {
throw invalidNextToken();
}
return index;
}
private AwsException invalidNextToken() {
return new AwsException("InvalidParameterException", "The specified nextToken is invalid.", 400);
}
/**
@@ -485,6 +485,64 @@ class CloudWatchLogsServiceTest {
assertEquals("f/3", atEnd.nextForwardToken(), "token must echo back to signal end of stream");
}
@Test
void getLogEventsRejectsMalformedNextToken() {
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "stream-1", REGION);
putEvents("/app/logs", "stream-1", System.currentTimeMillis(), 3);
AwsException exception = assertThrows(AwsException.class, () ->
service.getLogEvents("/app/logs", "stream-1", null, null, 10, true, "f/not-a-token", REGION));
assertEquals("InvalidParameterException", exception.getErrorCode());
assertEquals(400, exception.getHttpStatus());
assertEquals("The specified nextToken is invalid.", exception.getMessage());
}
@Test
void getLogEventsRejectsNegativeNextToken() {
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "stream-1", REGION);
putEvents("/app/logs", "stream-1", System.currentTimeMillis(), 3);
AwsException exception = assertThrows(AwsException.class, () ->
service.getLogEvents("/app/logs", "stream-1", null, null, 10, true, "b/-1", REGION));
assertEquals("InvalidParameterException", exception.getErrorCode());
assertEquals(400, exception.getHttpStatus());
assertEquals("The specified nextToken is invalid.", exception.getMessage());
}
@Test
void getLogEventsRejectsOverflowNextToken() {
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "stream-1", REGION);
putEvents("/app/logs", "stream-1", System.currentTimeMillis(), 3);
AwsException exception = assertThrows(AwsException.class, () ->
service.getLogEvents("/app/logs", "stream-1", null, null, 10, true, "f/2147483648", REGION));
assertEquals("InvalidParameterException", exception.getErrorCode());
assertEquals(400, exception.getHttpStatus());
assertEquals("The specified nextToken is invalid.", exception.getMessage());
}
@Test
void getLogEventsRejectsUnrecognizedNextTokens() {
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "stream-1", REGION);
putEvents("/app/logs", "stream-1", System.currentTimeMillis(), 3);
for (String token : List.of("", "x/1", "garbage")) {
AwsException exception = assertThrows(AwsException.class, () ->
service.getLogEvents("/app/logs", "stream-1", null, null, 10, true, token, REGION));
assertEquals("InvalidParameterException", exception.getErrorCode());
assertEquals(400, exception.getHttpStatus());
assertEquals("The specified nextToken is invalid.", exception.getMessage());
}
}
@Test
void getLogEventsStartFromTailWithNoToken() {
service.createLogGroup("/app/logs", null, null, REGION);