30
30
package com .iplanet .services .cdc ;
31
31
32
32
import com .iplanet .dpro .session .SessionException ;
33
- import com .iplanet .dpro .session .service .SessionService ;
34
33
import com .iplanet .dpro .session .TokenRestriction ;
35
-
34
+ import com .iplanet .dpro .session .service .SessionService ;
35
+ import com .iplanet .services .naming .WebtopNaming ;
36
36
import com .iplanet .sso .SSOException ;
37
37
import com .iplanet .sso .SSOToken ;
38
38
import com .iplanet .sso .SSOTokenManager ;
63
63
import com .sun .identity .saml .protocol .StatusCode ;
64
64
import com .sun .identity .shared .Constants ;
65
65
import com .sun .identity .shared .DateUtils ;
66
+ import com .sun .identity .shared .configuration .SystemPropertiesManager ;
66
67
import com .sun .identity .shared .debug .Debug ;
67
68
import com .sun .identity .shared .encode .Base64 ;
68
69
import com .sun .identity .shared .encode .CookieUtils ;
69
70
import com .sun .identity .shared .encode .URLEncDec ;
70
- import com .sun .identity .shared .configuration .SystemPropertiesManager ;
71
71
import com .sun .identity .sm .SMSEntry ;
72
- import org .forgerock .guice .core .InjectorHolder ;
73
- import org .forgerock .openam .ldap .LDAPUtils ;
74
72
75
73
import java .io .IOException ;
74
+ import java .net .HttpURLConnection ;
76
75
import java .net .InetAddress ;
77
76
import java .net .UnknownHostException ;
78
77
import java .text .ParseException ;
81
80
import java .util .Enumeration ;
82
81
import java .util .HashSet ;
83
82
import java .util .Iterator ;
84
- import java .util .StringTokenizer ;
85
83
import java .util .List ;
86
84
import java .util .Set ;
85
+ import java .util .StringTokenizer ;
87
86
import java .util .logging .Level ;
88
87
import javax .servlet .RequestDispatcher ;
89
88
import javax .servlet .ServletConfig ;
94
93
import javax .servlet .http .HttpServletRequest ;
95
94
import javax .servlet .http .HttpServletResponse ;
96
95
96
+ import org .forgerock .guice .core .InjectorHolder ;
97
+ import org .forgerock .openam .ldap .LDAPUtils ;
98
+ import org .forgerock .openam .utils .StringUtils ;
99
+
97
100
/**
98
101
* The <code>CDCServlet</code> is the heart of the Cross Domain Single
99
102
* Signon mechanism of OpenAM.
@@ -640,7 +643,25 @@ private void redirectForAuthentication(
640
643
dispatcher .forward (request , response );
641
644
}
642
645
} else {
643
- // Redirect the user to the authenticated URL
646
+ // Redirect the user to the OpenAM host that they originally authenticated against. The authURL is
647
+ // set by AuthClientUtils#setHostUrlCookie when in restricted cookie mode. It's not entirely clear
648
+ // exactly what this use-case is for, but we should validate the cookie against the known server list
649
+ // to prevent an unvalidated redirect.
650
+ boolean valid = false ;
651
+ for (String serverId : WebtopNaming .getAllServerIDs ()) {
652
+ String serverUrl = WebtopNaming .getServerFromID (serverId );
653
+ serverUrl = serverUrl .substring (0 , serverUrl .length () - deployDescriptor .length ());
654
+ if (StringUtils .compareCaseInsensitiveString (serverUrl , authURL )) {
655
+ valid = true ;
656
+ break ;
657
+ }
658
+ }
659
+
660
+ if (!valid ) {
661
+ response .sendError (HttpURLConnection .HTTP_BAD_REQUEST , "Invalid cookie" );
662
+ return ;
663
+ }
664
+
644
665
redirectURL .append (authURL ).append (deployDescriptor )
645
666
.append (CDCURI ).append (QUESTION_MARK )
646
667
.append (request .getQueryString ());
@@ -649,24 +670,16 @@ private void redirectForAuthentication(
649
670
* Reset the cookie value to null, to avoid continous loop
650
671
* when a load balancer is used.
651
672
*/
652
- if (authCookie != null ) {
653
- authCookie .setValue ("" );
654
- response .addCookie (authCookie );
655
- }
673
+ authCookie .setValue ("" );
674
+ response .addCookie (authCookie );
656
675
response .sendRedirect (redirectURL .toString ());
657
676
}
658
677
659
678
if (debug .messageEnabled ()) {
660
679
debug .message ("Forwarding for authentication to: " +
661
680
redirectURL );
662
681
}
663
- } catch (IOException e ) {
664
- debug .error ("CDCServlet.redirectForAuthentication" , e );
665
- showError (response );
666
- } catch (ServletException e ) {
667
- debug .error ("CDCServlet.redirectForAuthentication" , e );
668
- showError (response );
669
- } catch (IllegalStateException e ) {
682
+ } catch (Exception e ) {
670
683
debug .error ("CDCServlet.redirectForAuthentication" , e );
671
684
showError (response );
672
685
}
0 commit comments