[java-oidc-common] 03/04: JCOMOIDC-200 - Improve response handling in RemoteJwkSetUtils
Codeberg
noreply at shibboleth.net
Fri Nov 27 12:20:49 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/50d4fed6d52b3472f95250d4fa4981dfe0023785
commit 50d4fed6d52b3472f95250d4fa4981dfe0023785
Author: Henri Mikkonen <henri.mikkonen at iki.fi>
AuthorDate: Fri Nov 27 13:59:56 2026 +0200
JCOMOIDC-200 - Improve response handling in RemoteJwkSetUtils
https://shibboleth.atlassian.net/browse/JCOMOIDC-200
- Status: anything but 2xx status codes are rejected
- Missing entities are logged and don't throw anymore
- Size limit may be passed to the method, defaults to 1MB
---
.../oidc/jwk/support/RemoteJwkUtils.java | 52 ++++++++++++++++++----
1 file changed, 43 insertions(+), 9 deletions(-)
diff --git a/oidc-common-crypto-api/src/main/java/net/shibboleth/oidc/jwk/support/RemoteJwkUtils.java b/oidc-common-crypto-api/src/main/java/net/shibboleth/oidc/jwk/support/RemoteJwkUtils.java
index 6b011312..c0664915 100644
--- a/oidc-common-crypto-api/src/main/java/net/shibboleth/oidc/jwk/support/RemoteJwkUtils.java
+++ b/oidc-common-crypto-api/src/main/java/net/shibboleth/oidc/jwk/support/RemoteJwkUtils.java
@@ -17,14 +17,15 @@ package net.shibboleth.oidc.jwk.support;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
+import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
-import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
import org.opensaml.security.httpclient.HttpClientSecurityParameters;
import org.opensaml.security.httpclient.HttpClientSecuritySupport;
@@ -34,18 +35,23 @@ import org.slf4j.LoggerFactory;
import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.util.JSONObjectUtils;
+import net.shibboleth.shared.httpclient.HttpClientSupport;
+
/**
* Generic utility methods related to remote JWK sets.
*/
public final class RemoteJwkUtils {
-
+
+ /** The default value for maximum response content length. */
+ public static final int DEFAULT_MAX_RESPONSE_CONTENT_LENGTH = 1048576;
+
/**
* Constructor.
*/
private RemoteJwkUtils() {
// prevented
}
-
+
/**
* Fetches the JWK set from the given URI using the given client and security parameters.
*
@@ -58,6 +64,24 @@ public final class RemoteJwkUtils {
*/
public static JWKSet fetchRemoteJwkSet(final String logPrefix, final URI uri, final HttpClient httpClient,
final HttpClientSecurityParameters httpClientSecurityParameters) {
+ return fetchRemoteJwkSet(logPrefix, uri, httpClient, httpClientSecurityParameters,
+ DEFAULT_MAX_RESPONSE_CONTENT_LENGTH);
+ }
+
+ /**
+ * Fetches the JWK set from the given URI using the given client and security parameters.
+ *
+ * @param logPrefix log prefix
+ * @param uri the endpoint for the JWK set.
+ * @param httpClient HTTP client
+ * @param httpClientSecurityParameters security parameters
+ * @param maxContentLength maximum response content length
+ *
+ * @return The JWK set fetched from the endpoint, or null if it couldn't be fetched.
+ * @since 3.4.0
+ */
+ public static JWKSet fetchRemoteJwkSet(final String logPrefix, final URI uri, final HttpClient httpClient,
+ final HttpClientSecurityParameters httpClientSecurityParameters, final int maxContentLength) {
final Logger log = LoggerFactory.getLogger(RemoteJwkUtils.class);
final ClassicHttpRequest get = ClassicRequestBuilder.get().setUri(uri).build();
assert get != null;
@@ -71,15 +95,25 @@ public final class RemoteJwkUtils {
assert scheme != null;
HttpClientSecuritySupport.checkTLSCredentialEvaluated(clientContext, scheme);
if (response == null) {
- log.error("{} Could not get the JWK contents from {}", logPrefix, uri);
+ log.warn("{} Could not get the JWK contents from {}: null response", logPrefix, uri);
+ return null;
+ }
+ final HttpEntity responseEntity = response.getEntity();
+ final int responseCode = response.getCode();
+ if (responseCode < 200 || responseCode > 299) {
+ log.warn("{} Unexpected status code {} in the JWKS response from {}", logPrefix, responseCode, uri);
+ return null;
+ }
+ if (responseEntity == null) {
+ log.warn("{} Could not get the JWK contents from {}: empty response", logPrefix, uri);
return null;
}
- output = EntityUtils.toString(response.getEntity(), "UTF-8");
+ output = HttpClientSupport.toString(responseEntity, StandardCharsets.UTF_8, maxContentLength);
} catch (final ParseException | IOException e) {
- log.error("{} Could not parse the JWK contents from {}", logPrefix, uri);
+ log.warn("{} Could not parse the JWK contents from {}", logPrefix, uri, e);
return null;
} catch (final URISyntaxException e) {
- log.error("{} Could not build request URI from {}", logPrefix, uri, e);
+ log.warn("{} Could not build request URI from {}", logPrefix, uri, e);
return null;
}
log.trace("{} Fetched the following response body: {}", logPrefix, output);
@@ -92,12 +126,12 @@ public final class RemoteJwkUtils {
final Map<String, Object> json = JSONObjectUtils.parse(output);
// The following check is needed to avoid NPE from Nimbus if keys claim not found
if (JSONObjectUtils.getJSONArray(json, "keys") == null) {
- log.error("{} Could not find 'keys' array from the JSON from {}", logPrefix, uri);
+ log.warn("{} Could not find 'keys' array from the JSON from {}", logPrefix, uri);
return null;
}
jwkSet = JWKSet.parse(json);
} catch (final java.text.ParseException e) {
- log.error("{} Could not parse the contents from {}", logPrefix, uri, e);
+ log.warn("{} Could not parse the contents from {}", logPrefix, uri, e);
return null;
}
return jwkSet;
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list