Skip to content

Commit 317ee9b

Browse files
author
Neil Madden
committed
OPENAM-7362
1 parent 400629b commit 317ee9b

File tree

1 file changed

+31
-18
lines changed
  • openam-federation/OpenFM/src/main/java/com/iplanet/services/cdc

1 file changed

+31
-18
lines changed

openam-federation/OpenFM/src/main/java/com/iplanet/services/cdc/CDCServlet.java

+31-18
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
package com.iplanet.services.cdc;
3131

3232
import com.iplanet.dpro.session.SessionException;
33-
import com.iplanet.dpro.session.service.SessionService;
3433
import com.iplanet.dpro.session.TokenRestriction;
35-
34+
import com.iplanet.dpro.session.service.SessionService;
35+
import com.iplanet.services.naming.WebtopNaming;
3636
import com.iplanet.sso.SSOException;
3737
import com.iplanet.sso.SSOToken;
3838
import com.iplanet.sso.SSOTokenManager;
@@ -63,16 +63,15 @@
6363
import com.sun.identity.saml.protocol.StatusCode;
6464
import com.sun.identity.shared.Constants;
6565
import com.sun.identity.shared.DateUtils;
66+
import com.sun.identity.shared.configuration.SystemPropertiesManager;
6667
import com.sun.identity.shared.debug.Debug;
6768
import com.sun.identity.shared.encode.Base64;
6869
import com.sun.identity.shared.encode.CookieUtils;
6970
import com.sun.identity.shared.encode.URLEncDec;
70-
import com.sun.identity.shared.configuration.SystemPropertiesManager;
7171
import com.sun.identity.sm.SMSEntry;
72-
import org.forgerock.guice.core.InjectorHolder;
73-
import org.forgerock.openam.ldap.LDAPUtils;
7472

7573
import java.io.IOException;
74+
import java.net.HttpURLConnection;
7675
import java.net.InetAddress;
7776
import java.net.UnknownHostException;
7877
import java.text.ParseException;
@@ -81,9 +80,9 @@
8180
import java.util.Enumeration;
8281
import java.util.HashSet;
8382
import java.util.Iterator;
84-
import java.util.StringTokenizer;
8583
import java.util.List;
8684
import java.util.Set;
85+
import java.util.StringTokenizer;
8786
import java.util.logging.Level;
8887
import javax.servlet.RequestDispatcher;
8988
import javax.servlet.ServletConfig;
@@ -94,6 +93,10 @@
9493
import javax.servlet.http.HttpServletRequest;
9594
import javax.servlet.http.HttpServletResponse;
9695

96+
import org.forgerock.guice.core.InjectorHolder;
97+
import org.forgerock.openam.ldap.LDAPUtils;
98+
import org.forgerock.openam.utils.StringUtils;
99+
97100
/**
98101
* The <code>CDCServlet</code> is the heart of the Cross Domain Single
99102
* Signon mechanism of OpenAM.
@@ -640,7 +643,25 @@ private void redirectForAuthentication(
640643
dispatcher.forward(request, response);
641644
}
642645
} 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+
644665
redirectURL.append(authURL).append(deployDescriptor)
645666
.append(CDCURI).append(QUESTION_MARK)
646667
.append(request.getQueryString());
@@ -649,24 +670,16 @@ private void redirectForAuthentication(
649670
* Reset the cookie value to null, to avoid continous loop
650671
* when a load balancer is used.
651672
*/
652-
if (authCookie != null) {
653-
authCookie.setValue("");
654-
response.addCookie(authCookie);
655-
}
673+
authCookie.setValue("");
674+
response.addCookie(authCookie);
656675
response.sendRedirect(redirectURL.toString());
657676
}
658677

659678
if (debug.messageEnabled()) {
660679
debug.message("Forwarding for authentication to: " +
661680
redirectURL);
662681
}
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) {
670683
debug.error("CDCServlet.redirectForAuthentication", e);
671684
showError(response);
672685
}

0 commit comments

Comments
 (0)