Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(resourceexplorer2): reject invalid pagination tokens (#3444)

Franklin G. Mendozaf482a2d15e42
diff --git a/src/main/java/io/github/hectorvent/floci/services/resourceexplorer2/ResourceExplorer2Service.java b/src/main/java/io/github/hectorvent/floci/services/resourceexplorer2/ResourceExplorer2Service.java
index 992e342f81..6f3c62e0fe 100644
--- a/src/main/java/io/github/hectorvent/floci/services/resourceexplorer2/ResourceExplorer2Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/resourceexplorer2/ResourceExplorer2Service.java
@@ -999,10 +999,14 @@ public class ResourceExplorer2Service {
return 0;
}
try {
- return Integer.parseInt(new String(Base64.getDecoder().decode(token)));
+ int offset = Integer.parseInt(new String(Base64.getDecoder().decode(token)));
+ if (offset < 0) {
+ throw new IllegalArgumentException("negative offset");
+ }
+ return offset;
} catch (Exception e) {
- LOG.debugf("Invalid NextToken '%s', restarting from offset 0: %s", token, e.getMessage());
- return 0;
+ throw new AwsException("ValidationException",
+ "NextToken is invalid or has expired.", 400);
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/resourceexplorer2/ResourceExplorer2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/resourceexplorer2/ResourceExplorer2IntegrationTest.java
index a9fdfc5352..c52388a193 100644
--- a/src/test/java/io/github/hectorvent/floci/services/resourceexplorer2/ResourceExplorer2IntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/resourceexplorer2/ResourceExplorer2IntegrationTest.java
@@ -426,6 +426,19 @@ class ResourceExplorer2IntegrationTest {
.body("Resources.findAll { it.Service == 's3' }.size()", equalTo(0));
}
+ @Test
+ void listResourcesInvalidNextTokenReturnsValidationError() {
+ given()
+ .header("Authorization", AUTH)
+ .contentType("application/json")
+ .body("{\"NextToken\":\"!!!not-base64!!!\"}")
+ .when()
+ .post("/ListResources")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"));
+ }
+
@Test
void listResourcesInvalidFilterReturns400() {
given()
diff --git a/src/test/java/io/github/hectorvent/floci/services/resourceexplorer2/ResourceExplorer2PaginationTest.java b/src/test/java/io/github/hectorvent/floci/services/resourceexplorer2/ResourceExplorer2PaginationTest.java
index d7f93ed9a2..8e01d2c844 100644
--- a/src/test/java/io/github/hectorvent/floci/services/resourceexplorer2/ResourceExplorer2PaginationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/resourceexplorer2/ResourceExplorer2PaginationTest.java
@@ -7,6 +7,7 @@ import java.util.Base64;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Unit tests for the pure pagination math behind ListResources/Search:
@@ -63,9 +64,15 @@ class ResourceExplorer2PaginationTest {
}
@Test
- void garbageTokenDecodesToZeroOffset() {
- var b = ResourceExplorer2Service.pageBounds(50, 10, "!!!not-base64!!!", CAP);
- assertEquals(0, b.start());
+ void garbageTokenIsRejected() {
+ assertThrows(io.github.hectorvent.floci.core.common.AwsException.class,
+ () -> ResourceExplorer2Service.pageBounds(50, 10, "!!!not-base64!!!", CAP));
+ }
+
+ @Test
+ void negativeTokenOffsetIsRejected() {
+ assertThrows(io.github.hectorvent.floci.core.common.AwsException.class,
+ () -> ResourceExplorer2Service.pageBounds(50, 10, token(-1), CAP));
}
@Test