Skip to content

MCR-3903 fix getCurrentUser() returning the session user - #3205

Merged
sebhofmann merged 4 commits into
2023.06.xfrom
issues/MCR-3903-current_user_stored
Nov 27, 2026
Merged

sebhofmann merged 4 commits into
2023.06.xfrom
issues/MCR-3903-current_user_stored

Conversation

@golsch

@golsch golsch commented Nov 3, 2026

Copy link
Copy Markdown
Member

Link to jira.

Pull Request Checklist (Author)

Please go through the following checklist before assigning the PR for review:

Ticket & Documentation

  • The issue in the ticket is clearly described and the solution is documented.
  • Design decisions (if any) are explained.
  • The ticket references the correct source and target branches.
  • The fixed-version is correctly set in the ticket and matches the PR's target branch (main).

Feature & Improvement Specific Checks

  • Instructions on how to test or use the feature are included or linked (e.g. to documentation).
  • For UI changes: before & after screenshots are attached.
  • New features or migrations are documented.
  • Does this change affect existing applications, data, or configurations?
    • Yes: Is a migration required? If yes, describe it.
    • Breaking change is marked in the commit message.

Bugfix-Specific Checks

  • Affected version is listed in the ticket.
  • Minimal code changes were made (no refactoring).
  • This PR truly fixes only the reported bug.
  • No breaking changes are introduced.
  • A relevant test was added (if feasible).

Testing

  • I have tested the changes locally.
  • The feature behaves as described in the ticket.
  • Were existing tests modified?
    • Yes: explain the changes for reviewers.

MCR Conventions & Metadata

  • MCR naming conventions are followed
  • If the public API has changed:
    • Old API is deprecated or a migration is documented.
    • If not, no action needed.
  • Java license headers are added where necessary.
  • Javadoc is written for non-self-explanatory classes/methods (Clean Code).
  • All configuration options are documented in Javadoc and mycore.properties.
  • No default properties are hardcoded — all set via mycore.properties.

Multi-Repo Considerations

  • Is an equivalent PR in MIR required?
    • If yes, is it already created?

@golsch
golsch requested a review from Possommi November 3, 2026 10:07
@golsch golsch added the bug label Nov 3, 2026
@golsch
golsch requested a review from yagee-de November 6, 2026 13:16

@sebhofmann sebhofmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 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:

  1. Blocking: identity mapping for container logins (see inline comment on findUser).
  2. Superuser/janitor are not excluded, and MIRStrategy.canEditPI() (a permission check in MIR) now sees stored roles instead of session roles (inline).
  3. The session user is replaced before the transaction is committed (inline).
  4. Minor, documentation: getCurrentUser() now returns an entity that is managed by the current EntityManager. Before, the returned session object was usually detached, so changes without updateUser() stayed in the session; now they are flushed to the database on commit. All callers in mycore either only read or call updateUser() 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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System users are no longer mapped. MIRStrategy should use the session user information, I'll create a MIR ticket.

@yagee-de yagee-de left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.

@golsch

golsch commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

Thanks, I agree with the trust model: a JWT (or any other non-MCRUser user information) should only grant what it carries. I reworked the PR accordingly:

  1. JWT restrictions: getCurrentUser() no longer maps other user information to a stored account. JWT, container, servlet3, superuser/janitor and transient users get the MCRTransientUser as before. The stored user is only reloaded if the session already holds a stored MCRUser, so it is the same account, just not stale.
  2. Container logins: covered by 1., a user information without realm for a locally existing name now yields a locked MCRTransientUser without stored attributes (test added).
  3. Rollback: the session user is now replaced via session.onCommit(...), so a rolled back update does not reach the session (test added).
  4. System users: covered by 1., they are never mapped to stored users anymore.

The original issue (attributes added after login missing in user:current) is still fixed for sessions holding an MCRUser. For JWT users, clients have to request the needed attributes via ua (mcr:ua: claims), which getCurrentUser().getUserAttribute() already resolves. An explicit allowlist for ua and sa like MCR.Rest.JWT.Roles should be a separate ticket.

@sebhofmann sebhofmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(() -> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@golsch
golsch requested a review from sebhofmann November 26, 2026 14:26

@sebhofmann sebhofmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes requested by Thomas are now implemented.

@sebhofmann
sebhofmann merged commit a67c2e1 into 2023.06.x Nov 27, 2026
3 checks passed
@sebhofmann
sebhofmann deleted the issues/MCR-3903-current_user_stored branch November 27, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants