Skip to content

Commit

Permalink
Check for null client info from thread local to avoid NPE
Browse files Browse the repository at this point in the history
Error observed during deployment when users bounced to new server.
Indicates session replication probably not working but might as well
get exception with message rather than NPE.
  • Loading branch information
hdeadman committed Aug 11, 2020
1 parent 3d9a737 commit b827485
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ protected String buildCompoundCookieValue(final String givenCookieValue, final H
return builder.toString();
}


/**
* Make sure cookie is used from same IP and with same user-agent as when cookie created.
* Client info (with original client ip) may be null if cluster failover occurs and session replication not working.
*/
@Override
protected String obtainValueFromCompoundCookie(final String cookieValue, final HttpServletRequest request) {
val cookieParts = Splitter.on(String.valueOf(COOKIE_FIELD_SEPARATOR)).splitToList(cookieValue);
Expand All @@ -76,9 +81,14 @@ protected String obtainValueFromCompoundCookie(final String cookieValue, final H
}

val clientInfo = ClientInfoHolder.getClientInfo();
if (clientInfo == null) {
throw new InvalidCookieException("Unable to match required remote address "
+ remoteAddr + " because client ip at time of cookie creation unknown");
}

if (!remoteAddr.equals(clientInfo.getClientIpAddress())) {
throw new InvalidCookieException("Invalid cookie. Required remote address "
+ remoteAddr + " does not match " + clientInfo.getClientIpAddress());
+ remoteAddr + " does not match " + clientInfo.getClientIpAddress());
}

val agent = HttpRequestUtils.getHttpServletRequestUserAgent(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,14 @@ public void verifyBadAgent() {
assertThrows(InvalidCookieException.class, () -> mgr.obtainCookieValue("something@"
+ ClientInfoHolder.getClientInfo().getClientIpAddress() + "@agent", new MockHttpServletRequest()));
}

@Test
public void verifyMissingClientInfo() {
val props = new TicketGrantingCookieProperties();
val mgr = new DefaultCasCookieValueManager(CipherExecutor.noOp(), props);
ClientInfoHolder.clear();
assertThrows(InvalidCookieException.class, () -> mgr.obtainCookieValue("something@"
+ CLIENT_IP + "@" + USER_AGENT, new MockHttpServletRequest()));
}

}

0 comments on commit b827485

Please sign in to comment.