| | |
| | | package org.keycloak.protocol.oidc.utils; |
| | | |
| | | import org.jboss.logging.Logger; |
| | | import org.keycloak.common.util.Encode; |
| | | import org.keycloak.common.util.KeycloakUriBuilder; |
| | | import org.keycloak.common.util.UriUtils; |
| | | import org.keycloak.models.ClientModel; |
| | | import org.keycloak.models.Constants; |
| | | import org.keycloak.models.KeycloakSession; |
| | | import org.keycloak.models.KeycloakUriInfo; |
| | | import org.keycloak.models.RealmModel; |
| | | import org.keycloak.protocol.oidc.OIDCLoginProtocol; |
| | | import org.keycloak.services.Urls; |
| | | import org.keycloak.services.util.ResolveRelative; |
| | | |
| | |
| | | |
| | | private static final Logger logger = Logger.getLogger(RedirectUtils.class); |
| | | |
| | | /** |
| | | * This method is deprecated for performance and security reasons and it is available just for the |
| | | * backwards compatibility. It is recommended to use some other methods of this class where the client is given as an argument |
| | | * to the method, so we know the client, which redirect-uri we are trying to resolve. |
| | | */ |
| | | @Deprecated |
| | | public static String verifyRealmRedirectUri(KeycloakSession session, String redirectUri) { |
| | | Set<String> validRedirects = getValidateRedirectUris(session); |
| | | return verifyRedirectUri(session, null, redirectUri, validRedirects, true); |
| | |
| | | return resolveValidRedirects; |
| | | } |
| | | |
| | | @Deprecated |
| | | private static Set<String> getValidateRedirectUris(KeycloakSession session) { |
| | | return session.getContext().getRealm().getClientsStream() |
| | | .filter(ClientModel::isEnabled) |
| | | .map(c -> resolveValidRedirects(session, c.getRootUrl(), c.getRedirectUris())) |
| | | .flatMap(Collection::stream) |
| | | .collect(Collectors.toSet()); |
| | | RealmModel realm = session.getContext().getRealm(); |
| | | return session.clients().getAllRedirectUrisOfEnabledClients(realm).entrySet().stream() |
| | | .filter(me -> me.getKey().isEnabled() && OIDCLoginProtocol.LOGIN_PROTOCOL.equals(me.getKey().getProtocol()) && !me.getKey().isBearerOnly() && (me.getKey().isStandardFlowEnabled() || me.getKey().isImplicitFlowEnabled())) |
| | | .map(me -> resolveValidRedirects(session, me.getKey().getRootUrl(), me.getValue())) |
| | | .flatMap(Collection::stream) |
| | | .collect(Collectors.toSet()); |
| | | } |
| | | |
| | | private static String verifyRedirectUri(KeycloakSession session, String rootUrl, String redirectUri, Set<String> validRedirects, boolean requireRedirectUri) { |
| | | public static String verifyRedirectUri(KeycloakSession session, String rootUrl, String redirectUri, Set<String> validRedirects, boolean requireRedirectUri) { |
| | | KeycloakUriInfo uriInfo = session.getContext().getUri(); |
| | | RealmModel realm = session.getContext().getRealm(); |
| | | |
| | | if (redirectUri != null) { |
| | | try { |
| | | URI uri = URI.create(redirectUri); |
| | | redirectUri = uri.normalize().toString(); |
| | | } catch (IllegalArgumentException cause) { |
| | | logger.debug("Invalid redirect uri", cause); |
| | | return null; |
| | | } catch (Exception cause) { |
| | | logger.debug("Unexpected error when parsing redirect uri", cause); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | if (redirectUri == null) { |
| | | if (!requireRedirectUri) { |
| | | redirectUri = getSingleValidRedirectUri(validRedirects); |
| | |
| | | logger.debug("No Redirect URIs supplied"); |
| | | redirectUri = null; |
| | | } else { |
| | | redirectUri = lowerCaseHostname(redirectUri); |
| | | // Make the validations against fully decoded and normalized redirect-url. This also allows wildcards (case when client configured "Valid redirect-urls" contain wildcards) |
| | | String decodedRedirectUri = decodeRedirectUri(redirectUri); |
| | | decodedRedirectUri = getNormalizedRedirectUri(decodedRedirectUri); |
| | | if (decodedRedirectUri == null) return null; |
| | | |
| | | String r = redirectUri; |
| | | String r = decodedRedirectUri; |
| | | Set<String> resolveValidRedirects = resolveValidRedirects(session, rootUrl, validRedirects); |
| | | |
| | | boolean valid = matchesRedirects(resolveValidRedirects, r); |
| | | boolean valid = matchesRedirects(resolveValidRedirects, r, true); |
| | | |
| | | if (!valid && (r.startsWith(Constants.INSTALLED_APP_URL) || r.startsWith(Constants.INSTALLED_APP_LOOPBACK)) && r.indexOf(':', Constants.INSTALLED_APP_URL.length()) >= 0) { |
| | | int i = r.indexOf(':', Constants.INSTALLED_APP_URL.length()); |
| | |
| | | |
| | | r = sb.toString(); |
| | | |
| | | valid = matchesRedirects(resolveValidRedirects, r); |
| | | valid = matchesRedirects(resolveValidRedirects, r, true); |
| | | } |
| | | |
| | | // Return the original redirectUri, which can be partially encoded - for example http://localhost:8280/foo/bar%20bar%2092%2F72/3 . Just make sure it is normalized |
| | | redirectUri = getNormalizedRedirectUri(redirectUri); |
| | | |
| | | // We try to check validity also for original (encoded) redirectUrl, but just in case it exactly matches some "Valid Redirect URL" specified for client (not wildcards allowed) |
| | | if (!valid) { |
| | | valid = matchesRedirects(resolveValidRedirects, redirectUri, false); |
| | | } |
| | | |
| | | if (valid && redirectUri.startsWith("/")) { |
| | | redirectUri = relativeToAbsoluteURI(session, rootUrl, redirectUri); |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | private static String getNormalizedRedirectUri(String redirectUri) { |
| | | if (redirectUri != null) { |
| | | try { |
| | | URI uri = URI.create(redirectUri); |
| | | redirectUri = uri.normalize().toString(); |
| | | } catch (IllegalArgumentException cause) { |
| | | logger.debug("Invalid redirect uri", cause); |
| | | return null; |
| | | } catch (Exception cause) { |
| | | logger.debug("Unexpected error when parsing redirect uri", cause); |
| | | return null; |
| | | } |
| | | redirectUri = lowerCaseHostname(redirectUri); |
| | | } |
| | | return redirectUri; |
| | | } |
| | | |
| | | // Decode redirectUri. We don't decode query and fragment as those can be encoded in the original URL. |
| | | // URL can be decoded multiple times (in case it was encoded multiple times, or some of it's parts were encoded multiple times) |
| | | private static String decodeRedirectUri(String redirectUri) { |
| | | if (redirectUri == null) return null; |
| | | int MAX_DECODING_COUNT = 5; // Max count of attempts for decoding URL (in case it was encoded multiple times) |
| | | |
| | | try { |
| | | KeycloakUriBuilder uriBuilder = KeycloakUriBuilder.fromUri(redirectUri).preserveDefaultPort(); |
| | | String origQuery = uriBuilder.getQuery(); |
| | | String origFragment = uriBuilder.getFragment(); |
| | | String encodedRedirectUri = uriBuilder |
| | | .replaceQuery(null) |
| | | .fragment(null) |
| | | .buildAsString(); |
| | | String decodedRedirectUri = null; |
| | | |
| | | for (int i = 0; i < MAX_DECODING_COUNT; i++) { |
| | | decodedRedirectUri = Encode.decode(encodedRedirectUri); |
| | | if (decodedRedirectUri.equals(encodedRedirectUri)) { |
| | | // URL is decoded. We can return it (after attach original query and fragment) |
| | | return KeycloakUriBuilder.fromUri(decodedRedirectUri).preserveDefaultPort() |
| | | .replaceQuery(origQuery) |
| | | .fragment(origFragment) |
| | | .buildAsString(); |
| | | } else { |
| | | // Next attempt |
| | | encodedRedirectUri = decodedRedirectUri; |
| | | } |
| | | } |
| | | } catch (IllegalArgumentException iae) { |
| | | logger.debugf("Illegal redirect URI used: %s, Details: %s", redirectUri, iae.getMessage()); |
| | | } |
| | | logger.debugf("Was not able to decode redirect URI: %s", redirectUri); |
| | | return null; |
| | | } |
| | | |
| | | private static String lowerCaseHostname(String redirectUri) { |
| | | int n = redirectUri.indexOf('/', 7); |
| | | if (n == -1) { |
| | |
| | | return sb.toString(); |
| | | } |
| | | |
| | | private static boolean matchesRedirects(Set<String> validRedirects, String redirect) { |
| | | private static boolean matchesRedirects(Set<String> validRedirects, String redirect, boolean allowWildcards) { |
| | | logger.tracef("matchesRedirects: redirect URL to check: %s, allow wildcards: %b, Configured valid redirect URLs: %s", redirect, allowWildcards, validRedirects); |
| | | for (String validRedirect : validRedirects) { |
| | | if (validRedirect.endsWith("*") && !validRedirect.contains("?")) { |
| | | if (validRedirect.endsWith("*") && !validRedirect.contains("?") && allowWildcards) { |
| | | // strip off the query component - we don't check them when wildcards are effective |
| | | String r = redirect.contains("?") ? redirect.substring(0, redirect.indexOf("?")) : redirect; |
| | | // strip off * |