-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
OAuth2AuthorizationRequest not removed from session #7327
Comments
Thanks for the report @AndreasKl. You are right, Would you be interested in submitting a PR for this fix? |
@jgrandja Would be happy to provide the PR. Do you think it is of any value, if I also provide a PR with an optional facility that allows to limit the amount of Something like: void limitSizeOfAuthorizationRequest(
OAuth2AuthorizationRequest oAuth2AuthorizationRequest, WebSession webSession) {
requireNonNull(oAuth2AuthorizationRequest, "oAuth2AuthorizationRequest must not be null");
requireNonNull(webSession, "webSession must not be null");
removeOldAuthorizationRequest(
storeMostRecentAuthorizationRequests(oAuth2AuthorizationRequest, webSession), webSession);
}
private Map<String, String> storeMostRecentAuthorizationRequests(
OAuth2AuthorizationRequest oAuth2AuthorizationRequest, WebSession webSession) {
SizeLimitedHashMap<String, String> workingSet =
webSession.getAttribute(DEFAULT_AUTHORIZATION_REQUEST_LIMIT_ATTR_NAME);
if (workingSet == null) {
workingSet = new SizeLimitedHashMap<>(limit);
}
workingSet.put(oAuth2AuthorizationRequest.getState(), SOME_VALUE);
if (LOG.isDebugEnabled()) {
LOG.debug("The current active auth request states are: {}", workingSet.keySet());
}
webSession.getAttributes().put(DEFAULT_AUTHORIZATION_REQUEST_LIMIT_ATTR_NAME, workingSet);
return workingSet;
}
private void removeOldAuthorizationRequest(
Map<String, String> workingSet, WebSession webSession) {
Map<String, OAuth2AuthorizationRequest> oAuth2AuthorizationRequests =
webSession.getAttribute(DEFAULT_AUTHORIZATION_REQUEST_ATTR_NAME);
if (oAuth2AuthorizationRequests == null) {
return;
}
if (LOG.isDebugEnabled()) {
LOG.debug(
"Auth request state keys before cleanup: {}", oAuth2AuthorizationRequests.keySet());
}
oAuth2AuthorizationRequests.keySet().retainAll(workingSet.keySet());
if (LOG.isDebugEnabled()) {
LOG.debug("Auth request state keys after cleanup: {}", oAuth2AuthorizationRequests.keySet());
}
webSession
.getAttributes()
.put(DEFAULT_AUTHORIZATION_REQUEST_ATTR_NAME, oAuth2AuthorizationRequests);
} public class SizeLimitedHashMap<TKey extends Serializable, TValue extends Serializable>
extends LinkedHashMap<TKey, TValue> implements Serializable {
private static final long serialVersionUID = -4358903105100110082L;
private final int limit;
SizeLimitedHashMap(int limit) {
this.limit = limit;
}
@Override
protected boolean removeEldestEntry(Entry<TKey, TValue> eldest) {
return this.size() > limit;
}
} |
The existing issue #5145 tracks this enhancement. We would need this enhancement implemented on both the Servlet and Reactive side. It would be great if you can provide a PR? |
@jgrandja Thanks for the hint, wasn't aware there is already an open issue. |
Dirties the WebSession by putting the amended AUTHORIZATION_REQUEST map into the WebSession even it was already in the map. This causes common SessionRepository implementations like Redis to persist the updated attribute. Fixes gh-7327 Author: Andreas Kluth <[email protected]>
When #6215 was fixed only the adding of new
OAuth2AuthorizationRequest
s was fixed, not the removal of those. With a distributed session store we observed an increase in session size for users having long running sessions.A dump of the keys of the session attributes revealed a huge
HashMap
ofOAuth2AuthorizationRequest
. This is due toorg.springframework.security.oauth2.client.web.server.WebSessionOAuth2ServerAuthorizationRequestRepository#removeAuthorizationRequest
only removing theOAuth2AuthorizationRequest
from theHashMap
and not updating the session attributes leaving no clue to the session repository that the session was amended.The expected behaviour would be that the stateToAuthzRequest
HashMap
should not grow without limit andOAuth2AuthorizationRequest
should be removed after it was used to create a new session.Used version: spring-security-oauth2-client-5.1.6.RELEASE.jar
however the issue exists on master: https://github.com/spring-projects/spring-security/blob/master/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/server/WebSessionOAuth2ServerAuthorizationRequestRepository.java#L85
The text was updated successfully, but these errors were encountered: