From 8ef9e1ddbf286fe7c728e8c4d5d905bb210e2af0 Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Sun, 25 Oct 2026 02:36:01 +0900 Subject: [PATCH] refactor(core): share the token decode in Pagination (#6431) tokenStartsWith and decodeCursor each decoded the base64url token and caught the IllegalArgumentException of an unreadable one. Both now use a private decodeToken that returns the decoded cursor, or null for an absent or unreadable token. Behaviour is unchanged; PaginationTest now covers tokenStartsWith directly. --- .../floci/core/common/Pagination.java | 30 +++++++++---------- .../floci/core/common/PaginationTest.java | 21 +++++++++++++ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java b/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java index 564d8cae14..a9fea3d89c 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java @@ -117,14 +117,8 @@ public final class Pagination { * need to tell a token they issued under another scope from one that is not theirs at all. */ public static boolean tokenStartsWith(String token, String prefix) { - if (token == null || token.isEmpty()) { - return false; - } - try { - return new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8).startsWith(prefix); - } catch (IllegalArgumentException e) { - return false; - } + String decoded = decodeToken(token); + return decoded != null && decoded.startsWith(prefix); } private static String encodeToken(String cursor) { @@ -136,15 +130,21 @@ public final class Pagination { if (token == null || token.isEmpty()) { return null; } - String decoded; - try { - decoded = new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8); - } catch (IllegalArgumentException e) { - throw invalidToken.apply(token); - } - if (!decoded.startsWith(prefix)) { + String decoded = decodeToken(token); + if (decoded == null || !decoded.startsWith(prefix)) { throw invalidToken.apply(token); } return decoded.substring(prefix.length()); } + + private static String decodeToken(String token) { + if (token == null || token.isEmpty()) { + return null; + } + try { + return new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8); + } catch (IllegalArgumentException e) { + return null; + } + } } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java index 724d7d0e49..7611732f3f 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java @@ -11,9 +11,11 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.startsWith; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; class PaginationTest { @@ -93,6 +95,25 @@ class PaginationTest { assertEquals("Invalid Marker.", e.getMessage()); } + @Test + void tokenStartsWith_matchesTheDecodedCursorPrefix() { + String token = page(1, null, "ns").nextToken(); + + assertTrue(Pagination.tokenStartsWith(token, "ns:")); + assertFalse(Pagination.tokenStartsWith(token, "other:")); + } + + @Test + void tokenStartsWith_isFalseForATokenThatDoesNotDecode() { + assertFalse(Pagination.tokenStartsWith("!!!", "")); + } + + @Test + void tokenStartsWith_isFalseForAnAbsentToken() { + assertFalse(Pagination.tokenStartsWith(null, "")); + assertFalse(Pagination.tokenStartsWith("", "")); + } + private static PaginatedResult page(int limit, String token, String namespace) { return Pagination.paginate(ITEMS, Function.identity(), limit, token, namespace, INVALID_TOKEN); }