You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by ma...@apache.org on 2026/09/22 15:17:21 UTC

(grails-core) 01/01: forge: keep the browser's favicon fetch out of the starter security feature's login flow

This is an automated email from the ASF dual-hosted git repository.

matrei pushed a commit to branch fix/scaffolding-favicon-session-8.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit a054c7aadb6d7c3adf76009fd44498793f9c45b7
Author: Mattias Reichel <ma...@gmail.com>
AuthorDate: Tue Sep 22 17:16:50 2026 +0200

    forge: keep the browser's favicon fetch out of the starter security feature's login flow
    
    The Application generated by the spring-boot-starter-security feature permitted the
    home page, /error and /assets/** but not /favicon.ico itself, so the fetch the browser
    makes for Spring Security's login and logout pages on its own was bounced through
    /login, the request that starts a session of its own whenever the cookie it carries is
    stale and so overwrites the session a user has just signed in to. The generated chain
    now permits /favicon.ico as well, and a web app's UrlMappings sends /favicon.ico to the
    icon the asset pipeline already ships instead of answering the not-found page.
    
    The mapping is emitted for web apps only: a web plugin should not claim the favicon of
    the application it is deployed into, and a REST API ships no assets.
---
 .../grails/templates/urlMappings.rocker.raw        |  4 ++
 .../feature/lang/groovy/application.rocker.raw     |  4 +-
 .../feature/grails/GrailsUrlMappingsSpec.groovy    | 63 ++++++++++++++++++++++
 .../security/SpringBootStarterSecuritySpec.groovy  |  6 ++-
 4 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/grails-forge/grails-forge-core/src/main/java/org/grails/forge/feature/grails/templates/urlMappings.rocker.raw b/grails-forge/grails-forge-core/src/main/java/org/grails/forge/feature/grails/templates/urlMappings.rocker.raw
index 40edcd62da..379cb64d53 100644
--- a/grails-forge/grails-forge-core/src/main/java/org/grails/forge/feature/grails/templates/urlMappings.rocker.raw
+++ b/grails-forge/grails-forge-core/src/main/java/org/grails/forge/feature/grails/templates/urlMappings.rocker.raw
@@ -35,6 +35,10 @@ class UrlMappings {
         }
 
         "/"(view:"/index")
+@if(applicationType == ApplicationType.WEB) {
+        // Browsers request /favicon.ico for pages that declare no icon of their own
+        "/favicon.ico"(redirect: [uri: '/assets/favicon.ico', permanent: true])
+}
 }
 @if (applicationType == ApplicationType.REST_API) {
         delete "/$controller/$id(.$format)?"(action:"delete")
diff --git a/grails-forge/grails-forge-core/src/main/java/org/grails/forge/feature/lang/groovy/application.rocker.raw b/grails-forge/grails-forge-core/src/main/java/org/grails/forge/feature/lang/groovy/application.rocker.raw
index 979d778bb1..e4ad38eba2 100644
--- a/grails-forge/grails-forge-core/src/main/java/org/grails/forge/feature/lang/groovy/application.rocker.raw
+++ b/grails-forge/grails-forge-core/src/main/java/org/grails/forge/feature/lang/groovy/application.rocker.raw
@@ -72,7 +72,9 @@ class Application extends GrailsAutoConfiguration {
         bean('filterChain', SecurityFilterChain) { HttpSecurity http ->
             http
                 .authorizeHttpRequests {
-                    it.requestMatchers('/', '/index', '/index.gsp', '/error', '/assets/**').permitAll()
+                    // The browser fetches the favicon on its own, also for the login and logout pages;
+                    // bounced through /login with a stale cookie it would start a session of its own.
+                    it.requestMatchers('/', '/index', '/index.gsp', '/error', '/favicon.ico', '/assets/**').permitAll()
                       .requestMatchers('/user/**').hasRole('ADMIN')
                       .anyRequest().authenticated()
                 }
diff --git a/grails-forge/grails-forge-core/src/test/groovy/org/grails/forge/feature/grails/GrailsUrlMappingsSpec.groovy b/grails-forge/grails-forge-core/src/test/groovy/org/grails/forge/feature/grails/GrailsUrlMappingsSpec.groovy
new file mode 100644
index 0000000000..2fee16eb31
--- /dev/null
+++ b/grails-forge/grails-forge-core/src/test/groovy/org/grails/forge/feature/grails/GrailsUrlMappingsSpec.groovy
@@ -0,0 +1,63 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.grails.forge.feature.grails
+
+import org.grails.forge.ApplicationContextSpec
+import org.grails.forge.application.ApplicationType
+import org.grails.forge.fixture.CommandOutputFixture
+import org.grails.forge.options.DevelopmentReloading
+import org.grails.forge.options.Options
+import spock.lang.Unroll
+
+class GrailsUrlMappingsSpec extends ApplicationContextSpec implements CommandOutputFixture {
+
+    private static final String FAVICON_MAPPING = '"/favicon.ico"(redirect: [uri: \'/assets/favicon.ico\', permanent: true])'
+
+    void 'a web app maps the home page, the favicon the browser asks for on its own, and the error pages'() {
+        when:
+        def urlMappings = generate(ApplicationType.WEB, new Options(DevelopmentReloading.DEVTOOLS))['grails-app/controllers/example/grails/UrlMappings.groovy']
+
+        then:
+        urlMappings.contains('package example.grails')
+        urlMappings.contains('"/$namespace/$controller/$action?/$id?(.$format)?" {}')
+        urlMappings.contains('"/"(view:"/index")')
+
+        and: 'the favicon is served from the asset pipeline instead of answering the not-found page'
+        urlMappings.contains(FAVICON_MAPPING)
+
+        and: 'the error mappings follow'
+        urlMappings.contains('"500"(view:\'/error\')')
+        urlMappings.contains('"404"(view:\'/notFound\')')
+    }
+
+    @Unroll
+    void 'a #applicationType carries no favicon mapping'(ApplicationType applicationType) {
+        when:
+        def urlMappings = generate(applicationType, new Options(DevelopmentReloading.DEVTOOLS))['grails-app/controllers/example/grails/UrlMappings.groovy']
+
+        then:
+        urlMappings != null
+        !urlMappings.contains(FAVICON_MAPPING)
+        !urlMappings.contains('favicon')
+
+        where:
+        applicationType << [ApplicationType.WEB_PLUGIN, ApplicationType.REST_API]
+    }
+}
diff --git a/grails-forge/grails-forge-core/src/test/groovy/org/grails/forge/feature/security/SpringBootStarterSecuritySpec.groovy b/grails-forge/grails-forge-core/src/test/groovy/org/grails/forge/feature/security/SpringBootStarterSecuritySpec.groovy
index 7581ec32cc..18098a7d52 100644
--- a/grails-forge/grails-forge-core/src/test/groovy/org/grails/forge/feature/security/SpringBootStarterSecuritySpec.groovy
+++ b/grails-forge/grails-forge-core/src/test/groovy/org/grails/forge/feature/security/SpringBootStarterSecuritySpec.groovy
@@ -80,11 +80,15 @@ class SpringBootStarterSecuritySpec extends ApplicationContextSpec implements Co
         !application.contains('@Import(SecurityConfig)')
 
         and: 'the home page and static assets are public, the user admin is ROLE_ADMIN, the rest requires login'
-        application.contains("it.requestMatchers('/', '/index', '/index.gsp', '/error', '/assets/**').permitAll()")
+        application.contains("it.requestMatchers('/', '/index', '/index.gsp', '/error', '/favicon.ico', '/assets/**').permitAll()")
         application.contains(".requestMatchers('/user/**').hasRole('ADMIN')")
         application.contains('.anyRequest().authenticated()')
         !application.contains('anyRequest().permitAll()')
 
+        and: "the browser's own favicon fetch is served from the assets instead of being bounced through the login page"
+        output['grails-app/controllers/example/grails/UrlMappings.groovy']
+                .contains('"/favicon.ico"(redirect: [uri: \'/assets/favicon.ico\', permanent: true])')
+
         and: 'BootStrap seeds an admin user with a generated password'
         def bootStrap = output['grails-app/init/example/grails/BootStrap.groovy']
         bootStrap.contains('import org.springframework.security.crypto.factory.PasswordEncoderFactories')