Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KNOX-2973 - Fix redirect URI when host and port are query params of originalUrl #809

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ private Response getAuthenticationToken(int statusCode) {
return Response.seeOther(location).entity("{ \"redirectTo\" : " + original + " }").build();
}

private String getOriginalUrlFromQueryParams() {
protected String getOriginalUrlFromQueryParams() {
String original = request.getParameter(ORIGINAL_URL_REQUEST_PARAM);
StringBuilder buf = new StringBuilder(original);

Expand All @@ -362,7 +362,8 @@ private String getOriginalUrlFromQueryParams() {
&& !original.contains(entry.getKey() + "=")
&& !ssoExpectedparams.contains(entry.getKey())) {

if(first) {
/* Only add ? if not already present. See KNOX-2973 */
if(first && (buf.lastIndexOf("?") == -1) ) {
buf.append('?');
first = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,114 @@ public void testConcurrentSessionLimitHit() throws Exception {
Assert.assertThrows(WebApplicationException.class, webSSOResponse::doPost);
}

/**
* Test for cases where originalUrl has host and port parameters.
* @throws Exception
*/
@Test
public void testGetOriginalUrlFromQueryParamsWithHostPort() throws Exception {
/* &port=1001 is missing in the original URL because it gets stripped
out during request.getParameter(ORIGINAL_URL_REQUEST_PARAM) operation */
final String ORIG_URL_VALUE = "https://local.site/gateway/sandbox/hbase/webui/master?host=local.site";
final String ORIGINAL_URL= "originalUrl="+ORIG_URL_VALUE;
final String PORT_VALUE = "1001";
Map<String, String[]> params = new HashMap<>();
params.put("originalUrl", new String[] {ORIG_URL_VALUE});
params.put("port", new String[] {PORT_VALUE});

HttpServletRequest req = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(req.getParameter("originalUrl")).andReturn(ORIGINAL_URL);
EasyMock.expect(req.getParameterMap()).andReturn(params);
EasyMock.expect(req.getParameterMap()).andReturn(Collections.emptyMap());
EasyMock.expect(req.getServletContext()).andReturn(context).anyTimes();

Principal principal = EasyMock.createNiceMock(Principal.class);
EasyMock.expect(principal.getName()).andReturn("alice").anyTimes();
EasyMock.expect(req.getUserPrincipal()).andReturn(principal).anyTimes();

EasyMock.replay(req);

WebSSOResource webSSOResource = new WebSSOResource();


webSSOResource.request = req;
String result = webSSOResource.getOriginalUrlFromQueryParams();

/* make sure there no additional '?' at the end of ORIG_URL_VALUE */
assertEquals(ORIGINAL_URL+"&port="+PORT_VALUE, result);
}

/**
* Test for cases where originalUrl has host parameter.
* @throws Exception
*/
@Test
public void testGetOriginalUrlFromQueryParamsWithHost() throws Exception {
final String ORIG_URL_VALUE = "https://local.site/gateway/sandbox/hbase/webui/master?host=local.site";
final String ORIGINAL_URL= "originalUrl="+ORIG_URL_VALUE;
Map<String, String[]> params = new HashMap<>();
params.put("originalUrl", new String[] {ORIG_URL_VALUE});

HttpServletRequest req = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(req.getParameter("originalUrl")).andReturn(ORIGINAL_URL);
EasyMock.expect(req.getParameterMap()).andReturn(params);
EasyMock.expect(req.getParameterMap()).andReturn(Collections.emptyMap());
EasyMock.expect(req.getServletContext()).andReturn(context).anyTimes();

Principal principal = EasyMock.createNiceMock(Principal.class);
EasyMock.expect(principal.getName()).andReturn("alice").anyTimes();
EasyMock.expect(req.getUserPrincipal()).andReturn(principal).anyTimes();

EasyMock.replay(req);

WebSSOResource webSSOResource = new WebSSOResource();


webSSOResource.request = req;
String result = webSSOResource.getOriginalUrlFromQueryParams();

/* make sure there no additional '?' at the end of ORIG_URL_VALUE */
assertEquals(ORIGINAL_URL, result);
}


/**
* Test for cases where originalUrl does not have host and port parameters.
* normal case.
* @throws Exception
*/
@Test
public void testGetOriginalUrlFromQueryParams() throws Exception {
/* &port=1001 is missing in the original URL because it gets stripped
out during request.getParameter(ORIGINAL_URL_REQUEST_PARAM) operation */
final String ORIG_URL_VALUE = "https://local.site/gateway/sandbox/hbase/webui/master";
final String ORIGINAL_URL= "originalUrl="+ORIG_URL_VALUE;
Map<String, String[]> params = new HashMap<>();
params.put("originalUrl", new String[] {ORIG_URL_VALUE});

HttpServletRequest req = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(req.getParameter("originalUrl")).andReturn(ORIGINAL_URL);
EasyMock.expect(req.getParameterMap()).andReturn(params);
EasyMock.expect(req.getParameterMap()).andReturn(Collections.emptyMap());
EasyMock.expect(req.getServletContext()).andReturn(context).anyTimes();

Principal principal = EasyMock.createNiceMock(Principal.class);
EasyMock.expect(principal.getName()).andReturn("alice").anyTimes();
EasyMock.expect(req.getUserPrincipal()).andReturn(principal).anyTimes();

EasyMock.replay(req);

WebSSOResource webSSOResource = new WebSSOResource();


webSSOResource.request = req;
String result = webSSOResource.getOriginalUrlFromQueryParams();

/* make sure there no additional '?' at the end of ORIG_URL_VALUE */
assertEquals(ORIGINAL_URL, result);
}


/**
* A wrapper for HttpServletResponseWrapper to store the cookies
*/
Expand Down
Loading