/
LegacyRedirectResolver.java
288 lines (239 loc) · 12.7 KB
/
LegacyRedirectResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package org.cloudfoundry.identity.uaa.oauth.beans;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.cloudfoundry.identity.uaa.util.UaaUrlUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.common.exceptions.RedirectMismatchException;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.AbstractMap.SimpleEntry;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.Collections.emptySet;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static org.cloudfoundry.identity.uaa.util.UaaUrlUtils.normalizeUri;
import static org.springframework.util.StringUtils.isEmpty;
public class LegacyRedirectResolver extends org.cloudfoundry.identity.uaa.oauth.beans.org.springframework.security.oauth2.provider.endpoint.DefaultRedirectResolver {
private static final Logger logger = LoggerFactory.getLogger(LegacyRedirectResolver.class);
public static final String LEGACY_PORT_WILDCAR = ":*";
static final String MSG_TEMPLATE = "OAuth client %s is configured with a redirect_uri which performs implicit or " +
"wildcard matching in legacy redirect uri matching mode. In this instance, the requested uri %s matches the " +
"configured uri %s. Please consider configuring your requested redirect uri to exactly match the " +
"redirect_uri for this client.";
private final SpecCompliantRedirectMatcher specCompliantRedirectMatcher = new SpecCompliantRedirectMatcher();
@Override
protected boolean redirectMatches(String requestedRedirect, String clientRedirect) {
try {
String normalizedRequestedRedirect = normalizeUri(requestedRedirect);
String normalizedClientRedirect = normalizeWildcardUri(clientRedirect);
URI requestedRedirectURI = URI.create(normalizedRequestedRedirect);
ClientRedirectUriPattern clientRedirectUri = new ClientRedirectUriPattern(normalizedClientRedirect);
if (!clientRedirectUri.isValidRedirect()) {
logger.error(String.format("Invalid redirect uri: %s", normalizedClientRedirect));
return false;
}
if (clientRedirectUri.isWildcard(normalizedClientRedirect) &&
clientRedirectUri.isSafeRedirect(requestedRedirectURI) &&
clientRedirectUri.match(requestedRedirectURI)) {
return true;
}
return super.redirectMatches(normalizedRequestedRedirect, normalizedClientRedirect);
} catch (IllegalArgumentException e) {
logger.error(
String.format("Could not validate whether requestedRedirect (%s) matches clientRedirectUri (%s)",
requestedRedirect,
clientRedirect),
e);
return false;
}
}
@Override
public String resolveRedirect(String requestedRedirect, ClientDetails client) throws OAuth2Exception {
Set<String> registeredRedirectUris = ofNullable(client.getRegisteredRedirectUri()).orElse(emptySet());
if (registeredRedirectUris.isEmpty()) {
throw new RedirectMismatchException("Client registration is missing redirect_uri");
}
List<String> invalidUrls = registeredRedirectUris.stream()
.filter(url -> !UaaUrlUtils.isValidRegisteredRedirectUrl(url))
.collect(toList());
if (!invalidUrls.isEmpty()) {
throw new RedirectMismatchException("Client registration contains invalid redirect_uri: " + invalidUrls);
}
String resolveRedirect = super.resolveRedirect(requestedRedirect, client);
// This legacy resolver decided that the requested redirect URI was a match for one
// of the configured redirect uris (i.e. super.resolveRedirect() did not throw), so
// check to see if we need to log some warnings before returning.
logConfiguredRedirectUrisWhichOnlyMatchFuzzily(client.getClientId(), registeredRedirectUris, requestedRedirect);
return resolveRedirect;
}
private void logConfiguredRedirectUrisWhichOnlyMatchFuzzily(String clientId, Set<String> registeredRedirectUris, String requestedRedirect) {
// For each registered redirect uri considered to be a match by this class, log a warning
// when the standard Spring library class disagrees (i.e. when it acts more strictly).
registeredRedirectUris.stream()
.filter(registeredRedirectUri ->
registeredRedirectUri.contains(LEGACY_PORT_WILDCAR) ||
(requestedRedirect != null &&
this.redirectMatches(requestedRedirect, registeredRedirectUri) &&
!specCompliantRedirectMatcher.redirectMatches(requestedRedirect, registeredRedirectUri))
)
.forEach(registeredRedirectUri ->
logger.warn(String.format(MSG_TEMPLATE, clientId,
redactSensitiveInformation(requestedRedirect), registeredRedirectUri)
)
);
}
private static String normalizeWildcardUri(String uriClient) {
boolean hasWildcarPort = uriClient.contains(LEGACY_PORT_WILDCAR);
String uri = hasWildcarPort ? uriClient.replace(LEGACY_PORT_WILDCAR, StringUtils.EMPTY) : uriClient;
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri);
UriComponents nonNormalizedUri = uriComponentsBuilder.build();
try {
if (nonNormalizedUri.getHost() != null) {
uriComponentsBuilder.host(nonNormalizedUri.getHost().toLowerCase());
}
if (nonNormalizedUri.getScheme() != null) {
uriComponentsBuilder.scheme(nonNormalizedUri.getScheme().toLowerCase());
}
if (hasWildcarPort) {
uriComponentsBuilder.port(99999);
}
} catch (NullPointerException e) {
throw new IllegalArgumentException("URI host and scheme must not be null");
}
return uriComponentsBuilder.build().toString().replace(":99999", LEGACY_PORT_WILDCAR);
}
private static String redactSensitiveInformation(String uri) {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uri);
redactQueryParams(builder);
redactUserInfo(builder);
redactHashFragment(builder);
return builder.toUriString();
}
private static void redactQueryParams(UriComponentsBuilder builder) {
MultiValueMap<String, String> originalParams = builder.build().getQueryParams();
Map<String, List<String>> redactedParams = originalParams.entrySet()
.stream()
.map(e -> new SimpleEntry<>(e.getKey(), e.getValue().stream().map(v -> "REDACTED").collect(toList())))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
builder.replaceQueryParams(new LinkedMultiValueMap<>(redactedParams));
}
private static void redactUserInfo(UriComponentsBuilder builder) {
String userInfo = builder.build().getUserInfo();
if (!isEmpty(userInfo)) {
builder.userInfo("REDACTED:REDACTED");
}
}
private static void redactHashFragment(UriComponentsBuilder builder) {
if (!isEmpty(builder.build().getFragment())) {
builder.fragment("REDACTED");
}
}
private class SpecCompliantRedirectMatcher {
private final CurrentVersionOfSpringResolverWithMethodExposedAndSubdomainsOff matcher =
new CurrentVersionOfSpringResolverWithMethodExposedAndSubdomainsOff();
boolean redirectMatches(String requestedRedirect, String redirectUri) {
return matcher.redirectMatches(requestedRedirect, redirectUri);
}
private class CurrentVersionOfSpringResolverWithMethodExposedAndSubdomainsOff extends org.springframework.security.oauth2.provider.endpoint.DefaultRedirectResolver {
CurrentVersionOfSpringResolverWithMethodExposedAndSubdomainsOff() {
super();
setMatchSubdomains(false);
}
public boolean redirectMatches(String requestedRedirect, String redirectUri) {
return super.redirectMatches(requestedRedirect, redirectUri);
}
}
}
private static class ClientRedirectUriPattern {
// The URI spec provides a regex for matching URI parts
// https://tools.ietf.org/html/rfc3986#appendix-B
private static final Pattern URI_EXTRACTOR =
Pattern.compile("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");
private static final int URI_EXTRACTOR_AUTHORITY_GROUP = 4; // "Authority" means "user:password@example.com"
private static final String WILDCARD_PORT = "99999";
private static final String WILDCARD_PORT_PATTERN = ":" + WILDCARD_PORT;
private Matcher redirectMatcher;
private boolean isValidRedirect = true;
private boolean hasWildcardPort = false;
private AntPathMatcher matcher;
private String redirectUri;
ClientRedirectUriPattern(String redirectUri) {
if (redirectUri == null) {
throw new IllegalArgumentException("Client Redirect URI was null");
}
this.redirectUri = redirectUri;
matcher = new AntPathMatcher();
this.redirectMatcher = URI_EXTRACTOR.matcher(redirectUri);
if (!redirectMatcher.matches()) {
isValidRedirect = false;
}
this.hasWildcardPort = isWildcardPort(redirectUri);
}
boolean isSafeRedirect(URI requestedRedirect) {
// We iterate backwards through the hosts to make sure the TLD and domain match
String[] configuredRedirectHost = splitAndReverseHost(getHost());
String[] requestedRedirectHost = splitAndReverseHost((Optional.ofNullable(requestedRedirect.getHost()).orElse("")));
if (requestedRedirectHost.length < configuredRedirectHost.length) {
return false;
}
boolean isSafe = true;
for (int i = 0; i < configuredRedirectHost.length && !isWildcard(configuredRedirectHost[i]); i++) {
isSafe = isSafe && configuredRedirectHost[i].equals(requestedRedirectHost[i]);
}
return isSafe;
}
boolean isValidRedirect() {
return isValidRedirect;
}
boolean match(URI requestedRedirect) {
if(hasWildcardPort) {
if(requestedRedirect.getPort() > 0) {
return matcher.match(redirectUri, requestedRedirect.toString().replace(String.valueOf(requestedRedirect.getPort()), WILDCARD_PORT));
} else {
return matcher.match(redirectUri.replace(WILDCARD_PORT_PATTERN, StringUtils.EMPTY), requestedRedirect.toString());
}
}
return matcher.match(redirectUri, requestedRedirect.toString());
}
private boolean isWildcard(String configuredRedirectPattern) {
return configuredRedirectPattern.contains("*") || hasWildcardPort;
}
private boolean isWildcardPort(String configuredRedirectPattern) {
return configuredRedirectPattern.contains(WILDCARD_PORT_PATTERN);
}
private String getHost() {
String authority = redirectMatcher.group(URI_EXTRACTOR_AUTHORITY_GROUP);
return stripPort(stripAuthority(authority));
}
private String stripAuthority(String authority) {
if (authority.contains("@")) {
return authority.split("@")[1];
}
return authority;
}
private String stripPort(String hostAndPort) {
if (hostAndPort.contains(":")) {
return hostAndPort.split(":")[0];
}
return hostAndPort;
}
private static String[] splitAndReverseHost(String host) {
String[] parts = host.split("\\.");
ArrayUtils.reverse(parts);
return parts;
}
}
}