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