Grails 8 returning 500 error instead of 401 for an unauthenticated request
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 55/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- groovy
- Área
- authentication, security
Línea de trabajo
Start with the reproduction repo linked in the issue (tprebs/grails-8-security-401-regression) and run runGrails8.sh to see the 500. The relevant code is the re-implemented FilterSecurityInterceptor and AccessDecisionVoter path in grails-spring-security-compat, where WebExpressionVoter.vote asserts on a null Authentication. Done means an unauthenticated request to a @Secured action returns 401 with no AssertionError, and the Grails 7 behaviour is matched.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Expected Behavior
An unauthenticated request to a @Secured controller should be answered with a 401 Unauthorized. When no authentication is provided grails 8 is responding with a 500 error and an page. Previous versions of grails returned 401 Unauthorized
Actual Behaviour
On 8.0.0 the same request is answered with 500 Internal Server Error and an HTML error page, and the server log carries an AssertionError.
This happens when the filter chain legitimately contains no anonymous authentication filter, so the request reaches the authorisation interceptor with a null Authentication. That is ordinary, documented
configuration, the Spring Security Core plugin's filters.adoc describes composing a chain with
JOINED_FILTERS and filter names prefixed with -, and removing anonymousAuthenticationFilter is a
deliberate choice for a stateless API that must not grant anonymous identities:
grails.plugin.springsecurity.filterChain.chainMap = [
[
pattern: '/**',
filters: 'JOINED_FILTERS,-anonymousAuthenticationFilter,-exceptionTranslationFilter,' +
'-authenticationProcessingFilter,-securityContextPersistenceFilter,' +
'-rememberMeAuthenticationFilter'
],
]
Grails builds with no anonymous filter, as configured:
Creating filter chain: Ant [pattern='/**'],
[SecurityRequestHolderFilter, MutableLogoutFilter, RestAuthenticationFilter,
SecurityContextHolderAwareRequestFilter, RestTokenValidationFilter,
ExceptionTranslationFilter, FilterSecurityInterceptor]
An unauthenticated GET /secret then produces:
ERROR o.a.c.c.C.[.[.[.[dispatcherServletRegistration] : Servlet.service() for servlet
[dispatcherServletRegistration] threw exception [Filter execution threw an exception] with root cause
java.lang.AssertionError: authentication cannot be null. Expression: authentication. Values: authentication = null
at grails.plugin.springsecurity.web.access.expression.WebExpressionVoter.vote(WebExpressionVoter.groovy:47)
at grails.plugin.springsecurity.access.vote.AuthenticatedVetoableDecisionManager.decide(...)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.groovy:54)
ERROR o.a.c.c.C.[Tomcat].[localhost] : Exception Processing [ErrorPage[errorCode=500, location=/error]]
Cause
Spring Security 7.1.1 removed FilterSecurityInterceptor and the AccessDecisionManager/AccessDecisionVoter
API. Grails 8 ships org.apache.grails:grails-spring-security-compat to re-implement those classes in Spring
Security's own package namespace, which is why the stack frame above reads FilterSecurityInterceptor.groovy
rather than Spring's Java original.
The re-implementation omits the null-Authentication guard. Spring Security's
AbstractSecurityInterceptor.authenticateIfRequired() begins:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new AuthenticationCredentialsNotFoundException(
"An Authentication object was not found in the SecurityContext");
}
AuthenticationCredentialsNotFoundException extends AuthenticationException, so
ExceptionTranslationFilter catches it and the entry point returns 401. That is the path that works on
Grails 7.
In grails-spring-security-compat:8.0.0 there is no null guard, and the null Authentication is passed to the decision manager and reaches WebExpressionVoter, whose Groovy assert throws an AssertionError. That is an Error, not an AuthenticationException, so ExceptionTranslationFilter does not catch it and the request fails as a 500.
Steps To Reproduce
- Create a Grails 8.0.0 application with
grails-spring-securityandgrails-spring-security-rest. - Add one controller with
@Secured(['ROLE_USER']). - Set the catch-all chain in
grails-app/conf/application.groovy:
[pattern: '/**', filters: 'JOINED_FILTERS,-anonymousAuthenticationFilter'](the other exclusions above
are not required to trigger it; removing the anonymous filter is). - Request the secured action with no
Authorizationheader:curl -i http://localhost:8080/secret. - Observe
HTTP 500and an HTML error page. Expected401. - Check the server log for
java.lang.AssertionError: authentication cannot be nulloriginating in
WebExpressionVoter.vote. - Run the same application on Grails 7.2.3 and the request is answered with
401.
recreation available here https://github.com/tprebs/grails-8-security-401-regression
A minimal Grails application with one @Secured controller and two scripts:
./runGrails7.sh # passes - answered with 401
./runGrails8.sh # fails - answered with 500
Environment Information
- Grails 8.0.0
- Gradle 9.8.0,
- Java 21
- macOS
Example Application
https://github.com/tprebs/grails-8-security-401-regression
Version
8.0.0
- Lenguaje dominante
- Groovy
- Estrellas
- 2.9k
- Forks
- 976
- Merge medio
- 20 h 13 min
- PR fusionados (30 d)
- 168
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de apache/grails-core
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
apache/grails-core#16635 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
apache/grails-core#16289 · 5 comentarios ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
apache/grails-core#15764 ·
Los mantenedores suelen responder en 1 día
-
Update documentation for Java 25Posiblemente ocupada @ruthst00 la tomó hace 7 días. Abiertorelates-to: documentation weekly-action-item
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
apache/grails-core#15725 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 4/5 3-5 días Aptitud para principiantes 18/100
apache/grails-core#16634 ·
Los mantenedores suelen responder en 1 día
Todos los issues de apache/grails-core
Issues similares
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
chaitin/agent-compose#770 ·
Los mantenedores suelen responder en 1 día
-
[Bug]: Streamed /v1/chat/completions sends unredacted agent error text (keys) in the final chunkPosiblemente ocupada @rodricksz4h5 la tomó hoy. Abiertoarea/streaming comp/gateway P2 type/security
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
NousResearch/hermes-agent#138245 ·
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 84/100
ansible-middleware/amq#283 ·
-
cloudtrail_multi_region_enabled_logging_management_events fails when the advanced event selector has a custom namePosiblemente ocupada @12YYCCYY16 la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
prowler-cloud/prowler#13033 ·
Los mantenedores suelen responder en 1 día