Repository navigation
fix(security): keep the login form entry point when X.509 may fall through to other mechanisms - #17250
fix(security): keep the login form entry point when X.509 may fall through to other mechanisms#17250borinquenkid wants to merge 9 commits into
Conversation
…fall through useX509 replaced the authenticationEntryPoint bean with a Http403ForbiddenEntryPoint unconditionally, so a request without a usable certificate got a bare 403 even with x509.continueFilterChainOnUnsuccessfulAuthentication = true (the default), which is documented as letting other authentication mechanisms process the request. The 403 entry point is now defined only when that property is false, that is when certificates are the only way in. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…ion as one boolean A value set in application.properties or on the command line is the string 'false', which Groovy truth reads as true, so the entry point condition kept the login form while the filter, to which Spring converts the string, refused the request. The plugin now parses the setting once and uses it for both. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…ion the way the filter property does Answers the review of #16974: Boolean.parseBoolean read yes, on, 1 and a padded true as false, so a setting that used to continue the chain after a failed certificate refused the request. The setting is now converted by Spring's own converter, which the filter property always used, so the entry point and the filter agree for every spelling. The spec tries each spelling and a value that is no boolean. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jdaugherty
left a comment
There was a problem hiding this comment.
The fix does what #15863 and #15864 ask: with the default, a request without a usable certificate now reaches the login form, and certificate login and form login both work side by side. The spec is solid too (reverting the plugin change fails 17 of its 25 tests, and none of the mutations I tried on the new lines survives). What I am asking for is around it: the upgrade note, the way back to the old result, the new doc sentence, one conversion detail, and a spec that pins the request-level behavior.
1. Upgrade note
This fix changes a result: an application with useX509 and the default setting used to answer 403 and now answers 302 to /login/auth. Per "Fixes That Change a Result" in CONTRIBUTING.md it needs a section in upgrading60x.adoc and the same entry in grails-skills/upgrade/skills/grails-upgrade/SKILL.md (#17249 has the shape). The note should cover:
- what was returned before and what is returned now, for a request without a certificate and for a certificate that maps to no user (both change);
- how to keep the old result. That is not
x509.continueFilterChainOnUnsuccessfulAuthentication = false: withfalse, a certificate that fails authentication makes the X.509 filter rethrow, and since that filter runs before the exception translation filter the request ends in a 500, on every URL including public ones. With the old default the same request got 403 on a secured URL and public pages were served. The equivalent is to define the bean inresources.groovy:authenticationEntryPoint(Http403ForbiddenEntryPoint); - that
authenticationEntryPointBeanClassnow applies to the default definition when X.509 continues the chain, so a class without the login-form constructor (for exampleHttp403ForbiddenEntryPointitself) no longer starts: "Could not resolve matching constructor". Before, the X.509 block built that class with no arguments; - that an application using
InterceptUrlMaporRequestmapneeds a rule that permits/login/**, or the redirect is itself rejected (I read this fromrejectIfNoRule, I did not run it).
2. A spec for the request-level behavior
X509EntryPointSpec checks which bean class is registered. What an application sees is the response, and that is what we will want to look up later, so please add a data-driven spec with one where: row per row of this table, and put the table in the spec's Javadoc. I measured it on this head and on the merge base by building the plugin's real springSecurityFilterChain in a GrailsWebUnitTest and sending it MockHttpServletRequests with the jakarta.servlet.request.X509Certificate attribute set (a keytool-generated certificate is enough, no TLS needed); the same requests against an embedded Tomcat with a TLS connector gave the same answers.
alice has a user, mallory has none, bob logs in with the form. /secure/page needs authentication, /login/auth is permitted to all.
continue… |
Request | Before | With this PR |
|---|---|---|---|
true (default) |
no certificate, /secure/page |
403 | 302 to /login/auth |
true |
no certificate, Ajax, /secure/page |
403 | 302 to /login/authAjax |
true |
alice's certificate, /secure/page |
200 as alice | 200 as alice |
true |
alice's certificate, a URL that needs a role she lacks | 403, forward to /login/denied |
same |
true |
mallory's certificate, /secure/page |
403 | 302 to /login/auth |
true |
mallory's certificate, /login/auth |
200 anonymous | 200 anonymous |
true |
bob, no certificate: /secure/page, then POST /login/authenticate, then /secure/page |
403 (the login POST itself works) | 302 to the form, 302 back, 200 as bob |
true |
bob presenting mallory's certificate, same three steps | as above | 302, 302, 200 as bob |
false |
no certificate, /secure/page (plain and Ajax) |
403 | 403 |
false |
alice's certificate, /secure/page |
200 as alice | 200 as alice |
false |
mallory's certificate, any URL, /login/auth included |
UsernameNotFoundException out of the chain (500 on Tomcat) |
same |
false |
bob, no certificate: /login/auth, POST /login/authenticate, /secure/page |
200, 302, 200 as bob | same |
The false half is unchanged by this PR, which is the point of having it in the spec: it shows that only the default path moves. The 500 for a rejected certificate with false was there before, so I am not asking for it to be fixed here, only for the row to record it.
3. Inline
The rest is inline: the conversion target, the doc sentence, and two small things in the spec.
| // Spring to convert (true, on, yes and 1, with any letter case and surrounding space, are true; false, off, no and 0 are | ||
| // false), so the entry point is decided by the same conversion, not by Groovy truth or Boolean.parseBoolean | ||
| Object configuredContinue = conf.x509.continueFilterChainOnUnsuccessfulAuthentication | ||
| boolean continueFilterChain = configuredContinue == null ? true : (Boolean) new SimpleTypeConverter().convertIfNecessary(configuredContinue, Boolean) |
There was a problem hiding this comment.
The comment above says this is the same conversion the filter property got, but the filter's setter takes a primitive boolean, and Spring's editor for Boolean allows an empty value where the one for boolean does not. So '' or a blank string comes back as null here. That is a value an application can produce: continueFilterChainOnUnsuccessfulAuthentication: with nothing after it in YAML, ...= in a properties file, or a placeholder whose environment variable is set but empty.
Today the application still fails to start, because the null reaches the filter property, but the message is "A null value cannot be assigned to a primitive type" where it used to be "Invalid boolean value []". And it only fails because the variable is captured by the bean closure: the same expression in a plain method gives false without an error, so this is one refactoring away from a blank value silently selecting the 403 entry point.
Converting to the primitive keeps every spelling the spec lists and fails on a blank value with the old message:
| boolean continueFilterChain = configuredContinue == null ? true : (Boolean) new SimpleTypeConverter().convertIfNecessary(configuredContinue, Boolean) | |
| boolean continueFilterChain = configuredContinue == null ? true : (boolean) new SimpleTypeConverter().convertIfNecessary(configuredContinue, boolean) |
I tried this locally: '', ' ' and 'maybe' fail with "Invalid boolean value", 'false' and ' On ' convert as before. Please add '' and a blank string to the "fails at startup" feature.
| // certificates are the only way in, so a request without a usable one is simply forbidden; otherwise the | ||
| // default entry point stays, which sends the request to the login form (or the Ajax equivalent) |
There was a problem hiding this comment.
"certificates are the only way in" is not what false does: the form login, remember-me and basic filters stay in the chain, and a request without a certificate passes the X.509 filter whatever the flag says (Spring only consults it when a certificate was presented and rejected). With false and no certificate, a user can still open /login/auth and log in with a password. What the flag decides here is only which entry point answers an unauthenticated request. Could the comment say that instead?
| |x509.continueFilterChainOnUnsuccessfulAuthentication | ||
| |`true` | ||
| |Whether to proceed when an authentication attempt fails to allow other authentication mechanisms to process the request | ||
| |Whether to proceed when an authentication attempt fails to allow other authentication mechanisms to process the request. When `true`, a request without a usable certificate that needs authentication is sent to the login form like any other; when `false`, certificates are the only way in and such a request gets a 403 response |
There was a problem hiding this comment.
The true half is accurate. The false half is not, on both counts:
- "certificates are the only way in": form login still works with
false, so someone reading this could believe password login is switched off when it is not. - "such a request gets a 403 response": only when no certificate is sent. A certificate that fails authentication (no such user, and I would expect the same for a disabled or locked account) is rethrown by the filter and the request ends in a server error, on every URL.
Something like: "when false, a request without a certificate that needs authentication gets a 403 response instead of the login form, and a certificate that fails authentication makes the request fail with an error. Form login remains available in both cases."
| '0' || Http403ForbiddenEntryPoint | ||
| } | ||
|
|
||
| void 'the setting not set at all keeps the default, which continues the chain'() { |
There was a problem hiding this comment.
This feature sets the key to an explicit null, which survives the config merge, so it tests "set to null" rather than "not set at all". (That is a small change of its own: an explicit null used to fail at startup and now means true. Fine, but worth the accurate name.)
The case from the two issues, the key never set and the default from DefaultSecurityConfig applying, has no feature. Could you add one that leaves the key out of the config? It fails on the base and passes here.
|
|
||
| package grails.plugin.springsecurity | ||
|
|
||
| import org.springframework.beans.BeansException |
There was a problem hiding this comment.
Nit: this import belongs with the org.springframework group below, next to Http403ForbiddenEntryPoint. The style checks do not run on test sources, so nothing flags it.
…nt the result change and its way back The entry point was decided from the setting converted to a Boolean, so a blank value became null and failed with a different message than before; it is converted to the primitive, as the filter property is, and a blank value fails with Invalid boolean value. The comment and the table of the guide said that certificates are the only way in with false, which is not what the setting does: form login stays, and a certificate that fails authentication ends in an error. The change of result with the default (302 to the login form instead of 403) has its upgrade note and skill entry, with the way to keep the 403, and the spec has a feature for the setting never set, and for blank values. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Done in 4b13b29, point by point:
Not done in this commit: the data-driven request-level spec for your table. It needs the real |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An existing entry-point class can prevent startup, and an empty login URL can too.
| } | ||
|
|
||
| authenticationEntryPoint(classFor('authenticationEntryPoint', Http403ForbiddenEntryPoint)) | ||
| if (!continueFilterChain) { |
There was a problem hiding this comment.
With useX509=true, the default continuation setting, and authenticationEntryPointBeanClass=Http403ForbiddenEntryPoint, the plugin passes a login URL to a no-argument constructor. Spring reports that it could not resolve a matching constructor. The same configuration previously started and returned 403. Do not pass the login URL to a bean that does not take one. An empty auth.loginFormUrl fails the same way for the retained login entry point.
…-on-7.0.x # Conflicts: # grails-doc/src/en/guide/upgrading/upgrading60x.adoc # grails-skills/upgrade/skills/grails-upgrade/SKILL.md
…en it takes one With useX509 and the default continue setting, the plugin keeps the login entry point and gives it auth.loginFormUrl. An application that sets authenticationEntryPointBeanClass to a class without a login-URL constructor, such as Http403ForbiddenEntryPoint, or leaves auth.loginFormUrl blank, started with X.509 before and failed at startup after that change. The entry point is now built as it was, with no arguments and answering 403, when the configured class takes no login URL or there is no URL to give it. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Two commits: a merge of 7.0.x, and The merge. It clears conflicts in The finding. With I generated the class instead of fixing the two cases: the continue setting ( The Spring Security plugin module passes (322 tests) and |
…e filter chain the plugin builds The spec builds the real springSecurityFilterChain in a GrailsWebUnitTest and sends it requests with the jakarta.servlet.request.X509Certificate attribute set, with certificates made by keytool for alice (a user), mallory (no user), and bob, who logs in with the form: fourteen rows, with the table in the Javadoc, for the default and for x509.continueFilterChainOnUnsuccessfulAuthentication = false. The five rows of the default that change with this fix (no certificate, by Ajax, a certificate with no user, and the form login steps of bob) fail on 7.0.x; the nine others pass on both, the false rows among them, which shows that only the default path moves.
|
Pushed the spec you asked for in section 2 of the review of 12-03: what an application answers, through the filter chain the plugin builds. The rest of that review is in the commit of 12-03 (
With this change all 14 pass. With the plugin as it is on 7.0.x the five rows whose answer changes fail (a request with no certificate, the same by Ajax, mallory's certificate on a secured page, and the two form-login sessions of bob with the default), and the other nine pass, the six with Two things in the rows that the table did not spell out, in case they matter to you. A login starts a new session (session fixation protection), so each step of a row follows the session the previous response made, as a browser follows its cookie. And the form login lands on
|
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. The no-argument-only entry point and a blank login URL no longer prevent startup. An entry point that has both constructors can, and a login URL that is not a redirect URL can too.
| */ | ||
| private static boolean takesLoginFormUrl(Class entryPointType, Object loginFormUrl) { | ||
| loginFormUrl instanceof CharSequence && loginFormUrl.toString().trim() && | ||
| entryPointType.constructors.any { it.parameterTypes == [String] as Class[] } |
There was a problem hiding this comment.
An existing entry point with both a no-argument constructor and a String constructor, but without the form-login setters, now fails bean initialization. Previously the no-argument constructor was used. Do not pass the login URL to a bean that does not take the form-login properties.
| * Whether the entry point can be built for the login form: it has a constructor of a login URL, and there is one to give it. | ||
| */ | ||
| private static boolean takesLoginFormUrl(Class entryPointType, Object loginFormUrl) { | ||
| loginFormUrl instanceof CharSequence && loginFormUrl.toString().trim() && |
There was a problem hiding this comment.
auth.loginFormUrl = 'login/auth' is not blank, so this check keeps the form-login entry point. Spring Security then rejects the URL and startup fails. Previously an X.509 application started because that URL was unused. Accept only a URL the entry point can redirect to.
…finition can be built The definition of the login form entry point gives the class a login URL, the Ajax login URL and six properties of the form login. With X.509 an application that configured a class with both a no-argument and a login URL constructor but without those properties, a login URL that Spring Security rejects as a redirect (login/auth), or an Ajax login URL without a leading slash, started on 7.0.x because the X.509 configuration built the bean with no arguments, and failed to start with the first version of this fix. The login form entry point is now kept only when the class has the constructor and the properties, the login URL starts with a slash or is an absolute http(s) URL, and the Ajax login URL is unset or starts with a slash; in every other case the bean is built as it always was. The table of X509EntryPointSpec has 672 rows over the continue setting, six classes, seven login URLs and four Ajax URLs, 80 of which fail with the previous check.
|
Pushed the next commit, which answers the review of 12-05 at a9f9a33. Both findings were regressions of the first version of this fix, against 7.0.x: there the X.509 configuration built the entry point with no arguments, so every application below started.
Both come from one rule that the commit now states in one place: the login form entry point is kept only when its whole definition can be built, which is what it sets and gives it. The class needs the
|
This comment has been minimized.
This comment has been minimized.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A blank login URL no longer keeps the form entry point. An entry point whose String constructor throws still does.
| private static boolean takesLoginForm(Class entryPointType, Object loginFormUrl, Object ajaxLoginFormUrl) { | ||
| loginFormUrl instanceof CharSequence && UrlUtils.isValidRedirectUrl(loginFormUrl.toString()) && | ||
| (ajaxLoginFormUrl == null || ajaxLoginFormUrl instanceof CharSequence && ajaxLoginFormUrl.toString().startsWith('/')) && | ||
| entryPointType.constructors.any { it.parameterTypes == [String] as Class[] } && |
There was a problem hiding this comment.
An entry point with both constructors and the form-login setters passes this check even when its String constructor throws. Previously the no-argument constructor was used and the application started. Now startup fails. Use the constructor that works.
…r X.509 keeps it The check predicted whether the definition of the login form entry point can be built, from the constructors, the setters and the form of the URLs. It could not know that a constructor, a setter or the initialization throws, nor that a protected constructor builds, so an entry point whose String constructor throws was kept and the application failed to start. The plugin now builds it once with the values the default definition gives it, then initializes it, and keeps the login form entry point only when that succeeds; the log names the class and the cause when it does not. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley an entry point whose
Table: 820 rows, 72 of them fail on the previous head. It varies the continue setting, the entry point class, the point where building the login form fails (the constructor, each of the six setters, the initialization, nowhere), the login URL and the Ajax URL. The module |
Replaces #16974, which was based on 7.2.x. The bug is also on 7.0.x, the oldest branch with it, so the same changes are rebuilt there. The review history is on #16974.
Fixes #15863
Fixes #15864
useX509replaced theauthenticationEntryPointbean with aHttp403ForbiddenEntryPointunconditionally. A request without a usable certificate that needed authentication therefore got a bare 403, even withx509.continueFilterChainOnUnsuccessfulAuthentication = true, which is the default and is documented as "proceed ... to allow other authentication mechanisms to process the request". Both issues report exactly that: form login cannot coexist with X.509, and the reporters worked around it with a URL mapping for the error status.The 403 entry point is now defined only when
x509.continueFilterChainOnUnsuccessfulAuthenticationisfalse, that is when certificates are the only way in. Otherwise the plugin's normal entry point stays, so the request goes to the login form (or its Ajax equivalent).x509.adocnow says what each value does.Behavior change
With the default (
true), an application that uses X.509 only and relied on the bare 403 for a request without a certificate now redirects that request to/login/auth. Such an application keeps the old behavior by settinggrails.plugin.springsecurity.x509.continueFilterChainOnUnsuccessfulAuthentication = false, which also matches what the property says. I judged that better than a new property, but it is the point for reviewers to weigh.Tests
X509EntryPointSpec(new) builds the plugin's real bean definitions withuseX509on and checks theauthenticationEntryPointbean for both values of the property. Thetruecase fails before the change (the bean was the 403 entry point).codeStyle. With the change to the plugin class taken out, 17 of the 25 tests ofX509EntryPointSpecfail.Not covered: a request with a real client certificate through a running server; the functional-test-app has no TLS setup for it.
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL