Repository navigation
Add support for wildcards in subdomains for valid redirect URIs #5826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
services/src/main/java/org/keycloak/protocol/oidc/utils/Regexify.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package org.keycloak.protocol.oidc.utils; | ||
|
|
||
| import java.util.function.Function; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Class responsible for taking common wildcard syntax (*), and turning it into Java regex syntax | ||
| * <p> | ||
| * I.E. | ||
| * foo*bar -> ^foo*.bar$ | ||
| * with-chars-need-escaping -> ^with\-chars\-need\-escaping$ | ||
| * | ||
| * Note, that if null is passed in, it will match NOTHING. | ||
| */ | ||
| public class Regexify { | ||
|
|
||
| public static final String MATCH_ANYTHING_REGEX = "^.*$"; | ||
| public static final String MATCH_NOTHING_REGEX = "1^"; | ||
|
|
||
| public static Function<String, String> asString = stringToRegexify -> { | ||
| if (stringToRegexify == null) { | ||
| return MATCH_NOTHING_REGEX; | ||
| } | ||
| // Per http://docs.oracle.com/javase/tutorial/essential/regex/literals.html | ||
| // the Java regex metachars are: <([{\^-=$!|]})?*+.> | ||
| return "^" + stringToRegexify.replaceAll("[\\<\\(\\[\\{\\\\\\^\\-\\=\\$\\!\\|\\]\\}\\)\\?\\+\\.\\>]", "\\\\$0").replaceAll("\\*", ".*") + "$"; | ||
| }; | ||
|
|
||
| public static Function<String, Pattern> asPattern = stringToRegexify -> Pattern.compile(asString.apply(stringToRegexify)); | ||
| } |
36 changes: 36 additions & 0 deletions
36
services/src/main/java/org/keycloak/protocol/oidc/utils/url/WildcardStringMatches.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package org.keycloak.protocol.oidc.utils.url; | ||
|
|
||
| import java.net.URL; | ||
| import java.util.function.Predicate; | ||
|
|
||
| /** | ||
| * Wrapper class to handle conversion of strings to URLs when attempting to find out what matches. If any problems are encountered | ||
| * constructing a URL from the given strings (either in the base case or predicate test case), then result will always be "false". | ||
| * | ||
| * @see org.keycloak.procotol.oidc.utils.url.WildcardUrlMatches for full description on what will and will not match. | ||
| * @see java.net.URL for more info on URL construction | ||
| */ | ||
| public class WildcardStringMatches implements Predicate<String> { | ||
|
|
||
| private Predicate<URL> urlStringMatches; | ||
|
|
||
| /** | ||
| * @param urlToMatch base case against which tests should be applied. Is allowed to have wildcards in either part of the host name, or the path. | ||
| */ | ||
| public WildcardStringMatches(final String urlToMatch) { | ||
| try { | ||
| urlStringMatches = new WildcardUrlMatches(new URL(urlToMatch)); | ||
| } catch (Exception e) { | ||
| urlStringMatches = url -> false; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean test(final String matchString) { | ||
| try { | ||
| return urlStringMatches.test(new URL(matchString)); | ||
| } catch (Exception e) { | ||
| return false; | ||
| } | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
services/src/main/java/org/keycloak/protocol/oidc/utils/url/WildcardUrlMatches.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package org.keycloak.protocol.oidc.utils.url; | ||
|
|
||
| import org.keycloak.protocol.oidc.utils.Regexify; | ||
|
|
||
| import java.net.URL; | ||
| import java.util.Optional; | ||
| import java.util.function.Predicate; | ||
|
|
||
| /** | ||
| * Attempts to determine whether the given wildcard url string matches an input string assumed to be a normal url. Note that | ||
| * this *only* currently evaluates protocol, host, port, and path. Query parameters and fragments are not evaluated. | ||
| * | ||
| * E.X. | ||
| * new WildcardUrlStringMatch(http://www.redhat.com/*).test("http://www.redhat.com/foo.html?lang=en) = true | ||
| */ | ||
| public class WildcardUrlMatches implements Predicate<URL> { | ||
|
|
||
| public static final String MATCH_ANYTHING_REGEX = "^.*$"; | ||
| private final URL baseCaseWithWildcards; | ||
|
|
||
| public WildcardUrlMatches(final URL baseCaseWithWildcards) { | ||
| this.baseCaseWithWildcards = baseCaseWithWildcards; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean test(final URL regularUrlString) { | ||
|
|
||
| // first check on the protocol, host, and port | ||
| boolean matches = regularUrlString.getProtocol().matches(Regexify.asString.apply(baseCaseWithWildcards.getProtocol())) && | ||
| regularUrlString.getHost().matches(Regexify.asString.apply(baseCaseWithWildcards.getHost())) && | ||
| String.valueOf(regularUrlString.getPort()).matches(Regexify.asString.apply(String.valueOf(baseCaseWithWildcards.getPort()))); | ||
|
|
||
| // Special Snowflake Case = when the path ends in 'foo/*', then 'foo' must also be accepted as valid | ||
| final String wildcardPathString = Optional.ofNullable(baseCaseWithWildcards.getPath()).orElse(""); | ||
| final boolean specialSnowflakeCase = wildcardPathString.endsWith("/*"); | ||
| if (matches) { | ||
| final String regularUrlPathString = Optional.ofNullable(regularUrlString.getPath()).orElse(""); | ||
| matches = regularUrlPathString.matches(Regexify.asString.apply(wildcardPathString)) || | ||
| (specialSnowflakeCase && regularUrlPathString.matches(Regexify.asString.apply(wildcardPathString.substring(0, wildcardPathString.length() - 2)))); | ||
| } | ||
|
|
||
| // Now perform a naive check on the query params (if applicable) | ||
| if (matches && !specialSnowflakeCase) { | ||
| matches = Optional.ofNullable(regularUrlString.getQuery()).orElse("").equals(Optional.ofNullable(baseCaseWithWildcards.getQuery()).orElse("")); | ||
| } | ||
|
|
||
| return matches; | ||
| } | ||
| } |
117 changes: 117 additions & 0 deletions
117
services/src/test/java/org/keycloak/protocol/oidc/utils/RedirectUtilsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package org.keycloak.protocol.oidc.utils; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look at OAuthRedirectUriTest which can be extended to cover these tests properly |
||
|
|
||
| import org.jboss.resteasy.spi.ResteasyUriInfo; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.keycloak.models.ClientModel; | ||
| import org.keycloak.models.RealmModel; | ||
| import org.mockito.Mock; | ||
|
|
||
| import javax.ws.rs.core.UriInfo; | ||
| import java.net.URI; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.equalTo; | ||
| import static org.hamcrest.CoreMatchers.nullValue; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.mockito.MockitoAnnotations.initMocks; | ||
|
|
||
| public class RedirectUtilsTest { | ||
|
|
||
| @Mock | ||
| ClientModel client; | ||
| @Mock | ||
| RealmModel realm; | ||
| private Set<String> validRedirects; | ||
| private UriInfo uriInfo; | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| initMocks(this); | ||
|
|
||
| validRedirects = new HashSet<>(Arrays.asList( | ||
| "http://example.com", | ||
| "http://with-dash.example.com/*", | ||
| "http://sub.domain.example.com/foo", | ||
| "http://localhost:8180/*", | ||
| "http://*.subwildcard.example.com")); | ||
| uriInfo = new ResteasyUriInfo(new URI("http://localhost:8180/auth/realms/test/protocol/openid-connect/auth")); | ||
|
|
||
| when(realm.getName()).thenReturn("test-realm"); | ||
| when(client.getRedirectUris()).thenReturn(validRedirects); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnNull_whenNoRedirectUriInput() throws Exception { | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, null, realm, client), nullValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnNull_whenNoValidRedirectUrisSpecified() throws Exception { | ||
| when(client.getRedirectUris()).thenReturn(Collections.EMPTY_SET); | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://localhost:8180/", realm, client), nullValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnNull_whenPortNumberIncorrect() throws Exception { | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://localhost:8181/", realm, client), nullValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnNull_whenNewPortAddedToHostname() throws Exception { | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://with-dash.example.com:8080", realm, client), nullValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnRequestedRedirect_withDash() throws Exception { | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://with-dash.example.com", realm, client), | ||
| equalTo("http://with-dash.example.com")); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnRequestedRedirect_withQueryParams() throws Exception { | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://with-dash.example.com/return?key=value/", realm, client), | ||
| equalTo("http://with-dash.example.com/return?key=value/")); | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://with-dash.example.com/return?key", realm, client), | ||
| equalTo("http://with-dash.example.com/return?key")); | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://with-dash.example.com/return?key&otherKey=otherValue", realm, client), | ||
| equalTo("http://with-dash.example.com/return?key&otherKey=otherValue")); | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://with-dash.example.com/return?key&otherKey", realm, client), | ||
| equalTo("http://with-dash.example.com/return?key&otherKey")); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnRequestedRedirect_forVariousPathsAfterWildcard() throws Exception { | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://with-dash.example.com/somepath", realm, client), | ||
| equalTo("http://with-dash.example.com/somepath")); | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://with-dash.example.com/somepath/with/long/path", realm, client), | ||
| equalTo("http://with-dash.example.com/somepath/with/long/path")); | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://localhost:8180/somepath/with/long/path", realm, client), | ||
| equalTo("http://localhost:8180/somepath/with/long/path")); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnRequestedRedirect_whenDifferentCasedHostnameGiven() throws Exception { | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://wItH-DaSh.example.com", realm, client), | ||
| equalTo("http://with-dash.example.com")); | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "hTTP://wItH-DaSh.example.com", realm, client), | ||
| equalTo("http://with-dash.example.com")); | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "httP://sub.dOMAIn.example.com/foo", realm, client), | ||
| equalTo("http://sub.domain.example.com/foo")); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnRequestedRedirect_forWildcardedSubdomain() throws Exception { | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "http://dev1.subwildcard.example.com", realm, client), | ||
| equalTo("http://dev1.subwildcard.example.com")); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnRequestedRedirect_forRelativeDomain() throws Exception { | ||
| assertThat(RedirectUtils.verifyRedirectUri(uriInfo, "/client1/landing", realm, client), equalTo("http://localhost:8180/client1/landing")); | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
services/src/test/java/org/keycloak/protocol/oidc/utils/RegexifyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package org.keycloak.protocol.oidc.utils; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import static org.hamcrest.core.IsEqual.equalTo; | ||
| import static org.junit.Assert.assertThat; | ||
|
|
||
| public class RegexifyTest { | ||
|
|
||
| @Test | ||
| public void testToRegexString_null() { | ||
| assertThat(Regexify.asString.apply(null), equalTo(Regexify.MATCH_NOTHING_REGEX)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToRegexString_empty() { | ||
| assertThat(Regexify.asString.apply(""), equalTo("^$")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToRegexString_basic() { | ||
| assertThat(Regexify.asString.apply("http://"), equalTo("^http://$")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToRegexString_wildcardStart() { | ||
| assertThat(Regexify.asString.apply("*tp://"), equalTo("^.*tp://$")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToRegexString_wildcardEnd() { | ||
| assertThat(Regexify.asString.apply("http*://"), equalTo("^http.*://$")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToRegexString_escapeMetachars() { | ||
| assertThat(Regexify.asString.apply("with-dash.example.com"), equalTo("^with\\-dash\\.example\\.com$")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToRegexString_escapeMetacharsWithWidlcard() { | ||
| assertThat(Regexify.asString.apply("dev.*.example.com"), equalTo("^dev\\..*\\.example\\.com$")); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have decided not to use mocking frameworks so we can't accept this