Skip to content

Commit ad6937e

Browse files
committed
KNOX-3232: Handle pac4j cookies with "null" value
1 parent d99996a commit ad6937e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

gateway-provider-security-pac4j/src/main/java/org/apache/knox/gateway/pac4j/session/KnoxSessionStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public Optional<String> getSessionId(WebContext context, boolean createSession)
105105
}
106106

107107
private Object uncompressDecryptBase64(final String v) {
108-
if (v != null && !v.isEmpty()) {
108+
if (v != null && !v.isEmpty() && !"null".equals(v)) {
109109
byte[] bytes = Base64.decodeBase64(v);
110110
EncryptionResult result = EncryptionResult.fromByteArray(bytes);
111111
byte[] clear = cryptoService.decryptForCluster(this.clusterName,

gateway-provider-security-pac4j/src/test/java/org/apache/knox/gateway/pac4j/session/KnoxSessionStoreTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
import org.easymock.EasyMock;
2525
import org.junit.Assert;
2626
import org.junit.Test;
27+
import org.pac4j.core.context.Cookie;
2728
import org.pac4j.core.profile.CommonProfile;
2829
import org.pac4j.core.util.Pac4jConstants;
2930
import org.pac4j.jee.context.JEEContext;
3031
import org.pac4j.saml.profile.SAML2Profile;
3132

3233
import javax.servlet.http.HttpServletResponse;
3334
import java.util.Arrays;
35+
import java.util.Collections;
3436
import java.util.HashMap;
3537
import java.util.HashSet;
3638
import java.util.Map;
@@ -44,6 +46,7 @@
4446
import static org.apache.knox.gateway.pac4j.filter.Pac4jDispatcherFilter.PAC4J_SESSION_STORE_EXCLUDE_ROLES;
4547
import static org.apache.knox.gateway.pac4j.filter.Pac4jDispatcherFilter.PAC4J_SESSION_STORE_EXCLUDE_ROLES_DEFAULT;
4648
import static org.apache.knox.gateway.pac4j.session.KnoxSessionStore.PAC4J_PASSWORD;
49+
import static org.apache.knox.gateway.pac4j.session.KnoxSessionStore.PAC4J_SESSION_PREFIX;
4750

4851
public class KnoxSessionStoreTest {
4952
private static final String CLUSTER_NAME = "knox";
@@ -158,4 +161,29 @@ public void filterConfigParamsTest()
158161
Assert.assertNotNull(samlProfile.getAttribute("https://knox.apache.org/SAML/Attributes/groups"));
159162
Assert.assertNotNull(samlProfile.getAttribute("https://knox.apache.org/SAML/Attributes/groups2"));
160163
}
164+
165+
@Test
166+
public void testNullCookieValue() throws AliasServiceException {
167+
final AliasService aliasService = EasyMock.createNiceMock(AliasService.class);
168+
EasyMock.expect(aliasService.getPasswordFromAliasForCluster(CLUSTER_NAME, PAC4J_PASSWORD, true))
169+
.andReturn(PAC4J_PASSWORD.toCharArray()).anyTimes();
170+
EasyMock.expect(aliasService.getPasswordFromAliasForCluster(CLUSTER_NAME, PAC4J_PASSWORD))
171+
.andReturn(PAC4J_PASSWORD.toCharArray()).anyTimes();
172+
EasyMock.replay(aliasService);
173+
174+
final DefaultCryptoService cryptoService = new DefaultCryptoService();
175+
cryptoService.setAliasService(aliasService);
176+
177+
final Map<String, String> sessionStoreConfigs = new HashMap<>();
178+
179+
final JEEContext mockContext = EasyMock.createNiceMock(JEEContext.class);
180+
String keyWithNullValue = "CasClient$attemptedAuthentication";
181+
Cookie cookie = new Cookie(PAC4J_SESSION_PREFIX + keyWithNullValue, "null");
182+
EasyMock.expect(mockContext.getRequestCookies()).andReturn(Collections.singletonList(cookie));
183+
EasyMock.replay(mockContext);
184+
185+
final KnoxSessionStore sessionStore = new KnoxSessionStore(cryptoService, CLUSTER_NAME, null, sessionStoreConfigs);
186+
Assert.assertTrue(sessionStore.get(mockContext, keyWithNullValue).isEmpty());
187+
}
188+
161189
}

0 commit comments

Comments
 (0)