[java-oidc-common] 04/04: JCOMOIDC-201 - Facilitate URI validation and max response length in RemoteJwkSetCache
Codeberg
noreply at shibboleth.net
Fri Nov 27 12:20:50 UTC 2026
This is an automated email from the git hooks/post-receive script.
codeberg pushed a commit to branch maint-3.4
in repository java-oidc-common.
View the commit online:
https://codeberg.org/Shibboleth/java-oidc-common/commit/19a9a89e57c98be229360e67c34efbea505e9ae7
commit 19a9a89e57c98be229360e67c34efbea505e9ae7
Author: Henri Mikkonen <henri.mikkonen at iki.fi>
AuthorDate: Fri Nov 27 14:12:13 2026 +0200
JCOMOIDC-201 - Facilitate URI validation and max response length in RemoteJwkSetCache
https://shibboleth.atlassian.net/browse/JCOMOIDC-201
- optional uriValidationCondition and maxResponseContentLength (defaults to 1MB)
---
.../net/shibboleth/oidc/jwk/RemoteJwkSetCache.java | 40 +++++++++-
.../shibboleth/oidc/jwk/RemoteJwkSetCacheTest.java | 89 +++++++++++++++++++++-
2 files changed, 126 insertions(+), 3 deletions(-)
diff --git a/oidc-common-crypto-api/src/main/java/net/shibboleth/oidc/jwk/RemoteJwkSetCache.java b/oidc-common-crypto-api/src/main/java/net/shibboleth/oidc/jwk/RemoteJwkSetCache.java
index 72eba4a5..9ac33ef7 100644
--- a/oidc-common-crypto-api/src/main/java/net/shibboleth/oidc/jwk/RemoteJwkSetCache.java
+++ b/oidc-common-crypto-api/src/main/java/net/shibboleth/oidc/jwk/RemoteJwkSetCache.java
@@ -18,6 +18,7 @@ import java.io.IOException;
import java.net.URI;
import java.time.Duration;
import java.time.Instant;
+import java.util.function.Predicate;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -61,7 +62,13 @@ public class RemoteJwkSetCache extends AbstractIdentifiableInitializableComponen
/** HTTP client security parameters. */
@Nullable private HttpClientSecurityParameters httpClientSecurityParameters;
-
+
+ /** Condition to validate the URI before using it in the HTTP request. */
+ @Nullable private Predicate<URI> uriValidationCondition;
+
+ /** Maximum response content length. */
+ private int maxResponseContentLength;
+
/**
* The forced refresh cooldown period. If a JWKS has been forcibly refreshed within this period, another forced
* refresh will not be attempted. A duration of {@link Duration#ZERO} disables the cooldown period.
@@ -73,6 +80,7 @@ public class RemoteJwkSetCache extends AbstractIdentifiableInitializableComponen
public RemoteJwkSetCache(){
// Disable the refresh cooldown
forcedRefreshCooldown = Duration.ZERO;
+ maxResponseContentLength = RemoteJwkUtils.DEFAULT_MAX_RESPONSE_CONTENT_LENGTH;
}
/**
@@ -134,6 +142,30 @@ public class RemoteJwkSetCache extends AbstractIdentifiableInitializableComponen
forcedRefreshCooldown = cooldown;
}
+ /**
+ * Set the condition to validate the URI before using it in the HTTP request.
+ *
+ * @param condition the URI validation condition
+ * @since 3.4.0
+ */
+ public void setUriValidationCondition(@Nullable final Predicate<URI> condition) {
+ checkSetterPreconditions();
+ uriValidationCondition = condition;
+ }
+
+ /**
+ * Set the maximum response content length.
+ *
+ * @param maxLength maximum length
+ * @since 3.4.0
+ */
+ public void setMaxResponseContentLength(final int maxLength) {
+ checkSetterPreconditions();
+
+ Constraint.isGreaterThan(0, maxLength, "Maximum response content length must be greater than zero");
+ maxResponseContentLength = maxLength;
+ }
+
/** {@inheritDoc} */
@Override
public void doInitialize() throws ComponentInitializationException {
@@ -232,9 +264,13 @@ public class RemoteJwkSetCache extends AbstractIdentifiableInitializableComponen
*/
@Nullable private JWKSet fetchAndStore(@Nonnull @NotEmpty final String context, @Nonnull final String cacheKey,
@Nonnull final URI uri, @Nonnull final Instant expires, final boolean overwriteCache) {
+ if (uriValidationCondition != null && !uriValidationCondition.test(uri)) {
+ log.warn("URI validation failed for {}", uri.toString());
+ return null;
+ }
try {
final JWKSet remoteJwkSet = RemoteJwkUtils.fetchRemoteJwkSet("RemoteJwkSetCache", uri, httpClient,
- httpClientSecurityParameters);
+ httpClientSecurityParameters, maxResponseContentLength);
if (remoteJwkSet != null && remoteJwkSet.getKeys() != null && !remoteJwkSet.getKeys().isEmpty()) {
final String value = remoteJwkSet.toString();
assert value != null;
diff --git a/oidc-common-crypto-api/src/test/java/net/shibboleth/oidc/jwk/RemoteJwkSetCacheTest.java b/oidc-common-crypto-api/src/test/java/net/shibboleth/oidc/jwk/RemoteJwkSetCacheTest.java
index 33ec7768..875a0f66 100644
--- a/oidc-common-crypto-api/src/test/java/net/shibboleth/oidc/jwk/RemoteJwkSetCacheTest.java
+++ b/oidc-common-crypto-api/src/test/java/net/shibboleth/oidc/jwk/RemoteJwkSetCacheTest.java
@@ -38,6 +38,7 @@ import org.testng.annotations.Test;
import com.nimbusds.jose.jwk.JWKSet;
import net.shibboleth.shared.component.ComponentInitializationException;
+import net.shibboleth.shared.logic.PredicateSupport;
/**
* Unit tests for {@link RemoteJwkSetCache}.
@@ -91,7 +92,82 @@ public class RemoteJwkSetCacheTest {
Thread.sleep(5001);
Assert.assertNull(storageService.read(RemoteJwkSetCache.CONTEXT_NAME, uri));
}
-
+
+ @Test
+ public void testSuccessUriValidated() throws ClientProtocolException, IOException,
+ ComponentInitializationException, URISyntaxException, InterruptedException {
+ jwkSetCache.setStorage(storageService);
+ jwkSetCache.setHttpClient(createMockHttpClient(validJwkSet()));
+ jwkSetCache.setHttpClientSecurityParameters(null);
+ jwkSetCache.setUriValidationCondition(PredicateSupport.alwaysTrue());
+ jwkSetCache.initialize();
+ final String uri = "http://example.org";
+ JWKSet jwkSet = jwkSetCache.fetch(new URI(uri), Instant.now().plusSeconds(5));
+ Assert.assertNotNull(jwkSet);
+ }
+
+ @Test
+ public void testUriValidationFails() throws ClientProtocolException, IOException, ComponentInitializationException,
+ URISyntaxException, InterruptedException {
+ jwkSetCache.setStorage(storageService);
+ jwkSetCache.setHttpClient(createMockHttpClient(validJwkSet()));
+ jwkSetCache.setHttpClientSecurityParameters(null);
+ jwkSetCache.setUriValidationCondition(PredicateSupport.alwaysFalse());
+ jwkSetCache.initialize();
+ final String uri = "http://example.org";
+ JWKSet jwkSet = jwkSetCache.fetch(new URI(uri), Instant.now().plusSeconds(5));
+ Assert.assertNull(jwkSet);
+ }
+
+ @Test
+ public void testInvalidCode() throws ClientProtocolException, IOException, ComponentInitializationException,
+ URISyntaxException, InterruptedException {
+ jwkSetCache.setStorage(storageService);
+ jwkSetCache.setHttpClient(createMockHttpClient(validJwkSet(), 500));
+ jwkSetCache.setHttpClientSecurityParameters(null);
+ jwkSetCache.initialize();
+ final String uri = "http://example.org";
+ JWKSet jwkSet = jwkSetCache.fetch(new URI(uri), Instant.now().plusSeconds(5));
+ Assert.assertNull(jwkSet);
+ }
+
+ @Test
+ public void testNullEntity() throws ClientProtocolException, IOException, ComponentInitializationException,
+ URISyntaxException, InterruptedException {
+ final HttpClient httpClient = Mockito.mock(HttpClient.class);
+ final ClassicHttpResponse httpResponse = Mockito.mock(ClassicHttpResponse.class);
+ Mockito.when(httpResponse.getCode()).thenReturn(200);
+ Mockito.when(httpResponse.getEntity()).thenReturn(null);
+ Mockito.when(httpClient.executeOpen(Mockito.any(), (ClassicHttpRequest) Mockito.any(),
+ (HttpContext) Mockito.any())).thenReturn(httpResponse);
+ jwkSetCache.setStorage(storageService);
+ jwkSetCache.setHttpClient(httpClient);
+ jwkSetCache.setHttpClientSecurityParameters(null);
+ jwkSetCache.initialize();
+ final String uri = "http://example.org";
+ JWKSet jwkSet = jwkSetCache.fetch(new URI(uri), Instant.now().plusSeconds(5));
+ Assert.assertNull(jwkSet);
+ }
+
+ @Test
+ public void testTooLongEntity() throws ClientProtocolException, IOException, ComponentInitializationException,
+ URISyntaxException, InterruptedException {
+ final HttpClient httpClient = Mockito.mock(HttpClient.class);
+ final ClassicHttpResponse httpResponse = Mockito.mock(ClassicHttpResponse.class);
+ Mockito.when(httpResponse.getCode()).thenReturn(200);
+ Mockito.when(httpResponse.getEntity()).thenReturn(new StringEntity(validJwkSet()));
+ Mockito.when(httpClient.executeOpen(Mockito.any(), (ClassicHttpRequest) Mockito.any(),
+ (HttpContext) Mockito.any())).thenReturn(httpResponse);
+ jwkSetCache.setStorage(storageService);
+ jwkSetCache.setMaxResponseContentLength(10);
+ jwkSetCache.setHttpClient(httpClient);
+ jwkSetCache.setHttpClientSecurityParameters(null);
+ jwkSetCache.initialize();
+ final String uri = "http://example.org";
+ JWKSet jwkSet = jwkSetCache.fetch(new URI(uri), Instant.now().plusSeconds(5));
+ Assert.assertNull(jwkSet);
+ }
+
@Test
public void testKeyIdNotInCachedSet() throws Exception{
jwkSetCache.setStorage(storageService);
@@ -199,6 +275,7 @@ public class RemoteJwkSetCacheTest {
Mockito.when(httpResponse.getEntity()).thenReturn(new StringEntity(rotatedValidJwkSet()));
Mockito.when(client.executeOpen(Mockito.any(), (ClassicHttpRequest) Mockito.any(),
(HttpContext) Mockito.any())).thenReturn(httpResponse);
+ Mockito.when(httpResponse.getCode()).thenReturn(200);
// Now lookup key that is not in the cached set
final JWKSet jwkSet = jwkSetCache.fetch(new URI(uri), "key-in-rotated-keyset", Instant.now().plusSeconds(500));
@@ -217,8 +294,14 @@ public class RemoteJwkSetCacheTest {
}
protected HttpClient createMockHttpClient(final String output) throws ClientProtocolException, IOException {
+ return createMockHttpClient(output, 200);
+ }
+
+ protected HttpClient createMockHttpClient(final String output, final int code) throws ClientProtocolException,
+ IOException {
final HttpClient httpClient = Mockito.mock(HttpClient.class);
final ClassicHttpResponse httpResponse = Mockito.mock(ClassicHttpResponse.class);
+ Mockito.when(httpResponse.getCode()).thenReturn(code);
Mockito.when(httpResponse.getEntity()).thenReturn(new StringEntity(output));
Mockito.when(httpClient.executeOpen(Mockito.any(), (ClassicHttpRequest) Mockito.any(),
(HttpContext) Mockito.any())).thenReturn(httpResponse);
@@ -233,6 +316,7 @@ public class RemoteJwkSetCacheTest {
Mockito.when(httpClient.executeOpen(Mockito.any(), (ClassicHttpRequest) Mockito.any(),
(HttpContext) Mockito.any())).thenReturn(httpResponse)
.thenThrow(new IOException("Failure"));
+ Mockito.when(httpResponse.getCode()).thenReturn(200);
return httpClient;
}
@@ -243,12 +327,15 @@ public class RemoteJwkSetCacheTest {
final ClassicHttpResponse responseA = Mockito.mock(ClassicHttpResponse.class);
Mockito.when(responseA.getEntity()).thenReturn(new StringEntity(keySetA));
+ Mockito.when(responseA.getCode()).thenReturn(200);
final ClassicHttpResponse responseB = Mockito.mock(ClassicHttpResponse.class);
Mockito.when(responseB.getEntity()).thenReturn(new StringEntity(KeySetB));
+ Mockito.when(responseB.getCode()).thenReturn(200);
final ClassicHttpResponse responseC = Mockito.mock(ClassicHttpResponse.class);
Mockito.when(responseC.getEntity()).thenReturn(new StringEntity(keySetC));
+ Mockito.when(responseC.getCode()).thenReturn(200);
Mockito.when(httpClient.executeOpen(
Mockito.any(),
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list