-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check the redirect URI is http(s) when used for a form Post (#22)
Closes keycloak/security#22 Co-authored-by: Stian Thorgersen <[email protected]>
- Loading branch information
Showing
9 changed files
with
263 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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,3 @@ | ||
= Changes in validating schemes for valid redirect URIs | ||
|
||
If an application client is using non http(s) custom schemes, from now on the validation requires that a valid redirect pattern explicitly allows that scheme. Example patterns for allowing `custom` scheme are `custom:/test`, `custom:/test/\*` or `custom:*`. For security reasons a general pattern like `*` does not cover them anymore. |
This file contains 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 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
117 changes: 117 additions & 0 deletions
117
services/src/test/java/org/keycloak/protocol/oidc/utils/RedirectUtilsTest.java
This file contains 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 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.keycloak.protocol.oidc.utils; | ||
|
||
import java.net.URI; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.jboss.resteasy.core.ResteasyContext; | ||
import org.jboss.resteasy.mock.MockHttpRequest; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.keycloak.common.Profile; | ||
import org.keycloak.common.crypto.CryptoIntegration; | ||
import org.keycloak.common.crypto.CryptoProvider; | ||
import org.keycloak.http.HttpRequest; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.services.DefaultKeycloakSession; | ||
import org.keycloak.services.DefaultKeycloakSessionFactory; | ||
import org.keycloak.services.HttpRequestImpl; | ||
|
||
/** | ||
* <p>Little test class for RedirectUtils methods.</p> | ||
* | ||
* @author rmartinc | ||
*/ | ||
public class RedirectUtilsTest { | ||
|
||
private static KeycloakSession session; | ||
|
||
@BeforeClass | ||
public static void beforeClass() { | ||
HttpRequest httpRequest = new HttpRequestImpl(MockHttpRequest.create("GET", URI.create("https://keycloak.org/"), URI.create("https://keycloak.org"))); | ||
ResteasyContext.getContextDataMap().put(HttpRequest.class, httpRequest); | ||
Profile.defaults(); | ||
CryptoIntegration.init(CryptoProvider.class.getClassLoader()); | ||
DefaultKeycloakSessionFactory sessionFactory = new DefaultKeycloakSessionFactory(); | ||
sessionFactory.init(); | ||
session = new DefaultKeycloakSession(sessionFactory); | ||
} | ||
|
||
@Test | ||
public void testverifyRedirectUriHttps() { | ||
Set<String> set = Stream.of( | ||
"https://keycloak.org/test1", | ||
"https://keycloak.org/test2", | ||
"https://keycloak.org/parent/*" | ||
).collect(Collectors.toSet()); | ||
|
||
Assert.assertEquals("https://keycloak.org/test1", RedirectUtils.verifyRedirectUri(session, null, "https://keycloak.org/test1", set, false)); | ||
Assert.assertEquals("https://keycloak.org/test2", RedirectUtils.verifyRedirectUri(session, null, "https://keycloak.org/test2", set, false)); | ||
Assert.assertEquals("https://keycloak.org/parent", RedirectUtils.verifyRedirectUri(session, null, "https://keycloak.org/parent", set, false)); | ||
Assert.assertEquals("https://keycloak.org/parent/child", RedirectUtils.verifyRedirectUri(session, null, "https://keycloak.org/parent/child", set, false)); | ||
|
||
Assert.assertNull(RedirectUtils.verifyRedirectUri(session, null, "https://keycloak.org/test", set, false)); | ||
Assert.assertNull(RedirectUtils.verifyRedirectUri(session, null, "https://keycloak.org/test1/child", set, false)); | ||
Assert.assertNull(RedirectUtils.verifyRedirectUri(session, null, "https://keycloak.com/test", set, false)); | ||
} | ||
|
||
@Test | ||
public void testverifyRedirectUriMixedSchemes() { | ||
Set<String> set = Stream.of( | ||
"https://keycloak.org/*", | ||
"custom1:/test1", | ||
"custom1:/test2", | ||
"custom1:/parent/*", | ||
"custom2:*" | ||
).collect(Collectors.toSet()); | ||
|
||
Assert.assertEquals("custom1:/test1", RedirectUtils.verifyRedirectUri(session, null, "custom1:/test1", set, false)); | ||
Assert.assertEquals("custom1:/test2", RedirectUtils.verifyRedirectUri(session, null, "custom1:/test2", set, false)); | ||
Assert.assertEquals("custom1:/parent/child", RedirectUtils.verifyRedirectUri(session, null, "custom1:/parent/child", set, false)); | ||
Assert.assertEquals("custom2:/something", RedirectUtils.verifyRedirectUri(session, null, "custom2:/something", set, false)); | ||
Assert.assertEquals("https://keycloak.org/test", RedirectUtils.verifyRedirectUri(session, null, "https://keycloak.org/test", set, false)); | ||
|
||
Assert.assertNull(RedirectUtils.verifyRedirectUri(session, null, "custom1:/test", set, false)); | ||
Assert.assertNull(RedirectUtils.verifyRedirectUri(session, null, "custom1:/test1/test", set, false)); | ||
Assert.assertNull(RedirectUtils.verifyRedirectUri(session, null, "custom3:/test", set, false)); | ||
} | ||
|
||
@Test | ||
public void testverifyRedirectUriInvalidScheme() { | ||
Set<String> set = Stream.of( | ||
"custom1:/test1", | ||
"custom1:/test2", | ||
"custom1:/parent/*", | ||
"custom2:*", | ||
"*" | ||
).collect(Collectors.toSet()); | ||
|
||
Assert.assertEquals("custom1:/test1", RedirectUtils.verifyRedirectUri(session, null, "custom1:/test1", set, false)); | ||
Assert.assertEquals("custom1:/test2", RedirectUtils.verifyRedirectUri(session, null, "custom1:/test2", set, false)); | ||
Assert.assertEquals("custom1:/parent/child", RedirectUtils.verifyRedirectUri(session, null, "custom1:/parent/child", set, false)); | ||
Assert.assertEquals("custom2:/something", RedirectUtils.verifyRedirectUri(session, null, "custom2:/something", set, false)); | ||
Assert.assertEquals("https://keycloak.org/test", RedirectUtils.verifyRedirectUri(session, null, "https://keycloak.org/test", set, false)); | ||
Assert.assertEquals("http://keycloak.org/test", RedirectUtils.verifyRedirectUri(session, null, "http://keycloak.org/test", set, false)); | ||
Assert.assertEquals("https://keycloak.org/test", RedirectUtils.verifyRedirectUri(session, null, "/test", set, false)); | ||
Assert.assertEquals("https://keycloak.com/test", RedirectUtils.verifyRedirectUri(session, "https://keycloak.com", "/test", set, false)); | ||
|
||
Assert.assertNull(RedirectUtils.verifyRedirectUri(session, null, "custom3:/test", set, false)); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
services/src/test/java/org/keycloak/protocol/oidc/utils/ResteasyTestProvider.java
This file contains 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 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.keycloak.protocol.oidc.utils; | ||
|
||
import org.jboss.resteasy.core.ResteasyContext; | ||
import org.keycloak.common.util.ResteasyProvider; | ||
|
||
/** | ||
* <p>Resteasy provider to be used for the utils class.</p> | ||
* @author rmartinc | ||
*/ | ||
public class ResteasyTestProvider implements ResteasyProvider { | ||
|
||
@Override | ||
public <R> R getContextData(Class<R> type) { | ||
return ResteasyContext.getContextData(type); | ||
} | ||
|
||
@Override | ||
public void pushDefaultContextObject(Class type, Object instance) { | ||
} | ||
|
||
@Override | ||
public void pushContext(Class type, Object instance) { | ||
} | ||
|
||
@Override | ||
public void clearContextData() { | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
services/src/test/resources/META-INF/services/org.keycloak.common.util.ResteasyProvider
This file contains 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,17 @@ | ||
# | ||
# Copyright 2023 Red Hat, Inc. and/or its affiliates | ||
# and other contributors as indicated by the @author tags. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
org.keycloak.protocol.oidc.utils.ResteasyTestProvider |
This file contains 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
Oops, something went wrong.