Repository navigation
fix(security): do not redirect a remember-me login that is not a GET, or is an Ajax call - #16971
borinquenkid wants to merge 12 commits into
Conversation
… or is an Ajax call Spring Security's RememberMeAuthenticationFilter hands every request that logs in through remember-me to the authentication success handler, which redirects, and then does not run the rest of the filter chain. A POST, PUT, PATCH or DELETE became a GET and its action never ran, and an Ajax request was redirected to the Ajax success url. GrailsRememberMeAuthenticationFilter keeps the success handler and uses it, and saves the request, only for a GET that is not an Ajax call. Any other request goes on down the filter chain as the remembered user. GrailsRememberMeAuthenticationFilterSpec covers a GET (unchanged), POST, PUT, PATCH and DELETE, Ajax, and no handler. The guide says what happens to a remember-me request. Fixes #15879 Fixes #15873 Fixes #15874 Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
A remember-me login that is not a GET, or that is an Ajax call, is not redirected. A browser GET that should redirect still does.
jamesfredley
left a comment
There was a problem hiding this comment.
A remember-me login that is not a GET, or that is an Ajax call, is not redirected. A browser GET that should redirect still does.
…t after it does not loop Spring Security 6 and later saves the authentication of a remember-me login in the request only, unless the filter is given a security context repository. The page the success handler redirects to then found no user, logged in through the cookie again and redirected again; with a page that needs a full login the browser bounced between it and /login/auth. The remember-me filter now uses the plugin's session-backed securityContextRepository. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
… cookie logs the user in A page that needs a full login sends a remembered user to /login/auth, which the entry point does after clearing the security context. The cookie logged the user in again on that request and the success handler redirected back to the page, which sent the user to /login/auth again, and so on. The request for the login form now goes on down the filter chain, so the login controller can send a remembered user to the full login page. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Two more commits ( I reproduced it first, in a functional spec on #16973 that follows each redirect by hand. With a remember-me cookie and no session, a request for a page that needs a full login went Two causes, both in
Unit: |
The approval no longer holds: a login form URL written as an absolute address is not recognized, so that request is redirected.
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. A non-GET and an Ajax login are still not redirected, and a browser GET that should redirect still does. A login form URL written as an absolute address is not recognized, so that request is redirected.
…e login is not redirected Answers the review of #16971: the filter compared the request path with the configured login form url, so an absolute url such as https://example.test/login/auth never matched and the request for the login form was redirected. An absolute url is now compared by scheme, host, port and path, which includes the context path. The spec tries each url shape against each request shape. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Answers the absolute login-form URL case.
Not tried: a proxy that changes the scheme or host the request reports; a forwarded-header filter is expected to correct them before this filter runs. |
jamesfredley
left a comment
There was a problem hiding this comment.
The items this review found have been addressed. An absolute login form URL is recognised. A non-GET and an Ajax login are still not redirected.
jdaugherty
left a comment
There was a problem hiding this comment.
The main change is right: keeping the success handler in the Grails filter and using it only for a GET that is not an Ajax call matches what Spring Security's filter does in 6.5.11, and the spec covers it.
Not approving yet, for the login form matching: the loop is still reachable with an absolute login form URL behind a proxy, and with a path that has a query string. The reason given for the securityContextRepository wiring is also not what happens in Spring Security, and the changes in behavior need an upgrade note. Details inline.
| return false | ||
| } | ||
| int formPort = form.port != -1 ? form.port : ('https'.equalsIgnoreCase(form.scheme) ? 443 : 80) | ||
| form.scheme.equalsIgnoreCase(request.scheme) && form.host?.equalsIgnoreCase(request.serverName) && |
There was a problem hiding this comment.
An absolute login form URL is matched on scheme, host and port as the request reports them. Behind a proxy that does not forward them, they differ from the public URL: with the form at https://example.test/login/auth and the request arriving as http://app-internal:8080/login/auth, the request is not recognised as the login form, it is redirected, and the loop is back.
The request has already reached this application, so the path is enough to tell whether it is the login form. Please compare only the path of an absolute URL (which includes the context path) and drop the scheme, host and port checks. The rows of the spec that expect another host, scheme or port to be redirected then change to the login form.
| if (loginFormUrl.toLowerCase(Locale.ROOT).startsWith('http://') || loginFormUrl.toLowerCase(Locale.ROOT).startsWith('https://')) { | ||
| return isAbsoluteLoginForm(request) | ||
| } | ||
| request.requestURI == request.contextPath + loginFormUrl |
There was a problem hiding this comment.
A login form URL given as a path with a query string never matches: with auth.loginFormUrl = '/login/auth?lang=en', a GET of /login/auth is still redirected, so the loop remains for that configuration. The absolute branch ignores the query (the spec has a row for https://example.test/login/auth?x=1), the path branch does not.
Please take the path out of the configured URL the same way for both forms, so there is one comparison, and add a row for a path with a query string.
| // Spring Security 6 and later saves a remember-me login only in the request unless told where; saved in the session, | ||
| // the page the success handler redirects to finds the remembered user and does not log in and redirect again |
There was a problem hiding this comment.
This comment is not right for the versions we use. In Spring Security 6.5.11 (7.0.x) and 7.1.x, RememberMeAuthenticationFilter defaults to its own HttpSessionSecurityContextRepository, so the login was already saved in the session. In the default filter chain that default repository also saves through the response wrapper of the securityContextRepository bean, because securityContextPersistenceFilter runs first, so the scr.* settings were already applied.
Using the bean here is fine, but it is not what stops the loop; the login form exclusion is. Please correct the comment to say what the line does: the filter uses the plugin's repository and its scr.* settings, also in a chain without securityContextPersistenceFilter.
| } | ||
|
|
||
| @Issue('#15877') | ||
| void 'the remembered login is kept in the session, so the page redirected to does not log in again and loop'() { |
There was a problem hiding this comment.
This feature passes without the change it is meant to cover: with no repository given to the filter, the login is still in the session under the same key, because of Spring Security's default repository. Please either drop it, or make it fail without the wiring, for example a repository with its own springSecurityContextKey and an assertion on that key.
|
|
||
| Spring Security supports creating a remember-me cookie so that users are not required to log in with a username and password for each session. This is optional and is usually implemented as a checkbox on the login form; the default `auth.gsp` supplied by the plugin has this feature. | ||
|
|
||
| When a request arrives with a remember-me cookie and no session, the user is logged in from the cookie. A page that is fetched with a GET is then redirected to, through the authentication success handler, so that the browser lands on the page it asked for, except the request for the login form (`auth.loginFormUrl`): a page that needs a full login sends a remembered user there, and redirecting back to the page would bounce between the two. Any other request, that is a POST, PUT, PATCH or DELETE, or a request that comes from Ajax (see <<core-ajax>>), is not redirected: redirecting it would turn it into a GET, or into the Ajax success URL, and its action would not run. It goes on to the controller as the remembered user instead. |
There was a problem hiding this comment.
These are changes in behavior for an application that upgrades, so they need an entry in the upgrade guide (upgrading60x.adoc) as well as this page:
- a remember-me login on a POST, PUT, PATCH, DELETE, HEAD or Ajax request is no longer redirected, and nothing is saved in the request cache for it;
- a remember-me login on the request for the login form now reaches
LoginController; setAuthenticationSuccessHandlerno longer sets Spring Security's handler, and the redirect is decided inonSuccessfulAuthentication, so a subclass that overrides that method without callingsuperno longer redirects at all.
The note should say how to keep the old result, for example by overriding redirects(request).
Separately, this sentence is hard to follow ("is then redirected to, through the authentication success handler, ..., except the request for the login form"). A short list, one item per kind of request, would read better.
…the changes of remember-me are in the upgrade guide The request for the login form is recognised by its path alone, for a path and for an absolute url of auth.loginFormUrl: the query and the fragment of the configured url are left out, and the scheme, host and port the request reports are not compared, as behind a proxy that does not forward them they differ from the public url and the redirect loop was back. A configured url with a query string, such as /login/auth?lang=en, never matched before. The spec row for the session now gives the filter a repository with a key of its own and looks under that key, so it fails when the filter is not given the repository. The comment on the wiring says what it does, and the upgrade guide has a section on the requests that are no longer redirected and on how to keep the old result. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jdaugherty All five points are addressed in the new commit.
The plugin module (199 tests) and Branch policy: this commit adds no public type and no throw. Where the PR belongs is still open; I have not moved it. If you find another shape that does not reproduce first time, a failing spec on a throwaway branch, or the few lines you ran, would help me a lot. |
A login form address whose path has a dot segment, https://example.test/app/../login/auth or /app/../login/auth, never matched the request, which the browser sends without that segment, so a remembered user was redirected from the form. The configured path is normalized before it is compared. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley @jdaugherty: the configured login form address is compared without its dot segments now. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A login address with an encoded dot segment still misses the form.
| return false | ||
| } | ||
| // the path the browser sends has no dot segments, so the configured one has none either | ||
| return request.requestURI == (form.normalize().rawPath ?: '/') |
There was a problem hiding this comment.
The login address has an encoded dot segment. The browser asks for the path without that segment. This still compares the encoded path, so a remember-me user misses the form. Compare the path the browser sends.
…ments, encoded or not, as a browser sends it Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: a login address with an encoded dot segment, Before asking for another look, I ran 30 spellings: |
# Conflicts: # grails-doc/src/en/guide/upgrading/upgrading60x.adoc
✅ All tests passed ✅🏷️ Commit: 5b91fff Learn more about TestLens at testlens.app/docs. |
…rect-7.0.x # Conflicts: # grails-doc/src/en/guide/upgrading/upgrading60x.adoc
Draft: offered to the team. I am not working on this one any more. If you take it, start from the latest commit of this branch on a new branch from 7.0.x, open your own pull request, and tell me in Slack. This one stays as the reference for the history.
Fixes #15879, #15873 and #15874 (a merge to 7.0.x does not close them, so they are closed by hand once the merge-ups land). #15877 and #15875 describe related redirects and are checked after this.
Cause, read in the bytecode of Spring Security 7.1.1: when a
successHandleris set,RememberMeAuthenticationFilter.doFiltercalls it after a successful remember-me login and returns, so the filter chain does not run. The plugin sets the authentication success handler on its remember-me filter on purpose, andGrailsRememberMeAuthenticationFiltersaves the request so that an autologin lands on the page that was asked for. That is right for a page fetched with a GET. For a POST, PUT, PATCH or DELETE the redirect turns the request into a GET and the action never runs (#15879); for an Ajax request the handler redirects to the Ajax success URL instead of answering (#15873, #15874).Change:
GrailsRememberMeAuthenticationFilterkeeps the success handler itself instead of handing it to Spring's filter, and uses it, and saves the request, only for a GET that is not an Ajax call (SpringSecurityUtils.isAjax). The handler runs where Spring would have called it, after the success event. Any other request goes on down the filter chain as the remembered user, and nothing is saved in the request cache for it. The wiring inSpringSecurityCoreGrailsPluginis unchanged. The page of the remember-me cookie in the guide says what happens to each kind of request.Tests:
GrailsRememberMeAuthenticationFilterSpec(7 features): a GET is redirected to through the handler and the chain stops, as today (it passed before the change); POST, PUT, PATCH and DELETE, and an Ajax GET, continue down the chain with the remembered user in the security context, with no call to the handler and nothing saved (these five failed before the change, as run in the repo); and without a handler a GET continues. All 180 unit tests of the plugin pass; CodeNarc, Checkstyle and rat are clean. CI is off (no ci).Not covered: a running application with a real remember-me cookie. No functional test in the examples depends on the old redirect.
Two of the three reviewers (matrei, codeconsole, jdaugherty), as 7.0.x is released.
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL