refactor(core): decode base64url text through Base64Url (#6457)

Base64Url had encode but no decode, so 21 places in src/main decoded
base64url text by hand with new String(Base64.getUrlDecoder().decode(...),
UTF_8). Base64Url.decode now does that, and those callers use it. It is the
same JDK decoder, so padded and unpadded input still decode and invalid
input still throws IllegalArgumentException. The byte[] decodes of JWT
segments and keys are left as they are.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-10-24 20:04:57 -04:00
1 parent a813720674
commit eb99ea6655
21 files changed
+49 -46

No files matched your search

@@ -17,4 +17,13 @@ public final class Base64Url {
public static String encode(String text) {
return encode(text.getBytes(StandardCharsets.UTF_8));
}
/**
* Decodes {@code text}, padded or not, as UTF-8.
*
* @throws IllegalArgumentException if {@code text} is not base64url
*/
public static String decode(String text) {
return new String(Base64.getUrlDecoder().decode(text), StandardCharsets.UTF_8);
}
}
@@ -1,7 +1,5 @@
package io.github.hectorvent.floci.core.common;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
@@ -142,7 +140,7 @@ public final class Pagination {
return null;
}
try {
return new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);
return Base64Url.decode(token);
} catch (IllegalArgumentException e) {
return null;
}
@@ -6,7 +6,6 @@ import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Base64;
/**
* Pagination markers that name a cursor, as AWS's opaque tokens do. Each marker is signed with a
@@ -27,7 +26,7 @@ public final class SignedMarkers {
public String cursorOf(String marker) {
String decoded;
try {
decoded = new String(Base64.getUrlDecoder().decode(marker), StandardCharsets.UTF_8);
decoded = Base64Url.decode(marker);
} catch (IllegalArgumentException notIssued) {
// Not base64url, so not a marker this instance issued.
return null;
@@ -31,10 +31,8 @@ import jakarta.enterprise.event.Event;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
@@ -618,7 +616,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
int maxResults, String region) {
String[] parts;
try {
String raw = new String(Base64.getUrlDecoder().decode(nextToken), StandardCharsets.UTF_8);
String raw = Base64Url.decode(nextToken);
parts = raw.split(":", 3);
} catch (IllegalArgumentException e) {
throw invalidNextToken();
@@ -5608,8 +5608,7 @@ public class CognitoService implements ResourceProvider {
System.arraycopy(parts, usernameEnd, fields, 2, 3);
fields[5] = hasAuthTime ? parts[end].substring(REFRESH_TOKEN_AUTH_TIME_FIELD.length()) : "";
if (hasScopes) {
fields[6] = new String(Base64.getUrlDecoder().decode(
parts[parts.length - 1].substring(REFRESH_TOKEN_SCOPE_FIELD.length())), StandardCharsets.UTF_8);
fields[6] = Base64Url.decode(parts[parts.length - 1].substring(REFRESH_TOKEN_SCOPE_FIELD.length()));
}
return fields;
} catch (Exception ignored) { }
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.Base64Url;
import io.github.hectorvent.floci.core.common.Digests;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.Resettable;
@@ -624,7 +625,7 @@ public class DsqlService implements Resettable {
private static String[] tokenPosition(String nextToken) {
try {
String[] position = new String(Base64.getUrlDecoder().decode(nextToken), StandardCharsets.UTF_8)
String[] position = Base64Url.decode(nextToken)
.split(" ", 2);
if (position.length == 2) {
Instant.parse(position[0]);
@@ -86,7 +86,6 @@ import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
@@ -2488,8 +2487,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
return 0;
}
try {
return Integer.parseInt(new String(Base64.getUrlDecoder().decode(nextToken),
StandardCharsets.UTF_8));
return Integer.parseInt(Base64Url.decode(nextToken));
} catch (IllegalArgumentException e) {
throw new AwsException("InvalidParameterException", "The nextToken is not valid.", 400);
}
@@ -17,9 +17,7 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Base64;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@@ -165,7 +163,7 @@ public class EksAccessEntryService {
String after = "";
if (nextToken != null) {
try {
String decoded = new String(Base64.getUrlDecoder().decode(nextToken), StandardCharsets.UTF_8);
String decoded = Base64Url.decode(nextToken);
if (!decoded.startsWith(prefix + "\n")) {
throw invalid("nextToken does not belong to this cluster");
}
@@ -18,10 +18,8 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.time.Instant;
import java.util.Base64;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@@ -246,7 +244,7 @@ public class EksPodIdentityAssociationService {
String after = "";
if (nextToken != null && !nextToken.isBlank()) {
try {
String decoded = new String(Base64.getUrlDecoder().decode(nextToken), StandardCharsets.UTF_8);
String decoded = Base64Url.decode(nextToken);
if (!decoded.startsWith(prefix + "\n")) {
throw invalid("nextToken does not belong to this cluster");
}
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.eks;
import io.github.hectorvent.floci.core.common.Base64Url;
import io.github.hectorvent.floci.core.common.Digests;
import io.github.hectorvent.floci.services.iam.IamService;
import jakarta.enterprise.context.ApplicationScoped;
@@ -17,7 +18,6 @@ import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Locale;
@@ -118,7 +118,7 @@ class EksTokenValidator {
if (encodedRequest.isBlank()) {
return null;
}
String decodedRequest = new String(Base64.getUrlDecoder().decode(encodedRequest), StandardCharsets.UTF_8);
String decodedRequest = Base64Url.decode(encodedRequest);
URI request = URI.create(decodedRequest);
if (!"http".equals(request.getScheme()) && !"https".equals(request.getScheme())) {
return null;
@@ -22,11 +22,9 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
@@ -1312,7 +1310,7 @@ public class FisService implements TagHandler {
throw validation("Invalid nextToken.");
}
try {
String raw = new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);
String raw = Base64Url.decode(token);
int separator = raw.indexOf('\n');
if (separator < 0 || !expectedScope.equals(raw.substring(0, separator))
|| separator == raw.length() - 1) {
@@ -618,7 +618,7 @@ public class GuardDutyService {
return null;
}
try {
String decoded = new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);
String decoded = Base64Url.decode(token);
if (decoded.startsWith(PLAN_TOKEN_PREFIX) && decoded.length() > PLAN_TOKEN_PREFIX.length()) {
return decoded.substring(PLAN_TOKEN_PREFIX.length());
}
@@ -1074,7 +1074,7 @@ public class GuardDutyService {
return 0;
}
try {
String decoded = new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);
String decoded = Base64Url.decode(token);
if (!decoded.startsWith(TOKEN_PREFIX)) {
throw badRequest("nextToken is invalid.");
}
@@ -32,7 +32,6 @@ import jakarta.ws.rs.core.UriInfo;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -1375,7 +1374,7 @@ public class ScimController {
throw validation("cursor is invalid.");
}
try {
String decoded = new String(Base64.getUrlDecoder().decode(cursor), StandardCharsets.UTF_8);
String decoded = Base64Url.decode(cursor);
int separator = decoded.indexOf('\n');
if (separator <= 0) {
throw validation("cursor is invalid.");
@@ -23,7 +23,6 @@ import jakarta.inject.Inject;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@@ -267,7 +266,7 @@ public class IotFleetIndexingService {
}
Matcher token;
try {
token = PAGE_TOKEN.matcher(new String(Base64.getUrlDecoder().decode(nextToken), StandardCharsets.UTF_8));
token = PAGE_TOKEN.matcher(Base64Url.decode(nextToken));
} catch (IllegalArgumentException e) {
throw invalid("Invalid nextToken");
}
@@ -273,7 +273,7 @@ public class KinesisJsonHandler {
throw invalidNextToken();
}
try {
String cursor = new String(Base64.getUrlDecoder().decode(token.cursor()), StandardCharsets.UTF_8);
String cursor = Base64Url.decode(token.cursor());
if (!Base64Url.encode(cursor).equals(token.cursor())) {
throw invalidNextToken();
}
@@ -824,7 +824,7 @@ public class KinesisJsonHandler {
}
String decoded;
try {
decoded = new String(Base64.getUrlDecoder().decode(nextToken), StandardCharsets.UTF_8);
decoded = Base64Url.decode(nextToken);
} catch (IllegalArgumentException e) {
throw invalidNextToken();
}
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.Base64Url;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -30,11 +31,9 @@ import org.jboss.logging.Logger;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
@@ -1601,7 +1600,7 @@ public class LambdaWebService implements Resettable {
private static AwsException tokenError(String token) {
String decoded;
try {
decoded = new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);
decoded = Base64Url.decode(token);
} catch (IllegalArgumentException expected) {
// Not base64, so not a token Floci issued.
decoded = "";
@@ -12,10 +12,8 @@ import io.github.hectorvent.floci.services.macie2.model.MacieState;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@@ -196,7 +194,7 @@ public class MacieService implements Resettable {
return 0;
}
try {
int offset = Integer.parseInt(new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8));
int offset = Integer.parseInt(Base64Url.decode(token));
if (offset < 0 || offset > size) {
throw validation("nextToken is invalid.");
}
@@ -15,9 +15,7 @@ import io.github.hectorvent.floci.services.oam.model.OamSink;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
@@ -381,7 +379,7 @@ public class OamService {
public static int decodeToken(String token) {
if (token == null || token.isBlank()) return 0;
try {
int offset = Integer.parseInt(new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8));
int offset = Integer.parseInt(Base64Url.decode(token));
if (offset < 0) throw new NumberFormatException();
return offset;
} catch (Exception e) {
@@ -17,13 +17,11 @@ import io.github.hectorvent.floci.services.rum.model.AppMonitor;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
@@ -487,7 +485,7 @@ public class RumService implements ResourceProvider {
return 0;
}
try {
String decoded = new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);
String decoded = Base64Url.decode(token);
if (!decoded.startsWith(TOKEN_PREFIX)) {
throw validation("nextToken is invalid.");
}
@@ -3662,7 +3662,7 @@ public class S3Service implements Resettable, ResourceProvider {
return null;
}
try {
return new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);
return Base64Url.decode(token);
} catch (IllegalArgumentException e) {
throw new AwsException("InvalidArgument", "The continuation token you provided is invalid.", 400);
}