-
Notifications
You must be signed in to change notification settings - Fork 845
Fix: SAML metadata ACS URL ignores zone subdomain when entityBaseURL is set #3915
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
Open
fhanik
wants to merge
2
commits into
develop
Choose a base branch
from
pr/fix-saml-url-regression-from-pr-3739
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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 |
|---|---|---|
|
|
@@ -16,6 +16,10 @@ | |
| package org.cloudfoundry.identity.uaa.provider.saml; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.cloudfoundry.identity.uaa.util.UaaUrlUtils; | ||
| import org.cloudfoundry.identity.uaa.zone.IdentityZone; | ||
| import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder; | ||
| import org.cloudfoundry.identity.uaa.zone.ZonePathContextRewritingFilter; | ||
| import org.springframework.core.convert.converter.Converter; | ||
| import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; | ||
| import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository; | ||
|
|
@@ -85,7 +89,7 @@ public RelyingPartyRegistration resolve(HttpServletRequest request, String relyi | |
| if (relyingPartyRegistration == null) { | ||
| return null; | ||
| } else { | ||
| String baseUrl = StringUtils.hasText(entityBaseURL) ? entityBaseURL : getApplicationUri(request); | ||
| String baseUrl = resolveBaseUrl(entityBaseURL, request); | ||
| Function<String, String> templateResolver = this.templateResolver(baseUrl, relyingPartyRegistration); | ||
| String relyingPartyEntityId = templateResolver.apply(relyingPartyRegistration.getEntityId()); | ||
| String assertionConsumerServiceLocation = templateResolver.apply(relyingPartyRegistration.getAssertionConsumerServiceLocation()); | ||
|
|
@@ -152,6 +156,63 @@ private static Map<String, String> constructUriVariables(String baseUrl, Relying | |
| return uriVariables; | ||
| } | ||
|
|
||
| /** | ||
| * Determines the base URL used to expand {@code {baseUrl}} template variables in | ||
| * relying-party endpoint locations, accounting for both zone-access patterns: | ||
| * | ||
| * <ul> | ||
| * <li><b>Subdomain-based zones</b> ({@code http://zone.host/uaa/…}): when | ||
| * {@code entityBaseURL} is configured the zone subdomain is prepended to its | ||
| * host, e.g. {@code http://localhost:8080/uaa} → | ||
| * {@code http://zone.localhost:8080/uaa}.</li> | ||
| * <li><b>Path-based zones</b> ({@code http://host/z/{subdomain}/…}): | ||
| * {@link ZonePathContextRewritingFilter} has already rewritten | ||
| * {@code request.getContextPath()} to include {@code /z/{subdomain}}, so | ||
| * {@link #getApplicationUri} produces the correct zone-specific base URL. | ||
| * {@code entityBaseURL} is a static operator setting that cannot encode the | ||
| * per-zone path dynamically, therefore it is ignored for this access pattern.</li> | ||
| * </ul> | ||
| * | ||
| * When {@code entityBaseURL} is not configured the base URL is always derived from | ||
| * the incoming HTTP request, which already carries the correct zone information | ||
| * (subdomain in the {@code Host} header, or zone path in the rewritten context path). | ||
| */ | ||
| private static String resolveBaseUrl(String entityBaseURL, HttpServletRequest request) { | ||
| // Path-based zone: ZonePathContextRewritingFilter has embedded /z/{subdomain} | ||
| // into the context path. entityBaseURL is a static value that cannot reflect this | ||
| // dynamically, so always derive from the request for this access pattern. | ||
| if (isZonePathRequest(request)) { | ||
| return getApplicationUri(request); | ||
| } | ||
|
|
||
| if (!StringUtils.hasText(entityBaseURL)) { | ||
| return getApplicationUri(request); | ||
| } | ||
|
|
||
| // Subdomain-based zone with entityBaseURL configured: prepend the zone | ||
| // subdomain to the host so ACS/SLO endpoints resolve to the right virtual host. | ||
|
Comment on lines
+192
to
+193
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. this one as well |
||
| IdentityZone currentZone = IdentityZoneHolder.get(); | ||
| if (!currentZone.isUaa()) { | ||
| return UaaUrlUtils.addSubdomainToUrl(entityBaseURL, currentZone.getSubdomain()); | ||
| } | ||
| return entityBaseURL; | ||
| } | ||
|
|
||
| /** | ||
| * Returns {@code true} when the current request is being served under a path-based | ||
| * zone URL ({@code /z/{subdomain}/}), as signalled by the | ||
| * {@link ZonePathContextRewritingFilter#ZONE_ORIGINAL_CONTEXT_PATH} request attribute. | ||
| */ | ||
| private static boolean isZonePathRequest(HttpServletRequest request) { | ||
| Object origAttr = request.getAttribute(ZonePathContextRewritingFilter.ZONE_ORIGINAL_CONTEXT_PATH); | ||
| if (origAttr instanceof String originalContextPath) { | ||
| String contextPath = request.getContextPath(); | ||
| return contextPath != null | ||
| && contextPath.startsWith(originalContextPath + ZonePathContextRewritingFilter.ZONE_PATH_PREFIX); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static String getApplicationUri(HttpServletRequest request) { | ||
| UriComponents uriComponents = UriComponentsBuilder.fromUriString(UrlUtils.buildFullRequestUrl(request)).replacePath(request.getContextPath()).replaceQuery(null).fragment(null).build(); | ||
| return uriComponents.toUriString(); | ||
|
|
||
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.
Suggestion: This is already explained in the javadoc above and can be deleted.