diff --git a/WebContent/samlLogin/index.html b/WebContent/samlLogin/index.html
index 887dbc00f..c32ffc3c9 100644
--- a/WebContent/samlLogin/index.html
+++ b/WebContent/samlLogin/index.html
@@ -4,12 +4,14 @@
SAML Login
diff --git a/WebContent/samlSPLogin/index.html b/WebContent/samlSPLogin/index.html
index 100cd1623..f5ef66be1 100644
--- a/WebContent/samlSPLogin/index.html
+++ b/WebContent/samlSPLogin/index.html
@@ -4,12 +4,14 @@
SAML Login
diff --git a/WebContent/setAdmin/index.html b/WebContent/setAdmin/index.html
index 61a4ec76a..d3ac3f065 100644
--- a/WebContent/setAdmin/index.html
+++ b/WebContent/setAdmin/index.html
@@ -219,6 +219,19 @@
return '/';
}
+ // relative so the destination never comes from page text
+ function returnToBase() {
+ var path = window.location.pathname;
+ if (path.indexOf('/setAdmin') < 0) {
+ window.location.replace('/');
+ } else if (path.endsWith('/setAdmin')) {
+ window.location.replace('./');
+ } else {
+ var trailingSlashes = path.match(/\/+$/);
+ window.location.replace('../'.repeat(trailingSlashes ? trailingSlashes[0].length : 1));
+ }
+ }
+
function xhr(method, url, body, headers, callback) {
var req = new XMLHttpRequest();
req.open(method, url, true);
@@ -281,7 +294,7 @@
return;
}
- window.location.replace(getBasePrefix());
+ returnToBase();
return;
}
diff --git a/src/prerna/semoss/web/services/local/NameServer.java b/src/prerna/semoss/web/services/local/NameServer.java
index 2a2838c7b..0dddbf12b 100644
--- a/src/prerna/semoss/web/services/local/NameServer.java
+++ b/src/prerna/semoss/web/services/local/NameServer.java
@@ -145,7 +145,9 @@ public Response downloadFile(@QueryParam("insightId") String insightId, @QueryPa
String exportName = FilenameUtils.getName(filePath);
return Response.status(200).entity(exportFile)
- .header("Content-Disposition", "attachment; filename=\"" + exportName + "\"").build();
+ .header("Content-Disposition",
+ WebUtility.responseHeaderValue("attachment; filename=\"" + exportName + "\""))
+ .build();
} catch (Exception e) {
Map errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, e.getMessage());
diff --git a/src/prerna/semoss/web/services/local/ProjectResource.java b/src/prerna/semoss/web/services/local/ProjectResource.java
index c8e1cf845..ee502fcab 100644
--- a/src/prerna/semoss/web/services/local/ProjectResource.java
+++ b/src/prerna/semoss/web/services/local/ProjectResource.java
@@ -761,7 +761,7 @@ public Response downloadProjectImage(@Context final Request coreRequest, @Contex
EntityTag etag = new EntityTag(Integer.toHexString(exportFile.getAbsolutePath().hashCode()) + "-"
+ exportFile.lastModified() + "-" + exportFile.length());
return CatalogImageResponse.withBrowserCache(coreRequest, Response.ok(exportFile)
- .header("Content-Disposition", "attachment; filename=" + exportName).tag(etag).build());
+ .header("Content-Disposition", WebUtility.responseHeaderValue("attachment; filename=" + exportName)).tag(etag).build());
} else {
Map errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, "error sending image file");
@@ -869,7 +869,7 @@ public Response downloadInsightImage(@Context final Request coreRequest, @Contex
}
return CatalogImageResponse.withContentType(Response.ok(exportFile)
- .header("Content-Disposition", "attachment; filename=" + exportName).tag(etag).build());
+ .header("Content-Disposition", WebUtility.responseHeaderValue("attachment; filename=" + exportName)).tag(etag).build());
} else {
Map errorMap = new HashMap<>();
errorMap.put(Constants.ERROR_MESSAGE, "Error sending image file");
diff --git a/src/prerna/semoss/web/services/local/SessionResource.java b/src/prerna/semoss/web/services/local/SessionResource.java
index afceb6837..01ebd2d64 100644
--- a/src/prerna/semoss/web/services/local/SessionResource.java
+++ b/src/prerna/semoss/web/services/local/SessionResource.java
@@ -250,7 +250,7 @@ public void invalidateSession(@Context HttpServletRequest request, @Context Http
String customUrl = DBLoader.getCustomLogoutUrl();
if (customUrl != null && !customUrl.isEmpty()) {
- response.setHeader("redirect", customUrl);
+ response.setHeader("redirect", WebUtility.responseHeaderValue(customUrl));
response.sendError(302, "Need to redirect to " + customUrl);
} else {
String scheme = WebUtility.inputSQLSanitizer(request.getScheme()); // http
@@ -270,7 +270,7 @@ public void invalidateSession(@Context HttpServletRequest request, @Context Http
}
redirectUrl += contextPath + "/logout/";
redirectUrl = WebUtility.cleanHttpResponse(redirectUrl);
- response.setHeader("redirect", redirectUrl);
+ response.setHeader("redirect", WebUtility.responseHeaderValue(redirectUrl));
response.sendError(302, "Need to redirect to " + redirectUrl);
}
} else {
@@ -278,7 +278,7 @@ public void invalidateSession(@Context HttpServletRequest request, @Context Http
redirectUrl = redirectUrl + WebUtility.determineLoginExtension(request);
String encodedRedirectUrl = Encode.forHtml(redirectUrl);
- response.setHeader("redirect", encodedRedirectUrl);
+ response.setHeader("redirect", WebUtility.responseHeaderValue(encodedRedirectUrl));
response.sendError(302, "Need to redirect to " + encodedRedirectUrl);
}
diff --git a/src/prerna/semoss/web/services/local/UserResource.java b/src/prerna/semoss/web/services/local/UserResource.java
index 566f0555f..6e8b663fe 100644
--- a/src/prerna/semoss/web/services/local/UserResource.java
+++ b/src/prerna/semoss/web/services/local/UserResource.java
@@ -99,6 +99,7 @@
import prerna.util.linotp.LinOTPUtil;
import prerna.web.conf.DBLoader;
import prerna.web.conf.UserSessionLoader;
+import prerna.web.services.util.RedirectUtility;
import prerna.web.services.util.WebUtility;
import waffle.servlet.WindowsPrincipal;
@@ -243,7 +244,7 @@ public Response logout(@PathParam("provider") String provider,
String customUrl = DBLoader.getCustomLogoutUrl();
if (customUrl != null && !customUrl.isEmpty()) {
- response.setHeader("redirect", customUrl);
+ response.setHeader("redirect", WebUtility.responseHeaderValue(customUrl));
response.sendError(302, "Need to redirect to " + customUrl);
} else {
String redirectUrl = WebUtility.inputSanitizer(request.getHeader("referer"));
@@ -262,12 +263,12 @@ public Response logout(@PathParam("provider") String provider,
redirectUrl += ":" + serverPort;
}
redirectUrl += contextPath + "/logout/";
- response.setHeader("redirect", redirectUrl);
+ response.setHeader("redirect", WebUtility.responseHeaderValue(redirectUrl));
response.sendError(302, "Need to redirect to " + redirectUrl);
} else {
redirectUrl = redirectUrl + WebUtility.determineLoginExtension(request);
String encodedRedirectUrl = Encode.forHtml(redirectUrl);
- response.setHeader("redirect", encodedRedirectUrl);
+ response.setHeader("redirect", WebUtility.responseHeaderValue(encodedRedirectUrl));
response.sendError(302, "Need to redirect to " + encodedRedirectUrl);
}
}
@@ -434,7 +435,8 @@ public Response userinfoGeneric(@PathParam("provider") String provider, @Context
*/
private HttpSession initializeOAuthLoginSession(HttpServletRequest request) {
HttpSession session = request.getSession(false);
- String customRedirect = WebUtility.cleanHttpResponse(request.getParameter("redirect"));
+ // checked raw: html-encoding first would corrupt valid urls, and the check refuses control characters
+ String customRedirect = RedirectUtility.allowedReturnUrl(request, request.getParameter("redirect"));
if (customRedirect != null && !customRedirect.isEmpty()) {
if (session == null) {
session = request.getSession();
@@ -1350,7 +1352,8 @@ public Response loginNative(@Context HttpServletRequest request, @Context HttpSe
try {
String username = WebUtility.inputSQLSanitizer(request.getParameter("username"));
String password = WebUtility.inputSQLSanitizer(request.getParameter("password"));
- String redirect = WebUtility.cleanHttpResponse(request.getParameter("redirect"));
+ // validated raw in setMainPageRedirect
+ String redirect = request.getParameter("redirect");
// so that the default is to redirect
Boolean disableRedirect = Boolean.parseBoolean(request.getParameter("disableRedirect") + "");
@@ -1587,7 +1590,8 @@ public Response loginLinOTP(@Context HttpServletRequest request, @Context HttpSe
return WebUtility.getResponse(returnMap, 400);
}
- String redirect = WebUtility.cleanHttpResponse(request.getParameter("redirect"));
+ // validated raw in setMainPageRedirect
+ String redirect = request.getParameter("redirect");
// so that the default is to redirect
Boolean disableRedirect = Boolean.parseBoolean(request.getParameter("disableRedirect") + "");
boolean autoAdd = Boolean.parseBoolean(socialData.getProperty("linotp_auto_add", "true"));
@@ -1935,7 +1939,8 @@ public synchronized Response getAllLoginProperties(@Context HttpServletRequest r
* @param response outbound HTTP response
*/
private void setMainPageRedirect(@Context HttpServletRequest request, @Context HttpServletResponse response) {
- String customRedirect = WebUtility.cleanHttpResponse(request.getParameter("redirect"));
+ // validated raw in setMainPageRedirect(request, response, customRedirect)
+ String customRedirect = request.getParameter("redirect");
if (customRedirect == null || customRedirect.isEmpty()) {
customRedirect = (String) request.getSession().getAttribute(CUSTOM_REDIRECT_SESSION_KEY);
// also remove the attribute so additional logins dont do the redirect as well
@@ -1958,18 +1963,20 @@ private void setMainPageRedirect(@Context HttpServletRequest request, @Context H
// otherwise, we send them back to the FE
HttpSession session = request.getSession();
+ // the redirect can come from the request or the session, only return to an allowed destination
+ customRedirect = RedirectUtility.allowedReturnUrl(request, customRedirect);
boolean useCustom = customRedirect != null && !customRedirect.isEmpty();
boolean endpoint = session.getAttribute(Constants.ENDPOINT_REDIRECT_KEY) != null;
response.setStatus(302);
try {
addSessionCookieHeader(request, response, session);
if (useCustom) {
- response.addHeader("redirect", customRedirect);
+ response.addHeader("redirect", WebUtility.responseHeaderValue(customRedirect));
String encodedCustomRedirect = Encode.forHtml(customRedirect);
response.sendError(302, "Need to redirect to " + encodedCustomRedirect);
} else if (endpoint) {
String redirectUrl = session.getAttribute(Constants.ENDPOINT_REDIRECT_KEY) + "";
- response.addHeader("redirect", redirectUrl);
+ response.addHeader("redirect", WebUtility.responseHeaderValue(redirectUrl));
response.sendError(302, "Need to redirect to " + redirectUrl);
} else {
response.sendRedirect(socialData.getProperty("redirect"));
@@ -1994,7 +2001,7 @@ private void addSessionCookieHeader(HttpServletRequest request, HttpServletRespo
+ ((ClusterUtil.IS_CLUSTER || secureRequest)
? ("; Secure; SameSite=" + Utility.getSameSiteCookieValue())
: "");
- response.addHeader("Set-Cookie", setCookieString);
+ response.addHeader("Set-Cookie", WebUtility.responseHeaderValue(setCookieString));
}
//////////////////////////////////////////////////////////////////////
diff --git a/src/prerna/semoss/web/services/saml/IdpSSOServlet.java b/src/prerna/semoss/web/services/saml/IdpSSOServlet.java
index 3bc76c662..d6d34b320 100644
--- a/src/prerna/semoss/web/services/saml/IdpSSOServlet.java
+++ b/src/prerna/semoss/web/services/saml/IdpSSOServlet.java
@@ -44,7 +44,7 @@
import prerna.util.Constants;
import prerna.util.Utility;
import prerna.web.conf.util.SSOUtil;
-import prerna.web.services.util.WebUtility;
+import prerna.web.services.util.RedirectUtility;
/**
* Handles the IdP-initiated login entry point.
@@ -98,9 +98,8 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
String redirect = request.getParameter("redirect");
boolean hasRedirect = (redirect != null && !(redirect = redirect.trim()).isEmpty());
if (hasRedirect) {
- try {
- WebUtility.checkIfValidDomain(redirect);
- } catch (IllegalArgumentException | IllegalStateException e) {
+ // one policy: relative, this origin, the configured UI, redirect_allowed_origins or WHITE_LIST_DOMAINS
+ if (RedirectUtility.allowedReturnUrl(request, redirect) == null) {
response.sendError(HttpServletResponse.SC_FORBIDDEN, " Provided redirect is unauthorized");
return;
}
diff --git a/src/prerna/semoss/web/services/saml/SamlVerifierServlet.java b/src/prerna/semoss/web/services/saml/SamlVerifierServlet.java
index 287b54bba..13023a8fa 100644
--- a/src/prerna/semoss/web/services/saml/SamlVerifierServlet.java
+++ b/src/prerna/semoss/web/services/saml/SamlVerifierServlet.java
@@ -78,6 +78,8 @@
import prerna.util.Utility;
import prerna.web.conf.AdminStartupFilter;
import prerna.web.conf.util.SSOUtil;
+import prerna.web.services.util.RedirectUtility;
+import prerna.web.services.util.WebUtility;
/**
* Receives the SAML assertion callback from the IdP and finalizes login in this
@@ -161,7 +163,8 @@ public void verifySamlOutput(HttpServletRequest request, HttpServletResponse res
if ((relayUrl != null) && (relayUrl.length() != 0)) {
// something special for validation to send redirect
int stringPos = relayUrl.indexOf("sendRedirectForValidationNow=true");
- if (stringPos != -1) {
+ // RelayState is not signed, so only follow it to an allowed destination
+ if (stringPos != -1 && RedirectUtility.allowedReturnUrl(request, relayUrl) != null) {
response.sendRedirect(relayUrl);
return;
}
@@ -245,8 +248,10 @@ public void verifySamlOutput(HttpServletRequest request, HttpServletResponse res
// Complete the SSO transaction by returning the user to their original target.
String encodedRedirectUrl = Encode.forHtml(originalRedirect);
AdminStartupFilter.setSuccessfulRedirectUrl(encodedRedirectUrl);
- response.setHeader("redirect", encodedRedirectUrl);
- response.sendRedirect(encodedRedirectUrl);
+ // the stored value was checked when it was saved; html-encoding it here would break query strings
+ String redirectHeader = WebUtility.responseHeaderValue(originalRedirect);
+ response.setHeader("redirect", redirectHeader);
+ response.sendRedirect(redirectHeader);
} catch (SAML2Exception | IOException | SessionException | ServletException sme) {
classLogger.error("Failed to process SAML verification response.", sme);
diff --git a/src/prerna/upload/FileUploader.java b/src/prerna/upload/FileUploader.java
index a391983ad..d8baa276a 100644
--- a/src/prerna/upload/FileUploader.java
+++ b/src/prerna/upload/FileUploader.java
@@ -842,14 +842,20 @@ private List