Repository navigation
MCR-3903 fix getCurrentUser() returning the session user - #3205
Conversation
sebhofmann
left a comment
There was a problem hiding this comment.
🤖 This review was generated automatically by Claude (AI) on behalf of Sebastian Hofmann. Please treat the findings as suggestions and verify them.
Merge assessment: Mergeable after addressing the points marked as blocking below. The fix is reasonable for sessions that hold an MCRUser or a MyCoRe issued JWT, but it now maps every other kind of user information onto a stored account by name and realm, which changes what the user self service in MCRUserServlet allows.
What I checked (static reading only, no build): the diff and the ticket, all callers of MCRUserManager.getCurrentUser() in mycore (MCRUserServlet, MCRRoleResolver, MCRUserResolver, MCRPersistTransientUserEventHandler, MCRAccessKeyUtils, ORCID 1/2, OCFL) and in MIR (MIRStrategy.canEditPI()), all MCRUserInformation implementations that can end up in a session (MCRSystemUserInformation, MCRJWTUserInformation, MCRShibbolethUserInformation, ContainerUserInformation, Servlet3ContainerUserInformation, the janitor user information), the login servlets (local, Shibboleth, CAS, Servlet3, container), MCRSession.setUserInformation() and the JPA handling in findUser()/updateUser().
Good: ACL checks via MCRAccessManager still use the session user information, so they are not affected directly. The transition check in MCRSession.setUserInformation() allows the swap because the user ID is the same. The deleted user fallback and the guest/system shortcut look correct, and the tests cover the described scenarios (stale attributes, other user information, update of the session user, other user's session, deleted user).
Findings:
- Blocking: identity mapping for container logins (see inline comment on
findUser). - Superuser/janitor are not excluded, and
MIRStrategy.canEditPI()(a permission check in MIR) now sees stored roles instead of session roles (inline). - The session user is replaced before the transaction is committed (inline).
- Minor, documentation:
getCurrentUser()now returns an entity that is managed by the currentEntityManager. Before, the returned session object was usually detached, so changes withoutupdateUser()stayed in the session; now they are flushed to the database on commit. All callers in mycore either only read or callupdateUser()anyway, but since this is a bugfix on an LTS branch, it may be worth a sentence in the JavaDoc so applications are aware of it.
| return new MCRTransientUser(userInformation); | ||
| MCRUser sessionUser = userInformation instanceof MCRUser mcrUser ? mcrUser | ||
| : new MCRTransientUser(userInformation); | ||
| if (isInvalidUser(sessionUser) || MCREntityManagerProvider.getEntityManagerFactory() == null) { |
There was a problem hiding this comment.
isInvalidUser() only covers the guest and the system user. The superuser (MCRSystemUserInformation.getSuperUserInstance(), user ID from MCR.Users.Superuser.UserName, default administrator) is set by MCRCommandLineInterface and is usually also a stored account. Before, the CLI got an MCRTransientUser that has all system roles (the superuser answers isUserInRole with true); now it gets the stored administrator with only its stored roles. The janitor is not stored, but now causes two queries per call.
This matters for MIR: MIRStrategy.canEditPI() is part of the access strategy and calls MCRUserManager.getCurrentUser()::isUserInRole for write/delete checks on objects with a registered PI. With this change, that check (a) uses the stored roles instead of the session roles that every other ACL check uses, (b) does a user query and a role query for every uncached check, and (c) depends on the roles of the stored administrator in the CLI (fine with the MIR default MIR.Strategy.EditPIRoles=admin, but not with other roles configured). Should all MCRSystemUserInformation instances (superuser, janitor) return the session user here? And should MIRStrategy switch to the session user information (separate MIR ticket)?
There was a problem hiding this comment.
System users are no longer mapped. MIRStrategy should use the session user information, I'll create a MIR ticket.
yagee-de
left a comment
There was a problem hiding this comment.
My main concern is that limited authentication information can become the authority of a full stored account. Given the JWT trust model, this affects more than container logins.
- The restrictions expressed by a JWT can be bypassed.
A token might contain only reader, while the stored account with the same name also has editor. After this change, getCurrentUser() returns the stored user, including editor and all stored attributes. Callers of this method may therefore use permissions beyond those granted by the token. Regular ACL checks still use the session/token roles, creating inconsistent authorization decisions within the same request. - Container logins can take over local accounts.
If a container user and a local user share the same name, the new lookup can return the local account. The transient user’s previous locked status no longer applies. In a reproduction test, this allowed the container identity to change the local account’s password through the servlet password-change path. For applications using this authentication setup, this is a concrete security blocker. - Failed updates can remain effective in the session.
updateUser() replaces the session user before the transaction commits. If the transaction subsequently rolls back, the changes are absent from the database but remain in the session. Roles or access-key attributes can therefore affect authorization despite the failed update. I reproduced this behavior. - operations of system users (e.g. superuser) can unexpectedly lose permissions.
The superuser may receive only the roles of the stored administrator account instead of its effective superuser roles. Operations using getCurrentUser() for authorization can consequently fail, for example MIR’s PI checks when custom edit roles are configured.
The selected existing tests pass, but that does not address these regressions.
The potential expansion of token permissions and the demonstrated local-account takeover seems weird. JWT is the source of truth and we respect roles and user attributes and should not mix it with other sources. Making missing attributes available should preserve the existing trust model: a verified token authorizes the claims it carries; it should not implicitly grant all roles and attributes of a matching stored account.
A verified JWT should grant the roles and user attributes explicitly included in the token. We already restrict roles through MCR.Rest.JWT.Roles; user attributes (ua) should follow an equivalent explicit selection policy. Resolving the token’s subject to a stored account and returning all its roles and attributes can silently expand the token’s authority beyond those claims. The existing mcr:ua: claims provide a basis for making selected attributes available, including through user:current, without implicitly granting the permissions of a matching database account. These claims represent the state at issuance; obtaining later changes requires a defined refresh mechanism using a trusted, current source.
|
Thanks, I agree with the trust model: a JWT (or any other non-
The original issue (attributes added after login missing in |
sebhofmann
left a comment
There was a problem hiding this comment.
Merge assessment: Mergeable once the rollback gap below is closed. CI is green.
This review was generated by an AI assistant (Claude) on behalf of sebhofmann.
| } | ||
| MCRSession session = MCRSessionMgr.getCurrentSession(); | ||
| if (holdsStoredUser(session, user)) { | ||
| session.onCommit(() -> { |
There was a problem hiding this comment.
The rollback protection via onCommit has a gap: MCRTransactionHelper.rollbackTransaction() does not discard the queued onCommitTasks of the session. They are only cleared in passivate() or run by the next commitTransaction().
In MCRServlet this happens within the same request: updateUser() is called in think(), then the commit fails or an exception is thrown, processThinkPhase rolls back, and processRenderingPhase begins and commits a new transaction. That commit runs the stale task and puts the never persisted user (roles, attributes) into the session.
testUpdateUserDoesNotChangeSessionUserOnRollback does not cover this, because it does not commit after the rollback. Suggestion: clear the session's onCommitTasks in rollbackTransaction(), and extend the test with a commitTransaction() after the rollback before the assertSame.
This review was generated by an AI assistant (Claude) on behalf of sebhofmann.
There was a problem hiding this comment.
Fixed: MCRTransactionHelper.rollbackTransaction() now discards the session's onCommit tasks, and the test commits a new transaction after the rollback, like MCRServlet. This also applies to the other onCommit users (Solr/Neo4j indexing, job queue), which should not run after a rollback either.
sebhofmann
left a comment
There was a problem hiding this comment.
The changes requested by Thomas are now implemented.
Link to jira.
Pull Request Checklist (Author)
Please go through the following checklist before assigning the PR for review:
Ticket & Documentation
fixed-versionis correctly set in the ticket and matches the PR's target branch (main).Feature & Improvement Specific Checks
Bugfix-Specific Checks
Testing
MCR Conventions & Metadata
mycore.properties.mycore.properties.Multi-Repo Considerations
MIRrequired?