Repository navigation
makes redirect uris validation more relaxed #48004
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
34 changes: 34 additions & 0 deletions
34
...api/src/main/java/org/keycloak/representations/admin/v2/validation/ValidRedirectUris.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,34 @@ | ||
| package org.keycloak.representations.admin.v2.validation; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| import jakarta.validation.Constraint; | ||
| import jakarta.validation.Payload; | ||
|
|
||
| /** | ||
| * Constraint annotation for validating redirect URIs according to Keycloak's redirect URI rules. | ||
| * <p> | ||
| * Validation rules: | ||
| * <ul> | ||
| * <li>Wildcards (*) are only allowed at the end of the path</li> | ||
| * <li>Wildcards must be preceded by a slash (/)</li> | ||
| * <li>Wildcards cannot be followed by query parameters or fragments</li> | ||
| * <li>Only one wildcard is allowed</li> | ||
| * <li>Without a root URL, redirect URIs must be absolute (include scheme)</li> | ||
| * <li>With a root URL set, relative paths are allowed</li> | ||
| * <li>Special values: "*" (full wildcard), "+" and "-" (for post-logout) are always valid</li> | ||
| * </ul> | ||
| */ | ||
| @Target({ElementType.TYPE}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Constraint(validatedBy = {ValidRedirectUrisValidator.class}) | ||
| @Documented | ||
| public @interface ValidRedirectUris { | ||
| String message() default "Invalid redirect URI"; | ||
| Class<?>[] groups() default {}; | ||
| Class<? extends Payload>[] payload() default {}; | ||
| } |
120 changes: 120 additions & 0 deletions
120
...ain/java/org/keycloak/representations/admin/v2/validation/ValidRedirectUrisValidator.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,120 @@ | ||
| package org.keycloak.representations.admin.v2.validation; | ||
|
|
||
| import java.util.Set; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import jakarta.validation.ConstraintValidator; | ||
| import jakarta.validation.ConstraintValidatorContext; | ||
|
|
||
| import org.keycloak.representations.admin.v2.BaseClientRepresentation; | ||
|
|
||
| /** | ||
| * Validates redirect URIs according to Keycloak's redirect URI rules. | ||
| * <p> | ||
| * The validation is context-aware: when a root URL is set on the client, | ||
| * relative paths are allowed. Without a root URL, only absolute URIs are valid. | ||
| */ | ||
| public class ValidRedirectUrisValidator implements ConstraintValidator<ValidRedirectUris, BaseClientRepresentation> { | ||
|
|
||
| private static final Pattern SCHEME_PATTERN = Pattern.compile("^[a-zA-Z][a-zA-Z0-9+.-]*://"); | ||
|
|
||
| @Override | ||
| public boolean isValid(BaseClientRepresentation representation, ConstraintValidatorContext context) { | ||
| Set<String> redirectUris = representation.getRedirectUris(); | ||
| if (redirectUris == null || redirectUris.isEmpty()) { | ||
| return true; | ||
| } | ||
|
|
||
| boolean hasRootUrl = representation.getAppUrl() != null && !representation.getAppUrl().isBlank(); | ||
| boolean allValid = true; | ||
|
|
||
| for (String uri : redirectUris) { | ||
| String error = validateRedirectUri(uri, hasRootUrl); | ||
| if (error != null) { | ||
| context.disableDefaultConstraintViolation(); | ||
| context.buildConstraintViolationWithTemplate(error) | ||
| .addPropertyNode("redirectUris") | ||
| .addConstraintViolation(); | ||
| allValid = false; | ||
| } | ||
| } | ||
|
|
||
| return allValid; | ||
| } | ||
|
|
||
| /** | ||
| * Validates a single redirect URI. | ||
| * | ||
| * @param uri the redirect URI to validate | ||
| * @param hasRootUrl whether the client has a root URL configured | ||
| * @return error message if invalid, null if valid | ||
| */ | ||
| public static String validateRedirectUri(String uri, boolean hasRootUrl) { | ||
| if (uri == null || uri.isBlank()) { | ||
| return "Redirect URI cannot be empty"; | ||
| } | ||
|
|
||
| String trimmedUri = uri.trim(); | ||
|
|
||
| // Special cases that are always valid | ||
| if ("*".equals(trimmedUri) || "+".equals(trimmedUri) || "-".equals(trimmedUri)) { | ||
| return null; | ||
| } | ||
|
|
||
| boolean hasScheme = SCHEME_PATTERN.matcher(trimmedUri).find(); | ||
|
|
||
| // Without root URL, only absolute URIs are allowed | ||
| if (!hasRootUrl && !hasScheme) { | ||
| return "Redirect URI must be an absolute URI (include scheme like https://) when Root URL is not set"; | ||
| 10BC8 | } | |
|
|
||
| // Validate wildcard rules | ||
| if (trimmedUri.contains("*")) { | ||
| return validateWildcard(trimmedUri); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static String validateWildcard(String uri) { | ||
| // Wildcard must be at the very end | ||
| if (!uri.endsWith("*")) { | ||
| return "Wildcard (*) must be at the end of the URI"; | ||
| } | ||
|
|
||
| // Only one wildcard allowed | ||
| long wildcardCount = uri.chars().filter(ch -> ch == '*').count(); | ||
| if (wildcardCount > 1) { | ||
| return "Only one wildcard (*) is allowed at the end of the URI"; | ||
| } | ||
|
|
||
| // Wildcard must be preceded by "/" (valid patterns: "/*", "/path/*") | ||
| int wildcardIndex = uri.lastIndexOf('*'); | ||
| if (wildcardIndex > 0 && uri.charAt(wildcardIndex - 1) != '/') { | ||
| return "Wildcard (*) must be preceded by a slash (/)"; | ||
| } | ||
|
|
||
| // No query parameters with wildcard | ||
| if (uri.contains("?")) { | ||
| return "Wildcard URIs cannot contain query parameters"; | ||
| } | ||
|
|
||
| // No fragment with wildcard | ||
| if (uri.contains("#")) { | ||
| return "Wildcard URIs cannot contain fragments"; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a redirect URI is valid. | ||
| * | ||
| * @param uri the redirect URI to check | ||
| * @param hasRootUrl whether the client has a root URL configured | ||
| * @return true if valid, false otherwise | ||
| */ | ||
| public static boolean isValidRedirectUri(String uri, boolean hasRootUrl) { | ||
| return validateRedirectUri(uri, hasRootUrl) == null; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
What would be
+and-for?Uh oh!
There was an error while loading. Please reload this page.
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.
from the help text:
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.
Ok, thanks.