Skip to content

Commit e19a202

Browse files
committed
Refactor so Principal is never cached in session with cache==false
1 parent 0fded7d commit e19a202

File tree

3 files changed

+15
-26
lines changed

3 files changed

+15
-26
lines changed

java/org/apache/catalina/authenticator/AuthenticatorBase.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,10 +1135,11 @@ private void register(Request request, HttpServletResponse response, Principal p
11351135
}
11361136

11371137
// Cache the authentication information in our session, if any
1138-
if (cache) {
1139-
if (session != null) {
1138+
if (session != null) {
1139+
if (cache) {
11401140
session.setAuthType(authType);
11411141
session.setPrincipal(principal);
1142+
} else {
11421143
if (username != null) {
11431144
session.setNote(Constants.SESS_USERNAME_NOTE, username);
11441145
} else {

java/org/apache/catalina/authenticator/Constants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ public class Constants {
8282

8383
/**
8484
* The previously authenticated principal (if caching is disabled).
85+
*
86+
* @deprecated Unused. Will be removed in Tomcat 10.
8587
*/
88+
@Deprecated
8689
public static final String FORM_PRINCIPAL_NOTE = "org.apache.catalina.authenticator.PRINCIPAL";
8790

8891
/**

java/org/apache/catalina/authenticator/FormAuthenticator.java

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,6 @@ public void setLandingPage(String landingPage) {
132132
protected boolean doAuthenticate(Request request, HttpServletResponse response)
133133
throws IOException {
134134

135-
if (checkForCachedAuthentication(request, response, true)) {
136-
return true;
137-
}
138-
139135
// References to objects we will need later
140136
Session session = null;
141137
Principal principal = null;
@@ -154,9 +150,8 @@ protected boolean doAuthenticate(Request request, HttpServletResponse response)
154150
}
155151
principal = context.getRealm().authenticate(username, password);
156152
if (principal != null) {
157-
session.setNote(Constants.FORM_PRINCIPAL_NOTE, principal);
153+
register(request, response, principal, HttpServletRequest.FORM_AUTH, username, password);
158154
if (!matchRequest(request)) {
159-
register(request, response, principal, HttpServletRequest.FORM_AUTH, username, password);
160155
return true;
161156
}
162157
}
@@ -173,16 +168,6 @@ protected boolean doAuthenticate(Request request, HttpServletResponse response)
173168
if (log.isDebugEnabled()) {
174169
log.debug("Restore request from session '" + session.getIdInternal() + "'");
175170
}
176-
principal = (Principal) session.getNote(Constants.FORM_PRINCIPAL_NOTE);
177-
register(request, response, principal, HttpServletRequest.FORM_AUTH,
178-
(String) session.getNote(Constants.SESS_USERNAME_NOTE),
179-
(String) session.getNote(Constants.SESS_PASSWORD_NOTE));
180-
// If we're caching principals we no longer need the user name
181-
// and password in the session, so remove them
182-
if (cache) {
183-
session.removeNote(Constants.SESS_USERNAME_NOTE);
184-
session.removeNote(Constants.SESS_PASSWORD_NOTE);
185-
}
186171
if (restoreRequest(request, session)) {
187172
if (log.isDebugEnabled()) {
188173
log.debug("Proceed to restored request");
@@ -197,6 +182,12 @@ protected boolean doAuthenticate(Request request, HttpServletResponse response)
197182
}
198183
}
199184

185+
// This check has to be after the previous check for a matching request
186+
// because that matching request may also include a cached Principal.
187+
if (checkForCachedAuthentication(request, response, true)) {
188+
return true;
189+
}
190+
200191
// Acquire references to objects we will need to evaluate
201192
String contextPath = request.getContextPath();
202193
String requestURI = request.getDecodedRequestURI();
@@ -283,12 +274,7 @@ protected boolean doAuthenticate(Request request, HttpServletResponse response)
283274
return false;
284275
}
285276

286-
// Save the authenticated Principal in our session
287-
session.setNote(Constants.FORM_PRINCIPAL_NOTE, principal);
288-
289-
// Save the username and password as well
290-
session.setNote(Constants.SESS_USERNAME_NOTE, username);
291-
session.setNote(Constants.SESS_PASSWORD_NOTE, password);
277+
register(request, response, principal, HttpServletRequest.FORM_AUTH, username, password);
292278

293279
// Redirect the user to the original request URI (which will cause
294280
// the original request to be restored)
@@ -489,7 +475,7 @@ protected boolean matchRequest(Request request) {
489475
}
490476

491477
// Is there a saved principal?
492-
if (session.getNote(Constants.FORM_PRINCIPAL_NOTE) == null) {
478+
if (cache && session.getPrincipal() == null || !cache && request.getPrincipal() == null) {
493479
return false;
494480
}
495481

@@ -519,7 +505,6 @@ protected boolean restoreRequest(Request request, Session session)
519505
// Retrieve and remove the SavedRequest object from our session
520506
SavedRequest saved = (SavedRequest) session.getNote(Constants.FORM_REQUEST_NOTE);
521507
session.removeNote(Constants.FORM_REQUEST_NOTE);
522-
session.removeNote(Constants.FORM_PRINCIPAL_NOTE);
523508
if (saved == null) {
524509
return false;
525510
}

0 commit comments

Comments
 (0)