Skip to content

Commit

Permalink
[UNDERTOW-2192] session.getServletContext returns wrong context with …
Browse files Browse the repository at this point in the history
…shared-session-config
  • Loading branch information
aogburn authored and fl4via committed Mar 25, 2023
1 parent bbb9fee commit 9ed4eb9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.undertow.servlet.api.ThreadSetupHandler;
import io.undertow.servlet.handlers.ServletRequestContext;
import io.undertow.servlet.spec.HttpSessionImpl;
import io.undertow.servlet.spec.ServletContextImpl;

/**
* Class that bridges between Undertow native session listeners and servlet ones.
Expand Down Expand Up @@ -60,7 +61,10 @@ public Void call(HttpServerExchange exchange, Session session) throws ServletExc

@Override
public void sessionCreated(final Session session, final HttpServerExchange exchange) {
final HttpSessionImpl httpSession = SecurityActions.forSession(session, servletContext, true);
ServletContext sc = servletContext;
if (servletContext instanceof ServletContextImpl)
sc = exchange.removeAttachment(((ServletContextImpl) servletContext).contextAttachmentKey);
final HttpSessionImpl httpSession = SecurityActions.forSession(session, sc, true);
applicationListeners.sessionCreated(httpSession);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public class ServletContextImpl implements ServletContext {
private final ConcurrentMap<String, Object> attributes;
private final SessionCookieConfigImpl sessionCookieConfig;
private final AttachmentKey<HttpSessionImpl> sessionAttachmentKey = AttachmentKey.create(HttpSessionImpl.class);
public static final AttachmentKey<ServletContextImpl> contextAttachmentKey = AttachmentKey.create(ServletContextImpl.class);
private volatile Set<SessionTrackingMode> sessionTrackingModes = new HashSet<>(Arrays.asList(new SessionTrackingMode[]{SessionTrackingMode.COOKIE, SessionTrackingMode.URL}));
private volatile Set<SessionTrackingMode> defaultSessionTrackingModes = new HashSet<>(Arrays.asList(new SessionTrackingMode[]{SessionTrackingMode.COOKIE, SessionTrackingMode.URL}));
private volatile SessionConfig sessionConfig;
Expand Down Expand Up @@ -982,6 +983,7 @@ public String rewriteUrl(String originalUrl, String sessionId) {

try {
if (httpSession == null) {
exchange.putAttachment(contextAttachmentKey, this);
final Session newSession = sessionManager.createSession(exchange, c);
httpSession = SecurityActions.forSession(newSession, this, true);
exchange.putAttachment(sessionAttachmentKey, httpSession);
Expand Down

0 comments on commit 9ed4eb9

Please sign in to comment.