Skip to content
Draft
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 @@ -143,7 +143,7 @@ public void test() throws Exception {

requestedUrlSetCookie = findFirstCookieByName(response, Cookies.REQUESTED_URL);
assertTrue(requestedUrlSetCookie.isPresent());
assertTrue(cookieValueIsNull(requestedUrlSetCookie.get()));
assertTrue(cookieValueIsEmpty(requestedUrlSetCookie.get()));

// step 3: turn pac4j identity into KnoxSSO identity
request = new MockHttpServletRequest();
Expand All @@ -156,7 +156,7 @@ public void test() throws Exception {
assertEquals(0, response.getStatus());
adapter.doFilter(request, response, filterChain);

assertEquals(1, countCookiesWithNullValueByName(response, Cookies.USER_PROFILES));
assertEquals(1, countCookiesWithEmptyValueByName(response, Cookies.USER_PROFILES));
assertEquals(USERNAME, adapter.getTestIdentifier());
}

Expand Down Expand Up @@ -242,7 +242,7 @@ public void testValidIdAttribute() throws Exception {

requestedUrlSetCookie = findFirstCookieByName(response, Cookies.REQUESTED_URL);
assertTrue(requestedUrlSetCookie.isPresent());
assertTrue(cookieValueIsNull(requestedUrlSetCookie.get()));
assertTrue(cookieValueIsEmpty(requestedUrlSetCookie.get()));

// step 3: turn pac4j identity into KnoxSSO identity
request = new MockHttpServletRequest();
Expand All @@ -255,7 +255,7 @@ public void testValidIdAttribute() throws Exception {
assertEquals(0, response.getStatus());
adapter.doFilter(request, response, filterChain);

assertEquals(1, countCookiesWithNullValueByName(response, Cookies.USER_PROFILES));
assertEquals(1, countCookiesWithEmptyValueByName(response, Cookies.USER_PROFILES));
assertEquals(USERNAME, adapter.getTestIdentifier());
}

Expand Down Expand Up @@ -341,7 +341,7 @@ public void testInvalidIdAttribute() throws Exception {

requestedUrlSetCookie = findFirstCookieByName(response, Cookies.REQUESTED_URL);
assertTrue(requestedUrlSetCookie.isPresent());
assertTrue(cookieValueIsNull(requestedUrlSetCookie.get()));
assertTrue(cookieValueIsEmpty(requestedUrlSetCookie.get()));

// step 3: turn pac4j identity into KnoxSSO identity
request = new MockHttpServletRequest();
Expand All @@ -354,7 +354,7 @@ public void testInvalidIdAttribute() throws Exception {
assertEquals(0, response.getStatus());
adapter.doFilter(request, response, filterChain);

assertEquals(1, countCookiesWithNullValueByName(response, Cookies.USER_PROFILES));
assertEquals(1, countCookiesWithEmptyValueByName(response, Cookies.USER_PROFILES));
assertEquals(USERNAME, adapter.getTestIdentifier());
}

Expand Down Expand Up @@ -444,14 +444,14 @@ private static long countCookiesByName(MockHttpServletResponse response, String
.count();
}

private static long countCookiesWithNullValueByName(MockHttpServletResponse response, String name) {
private static long countCookiesWithEmptyValueByName(MockHttpServletResponse response, String name) {
return response.getHeaders().get("Set-Cookie").stream()
.filter(c -> c.startsWith(name+"=null;"))
.filter(c -> c.startsWith(name+"=;"))
.count();
}

private static boolean cookieValueIsNull(String setCookieHeader) {
return setCookieParser(setCookieHeader).getValue().equals("null");
private static boolean cookieValueIsEmpty(String setCookieHeader) {
return setCookieParser(setCookieHeader).getValue().isEmpty();
}

private static class Cookies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Map;
import java.util.Set;

import static org.apache.knox.gateway.pac4j.filter.Pac4jDispatcherFilter.PAC4J_COOKIE_MAX_AGE;
import static org.apache.knox.gateway.pac4j.filter.Pac4jDispatcherFilter.PAC4J_SESSION_STORE_EXCLUDE_CUSTOM_ATTRIBUTES;
import static org.apache.knox.gateway.pac4j.filter.Pac4jDispatcherFilter.PAC4J_SESSION_STORE_EXCLUDE_GROUPS;
import static org.apache.knox.gateway.pac4j.filter.Pac4jDispatcherFilter.PAC4J_SESSION_STORE_EXCLUDE_GROUPS_DEFAULT;
Expand Down Expand Up @@ -238,4 +239,55 @@ public void testNullCookieValue() throws AliasServiceException {
Assert.assertTrue(sessionStore.get(mockContext, keyWithNullValue).isEmpty());
}

@Test
public void testSetNullValueWritesEmptyCookie() {
final String header = captureSetCookieHeader(new HashMap<>(), "keyWithNullValue", null);
assertEmptyCookie(header, "keyWithNullValue");
Assert.assertFalse(header, header.contains("Max-Age"));
}

@Test
public void testSetNullValueKeepsConfiguredMaxAge() {
final Map<String, String> sessionStoreConfigs = new HashMap<>();
sessionStoreConfigs.put(PAC4J_COOKIE_MAX_AGE, "3600");
final String header = captureSetCookieHeader(sessionStoreConfigs, "keyWithNullValue", null);
assertEmptyCookie(header, "keyWithNullValue");
Assert.assertTrue(header, header.contains("Max-Age=3600"));
}

@Test
public void testSetEmptyMapWritesEmptyCookie() {
final String header = captureSetCookieHeader(new HashMap<>(), "keyWithEmptyMap", new HashMap<>());
assertEmptyCookie(header, "keyWithEmptyMap");
Assert.assertFalse(header, header.contains("Max-Age"));
}

private String captureSetCookieHeader(Map<String, String> sessionStoreConfigs, String key, Object value) {
final CryptoService cryptoService = EasyMock.createNiceMock(CryptoService.class);
EasyMock.replay(cryptoService);

final Capture<String> capturedHeader = EasyMock.newCapture();
final HttpServletResponse mockResponse = EasyMock.createNiceMock(HttpServletResponse.class);
mockResponse.addHeader(EasyMock.eq("Set-Cookie"), EasyMock.capture(capturedHeader));
EasyMock.replay(mockResponse);

final JEEContext mockContext = EasyMock.createNiceMock(JEEContext.class);
EasyMock.expect(mockContext.getNativeResponse()).andReturn(mockResponse).anyTimes();
EasyMock.expect(mockContext.getFullRequestURL()).andReturn("https://local.com/gateway/knoxsso/").anyTimes();
EasyMock.expect(mockContext.getServerName()).andReturn("local.com").anyTimes();
EasyMock.expect(mockContext.getScheme()).andReturn("https").anyTimes();
EasyMock.expect(mockContext.isSecure()).andReturn(true).anyTimes();
EasyMock.replay(mockContext);

final KnoxSessionStore sessionStore = new KnoxSessionStore(cryptoService, CLUSTER_NAME, null, sessionStoreConfigs);
sessionStore.set(mockContext, key, value);
return capturedHeader.getValue();
}

private void assertEmptyCookie(String header, String key) {
Assert.assertTrue(header, header.startsWith(PAC4J_SESSION_PREFIX + key + "=;"));
Assert.assertTrue(header, header.contains("Domain=local.com"));
Assert.assertFalse(header, header.contains("null"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void setAttribute(String name, String value) {
@Override
public String toString() {
StringBuilder sb = new StringBuilder(name);
sb.append('=').append(value);
sb.append('=').append(value == null ? "" : value);

if(path != null) {
sb.append("; Path=").append(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void testBasicSetCookie() {
@Test
public void testEmptyValueSetCookie() {
SetCookieHeader setCookieHeader = new SetCookieHeader("name", null);
assertEquals("name=null", setCookieHeader.toString());
assertEquals("name=", setCookieHeader.toString());
}

@Test
Expand Down
Loading