Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Langning Zhang12cf6e2fd879
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java
index 0726e8c212..90a527a0c9 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java
@@ -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);
}
/**
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java
index 2be2421000..581401dee9 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java
@@ -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);