/
LegacyRedirectResolverTest.java
778 lines (630 loc) · 36.1 KB
/
LegacyRedirectResolverTest.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
package org.cloudfoundry.identity.uaa.oauth.beans;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.security.oauth2.common.exceptions.RedirectMismatchException;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import static org.apache.logging.log4j.Level.WARN;
import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_AUTHORIZATION_CODE;
import static org.cloudfoundry.identity.uaa.util.AssertThrowsWithMessage.assertThrowsWithMessageThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* For additional tests, see also org.cloudfoundry.identity.uaa.oauth.beans.RedirectResolverTest
*
* @see RedirectResolverTest
*/
class LegacyRedirectResolverTest {
private final LegacyRedirectResolver resolver = new LegacyRedirectResolver();
private static ClientDetails createClient(String id, String... redirectUris) {
BaseClientDetails clientDetails = new BaseClientDetails();
clientDetails.setClientId(id);
clientDetails.setAuthorizedGrantTypes(Collections.singleton(GRANT_TYPE_AUTHORIZATION_CODE));
clientDetails.setRegisteredRedirectUri(new HashSet<>(Arrays.asList(redirectUris)));
return clientDetails;
}
private static String expectedWarning(String clientId, String requested, String configured) {
return String.format(LegacyRedirectResolver.MSG_TEMPLATE, clientId, requested, configured);
}
private static Matcher<LogEvent> warning(String msg) {
return new LogEventMatcher(WARN, msg, "a warning about implicit redirect matching");
}
private static class LogEventMatcher extends TypeSafeMatcher<LogEvent> {
private Level level;
private Matcher<String> msgMatcher;
private String matchFail;
LogEventMatcher(Level level, String msg, String matchFail) {
this.level = level;
this.msgMatcher = is(msg);
this.matchFail = matchFail;
}
@Override
protected boolean matchesSafely(LogEvent event) {
return event.getLevel().equals(level) && msgMatcher.matches(event.getMessage().getFormattedMessage());
}
@Override
public void describeTo(Description description) {
description.appendText(matchFail);
}
}
@Nested
class WithCapturedLogs {
private List<LogEvent> logEvents;
private AbstractAppender appender;
@BeforeEach
void setupLogger() {
logEvents = new ArrayList<>();
appender = new AbstractAppender("", null, null) {
@Override
public void append(LogEvent event) {
logEvents.add(event);
}
};
appender.start();
LoggerContext context = (LoggerContext) LogManager.getContext(false);
context.getRootLogger().addAppender(appender);
}
@AfterEach
void removeAppender() {
LoggerContext context = (LoggerContext) LogManager.getContext(false);
context.getRootLogger().removeAppender(appender);
}
@Test
void warnsOnImplicitDomainExpansion() {
final String configuredRedirectUri = "https://example.com";
final String requestedRedirectUri = "https://subdomain.example.com";
ClientDetails client = createClient("foo", configuredRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, hasItem(
warning(expectedWarning(client.getClientId(), requestedRedirectUri, configuredRedirectUri)))
);
}
@Test
void doesNotWarnOnEmptyRedirectUri() {
ClientDetails client = createClient("foo", "http://localhost");
resolver.resolveRedirect(null, client);
assertThat(logEvents, empty());
}
@Test
void warnsOnImplicitMultipleDomainExpansion() {
final String configuredRedirectUri = "https://example.com";
final String requestedRedirectUri = "https://another.subdomain.example.com";
ClientDetails client = createClient("foo", configuredRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, hasItem(
warning(expectedWarning(client.getClientId(), requestedRedirectUri, configuredRedirectUri)))
);
}
@Test
void warnsOnExplicitDomainExpansion() {
final String configuredRedirectUri = "https://*.example.com";
final String requestedRedirectUri = "https://subdomain.example.com";
ClientDetails client = createClient("foo", configuredRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, hasItem(
warning(expectedWarning(client.getClientId(), requestedRedirectUri, configuredRedirectUri)))
);
}
@Test
void warnsOnPortWildCard() {
final String configuredRedirectUri = "https://example.com:*/*";
final String requestedRedirectUri = "https://example.com:443/callback";
ClientDetails client = createClient("foo", configuredRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, hasItem(
warning(expectedWarning(client.getClientId(), requestedRedirectUri, configuredRedirectUri)))
);
}
@Test
void warnsOnImplicitPathExpansion() {
final String configuredRedirectUri = "https://example.com/";
final String requestedRedirectUri = "https://example.com/path";
ClientDetails client = createClient("foo", configuredRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, hasItem(warning(expectedWarning(client.getClientId(), requestedRedirectUri, configuredRedirectUri))));
}
@Test
void warnsOnImplicitMultiplePathExpansion() {
final String configuredRedirectUri = "https://example.com/";
final String requestedRedirectUri = "https://example.com/some/path";
ClientDetails client = createClient("foo", configuredRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, hasItem(warning(expectedWarning(client.getClientId(), requestedRedirectUri, configuredRedirectUri))));
}
@Test
void warnsOnExplicitPathExpansion() {
final String configuredRedirectUri = "https://example.com/*";
final String requestedRedirectUri = "https://example.com/path";
ClientDetails client = createClient("foo", configuredRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, hasItem(
warning(expectedWarning(client.getClientId(), requestedRedirectUri, configuredRedirectUri)))
);
}
@Test
void warnsOnAllConfiguredUrisWhichLegacyMatchButDoNotStrictlyMatch() {
final String configuredExplicitRedirectUri = "https://*.example.com/";
final String configuredImplicitRedirectUri = "https://example.com/";
final String requestedRedirectUri = "https://an.example.com/";
// the explicit redirect uri will match first, but we should still log
ClientDetails client = createClient("foo", configuredExplicitRedirectUri, configuredImplicitRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, hasItem(warning(expectedWarning(client.getClientId(), requestedRedirectUri, configuredImplicitRedirectUri))));
assertThat(logEvents, hasItem(warning(expectedWarning(client.getClientId(), requestedRedirectUri, configuredExplicitRedirectUri))));
}
@Test
void warnsOnlyAboutMatchingConfiguredUrisMWhenThereIsAMatch() {
final String configuredImplicitRedirectUri = "https://example.com";
final String configuredOtherRedirectUri = "https://other.com/";
final String requestedRedirectUri = "https://an.example.com/";
// the explicit redirect uri will match first, but we should still log
ClientDetails client = createClient("foo", configuredOtherRedirectUri, requestedRedirectUri, configuredImplicitRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, hasItem(warning(expectedWarning(client.getClientId(), requestedRedirectUri, configuredImplicitRedirectUri))));
// configured uri which matches both old and new resolvers is not logged
// and non-matching configured uri is also not logged
assertThat(logEvents.size(), is(1));
}
@Test
void redactsQueryParameterValues() {
final String configuredRedirectUri = "https://example.com/";
final String requestedRedirectUri = "https://example.com/path?foo=bar&foo=1234&baz=qux";
ClientDetails client = createClient("foo", configuredRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, hasItem(
warning(expectedWarning(client.getClientId(), "https://example.com/path?foo=REDACTED&foo=REDACTED&baz=REDACTED", configuredRedirectUri)))
);
}
@Test
void redactsHashFragment() {
final String configuredRedirectUri = "https://example.com";
final String requestedRedirectUri = "https://example.com/a/b#IAmAHash";
ClientDetails client = createClient("front-end-app", configuredRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, hasItem(
warning(expectedWarning(client.getClientId(), "https://example.com/a/b#REDACTED", configuredRedirectUri)))
);
}
@Test
void warnsOnImplicitAuthorizationExpansion() {
final String configuredRedirectUri = "https://example.com/";
final String requestedRedirectUri = "https://user:pass@example.com/";
ClientDetails client = createClient("myAppIsCool", configuredRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, hasItem(
warning(expectedWarning(client.getClientId(), "https://REDACTED:REDACTED@example.com/", configuredRedirectUri)))
);
}
@Test
void doesNotWarnForExactMatch() {
final String configuredRedirectUri = "https://example.com/";
final String requestedRedirectUri = "https://example.com/";
ClientDetails client = createClient("foo", configuredRedirectUri);
resolver.resolveRedirect(requestedRedirectUri, client);
assertThat(logEvents, empty());
}
@Test
void doesNotWarnForPortExpansion() {
final String configuredRedirectUri = "https://example.com/";
final String requestedRedirectUri = "https://example.com:65000/";
ClientDetails client = createClient("foo", configuredRedirectUri);
assertThrows(RedirectMismatchException.class,
() -> resolver.resolveRedirect(requestedRedirectUri, client));
assertThat(logEvents, empty());
}
@Test
void doesNotWarnWhenThereIsNoMatch() {
final String configuredRedirectUri = "https://example.com";
final String requestedRedirectUri = "https://other.com";
ClientDetails client = createClient("foo", configuredRedirectUri);
assertThrows(RedirectMismatchException.class,
() -> resolver.resolveRedirect(requestedRedirectUri, client));
assertThat(logEvents, empty());
}
}
@Nested
@DisplayName("matching http://domain.com")
class WhenMatchingAgainstJustTLD {
private final String clientRedirectUri = "http://domain.com";
@Test
void allSubdomainsShouldMatch() {
assertTrue(resolver.redirectMatches("http://subdomain.domain.com", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://another-subdomain.domain.com", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://one.two.domain.com", clientRedirectUri));
}
@Test
void allPathsShouldMatch() {
assertTrue(resolver.redirectMatches("http://domain.com/one", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://domain.com/another", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://domain.com/one/two", clientRedirectUri));
}
@Test
void allPathsInAnySubdomainShouldMatch() {
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/another", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one/two", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://another-subdomain.domain.com/one", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://another-subdomain.domain.com/another", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://another-subdomain.domain.com/one/two", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://one.two.domain.com/one", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://one.two.domain.com/another", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://one.two.domain.com/one/two", clientRedirectUri));
}
@Test
void doesNotMatchDifferentTld() {
assertFalse(resolver.redirectMatches("http://other-domain.com", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://domain.io", clientRedirectUri));
}
@Test
void doesNotMatchDifferentProtocol() {
assertFalse(resolver.redirectMatches("https://domain.com", clientRedirectUri));
assertFalse(resolver.redirectMatches("ws://domain.com", clientRedirectUri));
}
}
@Nested
@DisplayName("matching http://domain.com/*")
class WhenMatchingWithSinglePathPattern {
private final String clientRedirectUri = "http://domain.com/*";
@Test
void shouldNotMatchSubdomains() {
assertFalse(resolver.redirectMatches("http://subdomain.domain.com", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://another-subdomain.domain.com", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://one.two.domain.com", clientRedirectUri));
}
@Test
void allPathsShouldMatch() {
assertTrue(resolver.redirectMatches("http://domain.com/one", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://domain.com/another", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://domain.com/one/two", clientRedirectUri));
}
@Test
void shouldNotMatchSubdomainsWithPaths() {
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/one", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/another", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/one/two", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://another-subdomain.domain.com/one", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://another-subdomain.domain.com/another", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://another-subdomain.domain.com/one/two", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://one.two.domain.com/one", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://one.two.domain.com/another", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://one.two.domain.com/one/two", clientRedirectUri));
}
@Test
void doesNotMatchDifferentTld() {
assertFalse(resolver.redirectMatches("http://other-domain.com", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://domain.io", clientRedirectUri));
}
@Test
void doesNotMatchDifferentProtocol() {
assertFalse(resolver.redirectMatches("https://domain.com", clientRedirectUri));
assertFalse(resolver.redirectMatches("ws://domain.com", clientRedirectUri));
}
}
@Nested
@DisplayName("matching http://domain.com/**")
class WhenMatchingWithAllSubPathsPattern {
private final String clientRedirectUri = "http://domain.com/**";
@Test
void shouldNotMatchSubdomains() {
assertFalse(resolver.redirectMatches("http://subdomain.domain.com", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://another-subdomain.domain.com", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://one.two.domain.com", clientRedirectUri));
}
@Test
void allPathsShouldMatch() {
assertTrue(resolver.redirectMatches("http://domain.com/one", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://domain.com/another", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://domain.com/one/two", clientRedirectUri));
}
@Test
void shouldNotMatchSubdomainsWithPaths() {
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/one", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/another", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/one/two", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://another-subdomain.domain.com/one", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://another-subdomain.domain.com/another", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://another-subdomain.domain.com/one/two", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://one.two.domain.com/one", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://one.two.domain.com/another", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://one.two.domain.com/one/two", clientRedirectUri));
}
@Test
void doesNotMatchDifferentTld() {
assertFalse(resolver.redirectMatches("http://other-domain.com", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://domain.io", clientRedirectUri));
}
@Test
void doesNotMatchDifferentProtocol() {
assertFalse(resolver.redirectMatches("https://domain.com", clientRedirectUri));
assertFalse(resolver.redirectMatches("ws://domain.com", clientRedirectUri));
}
}
@Nested
@DisplayName("redirectMatches")
class RedirectMatches {
private final String requestedRedirectHttp = "http://subdomain.domain.com/path1/path2?query1=value1&query2=value2";
private final String requestedRedirectHttps = "https://subdomain.domain.com/path1/path2?query1=value1&query2=value2";
@Test
void trailingSlash() {
final String clientRedirectUri = "http://subdomain.domain.com/";
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one/", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one/two", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one/two/three", clientRedirectUri));
}
@Test
void trailingPath() {
final String clientRedirectUri = "http://subdomain.domain.com/one";
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one/", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one/two", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one/two/three", clientRedirectUri));
}
@Test
void singleTrailingAsterisk() {
final String clientRedirectUri = "http://subdomain.domain.com/*";
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/one/", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/one/two", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/one/two/three", clientRedirectUri));
}
@Test
void singleTrailingAsterisk_withPath() {
final String clientRedirectUri = "http://subdomain.domain.com/one*";
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one-foo-bar", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/one/", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/one/two", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/one/two/three", clientRedirectUri));
}
@Test
void singleAsterisk_insidePath() {
String clientRedirectUri = "http://subdomain.domain.com/one/*/four";
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/one/four", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/one/middle/four", clientRedirectUri));
assertFalse(resolver.redirectMatches("http://subdomain.domain.com/one/two/three/four", clientRedirectUri));
}
@Test
void matchesSchemeWildcard() {
String clientRedirectUri = "http*://subdomain.domain.com/**";
assertTrue(resolver.redirectMatches(requestedRedirectHttp, clientRedirectUri));
assertTrue(resolver.redirectMatches(requestedRedirectHttps, clientRedirectUri));
}
@Test
void matchesSchemeHttp() {
String clientRedirectUri = "http://subdomain.domain.com/**";
assertTrue(resolver.redirectMatches(requestedRedirectHttp, clientRedirectUri));
assertFalse(resolver.redirectMatches(requestedRedirectHttps, clientRedirectUri));
}
@Test
void matchesSchemeHttps() {
String clientRedirectUri = "https://subdomain.domain.com/**";
assertFalse(resolver.redirectMatches(requestedRedirectHttp, clientRedirectUri));
assertTrue(resolver.redirectMatches(requestedRedirectHttps, clientRedirectUri));
}
@Test
void matchesSchemeCustom() {
assertTrue(resolver.redirectMatches("myapp://callback", "myapp://callback"));
assertTrue(resolver.redirectMatches("myapp://callback#token=xyz123", "myapp://callback*"));
}
@Test
void matchesPathContainingAntPathMatcher() {
String clientRedirectUri = "http*://subdomain.domain.com/path1/path2**";
assertTrue(resolver.redirectMatches(requestedRedirectHttp, clientRedirectUri));
assertTrue(resolver.redirectMatches(requestedRedirectHttps, clientRedirectUri));
clientRedirectUri = "http*://subdomain.domain.com/path1/<invalid>**";
assertFalse(resolver.redirectMatches(requestedRedirectHttp, clientRedirectUri));
assertFalse(resolver.redirectMatches(requestedRedirectHttps, clientRedirectUri));
}
@Test
void matchesHashFragments() {
assertTrue(resolver.redirectMatches("http://uaa.com/#fragment", "http://uaa.com"));
}
@Test
void redirectSubdomain() {
String clientRedirectUri = "http*://*.domain.com/path1/path2**";
assertTrue(resolver.redirectMatches(requestedRedirectHttps, clientRedirectUri));
assertTrue(resolver.redirectMatches(requestedRedirectHttp, clientRedirectUri));
clientRedirectUri = "http*://*.domain.com/path1/<invalid>**";
assertFalse(resolver.redirectMatches(requestedRedirectHttps, clientRedirectUri));
assertFalse(resolver.redirectMatches(requestedRedirectHttp, clientRedirectUri));
}
@Test
void redirectSupportsMultipleSubdomainWildcards() {
String clientRedirectUri = "http://*.*.domain.com/";
assertTrue(resolver.redirectMatches("http://sub1.sub2.domain.com/", clientRedirectUri));
}
@Test
void subdomainMatchingRejectsDomainRedirectOnWildcardSubdomain() {
String clientRedirectUri = "http://*.domain.com/";
assertFalse(resolver.redirectMatches("http://other-domain.com?stuff.domain.com/", clientRedirectUri));
}
@Test
void subdomainMatchingRejectsDomainRedirectOnMultilevelWildcardSubdomain() {
String clientRedirectUri = "http://**.domain.com/";
assertFalse(resolver.redirectMatches("http://other-domain.com?stuff.domain.com/", clientRedirectUri));
}
@Test
void subdomainMatchingRejectsDomainRedirectOnWildcardSuffixedSubdomain() {
String clientRedirectUri = "http://sub*.example.com";
assertFalse(resolver.redirectMatches("http://sub.other-domain.com?stuff.example.com", clientRedirectUri));
}
@Test
void subdomainMatchingDoesNotBlowUpWhenRequestedRedirectIsShorterThanConfiguredRedirect() {
String clientRedirectUri = "http://sub*.domain.com/";
assertFalse(resolver.redirectMatches("http://domain.com/", clientRedirectUri));
}
@Test
void subdomainMatchingOnWildcardSubdomainWithBasicAuth() {
String clientRedirectUri = "http://u:p@*.domain.com/";
assertTrue(resolver.redirectMatches("http://u:p@sub.domain.com/", clientRedirectUri));
}
@Test
void matchesHostsWithPort() {
String clientRedirectUri = "http://*.domain.com:8080/";
assertTrue(resolver.redirectMatches("http://any.domain.com:8080/", clientRedirectUri));
}
@Test
void subdomainMatchingRejectsDomainRedirectOnAntPathVariableSubdomain() {
String clientRedirectUri = "http://foo.*.domain.com/";
assertFalse(resolver.redirectMatches("http://other-domain.com?stuff.domain.com/", clientRedirectUri));
}
@Test
void matchesPortWithWildcardPort() {
final String clientRedirectUri = "https://example.com:*/";
assertTrue(resolver.redirectMatches("https://example.com:65000/", clientRedirectUri));
}
@Test
void matchesPortWithWildcardPortAndPath() {
final String clientRedirectUri = "https://example.com:*/**";
assertTrue(resolver.redirectMatches("https://example.com:65000/path/subpath", clientRedirectUri));
}
@Test
void matchesEmptyPortWithWildcardPort() {
final String clientRedirectUri = "https://example.com:*/";
assertTrue(resolver.redirectMatches("https://example.com:80/", clientRedirectUri));
assertFalse(resolver.redirectMatches("https://example.com/", clientRedirectUri));
}
@Test
void matchesEmptyPortWithWildcardPortAndPath() {
final String clientRedirectUri = "https://example.com:*/**";
assertTrue(resolver.redirectMatches("https://example.com:80/path1/path2/path3", clientRedirectUri));
assertFalse(resolver.redirectMatches("https://example.com/path1/path2/path3", clientRedirectUri));
}
@Test
public void testIllegalUnderscoreDomain() {
final String clientRedirectUri = "http*://*.example.com/**";
assertFalse(resolver.redirectMatches("https://invalid_redirect.example.com/login/callback", clientRedirectUri));
}
@Test
public void testLegalDomain() {
final String clientRedirectUri = "http*://*.example.com/**";
assertTrue(resolver.redirectMatches("https://valid-redirect.example.com/login/callback", clientRedirectUri));
}
}
@Nested
@DisplayName("with caps")
class RedirectMatchesCaps {
@Test
void withClientHostCaps() {
final String clientRedirectUri = "http://SubDomain.Domain.com";
final String clientRedirectUriPort = "http://SubDomain.Domain.com:8080";
final String clientRedirectUriPath = "http://SubDomain.Domain.com/bee/Bop";
final String clientRedirectUriQuery = "http://SubDomain.Domain.com?rock=Steady";
final String clientRedirectUriFragment = "http://SubDomain.Domain.com";
assertTrue(resolver.redirectMatches("http://subdomain.domain.com", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com:8080", clientRedirectUriPort));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com/bee/Bop", clientRedirectUriPath));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com?rock=Steady", clientRedirectUriQuery));
assertTrue(resolver.redirectMatches("http://subdomain.domain.com#Shredder", clientRedirectUriFragment));
}
@Test
void withRequestedHostCaps() {
final String clientRedirectUri = "http://subdomain.domain.com";
final String clientRedirectUriPort = "http://subdomain.domain.com:8080";
final String clientRedirectUriPath = "http://subdomain.domain.com/bee/Bop";
final String clientRedirectUriQuery = "http://subdomain.domain.com?rock=Steady";
final String clientRedirectUriFragment = "http://subdomain.domain.com";
assertTrue(resolver.redirectMatches("http://sUBdOMAIN.dOMAIN.com", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://sUBdOMAIN.dOMAIN.com:8080", clientRedirectUriPort));
assertTrue(resolver.redirectMatches("http://sUBdOMAIN.dOMAIN.com/bee/Bop", clientRedirectUriPath));
assertTrue(resolver.redirectMatches("http://sUBdOMAIN.dOMAIN.com?rock=Steady", clientRedirectUriQuery));
assertTrue(resolver.redirectMatches("http://sUBdOMAIN.dOMAIN.com#Shredder", clientRedirectUriFragment));
}
@Test
void withWildCardHostCaps() {
final String clientRedirectUri = "http://SubDomain.Domain.com/**";
final String clientRedirectUriPort = "http://SubDomain.Domain.com:8080/**";
final String clientRedirectUriPath = "http://SubDomain.Domain.com/bee/Bop/**";
assertTrue(resolver.redirectMatches("http://sUBdOMAIN.dOMAIN.com", clientRedirectUri));
assertTrue(resolver.redirectMatches("http://sUBdOMAIN.dOMAIN.com:8080/", clientRedirectUriPort));
assertTrue(resolver.redirectMatches("http://sUBdOMAIN.dOMAIN.com/bee/Bop/", clientRedirectUriPath));
}
}
@Nested
class ResolveRedirect {
private ClientDetails mockClientDetails;
@BeforeEach
void setUp() {
mockClientDetails = mock(BaseClientDetails.class);
when(mockClientDetails.getAuthorizedGrantTypes()).thenReturn(Collections.singleton(GRANT_TYPE_AUTHORIZATION_CODE));
}
@Test
void clientMissingRedirectUri() {
when(mockClientDetails.getRegisteredRedirectUri()).thenReturn(new HashSet<>());
assertThrowsWithMessageThat(RedirectMismatchException.class,
() -> resolver.resolveRedirect("http://somewhere.com", mockClientDetails),
containsString("Client registration is missing redirect_uri"));
}
@Test
void clientWithInvalidRedirectUri() {
final String invalidRedirectUri = "*, */*";
mockRegisteredRedirectUri(invalidRedirectUri);
RedirectMismatchException exception = assertThrows(RedirectMismatchException.class,
() -> resolver.resolveRedirect("http://somewhere.com", mockClientDetails));
assertThat(exception.getMessage(), containsString("Client registration contains invalid redirect_uri"));
assertThat(exception.getMessage(), containsString(invalidRedirectUri));
}
private void mockRegisteredRedirectUri(String allowedRedirectUri) {
when(mockClientDetails.getRegisteredRedirectUri()).thenReturn(Collections.singleton(allowedRedirectUri));
}
}
@Nested
class PathTraversalBypass {
private static final String BASE_URI = "http://example.com/foo";
@ParameterizedTest(name = "\"" + BASE_URI + "{0}\" should match both \"" + BASE_URI + "\" and \"" + BASE_URI + "/**\"")
@ValueSource(strings = {
"/./bar",
"/%2e/bar", // %2e is . url encoded
"/%252e/bar", // %25 is % url encoded
"/%2525252e/bar", // path may be url decoded multiple times when passing through web servers, proxies and browser
})
void singleDotTraversal(String requestedSuffix) {
assertTrue(resolver.redirectMatches(BASE_URI + requestedSuffix, BASE_URI));
assertTrue(resolver.redirectMatches(BASE_URI + requestedSuffix, BASE_URI + "/**"));
}
@ParameterizedTest(name = "\"" + BASE_URI + "{0}\" should not match \"" + BASE_URI + "\" or \"" + BASE_URI + "/**\"")
@ValueSource(strings = {
"/../bar",
"/%2e./bar", // %2e is . url encoded
"/%252e./bar", // %25 is % url encoded
"/%2525252e./bar", // path may be url decoded multiple times when passing through web servers, proxies and browser
"/%25252525252525252525252e/bar",
"/%25252525252525252525252e./bar",
})
void doubleDotTraversal(String requestedSuffix) {
assertFalse(resolver.redirectMatches(BASE_URI + requestedSuffix, BASE_URI));
assertFalse(resolver.redirectMatches(BASE_URI + requestedSuffix, BASE_URI + "/**"));
}
}
}