fix(iam): use SecureRandom for secret credentials and tokens (#3960)

This commit is contained in:
danielvanza authored and GitHub committed 2026-09-20 16:17:50 -04:00
1 parent a03b00ce89
commit 94976360ac
7 files changed
+191 -7

No files matched your search

@@ -34,6 +34,7 @@ import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.time.Instant;
import java.util.Optional;
import java.util.Set;
@@ -90,6 +91,9 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
/** Guards the read-modify-write in the OIDC provider mutators. */
private final Object oidcProviderLock = new Object();
/** CSPRNG for long-term secret access keys; ordinary resource IDs keep using {@link ThreadLocalRandom}. */
private final SecureRandom secureRandom = new SecureRandom();
private static final String SERVICE_LINKED_ROLE_PATH = "/aws-service-role/";
private static final String SERVICE_LINKED_ROLE_NAME_PREFIX = "AWSServiceRoleFor";
private static final Map<String, String> SERVICE_LINKED_ROLE_NAMES = Map.of(
@@ -2590,11 +2594,11 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
return sb.toString();
}
private static String randomSecret(int length) {
private String randomSecret(int length) {
String secretChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
sb.append(secretChars.charAt(ThreadLocalRandom.current().nextInt(secretChars.length())));
sb.append(secretChars.charAt(secureRandom.nextInt(secretChars.length())));
}
return sb.toString();
}
@@ -21,6 +21,7 @@ import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
import java.security.SecureRandom;
import java.security.interfaces.RSAPublicKey;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
@@ -52,6 +53,9 @@ public class StsQueryHandler {
private final SAMLProviderService samlProviderService;
private final SAMLTrustPolicyEvaluator samlTrustEvaluator;
/** CSPRNG for session secret keys and session tokens; ordinary IDs keep using {@link ThreadLocalRandom}. */
private final SecureRandom secureRandom = new SecureRandom();
@Context
HttpHeaders headers;
@@ -488,10 +492,10 @@ public class StsQueryHandler {
return sb.toString();
}
private static String randomSecret(int length) {
private String randomSecret(int length) {
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
sb.append(CHARS.charAt(ThreadLocalRandom.current().nextInt(CHARS.length())));
sb.append(CHARS.charAt(secureRandom.nextInt(CHARS.length())));
}
return sb.toString();
}
@@ -31,7 +31,6 @@ import java.security.*;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Pattern;
import static io.github.hectorvent.floci.services.kms.model.KmsMessageType.RAW;
@@ -404,7 +403,7 @@ public class KmsService implements ResourceProvider {
requireNotPendingDeletion(key);
String grantId = UUID.randomUUID().toString();
byte[] tokenBytes = new byte[32];
ThreadLocalRandom.current().nextBytes(tokenBytes);
secureRandom.nextBytes(tokenBytes);
KmsGrant grant = new KmsGrant();
grant.setGrantId(grantId);
@@ -1762,7 +1761,7 @@ public class KmsService implements ResourceProvider {
int len = keySpec == null ? numberOfBytes : "AES_128".equals(keySpec) ? 16 : 32;
byte[] plaintext = new byte[len];
ThreadLocalRandom.current().nextBytes(plaintext);
secureRandom.nextBytes(plaintext);
EncryptResult encrypted = encrypt(keyId, plaintext, encryptionContext, null, region, operation);