Skip to content
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

Let TokenBasedRememberMeServices2 tolerate Authentication.principal not UserDetails #7724

Merged
merged 2 commits into from Mar 17, 2023

Conversation

jglick
Copy link
Member

@jglick jglick commented Mar 13, 2023

While experimenting with tests of SSO logins, I came across a case where clicking Remember me caused a CCE because the Authentication had a principal of type String rather than the UserDetails this class currently expects. Using a debugger I found that the String-based token was created here:

at org.springframework.security.authentication.AbstractAuthenticationToken.setDetails(AbstractAuthenticationToken.java:99)
at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.setDetails(UsernamePasswordAuthenticationFilter.java:126)
at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:84)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:231)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:221)
at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:99)
at jenkins.security.BasicHeaderProcessor.doFilter(BasicHeaderProcessor.java:97)
at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:99)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:117)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at hudson.security.HttpSessionContextIntegrationFilter2.doFilter(HttpSessionContextIntegrationFilter2.java:63)
at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:99)
at hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:111)
at hudson.security.HudsonFilter.doFilter(HudsonFilter.java:172)
at …

I think this arose because the test was actually mistaken at that point, trying to use the /login form when the configured security realm was not username/password-based. Probably UsernamePasswordAuthenticationFilter would not normally be used in this way. At any rate, the Javadoc for Object getPrincipal() says

In the case of an authentication request with username and password, this would be the username.

Many of the authentication providers will create a UserDetails object as the principal.

implying that String and UserDetails are the expected types (but that there might be others!); and so it would be best to tolerate anything.

Testing done

Prevented a CCE in the context of original experiments (closed source). Interactive sanity test logging in to built-in security realm.

Proposed changelog entries

  • Avoiding a ClassCastException from TokenBasedRememberMeServices2 (not known to occur in realistic environments).

Maintainer checklist

Before the changes are marked as ready-for-merge:

  • There are at least two (2) approvals for the pull request and no outstanding requests for change.
  • Conversations in the pull request are over, or it is explicit that a reviewer is not blocking the change.
  • Changelog entries in the pull request title and/or Proposed changelog entries are accurate, human-readable, and in the imperative mood.
  • Proper changelog labels are set so that the changelog can be generated automatically.
  • If the change needs additional upgrade steps from users, the upgrade-guide-needed label is set and there is a Proposed upgrade guidelines section in the pull request title (see example).
  • If it would make sense to backport the change to LTS, a Jira issue must exist, be a Bug or Improvement, and be labeled as lts-candidate to be considered (see query).

@timja timja added the bug For changelog: Minor bug. Will be listed after features label Mar 13, 2023
@NotMyFault
Copy link
Member

/label ready-for-merge


This PR is now ready for merge. We will merge it after ~24 hours if there is no negative feedback.
Please see the merge process documentation for more information about the merge process.
Thanks!

@comment-ops-bot comment-ops-bot bot added the ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback label Mar 16, 2023
@timja timja merged commit 3fe425f into jenkinsci:master Mar 17, 2023
15 checks passed
@jglick jglick deleted the TokenBasedRememberMeServices2 branch March 22, 2023 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug For changelog: Minor bug. Will be listed after features ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback
Projects
None yet
3 participants