fix(oidc): allow disable of SSRF filtering for OIDC config (#2413)
This commit is contained in:
@@ -15,7 +15,7 @@ public class AppProperties {
|
||||
private String version;
|
||||
private RemoteAuth remoteAuth;
|
||||
private OutboundRequests outbound;
|
||||
private Boolean forceDisableOidc = false;
|
||||
private OIDC oidc;
|
||||
|
||||
/**
|
||||
* Type of disk storage where library files are stored.
|
||||
@@ -49,4 +49,11 @@ public class AppProperties {
|
||||
private int readTimeout = 15;
|
||||
private List<String> restrictedRanges = List.of();
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class OIDC {
|
||||
private Boolean forceDisable = false;
|
||||
private Boolean allowUnsafeHosts = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.booklore.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
|
||||
import org.springframework.boot.http.client.HttpClientSettings;
|
||||
import org.springframework.boot.http.client.InetAddressFilter;
|
||||
@@ -8,7 +9,6 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@@ -34,6 +34,7 @@ public class RestClientConfig {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public ClientHttpRequestFactory clientHttpRequestFactory(InetAddressFilter outboundInetAddressFilter) {
|
||||
var outbound = appProperties.getOutbound();
|
||||
int connectTimeout = outbound.getConnectTimeout();
|
||||
@@ -49,6 +50,23 @@ public class RestClientConfig {
|
||||
.build(settings);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Qualifier("unsafe")
|
||||
public ClientHttpRequestFactory unsafeClientHttpRequestFactory() {
|
||||
var outbound = appProperties.getOutbound();
|
||||
int connectTimeout = outbound.getConnectTimeout();
|
||||
int readTimeout = outbound.getReadTimeout();
|
||||
|
||||
HttpClientSettings settings = HttpClientSettings.defaults()
|
||||
.withConnectTimeout(Duration.ofSeconds(connectTimeout))
|
||||
.withReadTimeout(Duration.ofSeconds(readTimeout))
|
||||
.withInetAddressFilter(InetAddressFilter.all());
|
||||
|
||||
return ClientHttpRequestFactoryBuilder
|
||||
.jdk()
|
||||
.build(settings);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RestClient restClient(ClientHttpRequestFactory clientHttpRequestFactory) {
|
||||
return RestClient.builder()
|
||||
@@ -63,10 +81,18 @@ public class RestClientConfig {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RestTemplate oidcRestTemplate() {
|
||||
var factory = new SimpleClientHttpRequestFactory();
|
||||
factory.setConnectTimeout(Duration.ofSeconds(10));
|
||||
factory.setReadTimeout(Duration.ofSeconds(10));
|
||||
return new RestTemplate(factory);
|
||||
@Qualifier("oidc")
|
||||
public RestTemplate oidcRestTemplate(
|
||||
ClientHttpRequestFactory clientHttpRequestFactory,
|
||||
@Qualifier("unsafe")
|
||||
ClientHttpRequestFactory unsafeClientHttpRequestFactory
|
||||
) {
|
||||
var isAllowingUnsafeHosts = appProperties.getOidc().getAllowUnsafeHosts();
|
||||
|
||||
if (isAllowingUnsafeHosts) {
|
||||
return new RestTemplate(unsafeClientHttpRequestFactory);
|
||||
}
|
||||
|
||||
return new RestTemplate(clientHttpRequestFactory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.booklore.config.security.oidc;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@@ -15,15 +15,23 @@ import java.util.regex.Pattern;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class OidcDiscoveryService {
|
||||
|
||||
private static final Pattern TRAILING_SLASH = Pattern.compile("/+$");
|
||||
private static final long CACHE_TTL_MS = 3_600_000; // 1 hour
|
||||
|
||||
private final ConcurrentMap<String, CachedDiscovery> cache = new ConcurrentHashMap<>();
|
||||
|
||||
@Qualifier("oidc")
|
||||
private final RestTemplate oidcRestTemplate;
|
||||
|
||||
public OidcDiscoveryService(
|
||||
@Qualifier("oidc")
|
||||
RestTemplate oidcRestTemplate
|
||||
) {
|
||||
this.oidcRestTemplate = oidcRestTemplate;
|
||||
}
|
||||
|
||||
public record DiscoveryDocument(
|
||||
String issuer,
|
||||
String authorizationEndpoint,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package org.booklore.config.security.oidc;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.model.dto.settings.OidcProviderDetails;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -18,12 +18,20 @@ import org.springframework.web.client.RestClientException;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class OidcTokenClient {
|
||||
|
||||
private final OidcDiscoveryService discoveryService;
|
||||
private final RestTemplate oidcRestTemplate;
|
||||
|
||||
public OidcTokenClient(
|
||||
OidcDiscoveryService discoveryService,
|
||||
@Qualifier("oidc")
|
||||
RestTemplate oidcRestTemplate
|
||||
) {
|
||||
this.discoveryService = discoveryService;
|
||||
this.oidcRestTemplate = oidcRestTemplate;
|
||||
}
|
||||
|
||||
public record TokenResponse(
|
||||
String accessToken,
|
||||
String idToken,
|
||||
|
||||
@@ -185,6 +185,14 @@ public class AppSettingService {
|
||||
.collect(Collectors.toMap(AppSettingEntity::getName, AppSettingEntity::getVal));
|
||||
}
|
||||
|
||||
private boolean isOIDCForceDisabled() {
|
||||
return (
|
||||
appProperties.getOidc() != null &&
|
||||
appProperties.getOidc().getForceDisable() != null &&
|
||||
appProperties.getOidc().getForceDisable()
|
||||
);
|
||||
}
|
||||
|
||||
private PublicAppSetting buildPublicSetting() {
|
||||
Map<String, String> settingsMap = getSettingsMap();
|
||||
PublicAppSetting.PublicAppSettingBuilder builder = PublicAppSetting.builder();
|
||||
@@ -198,7 +206,7 @@ public class AppSettingService {
|
||||
boolean oidcEnabled = Boolean.parseBoolean(settingPersistenceHelper.getOrCreateSetting(AppSettingKey.OIDC_ENABLED, "false"));
|
||||
boolean oidcForceOnlyMode = Boolean.parseBoolean(settingPersistenceHelper.getOrCreateSetting(AppSettingKey.OIDC_FORCE_ONLY_MODE, "false"));
|
||||
|
||||
if (appProperties.getForceDisableOidc() != null && appProperties.getForceDisableOidc()) {
|
||||
if (isOIDCForceDisabled()) {
|
||||
oidcEnabled = false;
|
||||
oidcForceOnlyMode = false;
|
||||
}
|
||||
@@ -259,8 +267,7 @@ public class AppSettingService {
|
||||
boolean oidcEnabled = Boolean.parseBoolean(settingPersistenceHelper.getOrCreateSetting(AppSettingKey.OIDC_ENABLED, "false"));
|
||||
boolean oidcForceOnlyMode = Boolean.parseBoolean(settingPersistenceHelper.getOrCreateSetting(AppSettingKey.OIDC_FORCE_ONLY_MODE, "false"));
|
||||
|
||||
// Env var flag to force disable OIDC
|
||||
if (appProperties.getForceDisableOidc() != null && appProperties.getForceDisableOidc()) {
|
||||
if (isOIDCForceDisabled()) {
|
||||
oidcEnabled = false;
|
||||
oidcForceOnlyMode = false;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package org.booklore.service.oidc;
|
||||
|
||||
import com.nimbusds.jose.jwk.JWKSet;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.booklore.model.dto.settings.OidcProviderDetails;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.booklore.util.FileUtils;
|
||||
@@ -15,7 +16,6 @@ import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class OidcDiagnosticService {
|
||||
|
||||
public record OidcTestResult(boolean success, List<OidcTestCheck> checks) {}
|
||||
@@ -24,11 +24,15 @@ public class OidcDiagnosticService {
|
||||
|
||||
public enum CheckStatus { PASS, FAIL, WARN, SKIP }
|
||||
|
||||
private static final int CONNECT_TIMEOUT_MS = 10_000;
|
||||
private static final int READ_TIMEOUT_MS = 10_000;
|
||||
|
||||
private final RestTemplate oidcRestTemplate;
|
||||
|
||||
public OidcDiagnosticService(
|
||||
@Qualifier("oidc")
|
||||
RestTemplate oidcRestTemplate
|
||||
) {
|
||||
this.oidcRestTemplate = oidcRestTemplate;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public OidcTestResult testConnection(OidcProviderDetails providerDetails) {
|
||||
List<OidcTestCheck> checks = new ArrayList<>();
|
||||
@@ -80,7 +84,13 @@ public class OidcDiagnosticService {
|
||||
// 3. Fetch JWKS
|
||||
if (jwksUri != null && !jwksUri.isBlank()) {
|
||||
try {
|
||||
JWKSet jwkSet = JWKSet.load(URI.create(jwksUri).toURL());
|
||||
Map<String, Object> jwksDoc = oidcRestTemplate.getForObject(jwksUri, Map.class);
|
||||
|
||||
if (jwksDoc == null) {
|
||||
jwksDoc = Map.of();
|
||||
}
|
||||
|
||||
JWKSet jwkSet = JWKSet.parse(jwksDoc);
|
||||
int keyCount = jwkSet.getKeys().size();
|
||||
checks.add(new OidcTestCheck("JWKS Keys", CheckStatus.PASS, keyCount + " key(s) found"));
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -19,9 +19,12 @@ app:
|
||||
header-groups: ${REMOTE_AUTH_HEADER_GROUPS:Remote-Groups}
|
||||
admin-group: ${REMOTE_AUTH_ADMIN_GROUP}
|
||||
groups-delimiter: ${REMOTE_AUTH_GROUPS_DELIMITER:\\s+}
|
||||
force-disable-oidc: ${FORCE_DISABLE_OIDC:false}
|
||||
disk-type: ${DISK_TYPE:LOCAL}
|
||||
|
||||
oidc:
|
||||
force-disable: ${FORCE_DISABLE_OIDC:false}
|
||||
allow-unsafe-hosts: ${OIDC_ALLOW_UNSAFE_HOSTS:false} # If enabled, turn off outbound SSRF filtering
|
||||
|
||||
outbound:
|
||||
connect-timeout: 15
|
||||
read-timeout: 15
|
||||
|
||||
Reference in New Issue
Block a user