diff --git a/.agents/skills/gradle-developer/SKILL.md b/.agents/skills/gradle-developer/SKILL.md index 26b27070dbe..e77854ffe73 100644 --- a/.agents/skills/gradle-developer/SKILL.md +++ b/.agents/skills/gradle-developer/SKILL.md @@ -367,6 +367,7 @@ A module the parent manages through an imported BOM belongs to the property the 3. Use the unversioned coordinate in module `dependencies {}`. 4. Run `./gradlew :that-module:validateDependencyVersions` (and affected consumers). 5. Security overrides: comment with CVE and previous Boot version (see existing httpcore5/jackson/logback pins). +6. Hibernate ORM 7 has no pin: `grails-hibernate7-bom` reads the version `spring-boot-dependencies` manages and strictly re-declares `hibernateOrmModules` at it. To move ahead of a Spring Boot that lags behind, set `hibernate7VersionOverride` in `dependencies.gradle`; the BOM then strictly re-declares at it every `org.hibernate.orm` module `spring-boot-dependencies` manages in its own POM (`PomVersions.managedArtifactIds`), not only `hibernateOrmModules`, so `enforcedPlatform` consumers move the whole family as a Maven consumer setting `hibernate.version` does. `validateHibernateVersion` fails while it is not newer than Spring Boot's version, so a Boot bump that catches up forces its removal. It also fails when `hibernate-models.version` differs from the version the managed Hibernate ORM release depends on. Configuring the project reads only `spring-boot-dependencies`' own entry for `hibernate-core` (`PomVersions.managedVersion`), never the BOMs it imports, so a task that resolves none of them does not fail when one is missing from the cache; both checks resolve in the task's inputs, and `check` and the BOM's POM generation run it. ### Exclusions diff --git a/AGENTS.md b/AGENTS.md index f7692dabab3..b6f8579ae56 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -122,8 +122,8 @@ These human-review concerns are intentionally not mechanized; `validateRepositor |-----------|---------| | JDK | 21+ (baseline 21) | | Groovy | 6.0.x | -| Spring Boot | 4.1.x | -| Spring Framework | 7.0.x | +| Spring Boot | 4.2.x | +| Spring Framework | 7.1.x | | Spock | 2.5-groovy-6.0 | | Gradle | 9.9.x | | Jakarta EE | 10 | @@ -152,7 +152,7 @@ All managed dependency versions live in `dependencies.gradle` (the single source - **Only libraries the Grails project also works on may use a snapshot version.** Between releases, `dependencies.gradle` often points at a `-SNAPSHOT` or release-candidate version of one of our own libraries, such as the Asset Pipeline (`cloud.wondrify`), SiteMesh 3 (`org.sitemesh`), or the Grails Publish plugin (`org.apache.grails.gradle:grails-publish`). These are switched to their published version before a Grails release (see `RELEASE.md`). Do not suggest replacing them with a released version, and do not flag them in reviews. Never use a snapshot of any other library, such as Jackson or Spring. The snapshot repositories in `GrailsRepoSettingsPlugin` only serve the `org.apache.grails*`, `org.apache.groovy*`, `cloud.wondrify*`, and `org.sitemesh*` groups, so a snapshot of any other library will not resolve (unless `GRAILS_INCLUDE_MAVEN_LOCAL` is set for a local build). Do not widen those content filters to get one. - **No dynamic versions in any BOM-bearing configuration.** The same task fails when any dependency of a BOM-bearing configuration is requested with a version range, a `+` prefix, or a `latest.*` selector, even when a BOM constraint overrides it (Gradle's own `failOnDynamicVersions()` misses that case). A dynamic version makes every resolution list the module's versions in the repository once the listing cache expires, which is every CI build, so the build depends on the repository answering even when every artifact is cached. Fix it where the range enters the graph: a build script of this repository pins the version or uses the BOM-managed coordinate; for a published module, exclude the module from that dependency and declare the BOM-managed coordinate next to it (see the `ehcache` declaration in `grails-spring-security`); a dependency *constraint* with a range (marked `(constraint)`, as Hibernate publishes for `log4j-core`) is not removed by an exclusion and needs a component metadata rule. `allowedDynamicVersions` (per-project ext, `group:name` keys) is the last resort, with a commented reason. - **`groovy-callsite` is never a framework dependency.** Grails 9 compiles with invokedynamic, the Groovy 6 compiler's default, and does not ship `org.apache.groovy:groovy-callsite`; an application that opts out with `grails { indy = false }` declares the module itself, at the version the BOM manages. Do not add it to `grails-common` or any other module's `api` or `implementation`: every application would ship it. CI's `-PgrailsIndy=false` lanes get it from `CompilePlugin`, on every source set. A module whose tests load a library compiled with classic call sites, such as `testcontainers-spock`, declares it as `testRuntimeOnly` next to that library. -- **Prefer inheriting from the Spring Boot BOM.** Do not re-pin a coordinate that `spring-boot-dependencies` (4.1.x) already manages unless you are intentionally overriding it to a newer version (e.g. a security fix); note the reason inline. +- **Prefer inheriting from the Spring Boot BOM.** Do not re-pin a coordinate that `spring-boot-dependencies` already manages unless you are intentionally overriding it to a newer version (e.g. a security fix); note the reason inline. - **An override of a Spring Boot managed version must use Spring Boot's property name, and must change the version.** `validateBomProperties` (plugin `org.apache.grails.buildsrc.bom-property-validator`, run in its own CI job) checks the POM each Grails BOM publishes against `spring-boot-dependencies` (`parentBoms` in `dependencies.gradle`). It fails when a pin is declared under a different property than the one Boot controls it with - for a module Boot manages through an imported BOM, that is the import's property (`jackson-bom.version`, not `jackson3.version`) - and when a pin repeats Boot's version. Rename the version key (and the dependency keys that must prefix it) or drop the pin. - **Every version key must be used.** The same task (opt out with `-PskipBomPropertyValidation`, not `skipDependencyValidation`) fails when a version property a BOM owns (`bomVersionProperties` in each BOM's build script) is used by no entry of its published POM - a key no dependency references, or one whose dependency keys do not reduce to it. Delete the key or add the dependency that should use it. The few deliberate exceptions to these rules live, each with its reason, in `bomUnusedVersionExemptions` / `bomPropertyNameExemptions` / `bomRedundantVersionExemptions` in `dependencies.gradle`. - **No two dependencies may provide the same class.** The `buildHealth` task (the `com.autonomousapps.build-health` settings plugin, configured in `gradle/dependency-analysis.settings.gradle`, which applies it to a build only when one of its tasks is requested by full name, so a plain build never pays for it) fails when two dependencies on a classpath provide the same fully qualified class name that the module's own code references, because compile and runtime behavior then depend on classpath order. The Build Health workflow runs it on every push and pull request, alongside the code style checks. Resolve a report by removing or excluding the redundant dependency; its other categories (unused or transitive dependencies, wrong configurations) are advice only. diff --git a/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/PomVersions.groovy b/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/PomVersions.groovy index db065940092..d4a82bd4a4d 100644 --- a/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/PomVersions.groovy +++ b/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/PomVersions.groovy @@ -24,6 +24,8 @@ import java.util.regex.Pattern import groovy.transform.CompileStatic +import org.gradle.api.GradleException + import org.apache.maven.model.Dependency import org.apache.maven.model.Model import org.apache.maven.model.io.xpp3.MavenXpp3Reader @@ -68,6 +70,47 @@ class PomVersions { dependency.scope == 'import' } + /** + * Returns the version the model's own {@code } gives {@code module}, a + * {@code group:artifact}, or {@code null} when it has no entry for it. The BOMs it imports are not + * read, so this resolves no other POM, and since a BOM's own entry wins over the ones it imports, + * a version found here is the one the BOM manages. + * + * @throws GradleException when the entry's version cannot be resolved from the model's own + * properties and the Maven built-ins + */ + static String managedVersion(Model model, String module) { + Map properties = properties(model) + Dependency entry = managedDependencies(model).find { Dependency dependency -> + !isImport(dependency) && key(dependency, properties) == module + } + if (entry == null) { + return null + } + String version = interpolate(entry.version, properties) + if (!version) { + throw new GradleException("Cannot resolve ${module}:${entry.version.trim()}, managed by " + + "${properties['project.groupId']}:${model.artifactId}:${properties['project.version']}, from that POM's own properties.") + } + version + } + + /** + * Returns the artifacts of {@code groupId} the model's own {@code } manages, in + * the order it lists them. As with {@link #managedVersion}, the BOMs it imports are not read, so this + * resolves no other POM; {@code } is not read either, since it manages plugins. + * An entry whose coordinates cannot be resolved from the model's own properties is not included. + */ + static List managedArtifactIds(Model model, String groupId) { + Map properties = properties(model) + String prefix = "${groupId}:" + managedDependencies(model).findAll { Dependency dependency -> !isImport(dependency) } + .collect { Dependency dependency -> key(dependency, properties) } + .findAll { String module -> module?.startsWith(prefix) } + .collect { String module -> module.substring(prefix.length()) } + .unique() + } + /** * The properties the model's coordinates and versions may refer to: its own * {@code }, and the Maven built-ins, which are never declared there. A parent POM's diff --git a/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/SbomPlugin.groovy b/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/SbomPlugin.groovy index a1eafe93c28..e064c884773 100644 --- a/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/SbomPlugin.groovy +++ b/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/SbomPlugin.groovy @@ -136,7 +136,7 @@ class SbomPlugin implements Plugin { 'pkg:maven/org.jline/jline-terminal-jni@3.30.16?type=jar' : 'BSD-3-Clause', 'pkg:maven/org.jruby/jzlib@1.1.5?type=jar' : 'BSD-3-Clause', // https://web.archive.org/web/20240822213507/http://www.jcraft.com/jzlib/LICENSE.txt shows it's a 3 clause 'pkg:maven/org.liquibase.ext/liquibase-hibernate5@4.27.0?type=jar': 'Apache-2.0', // maps incorrectly because of https://github.com/liquibase/liquibase/issues/2445 & the base pom does not define a license - 'pkg:maven/org.json/json@20251224?type=jar' : 'Public-Domain', // required due to jedis, https://issues.apache.org/jira/browse/LEGAL-666 approves this usage + 'pkg:maven/org.json/json@20260719?type=jar' : 'Public-Domain', // required due to jedis, https://issues.apache.org/jira/browse/LEGAL-666 approves this usage // Bouncy Castle Licence is the MIT license (https://www.bouncycastle.org/licence.html); pulled in transitively by the CAS client 'pkg:maven/org.bouncycastle/bcpkix-jdk18on@1.84?type=jar' : 'MIT', 'pkg:maven/org.bouncycastle/bcprov-jdk18on@1.84?type=jar' : 'MIT', diff --git a/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/PomVersionsSpec.groovy b/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/PomVersionsSpec.groovy new file mode 100644 index 00000000000..ba12973d0a1 --- /dev/null +++ b/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/PomVersionsSpec.groovy @@ -0,0 +1,135 @@ +/* + * 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.apache.grails.buildsrc + +import java.nio.file.Path + +import org.gradle.api.GradleException +import spock.lang.Specification +import spock.lang.TempDir + +import org.apache.maven.model.Model + +/** + * Reads the version a BOM's own entries give a module, as {@code grails-hibernate7-bom} reads the + * Hibernate ORM version from {@code spring-boot-dependencies}: + * + *
+ * test:parent-bom:1.0, with test:grand-parent:1.0 as its parent
+ *   test:lib-a                    ${lib-a.version} = 1.0
+ *   test:lib-literal              5.0
+ *   ${project.groupId}:lib-own    ${project.version}
+ *   test:lib-inherited            ${inherited.version} - declared only by the parent POM
+ *   other:lib-other               2.0
+ *   test:lib-literal:tests        5.0 - the same artifact again, with a classifier
+ *   test:family-bom               ${family-bom.version} = 3.0, imported, and managing test:family-core
+ *   test:test-maven-plugin        1.0 - in pluginManagement
+ * 
+ */ +class PomVersionsSpec extends Specification { + + @TempDir + Path testDir + + Model bom + + def setup() { + File pom = testDir.resolve('parent-bom-1.0.pom').toFile() + pom.text = ''' + + 4.0.0 + testgrand-parent1.0 + test + parent-bom + 1.0 + pom + + 1.0 + 3.0 + + + + testlib-a${lib-a.version} + testlib-literal5.0 + ${project.groupId}lib-own${project.version} + testlib-inherited${inherited.version} + otherlib-other2.0 + testlib-literal5.0tests + + testfamily-bom${family-bom.version} + pomimport + + + + + + + testtest-maven-plugin1.0 + + + + +''' + bom = PomVersions.read(pom) + } + + def "an entry's version is resolved from the BOM's own properties and the Maven built-ins"() { + expect: + PomVersions.managedVersion(bom, module) == version + + where: + module || version + 'test:lib-a' || '1.0' + 'test:lib-literal' || '5.0' + 'test:lib-own' || '1.0' + } + + def "a module the BOM has no entry for has no version"() { + expect: + PomVersions.managedVersion(bom, 'test:lib-unknown') == null + } + + def "the BOMs it imports are not read, so a module only an imported BOM manages has no version"() { + expect: 'test:family-core, which only test:family-bom manages, and the imported BOM itself' + PomVersions.managedVersion(bom, 'test:family-core') == null + PomVersions.managedVersion(bom, 'test:family-bom') == null + } + + def "an entry whose version only the parent POM declares fails instead of going unread"() { + when: + PomVersions.managedVersion(bom, 'test:lib-inherited') + + then: + GradleException e = thrown() + e.message == 'Cannot resolve test:lib-inherited:${inherited.version}, managed by test:parent-bom:1.0, from that POM\'s own properties.' + } + + def "the artifacts of a group are the ones the BOM's own entries list, in their order and each once"() { + expect: 'its own group resolved, and neither the BOM it imports, the plugin it manages nor the other group' + PomVersions.managedArtifactIds(bom, 'test') == ['lib-a', 'lib-literal', 'lib-own', 'lib-inherited'] + PomVersions.managedArtifactIds(bom, 'other') == ['lib-other'] + } + + def "a group the BOM manages nothing of has no artifacts, whatever it shares a prefix with"() { + expect: + PomVersions.managedArtifactIds(bom, 'tes').isEmpty() + PomVersions.managedArtifactIds(bom, 'unknown').isEmpty() + } +} diff --git a/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/SbomPluginSpec.groovy b/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/SbomPluginSpec.groovy index fd196ba84c1..0e3b047f441 100644 --- a/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/SbomPluginSpec.groovy +++ b/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/SbomPluginSpec.groovy @@ -114,6 +114,15 @@ class SbomPluginSpec extends Specification { 'pkg:maven/org.jline/jansi@4.3.1?type=jar', bsd4Choice()).id == 'BSD-3-Clause' } + void "org.json resolved through Jedis uses the public domain license approved in LEGAL-666"() { + given: + List publicDomainChoice = [[license: [name: 'Public Domain', url: 'https://github.com/stleary/JSON-java/blob/master/LICENSE']]] + + expect: + SbomPlugin.pickLicense(LOGGER, 'grails-redis', 'grails-redis', + 'pkg:maven/org.json/json@20260719?type=jar', publicDomainChoice).name == 'Public Domain' + } + void "JLine 4 license correction is limited to JLine 4 Maven jars"() { when: SbomPlugin.pickLicense(LOGGER, 'grails-console', 'grails-console', bomRef, bsd4Choice()) diff --git a/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy b/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy index 81dfc115a43..6b146735914 100644 --- a/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy +++ b/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy @@ -83,10 +83,6 @@ class VulnerabilityScanPlugin implements Plugin { 'and no fixed release exists. A long pause can terminate the pause-detector dispatch thread ' + 'and leave its queue growing, see https://github.com/LatencyUtils/LatencyUtils/issues/22. ' + 'Remove once a patched release is available or micrometer no longer requires LatencyUtils'), - new VulnerabilityExclusion('CVE-2026-113602', 'net.minidev:json-smart', - 'Managed by spring-boot-dependencies; every release through 2.6.0, the newest, is affected and ' + - 'no fixed release exists. Strict-mode parsing accepts a trailing 0x1A character. Remove ' + - 'once a patched release ships'), new VulnerabilityExclusion('CVE-2026-110974', 'com.vaadin.external.google:android-json', 'Pulled transitively via spring-boot-cli; com.vaadin.external.google:android-json is unmaintained ' + 'and 0.0.20131108.vaadin1, its newest release, is affected. Remove once spring-boot-cli no ' + @@ -96,10 +92,11 @@ class VulnerabilityScanPlugin implements Plugin { '5.7.x through 6.5.10 as affected. The 7.x line already carries the fix for the preceding ' + 'CVE-2026-22747. Remove once Sonatype narrows its affected range'), new VulnerabilityExclusion('CVE-2026-47842', 'org.springframework.security:spring-security-crypto', - 'Sonatype flags 7.1.1, which https://spring.io/security/cve-2026-47842 lists as fixed. ' + - 'Spring Boot 4.1.2 manages 7.1.2, past that fixed release. The exclusion stays until ' + - 'Sonatype narrows its affected range past the version Boot manages. ' + - 'Remove once Sonatype corrects its affected range; reassess on any Spring Security upgrade'), + 'Sonatype flagged 7.1.1, but https://spring.io/security/cve-2026-47842 lists only 7.1.0 on ' + + 'the 7.1 line as affected and 7.1.1 as fixed. Spring Boot 4.2.0-RC2 manages 7.2.0-RC1, which ' + + 'the advisory does not list and which carries the fix (the AesCbcBytesEncryptor and ' + + 'AesGcmBytesEncryptor replacements 7.1.1 introduced). Remove once Sonatype corrects its ' + + 'affected range; reassess on any Spring Security upgrade'), ].asImmutable() @Override diff --git a/dependencies.gradle b/dependencies.gradle index 074061841a6..452d0e4844e 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -25,7 +25,7 @@ ext { 'ant.version' : '1.10.18', 'asciidoctor-gradle-jvm.version': '4.0.5', 'asciidoctorj.version' : '3.0.1', - 'asm.version' : '9.10.1', + 'asm.version' : '9.11.1', 'asset-pipeline-gradle.version' : '5.2.3', 'commons-text.version' : '1.15.0', 'directory-watcher.version' : '0.19.1', @@ -39,7 +39,7 @@ ext { 'jna.version' : '5.19.1', 'maven-model.version' : '3.9.16', 'objenesis.version' : '3.6', - 'spring-boot.version' : '4.1.2', + 'spring-boot.version' : '4.2.0-RC2', ] // Note: the name of the dependency must be the prefix of the property name so properties in the pom are resolved correctly @@ -75,46 +75,31 @@ ext { 'bootstrap-icons.version' : '1.13.2', 'bootstrap.version' : '5.3.8', 'checker-qual.version' : '3.55.1', - 'commons-codec.version' : '1.22.1', 'geb-spock.version' : '8.0.1', - // Pinned as a drift tripwire even though 25.1 matches Spring Boot 4.1.2: graphql-java- - // extended-scalars is not managed by Spring Boot. Keeping this pin makes a later Boot - // graphql-java bump force a compatibility check of extended-scalars, not an equal-version - // upgrade. See https://github.com/apache/grails-core/issues/15674 - 'graphql-java.version' : '25.1', + // Pinned deliberately as a drift tripwire even though it matches Spring Boot's + // managed version: graphql-java-extended-scalars (below) is NOT managed by Spring + // Boot and must be upgraded in lockstep with graphql-java. Keeping this pin makes + // the dependency validator flag any Spring Boot graphql-java bump so we re-check + // extended-scalars compatibility. See https://github.com/apache/grails-core/issues/15674 + 'graphql-java.version' : '26.1', 'graphql-java-extended-scalars.version': '24.0', 'groovy.version' : '6.0.0', // Security: 33.7.2 addresses CVE-2026-102554 (uncontrolled resource consumption). 'guava.version' : '33.7.2-jre', - // Groovy YAML 6.0.0 requires Jackson 2.22.x, and 2.22.4 also overrides the - // 2.21.7 Spring Boot 4.1.2 manages: it adds the StreamReadConstraints length limit to - // GregorianCalendar/Duration (CVE-2026-68497) and restricts the URI schemes - // java.nio.file.Path deserialization accepts (CVE-2026-19032). Jackson 2 only releases - // jackson-annotations on the minor, hence the separate 2.22 below. 2.22.3 also fixes - // CVE-2026-89425, CVE-2026-89407, CVE-2026-91777 and CVE-2026-91776, and 2.22.4 fixes - // CVE-2026-110879 (type id validation in jackson-databind), which 2.21.7 does not carry. + // Groovy YAML 6.0.0 depends on Jackson 2.22.2, which wins over the 2.21.8 Spring Boot 4.2 + // manages, so the BOM must manage at least that. Jackson 2 only releases + // jackson-annotations on the minor, hence the separate 2.22 below. 2.22.4 also fixes + // CVE-2026-110879 (type id validation in jackson-databind). 'jackson-2-bom.version' : '2.22.4', 'jackson2-annotations.version' : '2.22', - // Security: overrides Jackson 3.1.7 from Spring Boot 4.1.2. The 3.2.4 BOM aligns the family - // and fixes CVE-2026-89425, CVE-2026-89407, CVE-2026-91777, CVE-2026-91776 and - // CVE-2026-110879, which 3.1.7 does not carry, and CVE-2026-110729, which affects only - // jackson-core 3.2.0 to 3.2.3. + // Overrides the 3.1.8 Spring Boot 4.2 manages. Pinned first as a security override of + // 3.1.6 (CVE-2026-89425, CVE-2026-89407, CVE-2026-91777 and CVE-2026-91776), which 3.1.7 + // and later also fix; kept so that moving to Spring Boot 4.2 does not take Jackson 3 from + // the 3.2 line Grails 8 and 9.0 manage back to 3.1. 3.2.4 also fixes CVE-2026-110879, and + // CVE-2026-110729, which affects only jackson-core 3.2.0 to 3.2.3. 'jackson-bom.version' : '3.2.4', 'jquery.version' : '4.0.0', - // Security: overrides the 1.5.38 Spring Boot 4.1.2 manages. 1.6.3 strips slashes from MDC - // values before SiftingAppender uses them as discriminating keys (CVE-2026-19880), and - // 1.6.5 completes that fix (CVE-2026-104721). The 1.6.x line keeps SLF4J 2.0.x and JDK 11. - // Spring Boot 4.1.2 manages SLF4J 2.0.20, so SLF4J is not pinned. - 'logback.version' : '1.6.5', - // Security: overrides the 5.8.1 Spring Boot 4.1 manages. 5.9.2 stopped ProxySettings - // .toString() writing the SOCKS5 proxy credentials into the application log - // (CVE-2026-18710); 5.12.0 is the current release of that line. - 'mongodb.version' : '5.12.0', 'nimbus-jose-jwt.version' : '10.10', - // Overrides the 1.62.0 Spring Boot 4.1 manages, on every classpath, so the BOM manages the - // version Selenium 4.49.0 pulls in transitively - see selenium.version. Can return to Boot's - // version when the Selenium pin is dropped. - 'opentelemetry.version' : '1.65.0', // Includes the follow-up logout redirect and direct-client CSRF fixes on the 6.5.x line. 'pac4j.version' : '6.5.9', // Security: overrides the transitive 3.5.1. 4.0.3 fixes CVE-2025-67030 flagged against plexus-utils. @@ -128,13 +113,6 @@ ext { 'rxjava2.version' : '2.2.21', 'sitemesh.version' : '2.6.0', 'scribejava.version' : '8.3.3', - // Overrides the 4.43.0 Spring Boot 4.1 manages. selenium/standalone-chrome, the image - // ContainerGebSpec starts, is only published for linux/arm64 from 4.47.0, so the default - // Geb functional test runs natively on Apple silicon. The image tag must match the - // Selenium version on the test classpath, so stay on a version with a published image. - // Boot's selenium-htmlunit.version (htmlunit3-driver) stays at 4.43.0: the newest - // htmlunit3-driver needs a HtmlUnit newer than Boot manages, and nothing here uses it. - 'selenium.version' : '4.49.0', 'spock.version' : '2.5-groovy-6.0', // Not managed by spring-boot-dependencies. Spring AI 2.0.x is the Spring Boot 4.0 / 4.1 // line; grails-bom imports its BOM so applications and grails-spring-ai share one Spring AI version. @@ -166,8 +144,6 @@ ext { 'asset-pipeline-bom': "cloud.wondrify:asset-pipeline-bom:${bomDependencyVersions['asset-pipeline-bom.version']}", 'groovy-bom' : "org.apache.groovy:groovy-bom:${bomDependencyVersions['groovy.version']}", 'jackson-bom' : "tools.jackson:jackson-bom:${bomDependencyVersions['jackson-bom.version']}", - 'opentelemetry-bom' : "io.opentelemetry:opentelemetry-bom:${bomDependencyVersions['opentelemetry.version']}", - 'selenium-bom' : "org.seleniumhq.selenium:selenium-bom:${bomDependencyVersions['selenium.version']}", 'spock-bom' : "org.spockframework:spock-bom:${bomDependencyVersions['spock.version']}", 'spring-ai-bom' : "org.springframework.ai:spring-ai-bom:${bomDependencyVersions['spring-ai-bom.version']}", ] @@ -178,7 +154,6 @@ ext { 'bootstrap-icons' : "org.webjars.npm:bootstrap-icons:${bomDependencyVersions['bootstrap-icons.version']}", 'cas-client-core' : "org.apereo.cas.client:cas-client-core:${bomDependencyVersions['cas-client-core.version']}", 'checker-qual' : "org.checkerframework:checker-qual:${bomDependencyVersions['checker-qual.version']}", - 'commons-codec' : "commons-codec:commons-codec:${bomDependencyVersions['commons-codec.version']}", 'geb-spock' : "org.apache.groovy.geb:geb-spock:${bomDependencyVersions['geb-spock.version']}", 'graphql-java' : "com.graphql-java:graphql-java:${bomDependencyVersions['graphql-java.version']}", 'graphql-java-extended-scalars': "com.graphql-java:graphql-java-extended-scalars:${bomDependencyVersions['graphql-java-extended-scalars.version']}", @@ -214,7 +189,7 @@ ext { 'groovy-xml' : "org.apache.groovy:groovy-xml:${bomDependencyVersions['groovy.version']}", 'groovy-yaml' : "org.apache.groovy:groovy-yaml:${bomDependencyVersions['groovy.version']}", 'guava' : "com.google.guava:guava:${bomDependencyVersions['guava.version']}", - // Security override of the 2.21.7 spring-boot-dependencies imports through + // Overrides the 2.21.8 spring-boot-dependencies imports through // com.fasterxml.jackson:jackson-bom - see jackson-2-bom.version. groovy-yaml 6.0.0 // also pulls jackson-dataformat-yaml, so the BOM must manage it at >= the resolved version. 'jackson2-annotations' : "com.fasterxml.jackson.core:jackson-annotations:${bomDependencyVersions['jackson2-annotations.version']}", @@ -222,22 +197,11 @@ ext { 'jackson-2-bom-databind' : "com.fasterxml.jackson.core:jackson-databind:${bomDependencyVersions['jackson-2-bom.version']}", 'jackson-2-bom-dataformat-yaml': "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${bomDependencyVersions['jackson-2-bom.version']}", 'jackson-2-bom-datatype-jsr310': "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${bomDependencyVersions['jackson-2-bom.version']}", - // Security override of the 3.1.7 spring-boot-dependencies imports through + // Override of the 3.1.8 spring-boot-dependencies imports through // tools.jackson:jackson-bom - see jackson-bom.version 'jackson-bom-core' : "tools.jackson.core:jackson-core:${bomDependencyVersions['jackson-bom.version']}", 'jackson-bom-databind' : "tools.jackson.core:jackson-databind:${bomDependencyVersions['jackson-bom.version']}", 'jquery' : "org.webjars.npm:jquery:${bomDependencyVersions['jquery.version']}", - // Security override of the transitive 1.5.38 - see logback.version - 'logback-classic' : "ch.qos.logback:logback-classic:${bomDependencyVersions['logback.version']}", - 'logback-core' : "ch.qos.logback:logback-core:${bomDependencyVersions['logback.version']}", - // Security override of the 5.8.1 spring-boot-dependencies imports through - // org.mongodb:mongodb-driver-bom - see mongodb.version. The whole driver family is - // pinned so a classpath cannot end up mixing driver versions. - 'mongodb-bson' : "org.mongodb:bson:${bomDependencyVersions['mongodb.version']}", - 'mongodb-bson-record-codec': "org.mongodb:bson-record-codec:${bomDependencyVersions['mongodb.version']}", - 'mongodb-driver-core' : "org.mongodb:mongodb-driver-core:${bomDependencyVersions['mongodb.version']}", - 'mongodb-driver-reactivestreams': "org.mongodb:mongodb-driver-reactivestreams:${bomDependencyVersions['mongodb.version']}", - 'mongodb-driver-sync' : "org.mongodb:mongodb-driver-sync:${bomDependencyVersions['mongodb.version']}", // Security override of the transitive 3.5.1 - see plexus-utils.version 'plexus-utils' : "org.codehaus.plexus:plexus-utils:${bomDependencyVersions['plexus-utils.version']}", // Required alongside plexus-utils 4.x - see plexus-xml.version @@ -300,8 +264,8 @@ ext { // Drift tripwire for extended-scalars compatibility on later Boot graphql-java bumps, // not an equal-version upgrade of extended-scalars - see graphql-java.version. 'graphql-java.version', - // The Hibernate 7 BOM strictly re-declares Boot's version so enforcedPlatform consumers - // are forced to it; the pin matches Spring Boot 4.1.2 on purpose. + // The Hibernate 7 BOM strictly re-declares the version Spring Boot manages, so that + // enforcedPlatform consumers are forced to it - see hibernate7VersionOverride. 'hibernate.version', ] @@ -316,7 +280,7 @@ ext { if (project.name in ['grails-bom', 'grails-hibernate5-bom'] || project.name.startsWith('grails-data-hibernate5')) { customBomVersions = [ 'liquibase-hibernate.version': '4.27.0', - 'liquibase.version' : '4.27.0', // liquibase-hibernate5 stops at 4.27.0, and Boot 4.1.2's Liquibase 5.0.4 is a new major. + 'liquibase.version' : '4.27.0', // liquibase-hibernate5 stops at 4.27.0, and Spring Boot's Liquibase 5.0.4 is a new major. 'hibernate.version' : '5.6.15.Final', ] combinedVersions += customBomVersions @@ -355,18 +319,29 @@ ext { ] combinedDependencies += customBomDependencies } else if (project.name in ['grails-hibernate7-bom'] || project.name.startsWith('grails-data-hibernate7')) { + // Hibernate ORM follows the version spring-boot-dependencies manages. grails-hibernate7-bom reads it + // from that POM's own entries and re-declares these modules at it, strictly, under Spring Boot's + // hibernate.version. + hibernateOrmModules = ['hibernate-core', 'hibernate-envers', 'hibernate-jcache'] + // A Hibernate ORM 7 release to manage instead, for when Spring Boot lags behind one Grails needs. + // It must be newer than the version Spring Boot manages, so validateHibernateVersion, which + // check and the BOM's POM generation run, fails once Spring Boot catches up with it: remove it then. + // Set, it moves every org.hibernate.orm module spring-boot-dependencies manages in its own POM, + // not only the ones above, as setting hibernate.version moves them all for a Maven consumer. + hibernate7VersionOverride = null customBomVersions = [ 'cache-ri-impl.version' : '1.1.1', 'derby.version' : '10.17.1.0', - // Spring Boot 4.1.2 selects Hibernate ORM 7.4.13.Final. This BOM strictly re-declares - // that version so enforcedPlatform consumers are forced to it; a platform() import - // is not enforced. 7.4.13.Final still requires hibernate-models 1.1.2. + // Spring Boot 4.2 can select Hibernate ORM 7.4.x through Spring Dependency + // Management, and Hibernate ORM 7.4.x requires hibernate-models 1.1.x. This BOM + // strictly re-declares the version so enforcedPlatform consumers are forced to it; a + // platform() import is not enforced. validateHibernateVersion fails when it differs + // from the version the managed Hibernate ORM release depends on. 'hibernate-models.version' : '1.1.2', 'hibernate-tools.version' : '7.3.13.Final', - 'hibernate.version' : '7.4.13.Final', 'jandex.version' : '3.6.0', 'liquibase-test-harness.version': '1.0.11', - 'liquibase.version' : '4.27.0', // liquibase-hibernate5 stops at 4.27.0, and Boot 4.1.2's Liquibase 5.0.4 is a new major. + 'liquibase.version' : '4.27.0', // liquibase-hibernate5 stops at 4.27.0, and Spring Boot's Liquibase 5.0.4 is a new major. 'mockito-inline.version' : '5.2.0', ] combinedVersions += customBomVersions @@ -376,9 +351,6 @@ ext { 'derbyclient' : "org.apache.derby:derbyclient:${combinedVersions['derby.version']}", 'derbyshared' : "org.apache.derby:derbyshared:${combinedVersions['derby.version']}", 'derbytools' : "org.apache.derby:derbytools:${combinedVersions['derby.version']}", - 'hibernate-core' : "org.hibernate.orm:hibernate-core:${combinedVersions['hibernate.version']}", - 'hibernate-envers' : "org.hibernate.orm:hibernate-envers:${combinedVersions['hibernate.version']}", - 'hibernate-jcache' : "org.hibernate.orm:hibernate-jcache:${combinedVersions['hibernate.version']}", 'hibernate-models' : "org.hibernate.models:hibernate-models:${combinedVersions['hibernate-models.version']}", 'hibernate-tools-orm' : "org.hibernate.tool:hibernate-tools-orm:${combinedVersions['hibernate-tools.version']}", 'hibernate-tools-utils' : "org.hibernate.tool:hibernate-tools-utils:${combinedVersions['hibernate-tools.version']}", diff --git a/end-to-end/README.md b/end-to-end/README.md index 8a12bc8bb2b..1462cd80360 100644 --- a/end-to-end/README.md +++ b/end-to-end/README.md @@ -81,6 +81,12 @@ else checks that a plugin's configuration reaches one. On a JVM the file is foun classes, in its JAR; an image has no JARs, so there it is found only through the copy the build makes of it under the plugin's name. +And it calls a method of a JDK class that nothing but its `src/native/resources/META-INF/native-image/reachability-metadata.json` +registers, which is the file `traceNativeMetadata` merges what it records into. Nothing else checks that what a trace +recorded reaches an image, and an image built without it starts and fails only on the requests that needed it. The class is +named by a property, because GraalVM resolves a reflective lookup of a constant name while it builds the image, which needs no +metadata and would pass whatever this file held. + ```bash cd end-to-end ./gradlew :native-i18n:check # the JVM half, part of `check` diff --git a/end-to-end/native-i18n/grails-app/conf/application.yml b/end-to-end/native-i18n/grails-app/conf/application.yml index 55e2ec9c1c0..b3c2e54c256 100644 --- a/end-to-end/native-i18n/grails-app/conf/application.yml +++ b/end-to-end/native-i18n/grails-app/conf/application.yml @@ -25,3 +25,8 @@ spring: # Not a base name, so resolving an unknown code to itself proves the rest of # MessageSourceProperties was bound too - they all arrive together or not at all. use-code-as-default-message: true +native: + # Named here, so that an image has to find it at runtime: a lookup of a constant name is resolved while the + # image is built, and needs no metadata. Registered only by src/native/resources, where a trace records. + recorded: + type: java.util.zip.Adler32 diff --git a/end-to-end/native-i18n/grails-app/init/nativei18n/Application.groovy b/end-to-end/native-i18n/grails-app/init/nativei18n/Application.groovy index cf43229fa42..15e46eb22ab 100644 --- a/end-to-end/native-i18n/grails-app/init/nativei18n/Application.groovy +++ b/end-to-end/native-i18n/grails-app/init/nativei18n/Application.groovy @@ -49,7 +49,9 @@ import org.springframework.context.MessageSource * derived from {@code spring.messages.basename} alone.

* *

It also reads a value from the plugin's {@code plugin.yml}, which on a JVM is found beside the plugin's - * classes and in an image only through the copy the build makes of it under the plugin's name.

+ * classes and in an image only through the copy the build makes of it under the plugin's name, and calls a method + * that in an image only the metadata in {@code src/native/resources} registers, where {@code traceNativeMetadata} + * records.

* *

Every run is told which environment it should have started in, through the property * {@code i18n.check.environment}, and fails in any other. An image reads its environment from the build @@ -121,6 +123,12 @@ class Application extends GrailsAutoConfiguration { "native.plugin.configuration was '${configured}'") } + // Not a bundle either, but what traceNativeMetadata records, read from where it records it. A JDK class, + // because every class of the application is registered from the build output whether this works or not. + String recorded = context.environment.getProperty('native.recorded.type') + Class recordedType = Class.forName(recorded) + recordedType.getMethod('getValue').invoke(recordedType.getDeclaredConstructor().newInstance()) + println "i18n-check: in ${expectedEnvironment}, every message bundle resolved, and the plugin configuration was read" } catch (Throwable failure) { diff --git a/end-to-end/native-i18n/src/native/resources/META-INF/native-image/reachability-metadata.json b/end-to-end/native-i18n/src/native/resources/META-INF/native-image/reachability-metadata.json new file mode 100644 index 00000000000..003d74ec218 --- /dev/null +++ b/end-to-end/native-i18n/src/native/resources/META-INF/native-image/reachability-metadata.json @@ -0,0 +1,11 @@ +{ + "reflection": [ + { + "type": "java.util.zip.Adler32", + "methods": [ + { "name": "", "parameterTypes": [] }, + { "name": "getValue", "parameterTypes": [] } + ] + } + ] +} diff --git a/gradle/rat-root-config.gradle b/gradle/rat-root-config.gradle index 83403a8d7af..a7c09b10ce5 100644 --- a/gradle/rat-root-config.gradle +++ b/gradle/rat-root-config.gradle @@ -178,6 +178,7 @@ tasks.named('rat') { '**/*.log', // exclude log files 'local-tasks.gradle', // exclude local helper scripts '**/spring-configuration-metadata.json', // JSON files cannot contain license headers + 'end-to-end/native-i18n/src/native/resources/META-INF/native-image/reachability-metadata.json', // JSON files cannot contain license headers 'grails-test-examples/spring-security/cas/test1/src/main/resources/cas/services/*.json', // CAS service registry definitions; JSON files cannot contain license headers 'grails-test-examples/benchmarks/src/reportTest/resources/jmh-golden/**', // byte-exact JMH report fixtures; a license header would alter the compared bytes ] + rootProject.subprojects.collectMany { project -> diff --git a/grails-async/core/src/main/groovy/grails/async/PromiseList.groovy b/grails-async/core/src/main/groovy/grails/async/PromiseList.groovy index fcdffb9da9e..dd7b6c4c25c 100644 --- a/grails-async/core/src/main/groovy/grails/async/PromiseList.groovy +++ b/grails-async/core/src/main/groovy/grails/async/PromiseList.groovy @@ -119,7 +119,9 @@ class PromiseList implements Promise> { @Override Promise> then(Closure callable) { - return Promises.onComplete(promises, { List values -> return values }).then(callable) + // onComplete makes the closure's result the List value, which its Closure parameter does not express + Closure allValues = { List values -> return values } + return Promises.onComplete(promises, allValues).then(callable) } /** diff --git a/grails-bom/hibernate7/build.gradle b/grails-bom/hibernate7/build.gradle index ca629ae6704..9203f403387 100644 --- a/grails-bom/hibernate7/build.gradle +++ b/grails-bom/hibernate7/build.gradle @@ -17,6 +17,9 @@ * under the License. */ +import org.apache.maven.model.Model + +import org.apache.grails.buildsrc.PomVersions import org.apache.grails.gradle.tasks.bom.ExtractDependenciesTask import org.apache.grails.gradle.tasks.bom.ExtractedDependencyConstraint import org.apache.grails.gradle.tasks.bom.PropertyNameCalculator @@ -53,6 +56,84 @@ ext { ] } +def pomFile = { String coordinates -> + Configuration pom = configurations.detachedConfiguration(dependencies.create("${coordinates}@pom")) + pom.transitive = false + pom.singleFile +} + +// Hibernate ORM follows the version spring-boot-dependencies manages, unless dependencies.gradle sets +// hibernate7VersionOverride to a newer release. Configuring this project reads only the parent BOMs' own +// entries, not the BOMs they import, so a task that needs none of them does not fail when one is missing +// from the cache; validateHibernateVersion checks the rest +List parentBomModels = parentBoms.collect { String parentBom -> PomVersions.read(pomFile(parentBom)) } +String springBootHibernateVersion = parentBomModels.findResult { Model parentBom -> + PomVersions.managedVersion(parentBom, 'org.hibernate.orm:hibernate-core') +} +String overrideHibernateVersion = hibernate7VersionOverride ?: '' +String hibernateVersion = overrideHibernateVersion ?: springBootHibernateVersion +// An override moves every Hibernate ORM module the parent BOMs manage, not only the ones Grails uses: an +// enforcedPlatform consumer would otherwise get it for those and Spring Boot's version for the rest, such +// as hibernate-hikaricp, while a Maven consumer setting the published hibernate.version moves them all +List managedHibernateOrmModules = overrideHibernateVersion ? + (hibernateOrmModules + parentBomModels.collectMany { Model parentBom -> + PomVersions.managedArtifactIds(parentBom, 'org.hibernate.orm') + }).unique() : + hibernateOrmModules +if (!hibernateVersion) { + throw new GradleException("${parentBoms.join(', ')} does not manage org.hibernate.orm:hibernate-core in its own POM: " + + 'set hibernate7VersionOverride in dependencies.gradle to the Hibernate ORM 7 release to manage') +} + +// Gradle's conflict resolution selects the newer of two requested versions, by its own version ordering +Provider newerHibernateVersion = provider { + if (!overrideHibernateVersion || !springBootHibernateVersion) { + return '' + } + Configuration bothVersions = configurations.detachedConfiguration( + dependencies.create("org.hibernate.orm:hibernate-core:${overrideHibernateVersion}"), + dependencies.create("org.hibernate.orm:hibernate-core:${springBootHibernateVersion}")) + bothVersions.transitive = false + bothVersions.incoming.resolutionResult.allComponents + .find { it.moduleVersion.name == 'hibernate-core' }.moduleVersion.version +} +// hibernate-models is strictly re-declared too, so it must move with Hibernate ORM +Provider ormHibernateModels = provider { + PomVersions.read(pomFile("org.hibernate.orm:hibernate-core:${hibernateVersion}")).dependencies + .find { it.groupId == 'org.hibernate.models' && it.artifactId == 'hibernate-models' }?.version ?: '' +} +String managedHibernateModels = customBomVersions['hibernate-models.version'] +String bootHibernateVersion = springBootHibernateVersion ?: '' +def validateHibernateVersion = tasks.register('validateHibernateVersion') { Task task -> + task.group = 'verification' + task.description = 'Validates that hibernate7VersionOverride is newer than the Hibernate ORM version Spring Boot manages, ' + + 'and that hibernate-models.version is the version the managed Hibernate ORM release depends on.' + task.inputs.property('newerHibernateVersion', newerHibernateVersion) + task.inputs.property('ormHibernateModels', ormHibernateModels) + task.doLast { + if (overrideHibernateVersion && bootHibernateVersion && + (overrideHibernateVersion == bootHibernateVersion || newerHibernateVersion.get() != overrideHibernateVersion)) { + throw new GradleException("hibernate7VersionOverride ${overrideHibernateVersion} is not newer than the " + + "Hibernate ORM ${bootHibernateVersion} that spring-boot-dependencies manages: remove it from dependencies.gradle") + } + String ormModels = ormHibernateModels.get() + if (ormModels && ormModels != managedHibernateModels) { + throw new GradleException("Hibernate ORM ${hibernateVersion} depends on hibernate-models ${ormModels}, " + + "but dependencies.gradle manages ${managedHibernateModels}: set hibernate-models.version to ${ormModels}") + } + } +} +tasks.named('check') { Task task -> task.dependsOn(validateHibernateVersion) } +tasks.withType(GenerateMavenPom).configureEach { GenerateMavenPom task -> task.dependsOn(validateHibernateVersion) } + +customBomVersions['hibernate.version'] = hibernateVersion +combinedVersions['hibernate.version'] = hibernateVersion +for (String module : managedHibernateOrmModules) { + String coordinates = "org.hibernate.orm:${module}:${hibernateVersion}" + customBomDependencies[module] = coordinates + combinedDependencies[module] = coordinates +} + dependencies { api platform(project(':grails-base-bom')) diff --git a/grails-controllers/src/main/groovy/org/apache/grails/controllers/aot/ControllerRuntimeHints.java b/grails-controllers/src/main/groovy/org/apache/grails/controllers/aot/ControllerRuntimeHints.java index d8aacb80b36..4417cb2eebc 100644 --- a/grails-controllers/src/main/groovy/org/apache/grails/controllers/aot/ControllerRuntimeHints.java +++ b/grails-controllers/src/main/groovy/org/apache/grails/controllers/aot/ControllerRuntimeHints.java @@ -20,11 +20,14 @@ import java.security.Principal; +import jakarta.servlet.http.HttpServletResponse; + import org.jspecify.annotations.Nullable; import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.http.HttpMethod; import grails.artefact.Controller; import grails.artefact.controller.support.AllowedMethodsHelper; @@ -69,7 +72,13 @@ public class ControllerRuntimeHints implements RuntimeHintsRegistrar { ResponseRenderer.class, // a view asking who is logged in reaches the request's principal, and Groovy makes that // call on the interface rather than the implementation the container supplies - Principal.class + Principal.class, + // Groovy's == calls compareTo on a Comparable unless the two are the same instance, so binding a command + // object reaches this for every method but POST, and a trace that only submits forms never records it + HttpMethod.class, + // the method check below refuses a method through the response, which a trace cannot record, since it fails on + // a request that answers with an error + HttpServletResponse.class }; /** diff --git a/grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsFormContentFilterAutoConfiguration.java b/grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsFormContentFilterAutoConfiguration.java index 17381dcfebd..fb649c0e1d0 100644 --- a/grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsFormContentFilterAutoConfiguration.java +++ b/grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsFormContentFilterAutoConfiguration.java @@ -35,8 +35,8 @@ /** * Guarantees a {@link FormContentFilter} for every Grails servlet web application, so - * {@code application/x-www-form-urlencoded} bodies of {@code PUT}, {@code PATCH} and {@code DELETE} - * requests are parsed into request parameters and are visible through the standard + * {@code application/x-www-form-urlencoded} bodies of {@code PUT}, {@code PATCH}, {@code DELETE} and + * {@code QUERY} requests are parsed into request parameters and are visible through the standard * {@code request.getParameter(...)} API. Only that content type is affected — other request bodies * (JSON, multipart, etc.) are untouched and remain available to data binding. * @@ -69,7 +69,7 @@ public OrderedFormContentFilter formContentFilter() { /** * Warns, once at startup, that form-content parsing has been switched off. Registered only when * {@code spring.mvc.formcontent.filter.enabled=false} is set and no {@link FormContentFilter} is - * present, so a deliberate opt-out is visible rather than silently leaving PUT/PATCH/DELETE form + * present, so a deliberate opt-out is visible rather than silently leaving PUT/PATCH/DELETE/QUERY form * bodies unparsed — for controllers or for a plugin such as Spring Security. */ @Bean @@ -77,7 +77,7 @@ public OrderedFormContentFilter formContentFilter() { @ConditionalOnProperty(name = "spring.mvc.formcontent.filter.enabled", havingValue = "false") public FormContentParsingDisabledWarning formContentParsingDisabledWarning() { logger.warn("Form-content parsing is disabled (spring.mvc.formcontent.filter.enabled=false): " + - "form-encoded PUT, PATCH and DELETE request bodies will not be parsed into request " + + "form-encoded PUT, PATCH, DELETE and QUERY request bodies will not be parsed into request " + "parameters, for controllers or for the Spring Security filter chain."); return new FormContentParsingDisabledWarning(); } diff --git a/grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsSecurityHeadersFilter.java b/grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsSecurityHeadersFilter.java index 2a676bed6e0..b27a0378cf1 100644 --- a/grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsSecurityHeadersFilter.java +++ b/grails-controllers/src/main/groovy/org/grails/plugins/web/controllers/GrailsSecurityHeadersFilter.java @@ -24,6 +24,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.UnaryOperator; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; @@ -39,6 +40,8 @@ import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.ForwardedHeaderUtils; +import grails.web.http.ContentSecurityPolicyNonce; + /** * Applies baseline browser hardening response headers. * @@ -57,6 +60,15 @@ * adds. See {@link GrailsSecurityHeadersProperties.Defaults} for the controlling * setting.

* + *

A {@code Content-Security-Policy} value may contain the {@code {nonce}} placeholder that + * Spring Security 7.2 introduced, for example {@code script-src 'self' 'nonce-{nonce}'}. The + * request then carries a {@link ContentSecurityPolicyNonce}, which a page renders into the + * {@code nonce} attribute of an inline script or style with the {@code cspNonce} tag, and every + * placeholder is replaced with that nonce when the header is written. The request attribute it is + * carried under is the one Spring Security's {@code ContentSecurityPolicyNonceGeneratingFilter} + * sets by default, so a request that already carries Spring Security's nonce keeps it, and the + * header and the view agree on one nonce whichever generated it.

+ * *

When {@link GrailsSecurityHeadersProperties#isEnabled()} is {@code false} the filter * passes every request through untouched. The auto-configuration does not register the * filter at all in that case; the check here covers filters an application constructs @@ -73,9 +85,12 @@ public class GrailsSecurityHeadersFilter extends OncePerRequestFilter { static final List REVERSE_PROXY_REQUEST_HEADERS = List.of( "Forwarded", "X-Forwarded-For", "X-Forwarded-Proto", "X-Forwarded-Host", "Via", "X-Real-IP"); + static final String NONCE_PLACEHOLDER = "{nonce}"; + private static final Logger logger = LoggerFactory.getLogger(GrailsSecurityHeadersFilter.class); private static final String HTTPS = "https"; + private static final String FORWARDED = "Forwarded"; private final GrailsSecurityHeadersProperties properties; @@ -85,6 +100,8 @@ public class GrailsSecurityHeadersFilter extends OncePerRequestFilter { private final Set blankValueLogged = ConcurrentHashMap.newKeySet(); + private final AtomicBoolean unreadableNonceLogged = new AtomicBoolean(); + public GrailsSecurityHeadersFilter(GrailsSecurityHeadersProperties properties) { this(properties, false); } @@ -114,8 +131,14 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse } boolean applyDefaults = shouldApplyDefaults(request); boolean secure = isEnabled(this.properties.getHsts()) && isSecure(request); + // Left alone when the request already has one: a nonce this filter set on an earlier + // dispatch of the same request, or the one Spring Security's nonce filter set when its + // chain runs ahead of this filter. Either is what a view of this request renders. + if (usesNonce() && request.getAttribute(ContentSecurityPolicyNonce.ATTRIBUTE) == null) { + request.setAttribute(ContentSecurityPolicyNonce.ATTRIBUTE, new ContentSecurityPolicyNonce()); + } SecurityHeadersResponseWrapper wrapped = new SecurityHeadersResponseWrapper(response, - () -> writeHeaders(response, applyDefaults, secure)); + () -> writeHeaders(request, response, applyDefaults, secure)); try { filterChain.doFilter(request, wrapped); } @@ -124,7 +147,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse } } - private void writeHeaders(HttpServletResponse response, boolean applyDefaults, boolean secure) { + private void writeHeaders(HttpServletRequest request, HttpServletResponse response, boolean applyDefaults, + boolean secure) { applyHeader(response, "X-Content-Type-Options", "content-type-options", this.properties.getContentTypeOptions(), applyDefaults); applyHeader(response, "X-Frame-Options", "frame-options", this.properties.getFrameOptions(), applyDefaults); @@ -134,7 +158,37 @@ private void writeHeaders(HttpServletResponse response, boolean applyDefaults, b applyHeader(response, "Strict-Transport-Security", "hsts", this.properties.getHsts(), applyDefaults); } applyHeader(response, "Content-Security-Policy", "content-security-policy", - this.properties.getContentSecurityPolicy(), applyDefaults); + this.properties.getContentSecurityPolicy(), applyDefaults, (policy) -> withNonce(policy, request)); + } + + private boolean usesNonce() { + GrailsSecurityHeadersProperties.Header policy = this.properties.getContentSecurityPolicy(); + return isEnabled(policy) && policy.getValue() != null && policy.getValue().contains(NONCE_PLACEHOLDER); + } + + /** + * The policy with each {@code {nonce}} replaced by the nonce the request carries when the + * header is written, or {@code null} when the request carries none that can be read. + */ + private String withNonce(String policy, HttpServletRequest request) { + if (!policy.contains(NONCE_PLACEHOLDER)) { + return policy; + } + if (request.getAttribute(ContentSecurityPolicyNonce.ATTRIBUTE) == null) { + // Removed after this filter set it, so the header gets one of its own. + request.setAttribute(ContentSecurityPolicyNonce.ATTRIBUTE, new ContentSecurityPolicyNonce()); + } + String nonce = ContentSecurityPolicyNonce.nonceOf(request); + if (nonce == null) { + if (this.unreadableNonceLogged.compareAndSet(false, true)) { + logger.warn("The Content-Security-Policy uses {} but the {} request attribute holds a {}, which " + + "is neither a Grails nor a Spring Security nonce, so the header is not sent.", + NONCE_PLACEHOLDER, ContentSecurityPolicyNonce.ATTRIBUTE, + request.getAttribute(ContentSecurityPolicyNonce.ATTRIBUTE).getClass().getName()); + } + return null; + } + return policy.replace(NONCE_PLACEHOLDER, nonce); } private boolean shouldApplyDefaults(HttpServletRequest request) { @@ -171,11 +225,10 @@ private static String detectReverseProxyHeader(HttpServletRequest request) { /** * Whether the client connection is secure. Falls back to the scheme a reverse proxy - * forwards in the RFC 7239 {@code Forwarded} header or in {@code X-Forwarded-Proto} - * (and {@code X-Forwarded-Ssl}), read the same way Spring's {@code ForwardedHeaderFilter} - * reads them, when the container itself saw plain HTTP. That is the case behind a - * TLS-terminating proxy that has not been configured through - * {@code server.forward-headers-strategy}. Trusting the forwarded scheme is safe for + * forwards in the RFC 7239 {@code Forwarded} header when the request has one, otherwise in + * {@code X-Forwarded-Proto} (and {@code X-Forwarded-Ssl}), when the container itself saw + * plain HTTP. That is the case behind a TLS-terminating proxy that has not been configured + * through {@code server.forward-headers-strategy}. Trusting the forwarded scheme is safe for * HSTS: user agents ignore a {@code Strict-Transport-Security} header received over a * non-secure transport (RFC 6797, section 8.1), so a spoofed header on a plain * connection has no effect. @@ -190,7 +243,14 @@ private static boolean isSecure(HttpServletRequest request) { // scheme alone rather than to its URL: java.net.URI rejects request paths the // container may accept, such as those allowed through Tomcat's relaxedPathChars. URI base = URI.create(request.getScheme() + "://localhost"); - String scheme = ForwardedHeaderUtils.adaptFromForwardedHeaders(base, headers).build().getScheme(); + ForwardedHeaderUtils.ForwardedInfo forwarded; + if (StringUtils.hasText(headers.getFirst(FORWARDED))) { + forwarded = ForwardedHeaderUtils.parseStandardHeader(base, headers); + } + else { + forwarded = ForwardedHeaderUtils.parseXForwardedHeaders(base, headers); + } + String scheme = forwarded.uriComponentsBuilder().build().getScheme(); return HTTPS.equalsIgnoreCase(scheme); } catch (IllegalArgumentException malformedForwardedHeaders) { @@ -205,6 +265,11 @@ private static boolean isEnabled(GrailsSecurityHeadersProperties.Header header) private void applyHeader(HttpServletResponse response, String name, String propertyName, GrailsSecurityHeadersProperties.Header header, boolean applyDefaults) { + applyHeader(response, name, propertyName, header, applyDefaults, UnaryOperator.identity()); + } + + private void applyHeader(HttpServletResponse response, String name, String propertyName, + GrailsSecurityHeadersProperties.Header header, boolean applyDefaults, UnaryOperator resolve) { if (!isEnabled(header)) { return; } @@ -220,7 +285,10 @@ private void applyHeader(HttpServletResponse response, String name, String prope return; } if (!response.containsHeader(name)) { - response.setHeader(name, header.getValue()); + String value = resolve.apply(header.getValue()); + if (value != null) { + response.setHeader(name, value); + } } } } diff --git a/grails-controllers/src/test/groovy/org/apache/grails/controllers/aot/ControllerRuntimeHintsSpec.groovy b/grails-controllers/src/test/groovy/org/apache/grails/controllers/aot/ControllerRuntimeHintsSpec.groovy index 32bf2ccba48..eafbb5f9db1 100644 --- a/grails-controllers/src/test/groovy/org/apache/grails/controllers/aot/ControllerRuntimeHintsSpec.groovy +++ b/grails-controllers/src/test/groovy/org/apache/grails/controllers/aot/ControllerRuntimeHintsSpec.groovy @@ -18,11 +18,14 @@ */ package org.apache.grails.controllers.aot +import jakarta.servlet.http.HttpServletResponse + import grails.artefact.controller.support.AllowedMethodsHelper import org.springframework.aot.hint.MemberCategory import org.springframework.aot.hint.RuntimeHints import org.springframework.aot.hint.TypeReference +import org.springframework.http.HttpMethod import spock.lang.Specification /** @@ -52,6 +55,16 @@ class ControllerRuntimeHintsSpec extends Specification { registered(AllowedMethodsHelper) } + void 'the response the method check refuses a method through is registered'() { + expect: 'a trace fails on a request that answers with an error, so it cannot record this' + registered(HttpServletResponse) + } + + void 'the comparison of HTTP methods binding a command object reaches is registered'() { + expect: 'Groovy compares two different methods with compareTo, so a trace that only submits forms never records it' + registered(HttpMethod) + } + void 'the controller trait Groovy dispatches through is registered'() { expect: registered(grails.artefact.Controller) diff --git a/grails-controllers/src/test/groovy/org/grails/plugins/web/controllers/GrailsSecurityHeadersAutoConfigurationSpec.groovy b/grails-controllers/src/test/groovy/org/grails/plugins/web/controllers/GrailsSecurityHeadersAutoConfigurationSpec.groovy index a61c75152e5..e1dbb7c63fe 100644 --- a/grails-controllers/src/test/groovy/org/grails/plugins/web/controllers/GrailsSecurityHeadersAutoConfigurationSpec.groovy +++ b/grails-controllers/src/test/groovy/org/grails/plugins/web/controllers/GrailsSecurityHeadersAutoConfigurationSpec.groovy @@ -37,11 +37,14 @@ import org.springframework.context.annotation.Configuration import org.springframework.mock.web.MockFilterChain import org.springframework.mock.web.MockHttpServletRequest import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.security.web.header.ContentSecurityPolicyNonceGeneratingFilter import org.springframework.security.web.header.HeaderWriterFilter +import org.springframework.security.web.header.writers.ContentSecurityPolicyHeaderWriter import org.springframework.security.web.header.writers.ReferrerPolicyHeaderWriter import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter import org.springframework.web.filter.CharacterEncodingFilter +import grails.web.http.ContentSecurityPolicyNonce import org.grails.web.config.http.GrailsFilters import spock.lang.Specification @@ -277,6 +280,170 @@ class GrailsSecurityHeadersAutoConfigurationSpec extends Specification { response.getHeader('Content-Security-Policy') == "default-src 'self'" } + void 'each {nonce} in the Content-Security-Policy is the nonce a view of the request renders'() { + given: + def request = new MockHttpServletRequest('GET', '/') + def response = new MockHttpServletResponse() + def properties = cspProperties("script-src 'self' 'nonce-{nonce}'; style-src 'nonce-{nonce}'") + String rendered = null + FilterChain view = { viewRequest, viewResponse -> + rendered = ContentSecurityPolicyNonce.nonceOf(viewRequest as HttpServletRequest) + } as FilterChain + + when: + new GrailsSecurityHeadersFilter(properties).doFilter(request, response, view) + + then: '128 random bits, Base64 without padding, as Spring Security generates a nonce' + rendered ==~ /[A-Za-z0-9+\/]{22}/ + response.getHeader('Content-Security-Policy') == + "script-src 'self' 'nonce-${rendered}'; style-src 'nonce-${rendered}'" + } + + void 'every request gets a nonce of its own'() { + given: + def filter = new GrailsSecurityHeadersFilter(cspProperties("script-src 'nonce-{nonce}'")) + + when: + def first = new MockHttpServletResponse() + filter.doFilter(new MockHttpServletRequest('GET', '/'), first, new MockFilterChain()) + def second = new MockHttpServletResponse() + filter.doFilter(new MockHttpServletRequest('GET', '/'), second, new MockFilterChain()) + + then: + first.getHeader('Content-Security-Policy') ==~ /script-src 'nonce-[A-Za-z0-9+\/]{22}'/ + first.getHeader('Content-Security-Policy') != second.getHeader('Content-Security-Policy') + } + + @Unroll + void 'a request carries no nonce when the Content-Security-Policy #description'() { + given: + def request = new MockHttpServletRequest('GET', '/') + def response = new MockHttpServletResponse() + def properties = cspProperties(policy) + properties.contentSecurityPolicy.enabled = enabled + Object carried = 'unset' + FilterChain view = { viewRequest, viewResponse -> + carried = viewRequest.getAttribute('_csp') + } as FilterChain + + when: + new GrailsSecurityHeadersFilter(properties).doFilter(request, response, view) + + then: + carried == null + response.getHeader('Content-Security-Policy') == header + + where: + description | enabled | policy || header + 'has no {nonce}' | true | "default-src 'self'" || "default-src 'self'" + 'is not sent' | false | "script-src 'nonce-{nonce}'" || null + } + + void 'an error dispatch of the request keeps the nonce its view rendered'() { + given: + def request = new MockHttpServletRequest('GET', '/') + def filter = new GrailsSecurityHeadersFilter(cspProperties("script-src 'nonce-{nonce}'")) + def original = new MockHttpServletResponse() + filter.doFilter(request, original, new MockFilterChain()) + String rendered = ContentSecurityPolicyNonce.nonceOf(request) + + when: 'the container dispatches the same request to its error page' + request.dispatcherType = DispatcherType.ERROR + def errorPage = new MockHttpServletResponse() + filter.doFilter(request, errorPage, new MockFilterChain()) + + then: + original.getHeader('Content-Security-Policy') == "script-src 'nonce-${rendered}'" + errorPage.getHeader('Content-Security-Policy') == "script-src 'nonce-${rendered}'" + } + + void 'Spring Security writing its own nonce-based policy inside the filter wins, with the nonce the view renders'() { + given: 'the chain the headers DSL builds when its policy uses {nonce}: the nonce filter, then the header writer' + def request = new MockHttpServletRequest('GET', '/') + def response = new MockHttpServletResponse() + def springSecurityNonce = new ContentSecurityPolicyNonceGeneratingFilter() + def springSecurityHeaders = new HeaderWriterFilter([ + new ContentSecurityPolicyHeaderWriter("script-src 'self' 'nonce-{nonce}'") + ]) + def view = new NonceRenderingServlet() + + when: + new GrailsSecurityHeadersFilter(cspProperties("default-src 'none'; script-src 'nonce-{nonce}'")) + .doFilter(request, response, new MockFilterChain(view, springSecurityNonce, springSecurityHeaders)) + + then: + response.contentAsString ==~ /[A-Za-z0-9+\/]{22}/ + response.getHeader('Content-Security-Policy') == "script-src 'self' 'nonce-${response.contentAsString}'" + } + + void 'the filter writes the nonce Spring Security put on the request when its chain runs ahead of the filter'() { + given: 'spring.security.filter.order ahead of the Grails filter, so the Grails writer is the inner one and writes first' + def request = new MockHttpServletRequest('GET', '/') + def response = new MockHttpServletResponse() + def springSecurityNonce = new ContentSecurityPolicyNonceGeneratingFilter() + def springSecurityHeaders = new HeaderWriterFilter([ + new ContentSecurityPolicyHeaderWriter("script-src 'self' 'nonce-{nonce}'") + ]) + def grails = new GrailsSecurityHeadersFilter(cspProperties("default-src 'none'; script-src 'nonce-{nonce}'")) + def view = new NonceRenderingServlet() + + when: + springSecurityNonce.doFilter(request, response, + new MockFilterChain(view, springSecurityHeaders, grails)) + + then: 'the Grails policy, with the nonce Spring Security generated and the view rendered' + response.contentAsString ==~ /[A-Za-z0-9+\/]{22}/ + response.getHeader('Content-Security-Policy') == + "default-src 'none'; script-src 'nonce-${response.contentAsString}'" + } + + void 'a request attribute that holds no nonce leaves the policy unsent and is warned about once'() { + given: + def logCapture = new LogCapture(GrailsSecurityHeadersFilter) + def filter = new GrailsSecurityHeadersFilter(cspProperties("script-src 'nonce-{nonce}'")) + + when: 'two requests arrive with something else under the attribute' + def first = new MockHttpServletResponse() + def firstRequest = new MockHttpServletRequest('GET', '/') + firstRequest.setAttribute('_csp', 'not a nonce') + filter.doFilter(firstRequest, first, new MockFilterChain()) + def second = new MockHttpServletResponse() + def secondRequest = new MockHttpServletRequest('GET', '/') + secondRequest.setAttribute('_csp', 'not a nonce') + filter.doFilter(secondRequest, second, new MockFilterChain()) + + then: 'a policy that names no nonce would block every inline script the view nonced' + first.getHeader('Content-Security-Policy') == null + second.getHeader('Content-Security-Policy') == null + first.getHeader('X-Content-Type-Options') == 'nosniff' + + and: + def warnings = logCapture.events.findAll { it.level == Level.WARN } + warnings.size() == 1 + warnings[0].formattedMessage.contains('{nonce}') + warnings[0].formattedMessage.contains('_csp') + warnings[0].formattedMessage.contains(String.name) + + cleanup: + logCapture.close() + } + + void 'a nonce-based policy binds from application configuration'() { + expect: + new WebApplicationContextRunner() + .withPropertyValues('grails.security.headers.content-security-policy.enabled=true', + "grails.security.headers.content-security-policy.value=script-src 'self' 'nonce-{nonce}'") + .withConfiguration(AutoConfigurations.of(GrailsSecurityHeadersAutoConfiguration)) + .run { context -> + def request = new MockHttpServletRequest('GET', '/') + def response = new MockHttpServletResponse() + context.getBean(GrailsSecurityHeadersFilter).doFilter(request, response, new MockFilterChain()) + String nonce = ContentSecurityPolicyNonce.nonceOf(request) + assert nonce ==~ /[A-Za-z0-9+\/]{22}/ + assert response.getHeader('Content-Security-Policy') == "script-src 'self' 'nonce-${nonce}'" + } + } + void 'filter respects per-header disable switches and existing response headers'() { given: def request = new MockHttpServletRequest('GET', '/') @@ -903,6 +1070,8 @@ class GrailsSecurityHeadersAutoConfigurationSpec extends Specification { 'Forwarded' | 'for=192.0.2.60; PROTO=https' || true 'Forwarded' | 'for=192.0.2.60;proto=http, proto=https' || false 'Forwarded' | 'for=192.0.2.60' || false + 'Forwarded' | 'for=unknown;proto=https' || true + 'Forwarded' | 'for="[2001:db8:cafe::17]:4711";proto=https' || true 'X-Forwarded-Ssl' | 'on' || true 'X-Forwarded-For' | '192.0.2.60' || false } @@ -930,6 +1099,30 @@ class GrailsSecurityHeadersAutoConfigurationSpec extends Specification { 'X-Forwarded-Port' | 'not-a-port' 'X-Forwarded-Host' | 'a:b:c' 'Forwarded' | 'host=a:b:c;proto=https' + 'Forwarded' | 'for=2001:db8:cafe::17;proto=https' + } + + @Unroll + void 'HSTS reads only the Forwarded header when the request has one: #headerName: #headerValue'() { + given: 'X-Forwarded-* headers are not consulted next to a Forwarded header' + def request = new MockHttpServletRequest('GET', '/') + request.secure = false + request.addHeader('Forwarded', 'for=192.0.2.60') + request.addHeader(headerName, headerValue) + def response = new MockHttpServletResponse() + def properties = new GrailsSecurityHeadersProperties() + properties.hsts.enabled = true + + when: + new GrailsSecurityHeadersFilter(properties).doFilter(request, response, new MockFilterChain()) + + then: + response.getHeader('Strict-Transport-Security') == null + + where: + headerName | headerValue + 'X-Forwarded-Proto' | 'https' + 'X-Forwarded-Ssl' | 'on' } @Unroll @@ -1211,6 +1404,22 @@ class GrailsSecurityHeadersAutoConfigurationSpec extends Specification { outcome = defaultsSent ? 'receives' : 'does not receive' } + private static GrailsSecurityHeadersProperties cspProperties(String policy) { + def properties = new GrailsSecurityHeadersProperties() + properties.contentSecurityPolicy.enabled = true + properties.contentSecurityPolicy.value = policy + properties + } + + /** Renders the nonce of the request as its body, as a view renders it into a script's nonce attribute. */ + static class NonceRenderingServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) { + response.writer.write(ContentSecurityPolicyNonce.nonceOf(request)) + } + } + @Configuration(proxyBeanMethods = false) static class UserFilterAndRegistration { diff --git a/grails-core/src/main/groovy/grails/config/Settings.groovy b/grails-core/src/main/groovy/grails/config/Settings.groovy index c5f7bf615f9..ed946a6ccb9 100644 --- a/grails-core/src/main/groovy/grails/config/Settings.groovy +++ b/grails-core/src/main/groovy/grails/config/Settings.groovy @@ -412,6 +412,18 @@ interface Settings { * @since 9.0 */ String SETTING_EXCEPTION_RESOLVER_SPRING_HANDLERS_DEFAULT = 'false' + + /** + * Whether an exception a controller action throws, that no {@code @ExceptionHandler} method handles, is written as an + * RFC 9457 problem, as Spring MVC writes one with {@code ProblemDetailsConfigurer.renderUnhandledExceptions}, except + * to a client that prefers HTML or where a URL mapping names the exception, which the Grails exception resolver + * resolves as without it. It turns on {@link #SETTING_EXCEPTION_RESOLVER_SPRING_HANDLERS} unless the application sets + * that, since a problem is the last resort after those methods. Defaults to {@code false}. + * + * @since 9.0 + */ + String SETTING_EXCEPTION_RESOLVER_PROBLEM_DETAILS = 'grails.exceptionresolver.problemDetails' + /** * The class to use for stacktrace filtering. Should be an instanceof {@link org.grails.exceptions.reporting.StackTraceFilterer} */ diff --git a/grails-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/grails-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json index fdffe19f262..fcadbeaaa3d 100644 --- a/grails-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/grails-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -250,6 +250,12 @@ "description": "Whether an exception a controller action throws is resolved first by the @ExceptionHandler methods Spring MVC resolves those of a handler method with: those of the controller's class and of the @ControllerAdvice beans that apply to it, Spring Boot's handler of spring.mvc.problemdetails.enabled among them. One none of them handles is resolved as before. Grails 10 makes it the default.", "defaultValue": false }, + { + "name": "grails.exceptionresolver.problemDetails", + "type": "java.lang.Boolean", + "description": "Whether an exception a controller action throws, that no @ExceptionHandler method handles, is written as an RFC 9457 problem, as Spring MVC writes one with ProblemDetailsConfigurer.renderUnhandledExceptions, except to a client that prefers HTML or where a URL mapping names the exception, which the Grails exception resolver resolves as without it. It turns on grails.exceptionresolver.springHandlers unless the application sets that.", + "defaultValue": false + }, { "name": "grails.controllers.responseBody", "type": "java.lang.Boolean", diff --git a/grails-core/src/test/groovy/grails/boot/config/GeneratedArtifactsInjectionSpec.groovy b/grails-core/src/test/groovy/grails/boot/config/GeneratedArtifactsInjectionSpec.groovy index f981bd7cd84..16889bb1d8d 100644 --- a/grails-core/src/test/groovy/grails/boot/config/GeneratedArtifactsInjectionSpec.groovy +++ b/grails-core/src/test/groovy/grails/boot/config/GeneratedArtifactsInjectionSpec.groovy @@ -29,10 +29,10 @@ import jakarta.annotation.PostConstruct import jakarta.annotation.PreDestroy import jakarta.annotation.Resource -import org.springframework.aot.generate.ClassNameGenerator import org.springframework.aot.generate.DefaultGenerationContext import org.springframework.aot.generate.GeneratedFiles import org.springframework.aot.generate.InMemoryGeneratedFiles +import org.springframework.aot.generate.NameGenerator import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.config.AutowireCapableBeanFactory import org.springframework.beans.factory.config.BeanDefinition @@ -303,7 +303,7 @@ class GeneratedArtifactsInjectionSpec extends Specification { InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles() DefaultGenerationContext generationContext = new DefaultGenerationContext( - new ClassNameGenerator(ClassName.get(getClass().packageName, 'Injection')), generatedFiles) + new NameGenerator(ClassName.get(getClass().packageName, 'Injection')), generatedFiles) ClassName initializer = new ApplicationContextAotGenerator().processAheadOfTime(generating, generationContext) generationContext.writeGeneratedContent() generating.close() diff --git a/grails-core/src/test/groovy/org/apache/grails/core/aot/ApplicationBytecodeBeanFactoryInitializationAotProcessorSpec.groovy b/grails-core/src/test/groovy/org/apache/grails/core/aot/ApplicationBytecodeBeanFactoryInitializationAotProcessorSpec.groovy index 22b343029f4..c9bdf411b89 100644 --- a/grails-core/src/test/groovy/org/apache/grails/core/aot/ApplicationBytecodeBeanFactoryInitializationAotProcessorSpec.groovy +++ b/grails-core/src/test/groovy/org/apache/grails/core/aot/ApplicationBytecodeBeanFactoryInitializationAotProcessorSpec.groovy @@ -20,7 +20,6 @@ package org.apache.grails.core.aot import java.util.function.LongBinaryOperator -import org.springframework.aot.generate.ClassNameGenerator import org.springframework.aot.generate.DefaultGenerationContext import org.springframework.aot.generate.InMemoryGeneratedFiles import org.springframework.aot.hint.MemberCategory @@ -37,6 +36,7 @@ import com.example.nativeapp.AuditedOrderFixture import com.example.nativeapp.NativeApplicationFixture import com.example.nativeapp.OrderStatusFixture import com.example.nativeapp.TotalsFixture +import org.springframework.aot.generate.NameGenerator /** * Covers what Groovy does reflectively on behalf of an application's own classes being possible in an image. @@ -56,7 +56,7 @@ class ApplicationBytecodeBeanFactoryInitializationAotProcessorSpec extends Speci def contribution = new ApplicationBytecodeBeanFactoryInitializationAotProcessor() .processAheadOfTime(context.beanFactory) def generation = new DefaultGenerationContext( - new ClassNameGenerator(ClassName.get('com.example', 'Hints')), new InMemoryGeneratedFiles()) + new NameGenerator(ClassName.get('com.example', 'Hints')), new InMemoryGeneratedFiles()) contribution.applyTo(generation, Mock(BeanFactoryInitializationCode)) generation.runtimeHints } diff --git a/grails-core/src/test/groovy/org/apache/grails/core/aot/ArtefactClassesBeanFactoryInitializationAotProcessorSpec.groovy b/grails-core/src/test/groovy/org/apache/grails/core/aot/ArtefactClassesBeanFactoryInitializationAotProcessorSpec.groovy index 2dc463e03f2..039286af4ff 100644 --- a/grails-core/src/test/groovy/org/apache/grails/core/aot/ArtefactClassesBeanFactoryInitializationAotProcessorSpec.groovy +++ b/grails-core/src/test/groovy/org/apache/grails/core/aot/ArtefactClassesBeanFactoryInitializationAotProcessorSpec.groovy @@ -18,10 +18,10 @@ */ package org.apache.grails.core.aot -import org.springframework.aot.generate.ClassNameGenerator import org.springframework.aot.generate.DefaultGenerationContext import org.springframework.aot.generate.GeneratedFiles import org.springframework.aot.generate.InMemoryGeneratedFiles +import org.springframework.aot.generate.NameGenerator import org.springframework.context.aot.ApplicationContextAotGenerator import org.springframework.context.support.GenericApplicationContext import org.springframework.javapoet.ClassName @@ -56,7 +56,7 @@ class ArtefactClassesBeanFactoryInitializationAotProcessorSpec extends Specifica InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles() DefaultGenerationContext generationContext = new DefaultGenerationContext( - new ClassNameGenerator(ClassName.get('com.example', 'Subject')), generatedFiles) + new NameGenerator(ClassName.get('com.example', 'Subject')), generatedFiles) new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext) generationContext.writeGeneratedContent() @@ -146,7 +146,7 @@ class ArtefactClassesBeanFactoryInitializationAotProcessorSpec extends Specifica private List writtenBy(contribution) { InMemoryGeneratedFiles files = new InMemoryGeneratedFiles() DefaultGenerationContext generation = new DefaultGenerationContext( - new ClassNameGenerator(ClassName.get('com.example', 'Written')), files) + new NameGenerator(ClassName.get('com.example', 'Written')), files) new ApplicationContextAotGenerator().processAheadOfTime(context, generation) generation.writeGeneratedContent() String source = files.getGeneratedFiles(GeneratedFiles.Kind.SOURCE) diff --git a/grails-core/src/test/groovy/org/apache/grails/core/aot/ArtefactReflectionBeanFactoryInitializationAotProcessorSpec.groovy b/grails-core/src/test/groovy/org/apache/grails/core/aot/ArtefactReflectionBeanFactoryInitializationAotProcessorSpec.groovy index 494abbd6e72..0ff759d6d7e 100644 --- a/grails-core/src/test/groovy/org/apache/grails/core/aot/ArtefactReflectionBeanFactoryInitializationAotProcessorSpec.groovy +++ b/grails-core/src/test/groovy/org/apache/grails/core/aot/ArtefactReflectionBeanFactoryInitializationAotProcessorSpec.groovy @@ -18,7 +18,6 @@ */ package org.apache.grails.core.aot -import org.springframework.aot.generate.ClassNameGenerator import org.springframework.aot.generate.DefaultGenerationContext import org.springframework.aot.generate.InMemoryGeneratedFiles import org.springframework.aot.hint.MemberCategory @@ -36,6 +35,7 @@ import grails.plugins.GrailsPlugin import grails.plugins.GrailsPluginManager import org.grails.core.DefaultGrailsServiceClass import org.grails.core.DefaultGrailsUrlMappingsClass +import org.springframework.aot.generate.NameGenerator /** * Covers what the framework creates reflectively as it registers artefacts being creatable in an image: @@ -64,7 +64,7 @@ class ArtefactReflectionBeanFactoryInitializationAotProcessorSpec extends Specif def contribution = new ArtefactReflectionBeanFactoryInitializationAotProcessor() .processAheadOfTime(context.beanFactory) def generation = new DefaultGenerationContext( - new ClassNameGenerator(ClassName.get('com.example', 'Hints')), new InMemoryGeneratedFiles()) + new NameGenerator(ClassName.get('com.example', 'Hints')), new InMemoryGeneratedFiles()) contribution.applyTo(generation, Mock(BeanFactoryInitializationCode)) generation.runtimeHints } diff --git a/grails-core/src/test/groovy/org/apache/grails/core/aot/BeanReflectionBeanFactoryInitializationAotProcessorSpec.groovy b/grails-core/src/test/groovy/org/apache/grails/core/aot/BeanReflectionBeanFactoryInitializationAotProcessorSpec.groovy index bd4384f9eb0..367e14b4972 100644 --- a/grails-core/src/test/groovy/org/apache/grails/core/aot/BeanReflectionBeanFactoryInitializationAotProcessorSpec.groovy +++ b/grails-core/src/test/groovy/org/apache/grails/core/aot/BeanReflectionBeanFactoryInitializationAotProcessorSpec.groovy @@ -18,9 +18,9 @@ */ package org.apache.grails.core.aot -import org.springframework.aot.generate.ClassNameGenerator import org.springframework.aot.generate.DefaultGenerationContext import org.springframework.aot.generate.InMemoryGeneratedFiles +import org.springframework.aot.generate.NameGenerator import org.springframework.aot.hint.MemberCategory import org.springframework.aot.hint.RuntimeHints import org.springframework.aot.hint.predicate.RuntimeHintsPredicates @@ -62,7 +62,7 @@ class BeanReflectionBeanFactoryInitializationAotProcessorSpec extends Specificat private RuntimeHints hints() { def contribution = new BeanReflectionBeanFactoryInitializationAotProcessor().processAheadOfTime(context.beanFactory) def generation = new DefaultGenerationContext( - new ClassNameGenerator(ClassName.get('com.example', 'Hints')), new InMemoryGeneratedFiles()) + new NameGenerator(ClassName.get('com.example', 'Hints')), new InMemoryGeneratedFiles()) contribution.applyTo(generation, Mock(BeanFactoryInitializationCode)) generation.runtimeHints } diff --git a/grails-core/src/test/groovy/org/apache/grails/core/aot/GeneratedInjectionBeanRegistrationAotProcessorSpec.groovy b/grails-core/src/test/groovy/org/apache/grails/core/aot/GeneratedInjectionBeanRegistrationAotProcessorSpec.groovy index e59632f6e0a..acf10f43d6a 100644 --- a/grails-core/src/test/groovy/org/apache/grails/core/aot/GeneratedInjectionBeanRegistrationAotProcessorSpec.groovy +++ b/grails-core/src/test/groovy/org/apache/grails/core/aot/GeneratedInjectionBeanRegistrationAotProcessorSpec.groovy @@ -18,10 +18,10 @@ */ package org.apache.grails.core.aot -import org.springframework.aot.generate.ClassNameGenerator import org.springframework.aot.generate.DefaultGenerationContext import org.springframework.aot.generate.GeneratedFiles import org.springframework.aot.generate.InMemoryGeneratedFiles +import org.springframework.aot.generate.NameGenerator import org.springframework.beans.factory.support.RootBeanDefinition import org.springframework.context.aot.ApplicationContextAotGenerator import org.springframework.context.support.GenericApplicationContext @@ -45,7 +45,7 @@ class GeneratedInjectionBeanRegistrationAotProcessorSpec extends Specification { private String generatedSource() { InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles() DefaultGenerationContext generationContext = new DefaultGenerationContext( - new ClassNameGenerator(ClassName.get('com.example', 'Subject')), generatedFiles) + new NameGenerator(ClassName.get('com.example', 'Subject')), generatedFiles) new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext) generationContext.writeGeneratedContent() diff --git a/grails-core/src/test/groovy/org/apache/grails/core/aot/PluginConfigurationBeanFactoryInitializationAotProcessorSpec.groovy b/grails-core/src/test/groovy/org/apache/grails/core/aot/PluginConfigurationBeanFactoryInitializationAotProcessorSpec.groovy index f9b42c5f510..43be8986a6f 100644 --- a/grails-core/src/test/groovy/org/apache/grails/core/aot/PluginConfigurationBeanFactoryInitializationAotProcessorSpec.groovy +++ b/grails-core/src/test/groovy/org/apache/grails/core/aot/PluginConfigurationBeanFactoryInitializationAotProcessorSpec.groovy @@ -20,7 +20,6 @@ package org.apache.grails.core.aot import java.nio.file.Path -import org.springframework.aot.generate.ClassNameGenerator import org.springframework.aot.generate.DefaultGenerationContext import org.springframework.aot.generate.GeneratedFiles.Kind import org.springframework.aot.generate.InMemoryGeneratedFiles @@ -35,6 +34,7 @@ import grails.plugins.GrailsPlugin import grails.plugins.GrailsPluginManager import org.apache.grails.core.plugins.PluginClassFixtures import org.apache.grails.core.plugins.PluginUtils +import org.springframework.aot.generate.NameGenerator /** * Covers each plugin's configuration reaching an application running on generated code, where every class is @@ -81,7 +81,7 @@ class PluginConfigurationBeanFactoryInitializationAotProcessorSpec extends Speci private DefaultGenerationContext generate() { def generation = new DefaultGenerationContext( - new ClassNameGenerator(ClassName.get('com.example', 'Configuration')), new InMemoryGeneratedFiles()) + new NameGenerator(ClassName.get('com.example', 'Configuration')), new InMemoryGeneratedFiles()) new PluginConfigurationBeanFactoryInitializationAotProcessor().processAheadOfTime(context.beanFactory) .applyTo(generation, Mock(BeanFactoryInitializationCode)) generation diff --git a/grails-core/src/test/groovy/org/apache/grails/core/aot/VarargsBeanRegistrationAotProcessorSpec.groovy b/grails-core/src/test/groovy/org/apache/grails/core/aot/VarargsBeanRegistrationAotProcessorSpec.groovy index 734335ad381..80d917cd79e 100644 --- a/grails-core/src/test/groovy/org/apache/grails/core/aot/VarargsBeanRegistrationAotProcessorSpec.groovy +++ b/grails-core/src/test/groovy/org/apache/grails/core/aot/VarargsBeanRegistrationAotProcessorSpec.groovy @@ -18,10 +18,10 @@ */ package org.apache.grails.core.aot -import org.springframework.aot.generate.ClassNameGenerator import org.springframework.aot.generate.DefaultGenerationContext import org.springframework.aot.generate.GeneratedFiles import org.springframework.aot.generate.InMemoryGeneratedFiles +import org.springframework.aot.generate.NameGenerator import org.springframework.beans.factory.config.RuntimeBeanReference import org.springframework.beans.factory.support.AbstractBeanDefinition import org.springframework.beans.factory.support.RootBeanDefinition @@ -59,7 +59,7 @@ class VarargsBeanRegistrationAotProcessorSpec extends Specification { InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles() DefaultGenerationContext generationContext = new DefaultGenerationContext( - new ClassNameGenerator(ClassName.get('com.example', 'Subject')), generatedFiles) + new NameGenerator(ClassName.get('com.example', 'Subject')), generatedFiles) new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext) generationContext.writeGeneratedContent() diff --git a/grails-core/src/test/groovy/org/grails/plugins/CoreGrailsPluginAotSpec.groovy b/grails-core/src/test/groovy/org/grails/plugins/CoreGrailsPluginAotSpec.groovy index 6d8fef80df6..9d11e3306a2 100644 --- a/grails-core/src/test/groovy/org/grails/plugins/CoreGrailsPluginAotSpec.groovy +++ b/grails-core/src/test/groovy/org/grails/plugins/CoreGrailsPluginAotSpec.groovy @@ -19,9 +19,9 @@ package org.grails.plugins import org.springframework.aot.AotDetector -import org.springframework.aot.generate.ClassNameGenerator import org.springframework.aot.generate.DefaultGenerationContext import org.springframework.aot.generate.InMemoryGeneratedFiles +import org.springframework.aot.generate.NameGenerator import org.springframework.beans.factory.BeanRegistrar import org.springframework.beans.factory.support.BeanRegistryAdapter import org.springframework.beans.factory.support.GenericBeanDefinition @@ -149,7 +149,7 @@ class CoreGrailsPluginAotSpec extends Specification { and: def generationContext = new DefaultGenerationContext( - new ClassNameGenerator(ClassName.get('org.apache.grails.common.aot.test', 'CoreAotTest')), + new NameGenerator(ClassName.get('org.apache.grails.common.aot.test', 'CoreAotTest')), new InMemoryGeneratedFiles()) when: 'the context is processed exactly as the processAot build task processes it' @@ -173,7 +173,7 @@ class CoreGrailsPluginAotSpec extends Specification { and: def generationContext = new DefaultGenerationContext( - new ClassNameGenerator(ClassName.get('org.apache.grails.common.aot.test', 'LiveInstanceAotTest')), + new NameGenerator(ClassName.get('org.apache.grails.common.aot.test', 'LiveInstanceAotTest')), new InMemoryGeneratedFiles()) when: diff --git a/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/Schema.groovy b/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/Schema.groovy index d0af2bd49de..d873110f301 100644 --- a/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/Schema.groovy +++ b/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/Schema.groovy @@ -23,10 +23,12 @@ import graphql.schema.DataFetcher import graphql.schema.GraphQLCodeRegistry import graphql.schema.GraphQLFieldDefinition import graphql.schema.GraphQLInputType +import graphql.schema.GraphQLNamedType import graphql.schema.GraphQLObjectType import graphql.schema.GraphQLOutputType import graphql.schema.GraphQLSchema import graphql.schema.GraphQLType +import graphql.schema.GraphQLTypeUtil import groovy.transform.CompileStatic import javassist.Modifier import org.grails.datastore.mapping.model.MappingContext @@ -531,9 +533,16 @@ class Schema { schemaInterceptor.interceptSchema(queryType, mutationType, additionalTypes) } + // graphql-java only accepts named types as additional types. Unwrapping list and + // non-null wrappers adds the type they wrap, which is what a wrapped type + // contributed to the schema before graphql-java 26 narrowed the signature. + Set namedAdditionalTypes = additionalTypes.collect(new LinkedHashSet()) { GraphQLType type -> + GraphQLTypeUtil.unwrapAll(type) + } + GraphQLSchema.Builder schema = GraphQLSchema.newSchema() .codeRegistry(codeRegistry.build()) - .additionalTypes(additionalTypes) + .additionalTypes(namedAdditionalTypes) GraphQLObjectType mutation = mutationType.build() if (mutation.fieldDefinitions) { diff --git a/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/QueryComplexityLimitsSpec.groovy b/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/QueryComplexityLimitsSpec.groovy new file mode 100644 index 00000000000..2d90b624843 --- /dev/null +++ b/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/QueryComplexityLimitsSpec.groovy @@ -0,0 +1,105 @@ +/* + * 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.gorm.graphql + +import graphql.ExecutionInput +import graphql.ParseAndValidate +import graphql.schema.GraphQLSchema +import graphql.validation.QueryComplexityLimits +import graphql.validation.ValidationError +import graphql.validation.ValidationErrorType +import org.grails.datastore.mapping.config.Settings +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.gorm.graphql.domain.general.GeneralPackage +import org.grails.gorm.graphql.domain.hibernate.HibernatePackage +import org.grails.orm.hibernate.HibernateDatastore +import spock.lang.AutoCleanup +import spock.lang.Shared +import spock.lang.Specification + +/** + * graphql-java 26 enforces query complexity limits by default; the upgrade guide tells + * applications to adjust them with {@link QueryComplexityLimits#setDefaultLimits}. + */ +class QueryComplexityLimitsSpec extends Specification { + + @Shared @AutoCleanup HibernateDatastore hibernateDatastore + @Shared GraphQLSchema schema + + void setupSpec() { + hibernateDatastore = new HibernateDatastore( + DatastoreUtils.createPropertyResolver(Collections.singletonMap(Settings.SETTING_DB_CREATE, 'create-drop')), + GeneralPackage.getPackage(), HibernatePackage.getPackage()) + schema = new Schema(hibernateDatastore.mappingContext).generate() + } + + void cleanup() { + QueryComplexityLimits.setDefaultLimits(QueryComplexityLimits.DEFAULT) + } + + void 'a query nested 100 levels deep passes the default limits'() { + expect: + errors(nestedQuery(100)).empty + } + + void 'a query nested more than 100 levels deep fails the default limits'() { + expect: + errors(nestedQuery(101)) == [ValidationErrorType.MaxQueryDepthExceeded.name()] + } + + void 'raised default limits let a deeper query pass'() { + given: + QueryComplexityLimits.setDefaultLimits(QueryComplexityLimits.newLimits() + .maxDepth(150) + .maxFieldsCount(200_000) + .build()) + + expect: + errors(nestedQuery(150)).empty + errors(nestedQuery(151)) == [ValidationErrorType.MaxQueryDepthExceeded.name()] + } + + void 'the limits can be turned off'() { + given: + QueryComplexityLimits.setDefaultLimits(QueryComplexityLimits.NONE) + + expect: + errors(nestedQuery(160)).empty + } + + /** + * DebugCircular refers to itself through otherCircular, so a query can nest as deep as + * needed: debugBarList and circular are the first two levels, every otherCircular adds one + * and id is the last. + */ + private static String nestedQuery(int depth) { + int nested = depth - 3 + '{ debugBarList { circular { ' + 'otherCircular { ' * nested + 'id' + ' }' * nested + ' } } }' + } + + /** + * The validation error types, or the class of any other error, so a query the parser + * rejects (graphql-java stops parsing past about 165 levels) cannot pass as valid. + */ + private List errors(String query) { + ParseAndValidate.parseAndValidate(schema, ExecutionInput.newExecutionInput(query).build()).errors.collect { + it instanceof ValidationError ? ((ValidationError) it).validationErrorType.name() : it.getClass().simpleName + } + } +} diff --git a/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/SchemaInterceptorSpec.groovy b/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/SchemaInterceptorSpec.groovy new file mode 100644 index 00000000000..62dfb81a9d6 --- /dev/null +++ b/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/SchemaInterceptorSpec.groovy @@ -0,0 +1,87 @@ +/* + * 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.gorm.graphql + +import graphql.Scalars +import graphql.schema.GraphQLFieldDefinition +import graphql.schema.GraphQLList +import graphql.schema.GraphQLNonNull +import graphql.schema.GraphQLObjectType +import graphql.schema.GraphQLSchema +import graphql.schema.GraphQLType +import org.grails.datastore.mapping.config.Settings +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.model.PersistentEntity +import org.grails.gorm.graphql.domain.general.GeneralPackage +import org.grails.gorm.graphql.domain.hibernate.HibernatePackage +import org.grails.gorm.graphql.interceptor.GraphQLSchemaInterceptor +import org.grails.gorm.graphql.interceptor.manager.DefaultGraphQLInterceptorManager +import org.grails.orm.hibernate.HibernateDatastore +import spock.lang.AutoCleanup +import spock.lang.Shared +import spock.lang.Specification + +class SchemaInterceptorSpec extends Specification { + + @Shared @AutoCleanup HibernateDatastore hibernateDatastore + + void setupSpec() { + hibernateDatastore = new HibernateDatastore( + DatastoreUtils.createPropertyResolver(Collections.singletonMap(Settings.SETTING_DB_CREATE, 'create-drop')), + GeneralPackage.getPackage(), HibernatePackage.getPackage()) + } + + void 'additional types from a schema interceptor are added to the schema, unwrapped from list and non-null'() { + given: + GraphQLObjectType standalone = objectType('Standalone') + GraphQLObjectType wrapped = objectType('Wrapped') + def interceptorManager = new DefaultGraphQLInterceptorManager() + interceptorManager.registerInterceptor(new GraphQLSchemaInterceptor() { + @Override + void interceptEntity(PersistentEntity entity, + List queryFields, + List mutationFields) { + } + + @Override + void interceptSchema(GraphQLObjectType.Builder queryType, + GraphQLObjectType.Builder mutationType, + Set additionalTypes) { + additionalTypes << standalone + additionalTypes << GraphQLNonNull.nonNull(GraphQLList.list(wrapped)) + } + }) + def schema = new Schema(hibernateDatastore.mappingContext) + schema.interceptorManager = interceptorManager + + when: + GraphQLSchema graphQLSchema = schema.generate() + + then: + graphQLSchema.getType('Standalone') == standalone + graphQLSchema.getType('Wrapped') == wrapped + } + + private static GraphQLObjectType objectType(String name) { + GraphQLObjectType.newObject() + .name(name) + .field(GraphQLFieldDefinition.newFieldDefinition().name('value').type(Scalars.GraphQLString)) + .build() + } +} diff --git a/grails-data-mongodb/embedded/build.gradle b/grails-data-mongodb/embedded/build.gradle index c595107b6e1..4476932ad76 100644 --- a/grails-data-mongodb/embedded/build.gradle +++ b/grails-data-mongodb/embedded/build.gradle @@ -55,6 +55,9 @@ dependencies { implementation 'org.slf4j:slf4j-api', { // impl: Logger, LoggerFactory } + implementation 'io.netty:netty-buffer', { + // impl: ByteBufAllocator — mongo-java-server only brings it to the runtime classpath + } // This module is written in Java so that a plain Spring Boot application can use it, // but every published module still produces a groovydoc jar, and groovydoc needs diff --git a/grails-data-mongodb/embedded/src/main/java/org/grails/datastore/gorm/mongodb/embedded/InMemoryMongoBackend.java b/grails-data-mongodb/embedded/src/main/java/org/grails/datastore/gorm/mongodb/embedded/InMemoryMongoBackend.java index 6c79126db94..85035750331 100644 --- a/grails-data-mongodb/embedded/src/main/java/org/grails/datastore/gorm/mongodb/embedded/InMemoryMongoBackend.java +++ b/grails-data-mongodb/embedded/src/main/java/org/grails/datastore/gorm/mongodb/embedded/InMemoryMongoBackend.java @@ -18,8 +18,19 @@ */ package org.grails.datastore.gorm.mongodb.embedded; +import java.io.DataInputStream; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; + import de.bwaldvogel.mongo.MongoServer; import de.bwaldvogel.mongo.backend.memory.MemoryBackend; +import io.netty.buffer.ByteBufAllocator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.util.ClassUtils; @@ -41,6 +52,19 @@ public class InMemoryMongoBackend implements EmbeddedMongoBackend { private static final String SERVER_CLASS = "de.bwaldvogel.mongo.MongoServer"; + private static final int SERVE_ONE_REQUEST_TIMEOUT_MILLIS = 5000; + + /** Netty tracks about one buffer in 128, so this many allocations reach that all but certainly. */ + private static final int ALLOCATIONS_TO_TRACK_ONE_BUFFER = 2048; + + private static final int OP_MSG = 2013; + + private static final byte BSON_STRING = 0x02; + + private static final byte BSON_INT32 = 0x10; + + private static final Logger log = LoggerFactory.getLogger(InMemoryMongoBackend.class); + @Override public String getName() { return NAME; @@ -70,9 +94,82 @@ public RunningEmbeddedMongo start(EmbeddedMongoSettings settings) { MemoryBackend backend = new RetainingMemoryBackend(); MongoServer server = new MongoServer(backend); server.bind("localhost", settings.getPort()); + serveOneRequest(addressOf(server)); + allocateUntilOneBufferIsTracked(); return new RunningInMemoryMongo(backend, server); } + /** + * Where {@link #serveOneRequest(InetSocketAddress)} sends its request: the address the server has + * just bound, which is not the port asked for when that was 0. + * + *

Not private, so that the specification can put a peer there that answers as no server would, + * and see that the server this started is handed back all the same. + */ + InetSocketAddress addressOf(MongoServer server) { + return server.getLocalAddress(); + } + + /** + * Allocates buffers until Netty's leak detection has tracked one, so that what only a tracked + * buffer needs is loaded as well. Netty picks the buffers it tracks at random, so the request + * {@link #serveOneRequest(InetSocketAddress)} sends rarely gets that far, and a buffer picked while + * the jars are closed fails as the first one would. + */ + private static void allocateUntilOneBufferIsTracked() { + for (int i = 0; i < ALLOCATIONS_TO_TRACK_ONE_BUFFER; i++) { + ByteBufAllocator.DEFAULT.ioBuffer(1).release(); + } + } + + /** + * Has the server answer one request, so that what its event loops need is loaded while the + * application is starting rather than when its first client connects. + * + *

That client is the MongoDB driver, which connects as the application context finishes + * refreshing, the moment Spring Boot closes the jars of an executable jar to clear its class + * loader's caches. A class loaded right then fails with {@code IllegalStateException}. Reading + * the driver's first bytes allocates the event loop's first buffer, and an event loop that fails + * to load the class of it ends its thread. The driver's connections on it then never answer, and + * stopping the server waits forever for them to close. + * + *

Whatever goes wrong here goes wrong again for the first real client, where it says more, so + * it is only logged, and the server is handed back all the same. That includes a reply that is + * cut short or gives a length it cannot have: the reply is read with what reports the end of the + * stream as an {@link IOException}, and a length shorter than the one already read skips nothing. + */ + private static void serveOneRequest(InetSocketAddress address) { + try (Socket client = new Socket()) { + client.connect(address, SERVE_ONE_REQUEST_TIMEOUT_MILLIS); + client.setSoTimeout(SERVE_ONE_REQUEST_TIMEOUT_MILLIS); + client.getOutputStream().write(isMaster()); + // the whole reply, since closing with some of it unread resets the connection, which the server logs as an error + DataInputStream reply = new DataInputStream(client.getInputStream()); + int length = Integer.reverseBytes(reply.readInt()); + reply.skipNBytes(length - (long) Integer.BYTES); + } + catch (IOException ex) { + log.debug("The embedded MongoDB at {} did not answer the request it serves as it starts", address, ex); + } + } + + /** The {@code isMaster} command the driver starts with, in the wire protocol's {@code OP_MSG}. */ + private static byte[] isMaster() { + byte[] command = "isMaster".getBytes(StandardCharsets.UTF_8); + byte[] databaseField = "$db".getBytes(StandardCharsets.UTF_8); + byte[] database = "admin".getBytes(StandardCharsets.UTF_8); + int documentLength = 4 + 1 + command.length + 1 + 4 + 1 + databaseField.length + 1 + 4 + database.length + 1 + 1; + int messageLength = 16 + 4 + 1 + documentLength; + ByteBuffer message = ByteBuffer.allocate(messageLength).order(ByteOrder.LITTLE_ENDIAN); + message.putInt(messageLength).putInt(1).putInt(0).putInt(OP_MSG); + message.putInt(0).put((byte) 0); + message.putInt(documentLength); + message.put(BSON_INT32).put(command).put((byte) 0).putInt(1); + message.put(BSON_STRING).put(databaseField).put((byte) 0).putInt(database.length + 1).put(database).put((byte) 0); + message.put((byte) 0); + return message.array(); + } + /** * A backend that keeps its collections when the server it is attached to shuts down. * diff --git a/grails-data-mongodb/embedded/src/test/groovy/org/grails/datastore/gorm/mongodb/embedded/EmbeddedMongoLifecycleSpec.groovy b/grails-data-mongodb/embedded/src/test/groovy/org/grails/datastore/gorm/mongodb/embedded/EmbeddedMongoLifecycleSpec.groovy index 02adee21cdf..f1279b12c56 100644 --- a/grails-data-mongodb/embedded/src/test/groovy/org/grails/datastore/gorm/mongodb/embedded/EmbeddedMongoLifecycleSpec.groovy +++ b/grails-data-mongodb/embedded/src/test/groovy/org/grails/datastore/gorm/mongodb/embedded/EmbeddedMongoLifecycleSpec.groovy @@ -313,6 +313,27 @@ class EmbeddedMongoLifecycleSpec extends Specification { application?.destroyForcibly() } + void 'a client sending its first bytes needs no class loaded by the event loop reading them'() { + given: + File log = temp.resolve('application.log').toFile() + + when: 'they arrive at the moment Spring Boot has the jars of an executable jar closed' + Process application = startApplication(27970, log, ClearedJarsServerApplication.name) + boolean exited = application.waitFor(30, TimeUnit.SECONDS) + + then: 'the event loop has no reason to end its thread, and stopping the server finds it there' + assert exited : "still running 30 seconds after its main method returned:\n${log.text}" + assert application.exitValue() == 0 : log.text + assert !log.text.contains('Zip file closed') : log.text + + and: 'having the server answer a request on the way there did not trouble it' + assert !log.text.contains('failed to handle') : log.text + assert !log.text.contains('Connection reset') : log.text + + cleanup: + application?.destroyForcibly() + } + private static void write(String url, String title) { try (MongoClient client = MongoClients.create(url)) { client.getDatabase('bookstore').getCollection('books').insertOne(new Document('title', title)) @@ -347,16 +368,15 @@ class EmbeddedMongoLifecycleSpec extends Specification { } /** - * {@link ContextStoppedServerApplication} in a JVM of its own, because whether that JVM exits is - * the question. The command is written to an argument file, since a test class path is longer + * An application in a JVM of its own, because whether that JVM exits is the question. The command is written to an argument file, since a test class path is longer * than some platforms allow a command line to be. */ - private Process startApplication(int port, File log) { + private Process startApplication(int port, File log, String mainClass = ContextStoppedServerApplication.name) { File arguments = temp.resolve('application.args').toFile() arguments.text = [ '-cp', System.getProperty('java.class.path'), - ContextStoppedServerApplication.name, + mainClass, String.valueOf(port), ].collect { String argument -> quoted(argument) }.join('\n') Process application = new ProcessBuilder( diff --git a/grails-data-mongodb/embedded/src/test/groovy/org/grails/datastore/gorm/mongodb/embedded/InMemoryMongoBackendSpec.groovy b/grails-data-mongodb/embedded/src/test/groovy/org/grails/datastore/gorm/mongodb/embedded/InMemoryMongoBackendSpec.groovy index 8e50972c8c0..c4f318f6740 100644 --- a/grails-data-mongodb/embedded/src/test/groovy/org/grails/datastore/gorm/mongodb/embedded/InMemoryMongoBackendSpec.groovy +++ b/grails-data-mongodb/embedded/src/test/groovy/org/grails/datastore/gorm/mongodb/embedded/InMemoryMongoBackendSpec.groovy @@ -20,11 +20,19 @@ package org.grails.datastore.gorm.mongodb.embedded import com.mongodb.client.MongoClient import com.mongodb.client.MongoClients +import de.bwaldvogel.mongo.MongoServer import org.bson.Document import spock.lang.Specification +import java.nio.ByteBuffer +import java.nio.ByteOrder +import java.nio.charset.StandardCharsets +import java.util.concurrent.CompletableFuture +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.TimeUnit + class InMemoryMongoBackendSpec extends Specification { InMemoryMongoBackend backend = new InMemoryMongoBackend() @@ -95,6 +103,62 @@ class InMemoryMongoBackendSpec extends Specification { running?.stop() } + void 'a server asked for any free port serves the request it sends itself on the port it bound'() { + given: 'the backend loaded by a class loader of its own, which notes the classes the server loads' + RecordingClassLoader loader = new RecordingClassLoader() + def inMemory = loader.loadClass(InMemoryMongoBackend.name).getConstructor().newInstance() + def settings = loader.loadClass(EmbeddedMongoSettings.name) + .getConstructor(int, String, String).newInstance(0, null, null) + + when: + def running = inMemory.start(settings) + + then: 'the server bound a port of its own' + running.port > 0 + + and: 'it has encoded a reply, which nothing but a request makes it do' + loader.loaded.contains('de.bwaldvogel.mongo.wire.bson.BsonEncoder') + + cleanup: + running?.stop() + loader?.close() + } + + void 'a reply #description to the request the server is sent as it starts does not keep it from being handed back'() { + given: 'a peer that answers that request the way no MongoDB does' + ServerSocket peer = new ServerSocket(0, 1, InetAddress.loopbackAddress) + peer.soTimeout = 10000 + CompletableFuture request = CompletableFuture.supplyAsync { answer(peer, reply as byte[]) } + InMemoryMongoBackend sendingToThePeer = new InMemoryMongoBackend() { + @Override + InetSocketAddress addressOf(MongoServer server) { + peer.localSocketAddress as InetSocketAddress + } + } + + when: + RunningEmbeddedMongo running = sendingToThePeer.start(new EmbeddedMongoSettings(0, null, null)) + + then: 'the request reached the peer' + new String(request.get(10, TimeUnit.SECONDS), StandardCharsets.ISO_8859_1).contains('isMaster') + + and: 'the server that was started is handed back, and answers a client' + insertOneDocument(running.host, running.port) + countDocuments(running.host, running.port) == 1 + + cleanup: + running?.stop() + peer?.close() + + where: + description | reply + 'that ends before it gives a length' | [] + 'that ends part way through its length' | [16, 0] + 'giving a length shorter than the length itself' | littleEndian(2) + 'giving a negative length' | littleEndian(-1) + 'that ends before the length it gave' | littleEndian(64) + [1, 2, 3] + } + private static void insertOneDocument(String host, int port) { try (MongoClient client = MongoClients.create("mongodb://${host}:${port}")) { client.getDatabase('test').getCollection('things').insertOne(new Document('name', 'Bob')) @@ -106,4 +170,50 @@ class InMemoryMongoBackendSpec extends Specification { return client.getDatabase('test').getCollection('things').countDocuments() } } + + /** + * Reads the whole request, so that closing does not reset the connection, then answers with the + * reply and waits for the client to close its end. + */ + private static byte[] answer(ServerSocket peer, byte[] reply) { + peer.accept().withCloseable { Socket client -> + DataInputStream input = new DataInputStream(client.inputStream) + byte[] length = input.readNBytes(Integer.BYTES) + byte[] rest = input.readNBytes(ByteBuffer.wrap(length).order(ByteOrder.LITTLE_ENDIAN).int - Integer.BYTES) + client.outputStream.write(reply) + client.outputStream.flush() + client.shutdownOutput() + input.read() + (length.toList() + rest.toList()) as byte[] + } + } + + private static List littleEndian(int value) { + ByteBuffer.allocate(Integer.BYTES).order(ByteOrder.LITTLE_ENDIAN).putInt(value).array().toList() + } + + /** + * Loads the server's classes itself rather than leaving them to the class loader the spec runs in, + * whose classes other features have already loaded, and notes each one it does. + */ + private static final class RecordingClassLoader extends URLClassLoader { + + final Set loaded = ConcurrentHashMap.newKeySet() + + RecordingClassLoader() { + super(classPath(), ClassLoader.platformClassLoader) + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + Class found = super.findClass(name) + loaded << name + found + } + + private static URL[] classPath() { + System.getProperty('java.class.path').split(File.pathSeparator) + .collect { String entry -> new File(entry).toURI().toURL() } as URL[] + } + } } diff --git a/grails-data-mongodb/embedded/src/test/java/org/grails/datastore/gorm/mongodb/embedded/ClearedJarsServerApplication.java b/grails-data-mongodb/embedded/src/test/java/org/grails/datastore/gorm/mongodb/embedded/ClearedJarsServerApplication.java new file mode 100644 index 00000000000..0ab288955b2 --- /dev/null +++ b/grails-data-mongodb/embedded/src/test/java/org/grails/datastore/gorm/mongodb/embedded/ClearedJarsServerApplication.java @@ -0,0 +1,135 @@ +/* + * 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.datastore.gorm.mongodb.embedded; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.Socket; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import io.netty.buffer.ByteBufAllocator; + +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.env.MapPropertySource; + +/** + * An application whose classes stop loading for a moment after the server has started, as they do + * in an executable jar: Spring Boot closes the jars of its class loader when the context has + * refreshed, and a class being loaded at that moment fails with {@code IllegalStateException}. + * A client sends its first bytes during that moment, as the MongoDB driver does on the way to that + * event, and stays connected. + * + *

{@code EmbeddedMongoLifecycleSpec} runs it in a JVM of its own and reads what it printed. Reading + * those bytes allocates the event loop's first buffer, which fails if the class of it is not loaded + * yet. Netty then logs that, which in an application fails as well, since Logback loads a class of its + * own to log an exception. That ends the event loop's thread, which leaves the client's connection on + * it open for good, and stopping the server then waits forever. The logger of this JVM cannot fail + * that way, so what the spec looks for is the first failure. + */ +public final class ClearedJarsServerApplication { + + private ClearedJarsServerApplication() { + } + + public static void main(String[] args) throws Exception { + int port = Integer.parseInt(args[0]); + ClearedJarsClassLoader loader = new ClearedJarsClassLoader(); + Thread.currentThread().setContextClassLoader(loader); + Class server = loader.loadClass(Server.class.getName()); + Runnable stop = (Runnable) server.getMethod("start", int.class).invoke(null, port); + Method allocate = server.getMethod("allocateAsAnEventLoopDoes"); + + try (Socket client = new Socket("localhost", port)) { + Thread.sleep(1000L); + loader.cleared = true; + client.getOutputStream().write(1); + client.getOutputStream().flush(); + try { + allocate.invoke(null); + } + catch (InvocationTargetException ex) { + System.out.println(ex.getCause()); + } + Thread.sleep(1000L); + loader.cleared = false; + + stop.run(); + } + } + + /** Created by the class loader above, so that what it loads is loaded through it. */ + public static final class Server { + + private Server() { + } + + /** + * As many buffers as it takes for Netty's leak detection to track one, which it does at random + * and which loads classes of its own. Without this, a server missing those classes would pass + * all but about once in 128 runs. + */ + public static void allocateAsAnEventLoopDoes() { + for (int i = 0; i < 2048; i++) { + ByteBufAllocator.DEFAULT.ioBuffer(1).release(); + } + } + + public static Runnable start(int port) { + GenericApplicationContext context = new GenericApplicationContext(); + context.getEnvironment().getPropertySources().addFirst(new MapPropertySource("application", Map.of( + EmbeddedMongoInitializer.BACKEND, InMemoryMongoBackend.NAME, + "grails.mongodb.url", "mongodb://embedded:" + port + "/bookstore"))); + new EmbeddedMongoInitializer().initialize(context); + EmbeddedMongoLifecycle lifecycle = context.getBeanFactory() + .getBean(EmbeddedMongoLifecycle.BEAN_NAME, EmbeddedMongoLifecycle.class); + return lifecycle::stop; + } + } + + private static final class ClearedJarsClassLoader extends URLClassLoader { + + private volatile boolean cleared; + + private ClearedJarsClassLoader() throws MalformedURLException { + super(classPath(), ClassLoader.getPlatformClassLoader()); + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + if (this.cleared) { + throw new IllegalStateException("Zip file closed"); + } + return super.findClass(name); + } + + private static URL[] classPath() throws MalformedURLException { + List urls = new ArrayList<>(); + for (String entry : System.getProperty("java.class.path").split(File.pathSeparator)) { + urls.add(new File(entry).toURI().toURL()); + } + return urls.toArray(new URL[0]); + } + } +} diff --git a/grails-doc/src/en/guide/REST/openApi.adoc b/grails-doc/src/en/guide/REST/openApi.adoc index 53d30e13c0d..90d7f8010fc 100644 --- a/grails-doc/src/en/guide/REST/openApi.adoc +++ b/grails-doc/src/en/guide/REST/openApi.adoc @@ -872,7 +872,7 @@ listing. Its other actions are described as any controller's are. * A mapping that accepts any HTTP method for an action `allowedMethods` does not restrict is documented as `GET`, because OpenAPI requires a concrete operation. -* A mapping declared for an HTTP method OpenAPI has no operation for is skipped, and logged. +* A mapping declared for an HTTP method the OpenAPI versions this generator writes have no operation for, such as `QUERY`, is skipped, and logged. * A `RestfulController` is known to be read only, and the resource it serves where it declares no type argument, from the controller the application serves requests with, which is not created to be described. So one in a scope other than singleton is described with its write actions, and one diff --git a/grails-doc/src/en/guide/conf/config/builtInOptions.adoc b/grails-doc/src/en/guide/conf/config/builtInOptions.adoc index 64ffb941a45..e75cb409351 100644 --- a/grails-doc/src/en/guide/conf/config/builtInOptions.adoc +++ b/grails-doc/src/en/guide/conf/config/builtInOptions.adoc @@ -29,6 +29,7 @@ On the runtime front, i.e. `grails-app/conf/application.yml`, there are quite a * `grails.enable.native2ascii` - Set this to false if you do not require native2ascii conversion of Grails i18n properties files (default: true). * `grails.converters.encoding` - The character encoding for converter output (JSON or XML) and for the response content types stamped by the default renderers used by `respond()` (default: 'UTF-8'). JSON that `respond` writes through Spring MVC, with `grails.web.rendering.json.spring` or a named configuration, and problems are written in UTF-8, as Spring MVC writes JSON. * `grails.exceptionresolver.springHandlers` - Whether an exception a controller action throws is resolved first by the `@ExceptionHandler` methods Spring MVC resolves those of a handler method with, those of the controller and of the `@ControllerAdvice` beans that apply to it, Spring Boot's handler of `spring.mvc.problemdetails.enabled` among them (default: false; true from Grails 10). See <>. +* `grails.exceptionresolver.problemDetails` - Whether an exception a controller action throws, that no exception handler method handles, is written as an RFC 9457 problem to a client that does not prefer HTML, while the application's error page stays for browsers. It turns on `grails.exceptionresolver.springHandlers` unless that is set (default: false). See <>. * `grails.controllers.responseBody` - Whether Spring MVC writes any value of a controller action annotated `@ResponseBody`, or of an action of a controller annotated so, as the body of the response, as it writes the value of a handler method annotated so, rather than Grails rendering the action's view for a model. A `ResponseEntity`, a `ProblemDetail` and the other values Spring MVC writes whatever a handler method is annotated with are written whatever it says (default: false; true from Grails 10). See <>. * `grails.views.default.codec` - Sets the default encoding regime for GSPs - can be one of 'none', 'html', or 'base64' (default: 'none'). To reduce risk of XSS attacks, set this to 'html'. * `grails.views.gsp.encoding` - The file encoding used for GSP source files (default: 'utf-8'). diff --git a/grails-doc/src/en/guide/conf/config/logging.adoc b/grails-doc/src/en/guide/conf/config/logging.adoc index f471d4216ce..56908ec16ce 100644 --- a/grails-doc/src/en/guide/conf/config/logging.adoc +++ b/grails-doc/src/en/guide/conf/config/logging.adoc @@ -96,7 +96,7 @@ More information can be found in the official https://logback.qos.ch/documentati === Structured Logging Grails does not enable structured logging. Existing Logback configuration and plain-text output remain unchanged -unless an application explicitly selects a Spring Boot structured format. Spring Boot 4.1 provides the JSON format +unless an application explicitly selects a Spring Boot structured format. Spring Boot provides the JSON format IDs `ecs` (Elastic Common Schema), `gelf` (Graylog Extended Log Format), and `logstash` for console and file output. For the complete reference, see the https://docs.spring.io/spring-boot/reference/features/logging.html#features.logging.structured[Spring Boot structured logging documentation]. diff --git a/grails-doc/src/en/guide/deployment/deploymentAot.adoc b/grails-doc/src/en/guide/deployment/deploymentAot.adoc index 94f2d79af28..d46b3531b35 100644 --- a/grails-doc/src/en/guide/deployment/deploymentAot.adoc +++ b/grails-doc/src/en/guide/deployment/deploymentAot.adoc @@ -143,10 +143,30 @@ grails { ./gradlew traceNativeMetadata ---- -The result is merged into `src/native/resources/META-INF/native-image`, alongside the source rather than under `build`, because which paths an image was built to cover is worth reviewing and worth committing. +The result is merged into `src/native/resources/META-INF/native-image`, alongside the source rather than under `build`, because which paths an image was built to cover is worth reviewing and worth committing. `nativeCompile` builds the image with it. A different `outputDirectory` has to be under a `META-INF/native-image` directory, which is where an image reads metadata from, and the build fails if it is not. `paths` are asked for. `forms` are asked for *and submitted*: the page is read, the fields the form declares are filled in, and they are posted to the action the form names. Submitting matters separately from rendering, because binding a form is a different half of the framework from rendering one — and it is the half that converts. A checkbox arrives as a string and lands on a boolean property, so a form submitted without one never asks the conversion service anything, and the image is built without the answer. +A field that carries a value sends it, as a hidden field does whatever it carries. A field rendered empty, as the fields plugin renders those of a new instance, is filled in with something its type accepts: a number within the field's `min` and `max`, a date for a date field, an address or a link on `example.com` for an email or URL field, and recognisable text for a text field, each no longer than the field's `maxlength`. `example.com` is kept for examples and accepts no mail, so a save that sends mail sends none. A form with an email field too short for `1@example.com`, or a URL field too short for `ftp://example.com`, is not submitted, because whatever fits is text the field rejects or an address on someone else's domain. The trace fails, naming the field, until it is given a value in `forms`. A select, which is how an enum, an association and each part of a date picker are rendered, sends the option a browser would: the one selected, or the first that can be chosen. A value its type does not accept would fail to bind, and the page would answer with the form again, which looks to the trace like a save that succeeded. + +==== Other methods and bodies + +`paths` are asked for with `GET` and `forms` are posted as forms, which reaches neither an action restricted to another method, such as `QUERY`, nor what binds a JSON body or writes a JSON response. `requests` makes a request as it is declared, after the forms and in their session: + +[source,groovy] +---- +grails { + nativeMetadata { + requests = [ + [method: 'QUERY', path: '/book/search', contentType: 'application/json', body: '{"title": "Grails"}', accept: 'application/json'], + [method: 'DELETE', path: '/book/1'] + ] + } +} +---- + +Each names its `method` and `path`, and may name its `contentType`, `body` and `accept`. A request that does not, or that names anything else, fails the trace before the application is started, and one that answers with an error fails it too. + ==== Pages behind a login Forms are submitted before paths are asked for, whichever order they appear in the configuration, and the session a form establishes is carried through the rest of the trace. That is what makes a protected page traceable: list its login form, and the pages named in `paths` are reached as a signed-in user. @@ -163,6 +183,20 @@ grails { A form may be told what to put in it with a query string, as `/login` is above. Only fields the form actually declares are set from it; a value naming a field that is not there is reported rather than posted, because it was meant for a form that has since changed. +An application using the Spring Security plugin traces its login the same way, naming the page the plugin renders: + +[source,groovy] +---- +grails { + nativeMetadata { + forms = ['/login/auth?username=admin&password=secret'] + paths = ['/', '/book'] + } +} +---- + +The plugin answers every request with a 503 and a `Retry-After` header until the application has started, so the trace and an AOT cache training run wait for that answer to change before they make their first request, for no longer than `startTimeoutSeconds`. A 503 without `Retry-After` is the application's own answer, which waiting will not change, so it is not waited for. What the plugin declares and the default configuration it loads are ready for an image. Its companion plugins (REST, LDAP, ACL, CAS, OAuth2 and UI) are not yet: each compiles a default configuration script as it starts, which an image cannot do. + ==== Choosing which form A page often carries more than one form — a layout's search box or sign-out button ahead of the page's own. The form declaring the most fields is the one submitted, and the trace output says which it was: @@ -190,3 +224,5 @@ Redirects are followed, so an application that sends `/` to its real home page r NOTE: The agent ships with GraalVM rather than with a JDK, and it must trace the archive the image will be built from — which is compiled for GraalVM's Java. The task uses the project toolchain, which for a project that builds an image is already a GraalVM; `grails.nativeMetadata.javaExecutable` points it elsewhere. WARNING: The agent records only what ran. A path that is not listed is not covered, and it will be missing in the way that only appears when somebody uses that page. Treat the list as the coverage it is, and extend it as the application grows. + +The trace fails on a request that answers with an error, so what the framework does to answer one is registered for you where it writes one itself, as it does for an exception written as an RFC 9457 problem with `grails.exceptionresolver.problemDetails`. diff --git a/grails-doc/src/en/guide/deployment/deploymentAotCache.adoc b/grails-doc/src/en/guide/deployment/deploymentAotCache.adoc index e30532c8ce8..fdbdaf84ec2 100644 --- a/grails-doc/src/en/guide/deployment/deploymentAotCache.adoc +++ b/grails-doc/src/en/guide/deployment/deploymentAotCache.adoc @@ -41,7 +41,7 @@ grails { } ---- -That adds two tasks. `extractAotCacheApplication` unpacks the executable jar into `build/aot-cache/application`, and `trainAotCache` runs what it unpacked, asks for each path, stops it, and leaves the cache beside the extracted application: +That adds two tasks. `extractAotCacheApplication` unpacks the executable archive into `build/aot-cache/application`, and `trainAotCache` runs what it unpacked, asks for each path, stops it, and leaves the cache beside the extracted application: [source,bash] ---- @@ -53,7 +53,11 @@ That adds two tasks. `extractAotCacheApplication` unpacks the executable jar int Trained myapp.aot (167 MB) over 4 paths ---- -The application is unpacked first because a cache is read against the layout it was trained on. An executable jar loads its dependencies through a nested-jar classloader; the extracted form loads them as ordinary jars on the classpath, and only the second is a layout a cache can be reused against. +The application is unpacked first because a cache is read against the layout it was trained on. An executable archive loads its dependencies through a nested-jar classloader; the extracted form loads them as ordinary jars on the classpath, and only the second is a layout a cache can be reused against. + +The archive unpacked is the one the application ships. An application that applies the `war` plugin, as a generated application does, ships the executable war that `bootWar` builds, and that is what is unpacked. Any other application, or one that switches `bootWar` off, ships the executable jar that `bootJar` builds. + +A war is unpacked as a jar of the same name, `myapp.jar` for `myapp.war`. The libraries in `WEB-INF/lib` and `WEB-INF/lib-provided` go to `lib/` beside it, and the files the war holds outside `WEB-INF`, such as the contents of `src/main/webapp`, go to the jar's `META-INF/resources`. The application serves those files as it serves them from the war. The executable jar leaves `src/main/webapp` out, which is why an application that ships its war is trained against the war. === Running with the cache diff --git a/grails-doc/src/en/guide/deployment/deploymentLayeredBuilds.adoc b/grails-doc/src/en/guide/deployment/deploymentLayeredBuilds.adoc index 6f60ce6bf7b..7bf25ce67db 100644 --- a/grails-doc/src/en/guide/deployment/deploymentLayeredBuilds.adoc +++ b/grails-doc/src/en/guide/deployment/deploymentLayeredBuilds.adoc @@ -206,8 +206,9 @@ Explicitly selecting `bootJar` matters when the `war` plugin is also applied: Sp ./gradlew clean check bootBuildImage --imageName=myapp:0.1 ---- +By default, Spring Boot {springBootVersion} uses the `paketobuildpacks/ubuntu-resolute-builder` builder, and the `paketobuildpacks/ubuntu-resolute-run-tiny:latest` run image, which has no shell. Use the builder supported by the Spring Boot plugin version in your application, or select a maintained builder and its compatible run image explicitly. -For repeatable release builds, pin both images to tested digests and schedule rebuilds when they change. +The builder does not pin the version of its run image, so for repeatable release builds, set both images, pinned to tested digests, and schedule rebuilds when they change. Check that the builder, run image, and buildpacks support your target CPU architecture and any required native libraries. Paketo's Java buildpack calculates JVM memory settings from the container memory limit and non-heap requirements. @@ -215,6 +216,18 @@ Set a container limit and tune its documented `BPL_JVM_*` runtime options for yo Keep the buildpack launcher as the entry point so its memory calculation, bindings, and runtime configuration execute. `BP_*` settings configure the image build; deployment-specific configuration and secrets belong at runtime. When image tags change on every release, use stable, application-specific `buildCache` and `launchCache` volume names if you want to retain buildpack caches across versions. +To share the build cache between hosts, such as CI runners, keep it in an image in a registry instead of a volume; `launchCache` stays local: + +[source,groovy] +---- +tasks.named('bootBuildImage') { + buildCache { + image { + name = 'registry.example.com/myapp-build-cache:latest' + } + } +} +---- For a local smoke test, supply the application's required external configuration and run the image with a memory limit: diff --git a/grails-doc/src/en/guide/deployment/deploymentTasks.adoc b/grails-doc/src/en/guide/deployment/deploymentTasks.adoc index ba55112e531..37c102fbab6 100644 --- a/grails-doc/src/en/guide/deployment/deploymentTasks.adoc +++ b/grails-doc/src/en/guide/deployment/deploymentTasks.adoc @@ -232,6 +232,8 @@ See the link:{springBootReference}actuator/endpoints.html#actuator.endpoints.kub Many deployments terminate TLS at a reverse proxy or ingress. Keep certificate renewal there and restrict direct access to the application port. Configure forwarded-header handling only for a trusted proxy path, and have the boundary proxy remove untrusted client-supplied `Forwarded` and `X-Forwarded-*` headers. Choose `server.forward-headers-strategy=NATIVE` for supported embedded-container handling or `FRAMEWORK` for Spring's handling, according to your proxy's headers and trust configuration. +`FRAMEWORK` reads the `+X-Forwarded-*+` headers; if your proxy sends the RFC 7239 `Forwarded` header instead, also set `spring.mvc.forwarded-headers.header-format=STANDARD`. +It ignores `X-Forwarded-Prefix`; if your proxy strips a path prefix and sends that header, also set `spring.mvc.forwarded-headers.use-forwarded-prefix=true`. Test redirects, absolute URLs, secure cookies, and client-address handling through that proxy. See the link:{springBootHowTo}webserver.html#howto.webserver.use-behind-a-proxy-server[Spring Boot proxy guidance]. To terminate TLS in the embedded server instead, merge settings such as these into `application.yml` and mount the keystore read-only: diff --git a/grails-doc/src/en/guide/introduction/whatsNew.adoc b/grails-doc/src/en/guide/introduction/whatsNew.adoc index f74ffae4ba4..7918c538eaa 100644 --- a/grails-doc/src/en/guide/introduction/whatsNew.adoc +++ b/grails-doc/src/en/guide/introduction/whatsNew.adoc @@ -24,7 +24,7 @@ This section covers all the new features introduced in Grails {grailsMajorVersio Grails {grailsMajorVersion} is a major release that includes new features, improvements, and dependency upgrades. This release focuses on enhancing the developer experience, improving performance, and ensuring compatibility with the latest technologies. -For detailed information on how to upgrade to Grails {grailsMajorVersion}, including major dependency changes, please see the xref:upgrading#upgrading80x[Upgrading from Grails 7 to Grails {grailsMajorVersion}] section. +For detailed information on how to upgrade to Grails {grailsMajorVersion}, including major dependency changes, please see the xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails {grailsMajorVersion}] section. Notable new features are included below. ==== Platform Baseline @@ -70,12 +70,40 @@ dependencies { An application that keeps the default never ships the module. The same dependency is needed, whatever the application's own setting, for a library that was compiled with classic call sites, because Groovy 6 can load such a class only with the module: `org.testcontainers:testcontainers-spock` is one, so declare the module as `testRuntimeOnly` next to it. -==== Spring Boot 4.1 and Spring Framework 7 +==== Spring Boot 4.2 and Spring Framework 7.1 Grails {grailsMajorVersion} is built on Spring Boot {springBootVersion} and Spring Framework {springVersion}. -This brings the Spring Boot 4 modular artifact layout, Spring Framework 7 API removals, Jackson 3, Tomcat 11 and Jakarta Servlet 6.1, plus Spring Boot 4.1 dependency management for Spring Security 7.1, Spring Data 2026.0 and Micrometer 1.17. +This brings the Spring Boot 4 modular artifact layout, Spring Framework 7 API removals, Jackson 3, Tomcat 11 and Jakarta Servlet 6.1, plus Spring Boot 4.2 dependency management for Spring Security 7.2, Spring Data 2026.1 and Micrometer 1.18. The Grails 8 upgrade guide calls out the major application-impacting changes and links to the Spring Boot 4.0 migration guide, Spring Boot 4.1 release notes and Spring Framework 7.0 release notes. +For the changes since Spring Boot 4.1, see the https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-4.2-Release-Notes[Spring Boot 4.2 Release Notes] and the https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-7.1-Release-Notes[Spring Framework 7.1 Release Notes]. +The xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails {grailsMajorVersion}] section covers the upgrade, including the graphql-java 26 changes that affect GraphQL applications. + +Spring Boot 4.2 can switch metrics and spans to OpenTelemetry's semantic conventions: + +[source,yaml] +---- +management: + observations: + conventions: open-telemetry +---- + +`http.server.requests` then becomes `http.server.request.duration`, tagged with `http.route` and `error.type` in place of `uri` and `exception`. +In a Grails application `http.route` is `/controller/action`, not the pattern of the URL mapping, which keeps it low in cardinality. +Spring Boot's https://docs.spring.io/spring-boot/reference/actuator/observability.html#actuator.observability.semantic-conventions[documentation] lists what else changes, such as the properties that customize meter names no longer taking effect. + +==== The HTTP QUERY Method + +Spring Framework 7.1 supports the `QUERY` method of https://www.rfc-editor.org/info/rfc10008/[RFC 10008], a safe, idempotent request with a body. URL mappings gain a `query` prefix for it, next to `get`, `post` and the others, and a command object is bound to the body of a `QUERY`, which is JSON or form-encoded: + +[source,groovy] +---- +static mappings = { + query "/books/search"(controller: 'book', action: 'search') +} +---- + +See xref:theWebLayer#mappingHTTP[Mapping to HTTP methods]. ==== JSON Dates and Times Render as Spring Boot Renders Them @@ -126,6 +154,11 @@ of the controller and of the `@ControllerAdvice` beans that apply to it, Spring `spring.mvc.problemdetails.enabled` among them, which answers the exceptions of Spring MVC with RFC 9457 problems. See xref:theWebLayer#controllerExceptionHandling[Declarative Controller Exception Handling]. +With `grails.exceptionresolver.problemDetails: true`, an exception no such method handles is written as a problem to a +client that does not prefer HTML, with the translation Spring Framework 7.1 adds, which keeps the status and detail of an +`ErrorResponse` or a `@ResponseStatus` exception and does not expose the message of an unexpected one. The application's +error page stays for browsers, and for an exception a URL mapping names. + An action can return what a Spring MVC handler method returns to write the response, such as a `ResponseEntity`, a `ProblemDetail` or a `StreamingResponseBody`, and Spring MVC writes it with the return value handlers of its handler adapter, the `ResponseBodyAdvice` beans that apply to the controller, and the action's `@JsonView` and @@ -578,6 +611,37 @@ deployment whose reverse proxy already sends these headers can set `grails.secur only explicitly configured headers on proxied requests. See the xref:security[Security] section of the user guide and the xref:upgrading#upgrading80x[upgrade guide] for details. +==== Nonce-based Content Security Policy + +`grails.security.headers.content-security-policy.value` can name a per-request nonce with the `+{nonce}+` placeholder +that Spring Security 7.2 introduced, so a policy can allow an inline script or style by nonce rather than with +`'unsafe-inline'`: + +[source,yaml] +.grails-app/conf/application.yml +---- +grails: + security: + headers: + content-security-policy: + enabled: true + value: "script-src 'self' 'nonce-{nonce}'" +---- + +A view renders the request's nonce with the new `` tag, as in ` + +---- + +The link:{gspTagsRef}javascript.html[javascript] tag adds the nonce to the inline script it writes. +Code outside a view, such as a controller or a service, reads the nonce with `ContentSecurityPolicyNonce.nonceOf(request)` from the `grails.web.http` package, which returns `null` when the request carries none. +It compiles under `@GrailsCompileStatic` as well as in dynamic code, and it reads the nonce whichever of Grails or Spring Security generated it. +A script or stylesheet loaded from the application's own origin, such as an asset, is allowed by `'self'` and needs no nonce. + +`_csp` is also the attribute Spring Security's `ContentSecurityPolicyNonceGeneratingFilter` sets by default, so a view renders the right nonce whichever of the two generated it, and the header carries the same nonce: + +* When Spring Security's header configuration also writes a nonce-based policy, at Spring Boot's default filter order, Spring Security's nonce replaces the Grails one on the request. +Spring Security's header is written first, and the Grails policy is not sent. +* When the Spring Security filter chain is ordered ahead of `GrailsFilters.FIRST`, Grails keeps the nonce Spring Security put on the request and writes the Grails policy with it. + +The `cspNonce` and `javascript` tags read the nonce from `_csp`. +If Spring Security's header configuration names another attribute with `nonceAttributeName`, render the nonce from that attribute instead. + +If the `_csp` attribute holds anything other than a Grails or Spring Security nonce, the policy is not sent, since a policy without the nonce would block every inline script the view gave one. +Grails logs a warning the first time that happens. diff --git a/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/authentication/basicAndDigestAuth.adoc b/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/authentication/basicAndDigestAuth.adoc index 4c0e7e3911a..b4ee17eaa11 100644 --- a/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/authentication/basicAndDigestAuth.adoc +++ b/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/authentication/basicAndDigestAuth.adoc @@ -89,7 +89,7 @@ https://en.wikipedia.org/wiki/Digest_access_authentication[Digest Authentication |digest.passwordAlreadyEncoded |`false` -|Whether you are managing the password hashing yourself +|Whether what is stored is already the hash a browser computes. Set for you unless `digest.useCleartextPasswords` is `true` |digest.createAuthenticatedToken |`false` @@ -97,12 +97,19 @@ https://en.wikipedia.org/wiki/Digest_access_authentication[Digest Authentication |digest.useCleartextPasswords |`false` -|If `true`, a cleartext password encoder is used (not recommended). If `false`, passwords hashed by `DigestAuthPasswordEncoder` are stored in the database +|If `true`, passwords are stored as they were typed (not recommended). If `false`, the hash a browser computes from the username, realm name and password is stored |==================== Digest authentication has a problem in that by default you store cleartext passwords in your database. This is because the browser hashes your password along with the username and Realm name, and this is compared to the password hashed using the same algorithm during authentication. The browser does not know about your `MessageDigest` algorithm or salt source, so to hash them the same way you need to load a cleartext password from the database. -The plugin does provide an alternative, although it has no configuration options (in particular the digest algorithm cannot be changed). If `digest.useCleartextPasswords` is `false` (the default), then the `passwordEncoder` bean is replaced with an instance of `grails.plugin.springsecurity.authentication.encoding.DigestAuthPasswordEncoder`. This encoder uses the same approach as the browser, that is, it combines your password along with your username and Realm name essentially as a salt, and hashes with MD5. MD5 is not recommended in general, but given the typical size of the salt it is reasonably safe to use. +The alternative is to store what the browser computes instead of the password. That is the default: with `digest.useCleartextPasswords` set to `false`, what a user has stored must be the MD5 hash of `username:realm:password`, which is what the filter needs and no more. MD5 is not recommended in general, but the username and realm name act as a salt. Changing the realm name makes every stored hash stop matching. + +The `passwordEncoder` bean cannot compute this hash, because an encoder is not given the username, so your application computes it wherever it saves a password: + +[source,groovy] +---- +password = "${username}:${realmName}:${password}".md5() +---- The only required attribute is `useDigestAuth`, which you must set to `true`, but you probably also want to change the realm name: @@ -116,17 +123,7 @@ Digest authentication cannot be applied to a subset of URLs like Basic authentic [NOTE] ==== -Note that since the Digest authentication password encoder is different from the typical encoders you must pass the username as the "`salt`" value. The code in the generated User class assumes you're not using a salt value, so you'll need to change the code in `encodePassword()` from +What `s2-quickstart` generates hashes a password with `springSecurityService.encodePassword(password)`, in the user class or in its password encoder listener, which gives the wrong value for Digest authentication. Replace that call with the hash above, or set `digest.useCleartextPasswords` to `true` and store the password as it was typed. -[source,groovy] ----- -password = springSecurityService.encodePassword(password) ----- - -to - -[source,groovy] ----- -password = springSecurityService.encodePassword(password, username) ----- +Digest authentication leaves `passwordEncoder` as configured, so `springSecurityService.encodePassword` and the users that other `UserDetailsService` beans or `spring.security.user.*` describe work as they do without it. The plugin's `daoAuthenticationProvider` does not use it: it hashes a password that a form or Basic authentication sends the way a browser does, so a form signs a user in with their password in either mode, and not with the hash stored. A class named with `daoAuthenticationProviderBeanClass` is given `passwordEncoder` instead, unless it extends `grails.plugin.springsecurity.authentication.DigestDaoAuthenticationProvider`. ==== diff --git a/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/filters.adoc b/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/filters.adoc index 5a31eb1ad6d..7406fc0d664 100644 --- a/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/filters.adoc +++ b/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/filters.adoc @@ -24,7 +24,7 @@ There are a few different approaches to configuring filter chains. === Requests that arrive before startup -While the application is starting, the plugin responds to every request with `503 Service Unavailable`. That includes a request for a static resource. Requests are allowed through only after both of these have happened, in either order: the plugin has finished `doWithApplicationContext()`, and Spring Boot has published `ApplicationStartedEvent`. +While the application is starting, the plugin responds to every request with `503 Service Unavailable` and a `Retry-After` header of one second, which says the unavailability is temporary. That includes a request for a static resource. Requests are allowed through only after both of these have happened, in either order: the plugin has finished `doWithApplicationContext()`, and Spring Boot has published `ApplicationStartedEvent`. The property is `grails.plugin.springsecurity.rejectRequestsDuringStartup`. It defaults to `true`. Set it to `false` to let requests through during startup: diff --git a/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/miscProperties.adoc b/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/miscProperties.adoc index fa466791d27..1d95ee8d883 100644 --- a/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/miscProperties.adoc +++ b/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/miscProperties.adoc @@ -73,6 +73,10 @@ under the License. |`true` |Whether caching `SavedRequest` can trigger the creation of a session +|requestCache.ignoredPatterns +|`['/favicon.*', '/apple-touch-icon*.png', '/manifest.json', '/manifest.webmanifest', '/browserconfig.xml', '/.well-known/appspecific/com.chrome.devtools.json']` +|Path patterns, relative to the context path, of requests that are never saved as the page to redirect to after a login. Browsers and their tools request these on their own, so a denied one would otherwise replace the page the user asked for. Set to `[]` to save every request + |roleHierarchy |_none_ |Hierarchical role definition. See <> diff --git a/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/passwords/locking.adoc b/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/passwords/locking.adoc index 3f5a9d122b3..4a71685fc4d 100644 --- a/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/passwords/locking.adoc +++ b/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/passwords/locking.adoc @@ -197,6 +197,8 @@ If the `cacheUsers` configuration property is set to `true`, Spring Security cac If you enable this feature, you must remove any cached instances after making a change that affects login. If you do not remove cached instances, even though a user's account is locked or disabled, logins succeed because the database is bypassed. By removing the cached data, you force at trip to the database to retrieve the latest updates. +The cache stores users by value: each lookup is given a copy, made by serializing the user, so erasing the password of the user that is signed in, which the authentication manager does by default, leaves the cached user with the password it is checked against. A user with a field that cannot be serialized is not cached and is loaded on every sign-in, with a warning; mark such a field `transient`. Changing a user read from the cache changes only that copy, so put it back with `userCache.putUserInCache` to change what is cached. In a native image the plugin registers its own user classes for this. A user class or id type of your own needs serialization metadata, which `traceNativeMetadata` records when the login form is listed in `forms`. + Here is a sample Quartz job that demonstrates how to find and disable users with passwords that are too old: [source,groovy] diff --git a/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/urlProperties.adoc b/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/urlProperties.adoc index fdbde23d67b..0d41168a6a9 100644 --- a/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/urlProperties.adoc +++ b/grails-doc/src/en/guide/security/securityPlugins/springSecurity/core/urlProperties.adoc @@ -177,7 +177,7 @@ The table shows configurable URL-related properties. |fii.alwaysReauthenticate |`false` -|If `true`, re-authenticates when there is a `Authentication` in the `SecurityContext` +|If `true`, re-authenticates the `Authentication` in the `SecurityContext` for every request to a secured URL, unless its credentials were erased once it was authenticated, as `providerManager.eraseCredentialsAfterAuthentication` does by default |fii.rejectPublicInvocations |`true` diff --git a/grails-doc/src/en/guide/theWebLayer/controllers/controllerExceptionHandling.adoc b/grails-doc/src/en/guide/theWebLayer/controllers/controllerExceptionHandling.adoc index c4b0eb39ba8..014f3dc6b9c 100644 --- a/grails-doc/src/en/guide/theWebLayer/controllers/controllerExceptionHandling.adoc +++ b/grails-doc/src/en/guide/theWebLayer/controllers/controllerExceptionHandling.adoc @@ -148,3 +148,36 @@ class BookExceptionAdvice { ---- Declare the advice as a bean, in `grails-app/conf/spring/resources.groovy` or as a `@Bean` of a configuration class. + +With `grails.exceptionresolver.problemDetails: true`, an exception an action throws that no exception handler method handles is written as an https://www.rfc-editor.org/rfc/rfc9457.html[RFC 9457] problem to a client that does not prefer HTML, using the translation Spring Framework 7.1 adds, and the application's error page stays for browsers. A problem is the last resort after those methods, so the setting turns on `grails.exceptionresolver.springHandlers` unless the application sets that to `false`: + +[source,yaml] +.grails-app/conf/application.yml +---- +grails: + exceptionresolver: + problemDetails: true +---- + +---- +HTTP/1.1 500 +Content-Type: application/problem+json + +{"status":500,"title":"Internal Server Error"} +---- + +* An `ErrorResponse`, a `ResponseStatusException` or an exception annotated `@ResponseStatus`, or an exception caused by one of the last two, keeps its status and detail. +* An exception of Spring such as a `TypeMismatchException` or an `HttpMessageNotReadableException` gets the status and detail `ResponseEntityExceptionHandler` gives it. +* Any other exception is a `500` without a detail, so its message is never exposed. +* The type, title and detail can be customized in the message bundles, with the codes `ErrorResponse` defines. +* The `ResponseBodyAdvice` beans that apply to the controller and the `ErrorResponse.Interceptor` beans apply to it, as to a problem a handler method returns. +* An unexpected exception is logged at `ERROR`, by Spring's `org.springframework.web.method.annotation.ProblemDetailExceptionHandler` logger with its stack trace, and by Grails with the method and URI of the request. The `grails.exceptionresolver` settings for what is logged, such as `logRequestParameters`, apply to what the Grails exception resolver logs, and so not to these. + +The Grails exception resolver and the application's error page still resolve an exception for: + +* A client that prefers HTML: a request whose format, from its URL or its `Accept` header, is `html`, or, where Grails resolves any format, as for a browser under `grails.mime.disable.accept.header.userAgents`, whose first concrete `Accept` type is HTML. A client that accepts any type, such as `curl`, gets the problem. +* An exception that a URL mapping names, or whose root cause it names, such as `"500"(controller: 'errors', action: 'denied', exception: AccessDeniedException)`. A `"500"` mapping without an `exception` is the error page of the clients that prefer HTML, and does not apply to the others. + +An exception a view throws while it renders is not one an action throws, and goes to the error page for every client, and an action that a page includes with `` is left to the Grails exception resolver too, so that a problem is not written into the page. + +WARNING: Do not use Spring Framework's `ProblemDetailsConfigurer.renderUnhandledExceptions(true)` in a `WebMvcConfigurer` for this. It makes Spring MVC's resolver, which runs ahead of the Grails exception resolver, answer every exception of every action with a problem, a browser's included, and `"500"` URL mappings no longer apply. diff --git a/grails-doc/src/en/guide/theWebLayer/urlmappings/mappingHTTP.adoc b/grails-doc/src/en/guide/theWebLayer/urlmappings/mappingHTTP.adoc index a3fa27a86ae..149cdc8c34d 100644 --- a/grails-doc/src/en/guide/theWebLayer/urlmappings/mappingHTTP.adoc +++ b/grails-doc/src/en/guide/theWebLayer/urlmappings/mappingHTTP.adoc @@ -17,7 +17,7 @@ specific language governing permissions and limitations under the License. //// -URL mappings can also be configured to map based on the HTTP method (GET, POST, PUT or DELETE). This is very useful for RESTful APIs and for restricting mappings based on HTTP method. +URL mappings can also be configured to map based on the HTTP method, such as GET, POST, PUT, DELETE or QUERY. This is very useful for RESTful APIs and for restricting mappings based on HTTP method. As an example the following mappings provide a RESTful API URL mappings for the `ProductController`: @@ -29,3 +29,35 @@ static mappings = { ---- Note that if you specify a HTTP method other than GET in your URL mapping, you also have to specify it when creating the corresponding link by passing the `method` argument to `g:link` or `g:createLink` to get a link of the desired format. + +The `QUERY` method of https://www.rfc-editor.org/info/rfc10008/[RFC 10008] is a safe, idempotent request with a body, for a search whose criteria do not fit in a URL. Map it with `method: 'QUERY'` or with the `query` prefix, and restrict an action to it with `allowedMethods`: + +[source,groovy] +.grails-app/controllers/UrlMappings.groovy +---- +static mappings = { + query "/books/search"(controller: 'book', action: 'search') +} +---- + +[source,groovy] +.grails-app/controllers/com/demo/BookController.groovy +---- +class BookController { + + static allowedMethods = [search: 'QUERY'] + + def search(BookSearch criteria) { + respond Book.findAllByTitleLike("%${criteria.title}%") + } +} +---- + +A JSON body is bound to the command object the action takes, and a form-encoded body is also available in `params`, through the same `FormContentFilter` that parses `PUT`, `PATCH` and `DELETE` bodies. As for `GET`, a domain class the action takes is looked up by its identifier but not bound to the body, because a `QUERY` does not change anything. + +Other things to know about `QUERY`: + +* A browser cannot send it from an HTML form. Call it from JavaScript or another HTTP client. +* For a native image, `traceNativeMetadata` reaches it through `requests`, as the <> describes. +* `grails-openapi` leaves these mappings out of the document it generates, and logs each one, because the OpenAPI versions it writes have no operation for the method. +* The Spring Security plugin accepts `QUERY` requests. An application that configures Spring Security with its own configuration DSL instead gets a `StrictHttpFirewall`, which rejects the method until it is added with `setAllowedHttpMethods`, and whichever one it uses, `CsrfFilter` asks a `QUERY` for a token where CSRF protection is on. diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index 9b4cb68887c..8a769c10e22 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -17,10 +17,9 @@ specific language governing permissions and limitations under the License. //// -=== Upgrade Instructions for Grails and Related Dependencies +=== Upgrade Instructions for Grails 8 to Grails 9 -To ensure compatibility with Grails {version}, please review the following upgrade instructions carefully. -This guide outlines the necessary steps and warnings to upgrade your Grails project from Grails 8.x to Grails 9. +This guide outlines the changes introduced in Grails 9 and the steps required to upgrade your application. [TIP] ==== @@ -38,10 +37,171 @@ skill 'org.apache.grails.skills:grails-9-upgrade:{version}' ==== 1. Groovy 6 -Grails 9 builds on Groovy {groovyVersion} and the matching Spock {spockVersion}; Java 21, Spring Boot {springBootVersion} and Spring Framework are unchanged from Grails 8. +Grails 9 builds on Groovy {groovyVersion} and the matching Spock {spockVersion}; Java 21 is unchanged from Grails 8. The Grails BOM manages Groovy and Spock, so remove any version an application pins for them and review the https://groovy-lang.org/releasenotes/groovy-6.0.html[Groovy 6 release notes] for language changes that affect application code. -==== 2. GPars Integration Deprecated in Favour of Groovy Concurrency +==== 2. Spring Boot 4.2 and Spring Framework 7.1 + +Grails 9 moves from Spring Boot 4.1 and Spring Framework 7.0, which Grails 8 uses, to Spring Boot {springBootVersion} and Spring Framework {springVersion}. +The managed versions that change include Spring Security 7.2, Spring Data 2026.1, Micrometer 1.18 and Reactor 2026.0. +Review the https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-4.2-Release-Notes[Spring Boot 4.2 Release Notes] and the https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-7.1-Release-Notes[Spring Framework 7.1 Release Notes] for the changes that affect your application. +The ones most likely to affect a Grails application are: + +* *`server.forward-headers-strategy: framework` reads only the `+X-Forwarded-*+` headers by default.* +Spring Framework 7.1's `ForwardedHeaderFilter` reads either the RFC 7239 `Forwarded` header or the `+X-Forwarded-*+` headers, and Spring Boot 4.2 configures it for `+X-Forwarded-*+`. +Before, it read `Forwarded` when the request carried one and `+X-Forwarded-*+` otherwise. +If your reverse proxy sends `Forwarded`, set `spring.mvc.forwarded-headers.header-format: standard`. +The filter also ignores `X-Forwarded-Prefix` now, where it used to add it to the context path, so if your proxy strips a path prefix and sends that header, absolute links and redirects lose the prefix until you set `spring.mvc.forwarded-headers.use-forwarded-prefix: true`. +Spring Boot 4.2 also registers the filter from its Spring MVC auto-configuration, which backs off when the application declares `@EnableWebMvc`; such an application must now register a `ForwardedHeaderFilter` itself. + +* *Embedded Tomcat keeps relative redirect locations relative.* +`server.tomcat.servlet.use-relative-redirects`, renamed from `server.tomcat.use-relative-redirects`, now defaults to `true`, Tomcat's own default. +A redirect sent through `HttpServletResponse.sendRedirect` with a path, such as a controller `chain` call, a Spring Security redirect or a `redirect:` view name, now has a relative `Location` header instead of an absolute URL. +A controller `redirect` sets the `Location` header itself and still sends an absolute URL unless you pass `absolute: false`. +Browsers resolve relative locations; set the property to `false` if a client or test expects an absolute URL. + +* *`TestRestTemplate` is deprecated for removal.* +Spring Framework 7.1 deprecates `RestTemplate`, and Spring Boot 4.2 deprecates `TestRestTemplate` and `@AutoConfigureTestRestTemplate` with it. +Move `@SpringBootTest` tests to `RestTestClient`, which `@AutoConfigureRestTestClient` injects: ++ +[source,groovy] +---- +// Before +import org.springframework.boot.resttestclient.TestRestTemplate +import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@AutoConfigureTestRestTemplate +class BookApiSpec extends Specification { + @Autowired TestRestTemplate rest +} + +// After +import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient +import org.springframework.test.web.servlet.client.RestTestClient + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@AutoConfigureRestTestClient +class BookApiSpec extends Specification { + @Autowired RestTestClient client +} +---- + +* *A problem detail no longer gets the request path as its `instance`.* +RFC 9457 defines `instance` as a URI that identifies the specific occurrence of a problem, so Spring Framework 7.1 no longer sets it to the request path when a handler method returns a `ProblemDetail` or an `ErrorResponse`. +`respond` follows it: a `ProblemDetail` or `ErrorResponse` it writes, and the problem it writes for validation errors, no longer get one either. +A client that reads `instance` gets it only where the application sets it: in a `ResponseEntityExceptionHandler` for Spring MVC controllers, and on the problem an action responds with, including one `validationProblemDetailFactory.create(errors)` makes for validation errors: ++ +[source,groovy] +---- +ProblemDetail problem = validationProblemDetailFactory.create(book.errors) +problem.instance = URI.create("urn:uuid:${UUID.randomUUID()}") +respond problem +---- + +* *Spring MVC no longer detects the JAXB XML converter.* +Spring Framework 7.1 stops auto-detecting the JAXB message converter for server applications. +Grails `render` and `respond` write XML through Grails' own converters and are unaffected. +A Spring MVC `@RestController` that returns JAXB-annotated objects needs the converter registered explicitly, as the Spring Framework 7.1 release notes show. + +* *Tests load static nested `@Configuration` classes from their superclasses and enclosing classes.* +The Spring TestContext Framework used to ignore such a class unless its class hierarchy declared `@ContextConfiguration`, and now adds it to the test's application context. +Grails 8 already logs a warning for each one it ignores, so a test run on Grails 8 shows the affected tests. +Remove the `@Configuration` annotation, or move the class to a top-level class, if the test should not load it. + +* *`bootBuildImage` builds on Ubuntu 26.04.* +The default builder is `paketobuildpacks/ubuntu-resolute-builder`, in place of `paketobuildpacks/builder-noble-java-tiny`, and images are based on Ubuntu 26.04 instead of 24.04. +With no `builder` configured, the run image is `paketobuildpacks/ubuntu-resolute-run-tiny:latest`, which still has no shell; with one configured, it is the run image of the builder, which for `ubuntu-resolute-builder` has a shell. +The builder no longer pins the version of the run image, so set both `builder` and `runImage` to specific versions for repeatable images, or set `builder` to `paketobuildpacks/builder-noble-java-tiny` to keep the previous one. + +* *An exception an action throws is now recorded as an error on the HTTP server observation.* +When an error view answered, as the `"500"(view: '/error')` mapping of a generated application does, `http.server.requests` tagged the 500 with `error` and `exception` values of `none`, and tracing did not see a failure. +A `500` mapping to a controller tagged it with `GrailsWrappedRuntimeException` and with the `uri` of that controller, so every failure it answered looked like the same route. +Both now report the exception the action threw and the route of the action, and the span of the request is marked as failed. +That includes an `Error`, such as a failed `assert`, rather than the `ServletException` Spring MVC wraps it in. +These requests were already counted with `outcome=SERVER_ERROR`, but an alert that counts a non-`none` `error` value will see more server errors than before, and a dashboard that reads the `uri` of an error controller will find those failures under the routes of the actions. +After a forward to an error controller, the `jakarta.servlet.error.exception` request attribute holds the exception the action threw rather than a `GrailsWrappedRuntimeException`, for a filter that reads it once the chain has returned. Inside the error controller it still holds the wrapper. +A client error is not recorded, and neither is a failure inside `g:include`, whose error page is rendered inside a page that goes on to answer the request. +The route is now set before the Grails interceptors run, so that a failure in one is reported under the route of the action. +An interceptor whose `before()` returns `false`, to render a 403 or to redirect, stopped the request before its route was set, so the request was reported with a `uri` of `UNKNOWN`, or `REDIRECTION` for a redirect. +It is now reported under the route of the action, so a dashboard or alert keyed on those `uri` values sees those requests move. + +* *`http.server.requests.active` is no longer registered by default.* +Spring Boot 4.2 makes this long task timer opt-in. +A dashboard or alert that reads it sets `management.metrics.observations.include-active-long-task-timer` to `true`, which replaces `management.metrics.observations.ignored-meters`. + +* *`spring-boot-starter-amqp` no longer means RabbitMQ.* +It is now the starter for generic AMQP 1.0, and RabbitMQ has starters of its own. +An application that uses RabbitMQ through `spring-boot-starter-amqp` or `spring-boot-starter-amqp-test` changes to `spring-boot-starter-rabbitmq` or `spring-boot-starter-rabbitmq-test`, and any import of `org.springframework.boot.amqp.autoconfigure` to `org.springframework.boot.rabbitmq.autoconfigure`. +RabbitMQ over AMQP 1.0 has `spring-boot-starter-amqp-rabbitmq`. + +* *The Spring Security plugin no longer saves the requests browsers send on their own.* +A denied request for `/apple-touch-icon.png`, `/manifest.json` or the `/.well-known/appspecific/com.chrome.devtools.json` that Chrome DevTools sends used to be saved, so a login redirected to it instead of to the page the user asked for. +Spring Security 7.2's configuration DSL ignores these paths, which the plugin does not use, so the plugin now ignores them too, with `grails.plugin.springsecurity.requestCache.ignoredPatterns`. +An application that secures a page at one of these paths and wants a login to return to it can leave the path out of that list. + +* *Digest authentication in the Spring Security plugin starts and signs users in.* +`useDigestAuth` with its default of hashed passwords used to fail at startup, naming an encoder that no longer exists. +What a user has stored is now the hash a browser computes, which the application computes, as the link:security.html#core-basicAndDigestAuth[guide] shows. +With `digest.useCleartextPasswords`, nobody could sign in with Digest authentication, because the plugin replaced `passwordEncoder` with one that stored `{noop}secret`. +`passwordEncoder` now stays as configured, and the application stores the password as it was typed, so an application with stored values such as `{noop}secret` removes the `{noop}` prefix from them and no longer stores them with `springSecurityService.encodePassword`. +A form or Basic authentication signs a user in with their password in either mode. + +* *`fii.alwaysReauthenticate` in the Spring Security plugin takes effect.* +The setting did nothing, because the plugin's security interceptor went straight to the access decision. +An application that sets it now has the `Authentication` of a request to a secured URL authenticated again, while it still holds its credentials, as it does with `providerManager.eraseCredentialsAfterAuthentication` set to `false`. +By default the credentials are erased at sign-in, so a user signed in through a form is not authenticated again. + +[TIP] +==== +The Spring Boot properties migrator, described in the link:upgrading.html#upgrading80x[Grails 8 upgrade guide], finds the configuration properties Spring Boot 4.2 renames, such as `server.tomcat.use-relative-redirects`. +Spring Boot 4.2 can also make the application fail to start while such a property is left: set `spring.properties.migrator.fail` to `on-warning` to fail on renamed and removed properties, or to `on-error` to fail on removed ones only. +The default, `never`, only logs them. +==== + +==== 3. graphql-java 26 for GraphQL Applications + +Spring Boot 4.2 manages graphql-java 26, so applications using the GraphQL plugin (`grails-data-graphql`) move from graphql-java 25 to graphql-java 26.1. +graphql-java 26.0 contains breaking changes, listed in its https://github.com/graphql-java/graphql-java/releases/tag/v26.0[release notes]. +The ones that can affect a Grails application are: + +* *Query complexity limits are enforced by default.* +A query nested more than 100 levels deep, or selecting more than 100,000 fields, now fails validation with a `MaxQueryDepthExceeded` or `MaxQueryFieldsExceeded` error. +To raise the limits, or to turn them off, set new defaults when the application starts, for example in `BootStrap.groovy`: ++ +[source,groovy] +---- +import graphql.validation.QueryComplexityLimits + +class BootStrap { + + def init = { + QueryComplexityLimits.setDefaultLimits(QueryComplexityLimits.newLimits() + .maxDepth(150) + .maxFieldsCount(200_000) + .build()) + // or, to turn the limits off: + // QueryComplexityLimits.setDefaultLimits(QueryComplexityLimits.NONE) + } +} +---- ++ +graphql-java's parser separately rejects a query nested more than about 165 levels deep, so a `maxDepth` above that has no effect. + +* *Schemas built in code are validated like schemas built from SDL.* +A required argument or input field (non-null with no default value) can no longer be deprecated. +The schema the plugin generates never deprecates an argument or an input field, but arguments and input types an application adds through a `GraphQLSchemaInterceptor` must follow the rule. + +* *Additional schema types must be named types.* +`GraphQLSchemaInterceptor.interceptSchema` still receives the additional types as a `Set`, and the plugin unwraps list and non-null wrappers before it builds the schema, so existing interceptors need no change. +Code that builds a `GraphQLSchema` directly must pass `GraphQLNamedType` instances to `GraphQLSchema.Builder.additionalTypes`. + +The other breaking changes (the validation rule filter, the removal of `DirectiveInfo`, and the JSpecify nullability annotations) only affect code that uses graphql-java directly. +The nullability annotations document existing behavior and do not change what graphql-java returns at runtime. + +The plugin continues to use graphql-java-extended-scalars 24.0, its latest release, which works with graphql-java 26.1. + +==== 4. GPars Integration Deprecated in Favour of Groovy Concurrency Groovy 6 brings a unified concurrency and parallel-processing toolkit into core under https://groovy-lang.org/releasenotes/groovy-6.0.html#concurrency-toolkit[GEP-18: Integrated Concurrency and Parallel Processing]. The `groovy.concurrent` package provides `Awaitable`, `DataflowVariable`, `Actor`, `Agent`, `Pool`, `ParallelScope` and the parallel collection methods such as `eachParallel` and `collectParallel`. The Groovy release notes describe the toolkit as modernising the patterns from GPars around virtual threads, structured concurrency and Groovy 6's `async`/`await`, and state that previous users of GPars will find much of its functionality there. @@ -66,7 +226,7 @@ Applications that use GPars directly, for example `GParsPool.withPool { list.eac NOTE: Groovy 6 marks the concurrency toolkit as incubating. The Grails modules that build on it expose only the Grails `Promise` and `EventBus` contracts, so changes to the Groovy API in later Groovy releases are absorbed by Grails rather than by application code. -==== 3. Groovy Code Is Compiled with invokedynamic +==== 5. Groovy Code Is Compiled with invokedynamic Grails 8 compiles every Groovy source set of an application without invokedynamic (`grails { indy = false }` was the default). Grails 9 leaves the setting unset, so the Groovy compiler's own default applies, and on Groovy 6 that is invokedynamic. An application that set `grails { indy = true }` can remove the line. Build logic that read the property, for example `grails.indy.get()`, must allow for it having no value now: use `grails.indy.getOrElse(true)` or check `grails.indy.present`. @@ -86,7 +246,7 @@ Classic call sites on Groovy 6 need `org.apache.groovy:groovy-callsite` on the c A library compiled with classic call sites needs the module too, whatever the application's own setting, because Groovy 6 loads such a class only with it; the failure is a `NoClassDefFoundError` naming `org.codehaus.groovy.runtime.callsite.CallSiteArray`. Spock 2.5, Geb 8 and the Asset Pipeline 5.2 are compiled with invokedynamic, but `org.testcontainers:testcontainers-spock` is not, so an application that uses it declares `testRuntimeOnly 'org.apache.groovy:groovy-callsite'` next to it. -==== 4. XML Web Support Moves to a Separate Module +==== 6. XML Web Support Moves to a Separate Module XML conversion, request-body binding, and REST renderers now live in the optional `grails-xml` module. In Grails 9 the web starter still brings it, and so does `grails-web-boot`, which the @@ -142,14 +302,14 @@ several, so their `groovyPageLocator` and `rendererRegistry` properties are no l application sets them. Without the module, nothing renders XML except what the application provides, such as markup views, and `respond` -treats XML as any format nothing renders (see section 13): it answers in the next type the client accepts that the +treats XML as any format nothing renders (see section 15): it answers in the next type the client accepts that the action responds in, and a client that accepts any type, by sending `*/*` or no `Accept` header, or a browser, in the first format the action lists that it can render the value in, so an action that lists `['xml', 'json']` answers those clients in JSON. A markup view renders XML for an action that has one; for an action without one, the client's next type is tried. Where no type the client accepts can be rendered, `respond` sends a `406 Not Acceptable` error, or a `404` one for validation errors where the client accepts one of the action's formats, so that the application's error pages apply, and writes a `ProblemDetail` as -`application/problem+json` (see section 11). An XML request body is not read and raises no error, while the +`application/problem+json` (see section 13). An XML request body is not read and raises no error, while the request parameters bind as they do for a request without a body, except that binding it to a collection with `bindData` fails, as for any body of a type nothing binds; and `request.XML` throws an `IllegalStateException` that names the module, while a class compiled with `@CompileStatic` or `@GrailsCompileStatic` @@ -193,11 +353,11 @@ APIs. This change does not affect Grails plugin descriptors. Plugin metadata continues to be generated and published in its existing descriptor format; `grails-xml` concerns application HTTP payloads only. -==== 5. Opt Into Spring JSON Conversion +==== 7. Opt Into Spring JSON Conversion Each JSON and XML path keeps its Grails 8 output by default in Grails 9, and has a setting that opts into the Jackson-based one. A `ProblemDetail` is the exception: `respond` writes it as Spring MVC writes it, whatever the -settings, with the exceptions section 11 lists. The smaller changes that apply whatever the settings, such as `Accept` +settings, with the exceptions section 13 lists. The smaller changes that apply whatever the settings, such as `Accept` headers matched whatever their case, are in sections 4, 7, 9, 11, 13, 14 and 16, and in this section, where `grails-converters` now brings Boot's `JsonMapper`, which Spring MVC's JSON converter then writes with. @@ -235,12 +395,12 @@ now brings Boot's `JsonMapper`, which Spring MVC's JSON converter then writes wi | unchanged until legacy XML conversion is removed in Grails 11 | `grails.exceptionresolver.springHandlers` -| Exceptions actions throw, resolved by Spring MVC's `@ExceptionHandler` methods (see section 15) +| Exceptions actions throw, resolved by Spring MVC's `@ExceptionHandler` methods (see section 17) | `false` | `true` in Grails 10 | `grails.controllers.responseBody` -| Values of actions annotated `@ResponseBody`, written by Spring MVC as the body (see section 16) +| Values of actions annotated `@ResponseBody`, written by Spring MVC as the body (see section 18) | `false` | `true` in Grails 10 |=== @@ -310,7 +470,7 @@ for the type, or a converter of another library, it is labelled with a JSON type `JsonMapper` too, and Spring MVC's JSON converter writes with it rather than with a mapper of its own. The standard Jackson converter uses an isolated mapper derived from Boot's configured `JsonMapper` for every value but -a `ProblemDetail`, which Spring's converter writes with Boot's mapper itself (see section 11). +a `ProblemDetail`, which Spring's converter writes with Boot's mapper itself (see section 13). It renders domain classes with the same Jackson serializer as the `JsonMapper` itself does when `grails.converters.json.domain.jackson.enabled` is set, and as `render ... as JSON` does with `grails.converters.json.legacy` set to `false` (see the section on domain classes below): the @@ -361,10 +521,10 @@ Controller unit tests use Boot's Jackson configuration and the same setting. Sup `JsonMapperBuilderCustomizer` from a nested Spring `@Configuration` class, so Boot can see it before evaluating its conditional beans. A mapper registered by the deprecated `doWithSpring` callback or by `beanRegistrar()` is too late to make Boot back off. Named JSON -configurations work through `render` and `respond` in `ControllerUnitTest`. Section 14 covers the +configurations work through `render` and `respond` in `ControllerUnitTest`. Section 16 covers the other changes to web unit tests. -==== 6. JSON Converter Customization Moves to Jackson +==== 8. JSON Converter Customization Moves to Jackson The `grails.converters.JSON.registerObjectMarshaller(...)` API remains supported for `render ... as JSON` and converter-based rendering. Registering a marshaller does not change the @@ -453,7 +613,7 @@ property of `JsonRenderer` and `JsonCollectionRenderer` beans, which selects one the `renderJson(JSON, RenderContext)` method their subclasses override to render with the legacy converter. This timetable does not deprecate `JSON.registerObjectMarshaller(...)`. -==== 7. `grails.converters.JSON` Can Write JSON With Spring Boot's JsonMapper +==== 9. `grails.converters.JSON` Can Write JSON With Spring Boot's JsonMapper In Grails 9, `render ... as JSON`, `respond` for a JSON request and every other use of `grails.converters.JSON` still render JSON as Grails 8 did, by default (see <>). Setting @@ -730,7 +890,7 @@ JSON.registerObjectMarshaller(Locale) { Locale locale -> locale.toString() } JSON.registerObjectMarshaller(byte[]) { byte[] bytes -> bytes as List } ---- -==== 8. Domain Classes Render as Jackson Beans of Their Persistent Properties +==== 10. Domain Classes Render as Jackson Beans of Their Persistent Properties With `grails.converters.json.legacy` set to `false`, the JSON converter renders a domain class instance with Jackson's own bean serializer, which Grails shapes to the domain class, as `DomainClassMarshaller` rendered it in Grails 8, with these @@ -788,7 +948,7 @@ The mapper renders associated instances as references of their id, such as `{"id or in full with `grails.converters.json.default.deep`, and proxies as the instances they proxy, and names the id of a reference as the referenced class names it. -==== 9. Rendering Single Values as JSON +==== 11. Rendering Single Values as JSON In Grails 8, rendering a single enum value with `render(Role.HEAD as JSON)` threw a `ConverterException`. It now produces the JSON string `"HEAD"`. The default enum marshaller uses the enum name, including when the @@ -803,7 +963,7 @@ no longer throws a `ConverterException` merely because the root is a scalar. `JSONWriter` accepts a single value outside any object or array whatever the `grails.converters.json.legacy` setting, as the section on writing JSON with Spring Boot's `JsonMapper` describes. -==== 10. Opt Into Jackson JSON Request Binding +==== 12. Opt Into Jackson JSON Request Binding Grails 9 retains `JsonSlurper` request binding by default and logs a startup deprecation notice. As in Grails 8, an application `JsonSlurper` bean, for example one using `JsonParserType.LAX`, replaces @@ -848,7 +1008,7 @@ as `BigDecimal` rather than `Double`, so binding a request value such as `1.15` retains exactly the digits the request sent. Whole numbers bind as `Integer`, `Long`, or `BigInteger` when too large for a `Long`, as with the legacy parser. -==== 11. REST Validation Errors Use RFC 9457 Problems +==== 13. REST Validation Errors Use RFC 9457 Problems With `grails.web.rendering.json.spring: true`, `respond()` renders Spring `Errors` as `application/problem+json` instead of the legacy `{"errors": [...]}` body. Grails 9 retains the legacy @@ -894,9 +1054,10 @@ Applications can also inject the factory when they need to create or customize t When `respond` receives a `ProblemDetail`, it hands it to the handler Spring MVC's handler adapter writes a `ResponseEntity` with, so the problem is written as a Spring MVC controller method that returns it is written: the application's `ResponseBodyAdvice` beans apply to it as to a method of the controller, and its -`ErrorResponse.Interceptor` beans see it. The response has the status the problem carries, and an absent `instance` is -set to the current resource path. A `status` argument decides the status instead, as the status of a `ResponseEntity` -does in Spring MVC: a problem that carries another keeps its own, and Spring MVC logs a warning. A Spring +`ErrorResponse.Interceptor` beans see it. The response has the status the problem carries, and a problem without an +`instance` is written without one, as Spring MVC writes it. A `status` argument decides the status instead, as the +status of a `ResponseEntity` does in Spring MVC: a problem that carries another keeps its own, and Spring MVC logs a +warning. A Spring `ErrorResponse`, such as an `ErrorResponseException`, is answered with its problem, whose message codes are resolved with the application's messages as Spring MVC's `ResponseEntityExceptionHandler` resolves them, and with its status and headers, where Grails 8 wrote it as a bean with a 200. Spring MVC's content @@ -951,14 +1112,14 @@ ValidationProblemDetailFactory includingRejectedValues(MessageSource messageSour } ---- -==== 12. HAL Rendering +==== 14. HAL Rendering Existing Grails HAL JSON renderers remain available through explicit renderer registration. Adding `spring-boot-starter-hateoas` alone does not register a Grails renderer for Spring HATEOAS `EntityModel` values. Return those models from a Spring MVC controller or provide an explicit Grails renderer for the desired media type. -==== 13. Grails Format Resolution and Spring MVC Content Negotiation +==== 15. Grails Format Resolution and Spring MVC Content Negotiation Grails format resolution -- `withFormat`, `respond`, and `response.format` -- applies the `grails.mime.*` rules. Spring MVC keeps its own content negotiation: an `@RestController`, an actuator endpoint or any other @@ -982,7 +1143,7 @@ target implements now handles its bodies in a content type no creator for `Objec interface of `java.lang.Class`, such as `Serializable`, no longer matches every class, nor does a renderer registered for one stand in as the fallback renderer of JSON and markup views. A value Spring rejects, such as a quality value above 1, is still read as Grails 8 read it, except where `respond` writes a -`ProblemDetail` as without a view, which is answered as Spring MVC answers such a client (see section 11). Parameter names are read ignoring case, +`ProblemDetail` as without a view, which is answered as Spring MVC answers such a client (see section 13). Parameter names are read ignoring case, as Spring reads them, so `text/html;Q=0.1` has the quality 0.1, where Grails 8 read only a lower-case `q`. The problem types of RFC 9457, `application/problem+json` and `application/problem+xml`, are the `json` and `xml` formats unless `grails.mime.types`, or a `MimeTypeProvider` bean, such as a renderer or binding source creator, provides a type of that name. A binding @@ -1000,7 +1161,7 @@ to `all`, so an action answered with its first format; action responds in XML. A client that sends only a problem type is therefore answered as one that asks for that format: `respond` sends `406 Not Acceptable` for an action that does not respond in it, where Grails 8 answered in the action's first format, and `withFormat` runs no block where it has neither one for that format nor a `*` one, where Grails 8 ran -its first block. `respond` writes a `ProblemDetail` in the type section 11 describes, whatever format this selects, +its first block. `respond` writes a `ProblemDetail` in the type section 13 describes, whatever format this selects, in XML only where the action responds in XML, and any other value as the format's own type, such as `application/xml`. In `Content-Type`, Grails 8 gave `null`: a body a client sends as `application/problem+json` is now the `json` format, which `withFormat` selects and the JSON binding source binds, and one sent as `application/problem+xml` the `xml` format, which the XML binding source of `grails-xml` binds. @@ -1038,7 +1199,7 @@ Spring MVC asks for its formats while it starts. Spring MVC negotiates with them only when the application enables parameter negotiation with `spring.mvc.contentnegotiation.favor-parameter: true`. Spring Boot leaves it disabled, so after the upgrade `?format=json` still does not change what a Spring MVC endpoint returns. Where it is enabled, it also decides the type `respond` -writes a `ProblemDetail` in, as for a Spring MVC controller (see section 11). Browser formats such +writes a `ProblemDetail` in, as for a Spring MVC controller (see section 13). Browser formats such as `html` and `js` are not registered, preserving Spring's reflected-file-download protection. Where a format has several media types, an `application/*` one is preferred over a `text/*` one, and Boot's `spring.mvc.contentnegotiation.media-types.*` overrides these defaults. Invalid names are skipped. @@ -1052,7 +1213,7 @@ method of `DefaultAcceptHeaderParser` takes the requested `MimeType`, where it t that overrode the Grails 8 method no longer overrides it. The settings are read once for each application context, which a unit test keeps for all the features of its spec, so a spec sets them in `doWithConfig` rather than in a feature. -==== 14. Web Unit Tests Set Up More of the Web Layer +==== 16. Web Unit Tests Set Up More of the Web Layer Web test traits such as `ControllerUnitTest` and `TagLibUnitTest` now always include the `restResponder` plugin, even when a spec overrides `getIncludePlugins()`. They also provide JSON @@ -1067,7 +1228,7 @@ on the test classpath, without loading the XML plugin's dependency graph. A spec asserts on the beans in its application context sees these additions. The manager is built as Spring MVC builds it, from `spring.mvc.contentnegotiation.*`, which a spec sets in `application.yml` or `doWithConfig`, and the spec's `WebMvcConfigurer` beans, so `respond` writes a `ProblemDetail` in a unit test in the type it writes it in for the -application (see section 11). The `format` parameter that `favor-parameter` reads is one the spec adds to the request, +application (see section 13). The `format` parameter that `favor-parameter` reads is one the spec adds to the request, with `request.addParameter`, as a query parameter is added, not `params.format`, which stands for the format a URL mapping gives and which Spring MVC does not read either. A spec for an application that declares `@EnableWebMvc`, which turns off Spring Boot's Spring MVC auto-configuration and with it those properties, declares the manager the @@ -1102,7 +1263,7 @@ own, a session locale resolver among them. Without `grails-xml` on the test clas rather than to a `String` of XML, or reading `request.XML` fails with an `IllegalStateException` that names the module. `GrailsUnitTest` and `ServiceUnitTest` do not load XML or the web plugins just because those modules are on the classpath. See <>. -==== 15. Opt Into Spring MVC Exception Handlers +==== 17. Opt Into Spring MVC Exception Handlers Spring MVC resolves an exception with the `@ExceptionHandler` methods of the controller and of the `@ControllerAdvice` beans only for a handler method, which a Grails action is not, so such methods, and Spring Boot's handler of @@ -1121,7 +1282,14 @@ grails: springHandlers: true ---- -==== 16. Actions Can Return Spring MVC Responses +Spring Framework 7.1 adds `ProblemDetailsConfigurer.renderUnhandledExceptions`, which renders any exception no exception +handler method handles as a problem. Turned on in a `WebMvcConfigurer`, it applies to Grails actions whatever +`grails.exceptionresolver.springHandlers` says, and answers every exception, a browser's included, ahead of the Grails +exception resolver, so `"500"` URL mappings no longer apply. Set `grails.exceptionresolver.problemDetails: true` instead, +which writes the same problems and leaves the error page to the clients that prefer HTML. See +<>. + +==== 18. Actions Can Return Spring MVC Responses An action that returns a `ResponseEntity` or another `HttpEntity`, a `ProblemDetail`, an `ErrorResponse`, `HttpHeaders`, a `StreamingResponseBody` or a `ResponseBodyEmitter` has it written as Spring MVC writes what a handler @@ -1144,7 +1312,7 @@ grails: responseBody: true ---- -==== 17. Generated GORM Identity and Version Properties Stay Within Domain Classes +==== 19. Generated GORM Identity and Version Properties Stay Within Domain Classes Grails 9 no longer adds generated `id` and `version` properties to a non-domain superclass of a domain class. Previously, these properties could be added to a shared abstract base class, causing command objects extending that same base class to inherit persistence properties and fail validation for `id` and `version`. @@ -1171,7 +1339,7 @@ When upgrading an affected application: See xref:theWebLayer#commandObjects[Command Objects] for a trait-based alternative to sharing a non-domain superclass with a domain class. -==== 18. RxGORM Runs on RxJava 3 +==== 20. RxGORM Runs on RxJava 3 RxGORM, the reactive variant of GORM in `org.apache.grails.data:grails-datamapping-rx`, is built on RxJava 3 instead of RxJava 1. Its dependency is now `io.reactivex.rxjava3:rxjava`, and the RxJava 1 artifact `io.reactivex:rxjava` is no longer on the classpath of an application that only uses RxGORM. @@ -1220,7 +1388,7 @@ A datastore implementation of RxGORM built for Grails 8 or earlier, such as the The `RxEntity` domain model has had no datastore implementation for several releases and its removal is under discussion for Grails 9, so an application that implements `RxEntity` should plan to move to `GormEntity` with the RxJava data services, which work on every GORM datastore. The import and blocking-call changes above apply to both. The `rx-mongodb` profile feature is removed, because the `org.grails.plugins:rx-mongodb` plugin it added has had no release since GORM 6.1 and has never been published to Maven Central, so an application generated with it could not resolve its dependencies. -==== 19. RxJava 1 and RxJava 2 Async and Events Modules Are Deprecated +==== 21. RxJava 1 and RxJava 2 Async and Events Modules Are Deprecated The RxJava 1 and RxJava 2 implementations of the Grails promise and event bus APIs, `grails-async-rxjava`, `grails-async-rxjava2`, `grails-events-rxjava` and `grails-events-rxjava2`, are deprecated for removal. They keep working in Grails 9 and log a warning when their promise factory or event bus is created. @@ -1230,19 +1398,19 @@ See <> and <>. `grails-async-rxjava3` carries a `null` value as the RxJava 1 factory and every other promise factory do: `createPromise()` and `createPromise(Class)` give a promise whose `get()` returns `null`, a task that returns `null` resolves to `null`, and a `null` value keeps its place in the list that `onComplete`, `onError` and `waitAll` deliver. `grails-async-rxjava2` does not: there `createPromise()` and `onComplete` over a list holding a bound promise of `null` fail with a `NullPointerException`, and a task that returns `null` resolves to the `java.lang.Void` class. -==== 20. Spring Boot Registers the Character Encoding Filter +==== 22. Spring Boot Registers the Character Encoding Filter Grails no longer registers a character encoding filter of its own. It used to, and Spring Boot's backed off, so `spring.servlet.encoding` had no effect in a Grails application. Spring Boot's filter now decodes requests: it runs at `Ordered.HIGHEST_PRECEDENCE`, ahead of every other filter, and `spring.servlet.encoding` configures it. -* The request encoding is now overridden by default, as in any Spring Boot application, so a request that declares a charset of its own is still decoded as UTF-8: its parameters, a body read through `getReader()`, `request.JSON` and `request.XML`, and a JSON or XML body bound to a command object or domain class. The parameters of a PUT, PATCH or DELETE form are the exception, as in Grails 8: the `FormContentFilter` that parses them decodes them with the declared charset, forced or not. To keep the charset a request declares, as Grails 8 did, set `spring.servlet.encoding.force-request: false`. +* The request encoding is now overridden by default, as in any Spring Boot application, so a request that declares a charset of its own is still decoded as UTF-8: its parameters, a body read through `getReader()`, `request.JSON` and `request.XML`, and a JSON or XML body bound to a command object or domain class. The parameters of a PUT, PATCH, DELETE or QUERY form are the exception, as in Grails 8: the `FormContentFilter` that parses them decodes them with the declared charset, forced or not. To keep the charset a request declares, as Grails 8 did, set `spring.servlet.encoding.force-request: false`. * Grails 8.1 already registers its encoding filter at `Ordered.HIGHEST_PRECEDENCE`, so an application coming from 8.1 sees no change in where the encoding filter runs. In an application coming from 8.0, filters ordered between `Ordered.HIGHEST_PRECEDENCE + 1` and -181, which used to run before Grails' encoding filter, now run after Spring Boot's, with the request encoding set, as section 5 of <> describes. asset-pipeline's filter at -190 is one: in a packaged application (a WAR or an executable jar) it copies the request's encoding to the precompiled assets it serves, so every asset response now declares a charset, for example `text/css;charset=UTF-8`, and browsers use it for stylesheets and scripts instead of detecting their encoding. A filter among them that reads a request body sent without a charset through `getReader()` now decodes it as UTF-8 instead of ISO-8859-1. * `grails.filter.encoding` and `grails.filter.forceEncoding` are deprecated. Grails 9 translates them into `spring.servlet.encoding.charset` and `spring.servlet.encoding.force` and logs a warning, and a `spring.servlet.encoding` setting of the application wins over them. Use the Spring Boot properties instead. -* `GrailsFilters.CHARACTER_ENCODING_FILTER` is deprecated, and its order is now `Ordered.HIGHEST_PRECEDENCE`, the order of Spring Boot's encoding filter, where Grails 8.0 had -180. A filter ordered at `GrailsFilters.CHARACTER_ENCODING_FILTER.order + 1` therefore runs right after the encoding filter, and now also ahead of `FormContentFilter` and of Grails' security headers filter at `GrailsFilters.FIRST`: it no longer sees the parameters of a PUT, PATCH or DELETE form, and a response it sends without continuing the chain gets no security headers. Order it at `Ordered.HIGHEST_PRECEDENCE + 1` instead, or at `GrailsFilters.ASSET_PIPELINE_FILTER.order + 1` to keep the place it had in Grails 8.0. +* `GrailsFilters.CHARACTER_ENCODING_FILTER` is deprecated, and its order is now `Ordered.HIGHEST_PRECEDENCE`, the order of Spring Boot's encoding filter, where Grails 8.0 had -180. A filter ordered at `GrailsFilters.CHARACTER_ENCODING_FILTER.order + 1` therefore runs right after the encoding filter, and now also ahead of `FormContentFilter` and of Grails' security headers filter at `GrailsFilters.FIRST`: it no longer sees the parameters of a PUT, PATCH, DELETE or QUERY form, and a response it sends without continuing the chain gets no security headers. Order it at `Ordered.HIGHEST_PRECEDENCE + 1` instead, or at `GrailsFilters.ASSET_PIPELINE_FILTER.order + 1` to keep the place it had in Grails 8.0. * `spring.servlet.encoding.enabled: false` turns the encoding filter off. * The CAS plugin's single sign-out filter, which reads a parameter of every POST, is registered right after the encoding filter. * A filter of your own that reads request parameters needs a later order than `Ordered.HIGHEST_PRECEDENCE`, or form input reaches controllers decoded as ISO-8859-1. One registered at exactly `Ordered.HIGHEST_PRECEDENCE` through a `FilterRegistrationBean` runs before the encoding filter. -==== 21. A Configuration Key Below a Value That Is Not a Map Is Absent +==== 23. A Configuration Key Below a Value That Is Not a Map Is Absent In Grails 8, `getProperty` on `grailsApplication.config` answered a dotted key that runs past a value that is not a map with that value: `server.url.port` when `server.url` is a string, `server.ports.0` when `server.ports` is a list, or `grails.gorm.default.constraints.nullable` when `grails.gorm.default.constraints` is a closure from `application.groovy`. `containsProperty` of such a key was `true`. In Grails 9 the key is absent: `getProperty` returns `null` or the default, `containsProperty` returns `false`, and `navigate` answers `null`. @@ -1274,3 +1442,14 @@ A property source of lower precedence that sets the deeper key now answers it. W * Code that reads a child of a string or a closure and relied on being answered with the parent reads the parent instead. * Code that read a whole list through a key ending in a whole number, such as `server.ports.0`, now gets the element; it reads the list as `server.ports`. * A typed read with a default below such a value, such as `getProperty('x.y.enabled', Boolean, true)` when `x.y` is `false`, reads the parent, or the configuration sets the deeper key. + +==== 24. The Spring Security User Cache Stores Copies + +With `cacheUsers` enabled, the Spring Security plugin's user cache now stores users by value, so each lookup gets its own copy, made by serializing the user. Grails 8 stored the instance itself. The authentication manager erases the password of the user it signs in, and it erased the cached instance too: the next sign-in missed the cache and loaded the user again. A copy is erased instead, so the cached user keeps its password and the user kept in the session holds none. In a native image the plugin registers its own user classes for this. A user class or id type of your own needs serialization metadata, which `traceNativeMetadata` records when the login form is listed in `forms`. + +===== Required Actions + +* A user class of your own is serialized, as `UserDetails` is `Serializable`. A field that cannot be serialized, such as a reference to a domain instance, makes the user uncached: it is loaded on every sign-in, and a warning is logged. Mark such a field `transient`, or hold its id instead. +* Code that changed a user it read from `userCache` and expected the cache to see the change puts the user back with `userCache.putUserInCache`. +* To store users by reference, as Grails 8 did, set `cacheConfig` on the `userCache` bean to a `MutableConfiguration` with `setStoreByValue(false)`. + diff --git a/grails-doc/src/en/ref/Controllers/respond.adoc b/grails-doc/src/en/ref/Controllers/respond.adoc index b5b33abafe7..23022689bce 100644 --- a/grails-doc/src/en/ref/Controllers/respond.adoc +++ b/grails-doc/src/en/ref/Controllers/respond.adoc @@ -89,7 +89,8 @@ Supported `arguments`: A Spring `ProblemDetail` is written as Spring MVC writes one that a handler method returns: `respond` hands it to the handler Spring MVC's handler adapter writes a `ResponseEntity` with, so the application's `ResponseBodyAdvice` beans apply to it as to a method of the controller, and its `ErrorResponse.Interceptor` beans see it. The response has the -status the problem carries, and an absent `instance` is set to the current resource path. A `status` argument decides +status the problem carries, and a problem without an `instance` is written without one, as Spring MVC writes it; set the +`instance` where the application identifies the occurrence of a problem. A `status` argument decides the status of the response, as the status of a `ResponseEntity` does in Spring MVC: a problem that carries another keeps its own, and Spring MVC logs a warning. Its properties are written by the mapper of Spring Boot's converter, as a Spring MVC controller writes them: a domain class instance, validation errors or a value that leads back to itself among them is diff --git a/grails-doc/src/en/ref/Tags - GSP/cspNonce.adoc b/grails-doc/src/en/ref/Tags - GSP/cspNonce.adoc new file mode 100644 index 00000000000..38671591d0b --- /dev/null +++ b/grails-doc/src/en/ref/Tags - GSP/cspNonce.adoc @@ -0,0 +1,57 @@ +//// +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. +//// + + +== cspNonce + + + +=== Purpose + + +Renders the nonce of the request's nonce-based Content Security Policy, for the `nonce` attribute of an inline script or style. + + +=== Examples + + +[source,xml] +---- + + + +---- + + +=== Description + + +A policy configured with `grails.security.headers.content-security-policy.value` names the nonce with the `+{nonce}+` placeholder, for example `script-src 'self' 'nonce-+{nonce}+'`. +Each request then carries a nonce of its own, which this tag renders, and the policy the response carries names the same nonce. +See link:{guidePath}/security.html[Security] for configuring the policy. + +The tag renders the nonce Spring Security's `ContentSecurityPolicyNonceGeneratingFilter` generated when that filter put one on the request, so it can be used with a policy Spring Security writes as well. + +It renders nothing when the request carries no nonce. + +The tag works in a statically compiled page as well as a dynamic one. Code outside a view, such as a controller, reads the nonce with `ContentSecurityPolicyNonce.nonceOf(request)` from the `grails.web.http` package. diff --git a/grails-doc/src/en/ref/Tags - GSP/javascript.adoc b/grails-doc/src/en/ref/Tags - GSP/javascript.adoc index 1a97be1fe71..2dab073a2b4 100644 --- a/grails-doc/src/en/ref/Tags - GSP/javascript.adoc +++ b/grails-doc/src/en/ref/Tags - GSP/javascript.adoc @@ -44,6 +44,7 @@ Includes JavaScript libraries and scripts as well as providing a shorthand for i If you do not include a "src" attribute but instead provide a body, the result is an inline script. +When the request carries the nonce of a nonce-based Content Security Policy, the inline script carries it in its `nonce` attribute, so the policy allows it to run. If you are doing more advanced JavaScript and asset management we recommend checking out the Asset Pipeline plugin. diff --git a/grails-gradle/plugins/build.gradle b/grails-gradle/plugins/build.gradle index 25b8d9a7388..87e84e748ee 100644 --- a/grails-gradle/plugins/build.gradle +++ b/grails-gradle/plugins/build.gradle @@ -79,6 +79,11 @@ dependencies { // Testing - Gradle TestKit is auto-added by java-gradle-plugin testImplementation('org.spockframework:spock-core') { transitive = false } testImplementation 'org.apache.groovy:groovy-test-junit5' + // The validators behind Grails' email and url constraints, so what traceNativeMetadata fills an email or URL + // field with is checked the way an application's save checks it, at the version grails-datamapping-validation uses. + testImplementation("commons-validator:commons-validator:$commonsValidatorVersion") { + transitive = false + } testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' // The asset pipeline plugin the Grails BOM manages, so the layout this plugin gives it is tested // against the real one diff --git a/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/ExtractApplicationTask.groovy b/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/ExtractApplicationTask.groovy index 467793ce12f..15d62722d9b 100644 --- a/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/ExtractApplicationTask.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/ExtractApplicationTask.groovy @@ -37,11 +37,15 @@ import org.gradle.jvm.toolchain.JavaLauncher import org.gradle.process.ExecOperations /** - * Unpacks an executable jar into the form an AOT cache is read against. + * Unpacks an executable jar or war into the form an AOT cache is read against. * - *

An executable jar loads its dependencies through a nested-jar class loader; the extracted form - * loads them as ordinary jars on the classpath. A cache records the class path it saw, so only the - * second is a layout a later run can reuse one against.

+ *

An executable archive loads its dependencies through a nested-jar class loader; the extracted + * form loads them as ordinary jars on the classpath. A cache records the class path it saw, so only + * the second is a layout a later run can reuse one against.

+ * + *

A war is extracted as a jar, with the libraries it provides beside its own and what it holds + * outside {@code WEB-INF} in the jar's {@code META-INF/resources}, which needs Spring Boot 4.2 or + * later in the war.

* *

The work is done through injected services rather than through the project, so that nothing is * reached at execution time that the configuration cache forbids.

@@ -52,7 +56,7 @@ import org.gradle.process.ExecOperations @CompileStatic abstract class ExtractApplicationTask extends DefaultTask { - /** The archive to unpack, which has to be the one the cache will be trained against. */ + /** The executable jar or war to unpack, which has to be the one the cache will be trained against. */ @InputFile @PathSensitive(PathSensitivity.RELATIVE) abstract RegularFileProperty getArchiveFile() @@ -79,6 +83,16 @@ abstract class ExtractApplicationTask extends DefaultTask { @Inject abstract ExecOperations getExecOperations() + /** + * The name of the application the extraction writes for an archive of the given name: a jar + * keeps its name, and a war is written as a jar of the same name. + */ + static String applicationFileName(String archiveFileName) { + archiveFileName.toLowerCase(Locale.ROOT).endsWith('.war') ? + archiveFileName.substring(0, archiveFileName.length() - 4) + '.jar' : + archiveFileName + } + @TaskAction void extract() { File target = destination.get().asFile diff --git a/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/NativeMetadataExtension.groovy b/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/NativeMetadataExtension.groovy index f92f355494d..5a1f654e4d1 100644 --- a/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/NativeMetadataExtension.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/NativeMetadataExtension.groovy @@ -45,6 +45,16 @@ abstract class NativeMetadataExtension { */ abstract ListProperty getForms() + /** + * Requests neither a path nor a form can make: a method other than GET and POST, such as QUERY, or a body that + * is not a form, such as JSON. Each names its {@code method} and {@code path}, and may name its + * {@code contentType}, {@code body} and {@code accept}. One that does not, or that names anything else, fails + * the trace before the application is started. + * + * @since 9.0 + */ + abstract ListProperty> getRequests() + abstract ListProperty getJvmArguments() abstract Property getPort() diff --git a/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/RunPort.groovy b/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/RunPort.groovy index 6d8d015ddc5..ea6e46fd468 100644 --- a/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/RunPort.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/RunPort.groovy @@ -41,6 +41,39 @@ final class RunPort { private RunPort() { } + /** + * Whether what listens on the port says it is not ready yet. A connection and the log saying the application + * started both come before its {@code ApplicationStartedEvent}, and a filter that holds requests until then, such + * as the Spring Security plugin's, answers them with 503, which a run would record in place of the page. + * + *

Only a 503 that carries {@code Retry-After}, which is how a 503 says the unavailability is temporary, and + * how the Spring Security plugin's says it. A 503 without it is the application's own answer, which waiting will + * not change, and a run that waited for it would sit out its whole start timeout before going on.

+ * + * @param port the port the run is reached on + * @return whether it answers with 503 and {@code Retry-After}, and not where nothing answers at all + * @since 9.0 + */ + static boolean answersNotReady(int port) { + try { + HttpURLConnection connection = (HttpURLConnection) new URI("http://localhost:${port}/").toURL().openConnection() + connection.requestMethod = 'HEAD' + connection.instanceFollowRedirects = false + connection.connectTimeout = 500 + connection.readTimeout = 2000 + try { + return connection.responseCode == HttpURLConnection.HTTP_UNAVAILABLE && + connection.getHeaderField('Retry-After') != null + } + finally { + connection.disconnect() + } + } + catch (IOException ignored) { + return false + } + } + /** * Refuses where something is already listening on the port. * diff --git a/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/TraceNativeMetadataTask.groovy b/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/TraceNativeMetadataTask.groovy index c9395dd82e5..92fc963e3d8 100644 --- a/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/TraceNativeMetadataTask.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/TraceNativeMetadataTask.groovy @@ -22,6 +22,10 @@ import java.net.http.HttpClient import java.net.http.HttpRequest import java.net.http.HttpResponse import java.time.Duration +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter +import java.time.temporal.ChronoUnit +import java.time.temporal.IsoFields import java.util.concurrent.TimeUnit import java.util.regex.Matcher import java.util.regex.Pattern @@ -85,6 +89,19 @@ import org.gradle.work.DisableCachingByDefault * -- the one declaring the most fields is submitted, and which it was is reported. A page whose * wanted form is not the fullest names it: {@code '/book/create#bookForm'}.

* + *

A request neither of those can make, with another method or with a body that is not a form, is + * declared whole. An action restricted to QUERY answers nothing else, and what writes a JSON response + * runs only for a client that asks for one:

+ * + *
+ * grails {
+ *     nativeMetadata {
+ *         requests = [[method: 'QUERY', path: '/book/search', contentType: 'application/json',
+ *                      body: '{"title": "Grails"}', accept: 'application/json']]
+ *     }
+ * }
+ * 
+ * *

Written to the application's sources rather than to the build directory: what an image was * built from should be reviewable, and a trace is only as good as the paths someone thought of.

* @@ -94,6 +111,9 @@ import org.gradle.work.DisableCachingByDefault @DisableCachingByDefault(because = 'Records what a run of the application did, which is not an output of its inputs') abstract class TraceNativeMetadataTask extends DefaultTask { + /** What a request in {@link #getRequests()} may name, so that a misspelt one is not silently left out. */ + private static final Set REQUEST_KEYS = ['method', 'path', 'contentType', 'body', 'accept'] as Set + /** How the agent is asked for, and the only way its output can be merged with what is there. */ private static final String AGENT = 'native-image-agent' @@ -105,10 +125,22 @@ abstract class TraceNativeMetadataTask extends DefaultTask { private static final Pattern FORM = Pattern.compile(/(?is)]*>.*?<\/form>/) private static final Pattern ACTION = Pattern.compile(/(?i)\baction\s*=\s*"([^"]*)"/) private static final Pattern ID = Pattern.compile(/(?i)\bid\s*=\s*"([^"]+)"/) - private static final Pattern FIELD = Pattern.compile(/(?is)<(?:input|select|textarea)\b[^>]*>/) + private static final Pattern FIELD = Pattern.compile(/(?is)<(input|select|textarea)\b[^>]*>/) + private static final Pattern SELECT_END = Pattern.compile(/(?i)<\/select\s*>/) + private static final Pattern OPTION = Pattern.compile(/(?is)]*)>([^<]*)/) + private static final Pattern SELECTED = Pattern.compile(/(?i)(? SHORT_URLS = ['https://example.com/', 'https://example.com', + 'http://example.com', 'ftp://example.com'] /** * Carries the session from one request to the next, which is what makes a form that @@ -147,6 +179,14 @@ abstract class TraceNativeMetadataTask extends DefaultTask { @Input abstract ListProperty getForms() + /** + * Requests to make as they are declared. + * + * @since 9.0 + */ + @Input + abstract ListProperty> getRequests() + @Input abstract Property getPort() @@ -162,6 +202,7 @@ abstract class TraceNativeMetadataTask extends DefaultTask { @TaskAction void trace() { + refuseRequestsThatCannotBeMade() File java = new File(javaExecutable.get()) File metadata = outputDirectory.get().asFile metadata.mkdirs() @@ -196,6 +237,7 @@ abstract class TraceNativeMetadataTask extends DefaultTask { // the login form, and what the agent records is the login page under the name of the // page that was wanted. The session the form establishes is carried by this client. forms.get().each { String path -> answered << submit(path) } + requests.get().each { Map request -> answered << send(request) } paths.get().each { String path -> answered << ask(path) } } finally { @@ -220,8 +262,8 @@ abstract class TraceNativeMetadataTask extends DefaultTask { 'traced, so what was recorded is not what was asked for:\n ' + "${failed*.line.join('\n ')}\nWhat it printed is in ${output}") } - logger.lifecycle('Traced {} forms and {} paths into {}', - forms.get().size(), paths.get().size(), metadata) + logger.lifecycle('Traced {} forms, {} requests and {} paths into {}', + forms.get().size(), requests.get().size(), paths.get().size(), metadata) } /** What one request recorded, and whether it recorded the thing it was asked for. */ @@ -242,6 +284,24 @@ abstract class TraceNativeMetadataTask extends DefaultTask { } } + /** + * Refuses a request that does not name its method and path, or names what a request cannot have, before + * anything is started. Found as the request was made, it failed the build only after the application had been + * started and the rest traced, which for a misspelt key is the whole wait for an answer known before it began. + */ + private void refuseRequestsThatCannotBeMade() { + List refused = requests.get().findAll { Map request -> + Map declared = request + (declared.keySet() - REQUEST_KEYS) || !declared.method || !declared.path + }.collect { Map request -> + "REQUEST ${request} -> a request names its method and path, and may name ${REQUEST_KEYS - ['method', 'path']}".toString() + } + if (refused) { + throw new GradleException('grails.nativeMetadata.requests names a request that cannot be made, so the ' + + "application was not started:\n ${refused.join('\n ')}") + } + } + /** * Refuses before starting rather than after. Without the agent the JVM stops at load with a * message about a library path, which reads as a broken machine rather than as the wrong JDK. @@ -277,6 +337,31 @@ abstract class TraceNativeMetadataTask extends DefaultTask { response.statusCode() < 400, elsewhere) } + /** + * Makes a request as it is declared, with the Accept header {@link #requestTo} gives one that names none, and + * reports where it was answered from as {@link #ask} does: a request that needs a login and has not got one is + * sent on to the login page, which records that page under the name of this one. + */ + private Answer send(Map request) { + Map declared = request + String method = declared.method.toString().toUpperCase(Locale.ROOT) + String path = declared.path.toString() + HttpRequest.Builder builder = requestTo(path) + if (declared.accept) { + builder.setHeader('Accept', declared.accept.toString()) + } + if (declared.contentType) { + builder.header('Content-Type', declared.contentType.toString()) + } + builder.method(method, declared.body == null ? HttpRequest.BodyPublishers.noBody() : + HttpRequest.BodyPublishers.ofString(declared.body.toString())) + HttpResponse response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString()) + String landed = response.uri().path + boolean elsewhere = landed != uriOf(path).path + String where = elsewhere ? " (landed on ${landed})" : '' + new Answer("${method} ${path} -> ${response.statusCode()}${where}".toString(), response.statusCode() < 400, elsewhere) + } + /** * Fills in the form on a page and submits it to wherever the form says. * @@ -351,6 +436,16 @@ abstract class TraceNativeMetadataTask extends DefaultTask { logger.warn('{} has no field named {}, so that value was not sent', path, name) } } + // Not submitted with a field too short for any address or link on example.com. Whatever fits it is either + // text it rejects, so the page answers with the form again and a 200 that reads as a save, or a value on + // someone else's domain. The form is given a value for it, or it is not traced. + List tooShort = tooShortFields(markup) - given.keySet() + if (tooShort) { + String first = tooShort.first() + return new Answer("FORM ${path} -> not submitted: ${tooShort.join(', ')} too short for an address or " + + "link on example.com. Give each a value in nativeMetadata.forms, as in ${path}?${first}=...".toString(), + false) + } String encoded = fields.collect { String name, String value -> "${URLEncoder.encode(name, 'UTF-8')}=${URLEncoder.encode(value, 'UTF-8')}" @@ -410,8 +505,11 @@ abstract class TraceNativeMetadataTask extends DefaultTask { /** * What the form would send. A field that carries a value sends it, which is how the token a - * security filter demands is carried; a checkbox sends what a ticked one sends; anything else - * sends something recognisable, so a row in a database says where it came from. + * security filter demands is carried, and a hidden field is the page's own, so it is sent as it + * is even when that is nothing. Any other field without a value is filled in, as the fields + * plugin renders every field of a new instance: sent blank it fails validation, the page answers + * with the form again, and what a save does when it succeeds is never recorded. A select sends + * one of its options. */ private static Map fieldsOf(String markup) { Map fields = [:] @@ -422,20 +520,226 @@ abstract class TraceNativeMetadataTask extends DefaultTask { if (!name.find()) { continue } + if (field.group(1).equalsIgnoreCase('select')) { + Matcher end = SELECT_END.matcher(markup) + fields[name.group(1)] = optionOf(end.find(field.end()) + ? markup.substring(field.end(), end.start()) + : markup.substring(field.end())) + continue + } Matcher value = VALUE.matcher(tag) Matcher type = TYPE.matcher(tag) - String kind = type.find() ? type.group(1).toLowerCase(Locale.ROOT) : 'text' + // The first word only: the fields plugin renders a decimal property as type="number decimal". + String kind = type.find() ? type.group(1).trim().toLowerCase(Locale.ROOT).split(/\s+/)[0] : 'text' if (kind in ['submit', 'button', 'reset', 'image', 'file']) { continue } - fields[name.group(1)] = value.find() ? value.group(1) - : kind == 'checkbox' || kind == 'radio' ? 'on' - : kind == 'password' ? 'traced-secret' - : "traced-${System.nanoTime()}".toString() + String carried = value.find() ? value.group(1) : null + fields[name.group(1)] = carried || kind == 'hidden' ? (carried ?: '') : fill(kind, tag) } fields } + /** + * The email and URL fields rendered empty with a {@code maxlength} too short for any address or link on + * {@code example.com}, which are filled with text they reject. + */ + private static List tooShortFields(String markup) { + List names = [] + Matcher field = FIELD.matcher(markup) + while (field.find()) { + String tag = field.group() + Matcher name = NAME.matcher(tag) + Matcher type = TYPE.matcher(tag) + Matcher value = VALUE.matcher(tag) + if (field.group(1).equalsIgnoreCase('select') || !name.find() || !type.find() || (value.find() && value.group(1))) { + continue + } + String kind = type.group(1).trim().toLowerCase(Locale.ROOT).split(/\s+/)[0] + String declaredLength = attribute(MAXLENGTH, tag) + if (declaredLength && fitsNothing(kind, Integer.valueOf(declaredLength))) { + names << name.group(1) + } + } + names + } + + /** Whether no address or link on {@code example.com} fits a field of this type and length. */ + private static boolean fitsNothing(String kind, int maxLength) { + (kind == 'email' && maxLength <= EMAIL_DOMAIN.length()) || (kind == 'url' && maxLength < SHORT_URLS.last().length()) + } + + /** + * What a browser sends for a select: the option selected, or the first that can be chosen where + * none is, and an option without a value sends its text. + * + *

The select's options rather than made-up text, because only one of them binds. An enum, an + * association and each part of a date picker are rendered as a select, and text sent for any of + * them fails to convert -- so the page answers with the form again, and a 200, and the save the + * form was listed for is never recorded.

+ */ + private static String optionOf(String options) { + String first = null + Matcher option = OPTION.matcher(options) + while (option.find()) { + String attributes = option.group(1) + // Without what the attributes say, so that an option whose value is "selected" is not taken for one that is. + String flags = attributes.replaceAll(/"[^"]*"/, '""') + if (DISABLED.matcher(flags).find()) { + continue + } + Matcher value = VALUE.matcher(attributes) + String sent = value.find() ? value.group(1) : option.group(2).trim() + if (SELECTED.matcher(flags).find()) { + return sent + } + if (first == null) { + first = sent + } + } + first != null ? first : '' + } + + /** + * Something a field of this type accepts, so that what fails is never the value the trace made up. + * + *

A number field sent text, or an email field sent something that is not an address, fails to + * bind or fails its constraint, and the page answers with the form again -- with a 200, which is + * what a save that succeeded answers with too. So the trace would report the form as covered + * while what a save does went unrecorded, which is the gap filling the field in was meant to + * close. A checkbox sends what a ticked one sends, and text sends something recognisable, so a + * row in a database says where it came from.

+ * + *

What the field says about itself is kept to: a number lies within its {@code min} and + * {@code max}, as a date does, and nothing is longer than its {@code maxlength}, which is how the + * fields plugin renders the constraints of the property behind it.

+ */ + private static String fill(String kind, String tag) { + String declaredLength = attribute(MAXLENGTH, tag) + Integer maxLength = declaredLength ? Integer.valueOf(declaredLength) : null + LocalDateTime now = LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES) + switch (kind) { + case 'checkbox': + case 'radio': + return 'on' + case 'number': + case 'range': + return numberWithin(tag) + case 'date': + return within(now.toLocalDate().toString(), tag) + case 'datetime-local': + return within(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm", Locale.ROOT)), tag) + case 'time': + return within(now.format(DateTimeFormatter.ofPattern('HH:mm', Locale.ROOT)), tag) + case 'month': + return within(now.format(DateTimeFormatter.ofPattern('yyyy-MM', Locale.ROOT)), tag) + case 'week': + return within(String.format(Locale.ROOT, '%d-W%02d', + now.get(IsoFields.WEEK_BASED_YEAR), now.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)), tag) + case 'email': + return address(maxLength) + case 'url': + return link(maxLength) + case 'tel': + return cut('5550100', maxLength) + case 'color': + return '#000000' + case 'password': + return cut('traced-secret', maxLength) + default: + return traced(maxLength) + } + } + + /** One, or the nearest number to it the field's {@code min} and {@code max} allow. */ + private static String numberWithin(String tag) { + BigDecimal number = BigDecimal.ONE + BigDecimal min = decimal(attribute(MIN, tag)) + BigDecimal max = decimal(attribute(MAX, tag)) + if (min != null && number < min) { + number = min + } + if (max != null && number > max) { + number = max + } + number.toPlainString() + } + + /** + * The value, or the nearest end of the field's {@code min} and {@code max}. A date, a time, a month and a week + * are each written so that the earlier of two sorts first, which is what lets them be compared as they are. + */ + private static String within(String value, String tag) { + String min = attribute(MIN, tag) + String max = attribute(MAX, tag) + if (min && value < min) { + return min + } + if (max && value > max) { + return max + } + value + } + + /** + * Something recognisable, and different from one trace to the next, so that a field the application keeps + * unique is given a value it has not seen. Where it does not fit, the name is given up before the digits that + * change are. + */ + private static String traced(Integer maxLength) { + String unique = String.valueOf(System.nanoTime() & Long.MAX_VALUE) + String value = "traced-${unique}".toString() + if (maxLength == null || value.length() <= maxLength) { + return value + } + int room = maxLength - 'traced-'.length() + room > 0 ? "traced-${unique.substring(unique.length() - room)}".toString() + : unique.substring(Math.max(unique.length() - maxLength, 0)) + } + + /** + * An address on {@code example.com}, which is kept for examples and accepts no mail, within the field's + * {@code maxlength}. Where not even one character and the domain fit, the traced name is sent alone: it is not + * an address, so the field fails as it would for anyone, and no shorter domain is used, because each that fits + * is someone's, and a save that sends mail would send it there. + */ + private static String address(Integer maxLength) { + if (maxLength == null || maxLength > EMAIL_DOMAIN.length()) { + return traced(maxLength == null ? null : maxLength - EMAIL_DOMAIN.length()) + EMAIL_DOMAIN + } + traced(maxLength) + } + + /** + * A link to {@code example.com} within the field's {@code maxlength}: a path under it where there is room for + * one, and otherwise the longest of the shorter links to it that fits. Where none does, the traced name is + * sent alone, for the reason an address is: a link cut to the length can name another host. + */ + private static String link(Integer maxLength) { + if (maxLength == null || maxLength > URL_BASE.length()) { + return URL_BASE + traced(maxLength == null ? null : maxLength - URL_BASE.length()) + } + SHORT_URLS.find { String url -> url.length() <= maxLength } ?: traced(maxLength) + } + + private static String cut(String value, Integer maxLength) { + maxLength == null || value.length() <= maxLength ? value : value.substring(0, maxLength) + } + + private static String attribute(Pattern pattern, String tag) { + Matcher matcher = pattern.matcher(tag) + matcher.find() ? matcher.group(1).trim() : null + } + + private static BigDecimal decimal(String text) { + try { + text ? new BigDecimal(text) : null + } + catch (NumberFormatException ignored) { + null + } + } + private HttpRequest.Builder requestTo(String path) { HttpRequest.newBuilder(uriOf(path)) .timeout(Duration.ofSeconds(60)) @@ -446,22 +750,31 @@ abstract class TraceNativeMetadataTask extends DefaultTask { URI.create("http://localhost:${port.get()}${path.startsWith('/') ? path : '/' + path}") } + /** + * Waits for the application to serve and, where it answers 503 with {@code Retry-After} as the Spring Security + * plugin does until the application has started, for that answer to change, up to the timeout. A 503 without + * {@code Retry-After} is the application's own answer, and is not waited for. + */ private void awaitStarted(Process process, File output) { long deadline = System.currentTimeMillis() + Duration.ofSeconds(startTimeoutSeconds.get()).toMillis() StartupLog log = new StartupLog(output) + boolean started = false while (System.currentTimeMillis() < deadline) { if (!process.isAlive()) { throw new GradleException('The application ended before it started serving. ' + "What it printed is in ${output}") } - if (log.saysItStarted()) { - return - } - if (serving()) { + started = log.saysItStarted() || serving() + if (started && !RunPort.answersNotReady(port.get())) { return } Thread.sleep(250L) } + if (started) { + // Its home page still says to come back after the whole of the timeout, which is longer than a start + // takes to finish, so the requests are its to answer. + return + } throw new GradleException("The application did not start within ${startTimeoutSeconds.get()}s. " + "What it printed is in ${output}") } diff --git a/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/TrainAotCacheTask.groovy b/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/TrainAotCacheTask.groovy index b4fff274c88..22d97067915 100644 --- a/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/TrainAotCacheTask.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/apache/grails/gradle/plugin/aot/TrainAotCacheTask.groovy @@ -67,6 +67,7 @@ abstract class TrainAotCacheTask extends DefaultTask { @PathSensitive(PathSensitivity.RELATIVE) abstract DirectoryProperty getApplicationDirectory() + /** The name of the extracted application's jar: the archive's own, which for a war is a jar's. */ @Input abstract Property getArchiveFileName() @@ -271,23 +272,31 @@ abstract class TrainAotCacheTask extends DefaultTask { * the earlier and clearer of the two, but it is an INFO log an application is free to turn off * or reword; a port that accepts a connection is the application's own doing and cannot be * configured away while it is still an application worth training.

+ * + *

Both come before its {@code ApplicationStartedEvent}, so a filter that holds requests until then, answering + * them with 503 and {@code Retry-After}, is waited for, up to the timeout. A 503 without {@code Retry-After} is the + * application's own answer, and is not.

*/ private void awaitStarted(Process process, File output) { long deadline = System.currentTimeMillis() + Duration.ofSeconds(startTimeoutSeconds.get()).toMillis() StartupLog log = new StartupLog(output) + boolean started = false while (System.currentTimeMillis() < deadline) { if (!process.isAlive()) { throw new GradleException('The training run ended before it started serving.' + whyItEnded(output) + " What it printed is in ${output}") } - if (log.saysItStarted()) { - return - } - if (serving()) { + started = log.saysItStarted() || serving() + if (started && !RunPort.answersNotReady(port.get())) { return } Thread.sleep(250L) } + if (started) { + // Its home page still says to come back after the whole of the timeout, which is longer than a start + // takes to finish, so the requests are its to answer. + return + } throw new GradleException("The training run did not start within ${startTimeoutSeconds.get()}s. " + "What it printed is in ${output}") } diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy index 778831b87a8..3a5208318f4 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy @@ -115,6 +115,8 @@ class GrailsGradlePlugin implements Plugin { private static final String ASSET_PIPELINE_PLUGIN = 'cloud.wondrify.asset-pipeline' + private static final String WAR_PLUGIN = 'war' + private static final int TRAINING_PORT = 18080 /** Not the training port: a trace and a training run are both a started application. */ @@ -1232,11 +1234,15 @@ ${importStatements} .extensions.create('nativeMetadata', NativeMetadataExtension) extension.paths.convention(['/']) extension.forms.convention([]) + extension.requests.convention([]) extension.jvmArguments.convention(['-Dgrails.env=production']) extension.port.convention(TRACING_PORT) extension.startTimeoutSeconds.convention(TRAINING_START_TIMEOUT_SECONDS) extension.outputDirectory.convention(project.layout.projectDirectory .dir('src/native/resources/META-INF/native-image')) + project.pluginManager.withPlugin(NATIVE_IMAGE_PLUGIN) { + addTracedMetadataToNativeImageClasspath(project, extension) + } project.pluginManager.withPlugin(SPRING_BOOT_PLUGIN) { TaskProvider bootJar = project.tasks.named('bootJar') @@ -1254,6 +1260,7 @@ ${importStatements} task.jvmArguments.set(extension.jvmArguments) task.paths.set(extension.paths) task.forms.set(extension.forms) + task.requests.set(extension.requests) task.port.set(extension.port) task.startTimeoutSeconds.set(extension.startTimeoutSeconds) task.outputDirectory.set(extension.outputDirectory) @@ -1261,6 +1268,37 @@ ${importStatements} } } + /** + * Puts what {@code traceNativeMetadata} recorded on the classpath the image is built from, without which + * recording it changes nothing: the image is built, starts, and fails on the first request that needs it. + * + *

Dynamically, for the reason {@link #addToNativeImageClasspath} is.

+ */ + @CompileDynamic + private static void addTracedMetadataToNativeImageClasspath(Project project, NativeMetadataExtension extension) { + def graalvm = project.extensions.findByName('graalvmNative') + graalvm?.binaries?.configureEach { binary -> + binary.classpath(extension.outputDirectory.map { Directory directory -> nativeMetadataRoot(directory.asFile) }) + } + } + + /** + * The classpath entry the metadata in a directory is read from: {@code native-image} looks for it under + * {@code META-INF/native-image} of each entry, so a directory anywhere else would never be read. + */ + private static File nativeMetadataRoot(File directory) { + File metaInf = directory + while (metaInf != null && metaInf.name != 'META-INF') { + metaInf = metaInf.parentFile + } + if (metaInf?.parentFile == null) { + throw new GradleException("grails.nativeMetadata.outputDirectory is ${directory}, which an image cannot " + + 'read metadata from. Use a directory under META-INF/native-image, as the default ' + + 'src/native/resources/META-INF/native-image is.') + } + metaInf.parentFile + } + protected void configureBootRunPidFile(Project project) { // Producer side of the run-app PID contract: the forked app writes its PID to 'run-app.pid' // under the Gradle build directory. The CLI stop-app command resolves the PID file @@ -1530,6 +1568,9 @@ ${importStatements} * archive is extracted, the extracted application is run and asked for its pages, and what the * run recorded is left beside it. An application asks for this with * {@code grails.aotCache.enabled}, and says which of its pages matter.

+ * + *

The archive is the executable war where the application applies the war plugin, as a + * generated application does, and the executable jar otherwise.

*/ protected void configureAotCache(Project project) { AotCacheExtension extension = ((ExtensionAware) project.extensions.getByName('grails')) @@ -1541,7 +1582,6 @@ ${importStatements} extension.startTimeoutSeconds.convention(TRAINING_START_TIMEOUT_SECONDS) project.pluginManager.withPlugin(SPRING_BOOT_PLUGIN) { - TaskProvider bootJar = project.tasks.named('bootJar') Provider application = project.layout.buildDirectory.dir('aot-cache/application') Provider launcher = trainingLauncher(project) @@ -1550,7 +1590,7 @@ ${importStatements} task.group = BasePlugin.BUILD_GROUP task.description = 'Extracts the application, which is the form the cache is read against' task.onlyIf { extension.enabled.get() } - task.archiveFile.set(archiveFileOf(bootJar)) + task.archiveFile.set(archiveFileOf(executableArchive(project))) // The launcher rather than its path: this task is cacheable, and a path is where a // JDK happens to live on the machine that ran the build. Set as the provider it is, // so the toolchain is not resolved -- and a JDK not provisioned -- by every build @@ -1565,7 +1605,8 @@ ${importStatements} task.onlyIf { extension.enabled.get() } task.dependsOn(extract) task.applicationDirectory.set(application) - task.archiveFileName.set(archiveFileOf(bootJar).map { RegularFile file -> file.asFile.name }) + task.archiveFileName.set(archiveFileNameOf(executableArchive(project)) + .map { String name -> ExtractApplicationTask.applicationFileName(name) }) // Beside the extracted application rather than inside it. Inside, the cache and its // metadata land in the directory this task declares as its input, so writing them // changes that input and the task can never be up to date -- every build would run @@ -1599,8 +1640,37 @@ ${importStatements} * task resolved while the build is configured is one every build pays for whether or not it * asked for an archive.

*/ - private static Provider archiveFileOf(TaskProvider bootJar) { - bootJar.flatMap { Task task -> (Provider) task.property('archiveFile') } + private static Provider archiveFileOf(TaskProvider archive) { + archive.flatMap { Task task -> (Provider) task.property('archiveFile') } + } + + /** The name of the archive a task builds, which is known before the task has run, unlike its file. */ + private static Provider archiveFileNameOf(TaskProvider archive) { + archive.flatMap { Task task -> (Provider) task.property('archiveFileName') } + } + + /** + * The executable archive the application ships: the war where it applies the war plugin, as a + * generated application does, and the jar otherwise. + * + *

Spring Boot extracts a war as of 4.2: its provided libraries, the embedded container among + * them, beside its own, and what sits outside {@code WEB-INF}, {@code src/main/webapp} included, + * in the extracted jar's {@code META-INF/resources}. The jar leaves {@code src/main/webapp} out, + * so a cache trained against the jar of an application that ships its war was trained against + * an application missing what the war serves. An application that switches its war off ships + * the jar.

+ * + *

Read as a task that consumes the archive is configured, by which time the build has applied + * its plugins and configured its archives.

+ */ + private static TaskProvider executableArchive(Project project) { + if (project.pluginManager.hasPlugin(WAR_PLUGIN)) { + TaskProvider bootWar = project.tasks.named('bootWar') + if (bootWar.get().enabled) { + return bootWar + } + } + project.tasks.named('bootJar') } /** diff --git a/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/ExtractApplicationTaskSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/ExtractApplicationTaskSpec.groovy index 4c20f391ea9..63d744a2ba6 100644 --- a/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/ExtractApplicationTaskSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/ExtractApplicationTaskSpec.groovy @@ -51,4 +51,16 @@ class ExtractApplicationTaskSpec extends Specification { expect: declared.isEmpty() } + + void 'the application extracted from #archive is #application'() { + expect: 'a jar keeps its name, and a war is written as a jar of the same name' + ExtractApplicationTask.applicationFileName(archive) == application + + where: + archive | application + 'myapp-0.1.jar' | 'myapp-0.1.jar' + 'myapp-0.1.war' | 'myapp-0.1.jar' + 'MYAPP.WAR' | 'MYAPP.jar' + 'myapp.war.jar' | 'myapp.war.jar' + } } diff --git a/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/RunPortSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/RunPortSpec.groovy index 941573d27f3..5e2d11c2322 100644 --- a/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/RunPortSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/RunPortSpec.groovy @@ -18,14 +18,55 @@ */ package org.apache.grails.gradle.plugin.aot +import com.sun.net.httpserver.HttpExchange +import com.sun.net.httpserver.HttpServer import org.gradle.api.GradleException import spock.lang.Specification +import spock.lang.Unroll /** - * Covers refusing a port something else is already answering on. + * Covers refusing a port something else is already answering on, and asking whether what answers on one is ready. */ class RunPortSpec extends Specification { + @Unroll + void 'an application answering #status is #description'() { + given: + HttpServer server = HttpServer.create(new InetSocketAddress('localhost', 0), 0) + server.createContext('/') { HttpExchange exchange -> + if (retryAfter) { + exchange.responseHeaders.add('Retry-After', '1') + } + exchange.sendResponseHeaders(status, -1) + exchange.close() + } + server.start() + + expect: + RunPort.answersNotReady(server.address.port) == notReady + + cleanup: + server?.stop(0) + + where: + status | retryAfter | notReady | description + 503 | true | true | 'not ready where it says to come back, as the Spring Security plugin does until the application has started' + 503 | false | false | 'ready where it does not say to come back, since that is the application\'s own answer' + 200 | false | false | 'ready' + 302 | false | false | 'ready, wherever it sends the request' + 404 | false | false | 'ready, whatever it has at its home page' + 500 | false | false | 'ready, since only 503 says to come back' + 500 | true | false | 'ready even with Retry-After, since only a 503 says it is not ready yet' + } + + void 'a port nothing answers on is not said to be not ready'() { + given: + int free = new ServerSocket(0).withCloseable { ServerSocket socket -> socket.localPort } + + expect: 'whether anything listens is what the wait for the port itself is for' + !RunPort.answersNotReady(free) + } + void 'a free port is not refused'() { given: 'a port nothing is listening on, found by letting the system choose one and closing it' int free = new ServerSocket(0).withCloseable { ServerSocket socket -> socket.localPort } diff --git a/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TraceNativeMetadataTaskSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TraceNativeMetadataTaskSpec.groovy index 1c27fb1687d..64317bb3d0e 100644 --- a/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TraceNativeMetadataTaskSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TraceNativeMetadataTaskSpec.groovy @@ -20,7 +20,15 @@ package org.apache.grails.gradle.plugin.aot import java.net.CookieHandler import java.net.CookieManager - +import java.time.Duration +import java.time.LocalDate +import java.time.LocalDateTime +import java.time.LocalTime +import java.time.YearMonth + +import org.apache.commons.validator.routines.EmailValidator +import org.apache.commons.validator.routines.RegexValidator +import org.apache.commons.validator.routines.UrlValidator import org.gradle.api.GradleException import org.gradle.api.Project import org.gradle.testfixtures.ProjectBuilder @@ -35,6 +43,10 @@ import spock.lang.TempDir */ class TraceNativeMetadataTaskSpec extends Specification { + /** The validator Grails' {@code url} constraint builds, which accepts any scheme. */ + private static final UrlValidator URL_CONSTRAINT = new UrlValidator((RegexValidator) null, + UrlValidator.ALLOW_ALL_SCHEMES + UrlValidator.ALLOW_2_SLASHES) + @TempDir File temporaryFolder @@ -159,6 +171,103 @@ exec '${new File(System.getProperty('java.home'), 'bin/java').absolutePath}' \\ !posted.contains('nosuchfield') } + void 'a field rendered with an empty value is filled in, so that the form can succeed'() { + given: 'blank fields fail validation, the page answers with the form again, and the save is never recorded' + TraceNativeMetadataTask task = task([], ['/scaffolded']) + + when: + task.trace() + + then: + String submitted = requested.find { String line -> line.startsWith('POST /scaffolded-save') } + submitted =~ /username=traced-\d+/ + submitted.contains('password=traced-secret') + + and: 'what a field does say is sent, and a hidden field is the page\'s own, whatever it says' + submitted.contains('nickname=kept') + submitted.contains('_csrf=token') + submitted =~ /version=(&|$)/ + } + + void 'a field rendered empty is filled in with something its type accepts'() { + given: 'a number or a date sent text fails to bind, and the form answering again with a 200 reads as a save' + TraceNativeMetadataTask task = task([], ['/typed?tinyContact=a@b.example&tinyHomepage=ftp://e.example']) + + when: + task.trace() + + then: + Map sent = postedTo('/typed-save') + with(sent) { + available == 'on' + (title =~ /^traced-\d+$/) && title.size() <= 12 + (notes =~ /^\d+$/) && notes.size() <= 5 + query =~ /^traced-\d+$/ + secret == 'traced-secret' + new BigDecimal(pages) >= 0 + new BigDecimal(rating) >= 3 && new BigDecimal(rating) <= 5 + new BigDecimal(price) <= 0.5 + new BigDecimal(volume) >= 10 && new BigDecimal(volume) <= 20 + LocalDate.parse(released) + launched == '2999-01-01' + LocalDateTime.parse(startsAt) + LocalTime.parse(opensAt) + YearMonth.parse(issue) + sprint ==~ /\d{4}-W\d{2}/ + contact ==~ /traced-\d+@example\.com/ + (shortContact ==~ /[^@]+@example\.com/) && shortContact.size() <= 16 + edgeContact ==~ /\d@example\.com/ + tinyContact == 'a@b.example' + new URI(homepage).scheme == 'https' && new URI(homepage).host + (shortHomepage ==~ /https:\/\/example\.com\/\d+/) && shortHomepage.size() <= 24 + siteHomepage == 'https://example.com' + plainHomepage == 'http://example.com' + ftpHomepage == 'ftp://example.com' + tinyHomepage == 'ftp://e.example' + phone ==~ /\d+/ + colour ==~ /#[0-9a-f]{6}/ + } + + and: 'each address and link passes the check the email and url constraints make, and stays on example.com' + [sent.contact, sent.shortContact, sent.edgeContact].every { String email -> + EmailValidator.instance.isValid(email) && email.endsWith('@example.com') + } + [sent.homepage, sent.shortHomepage, sent.siteHomepage, sent.plainHomepage, sent.ftpHomepage].every { String url -> + URL_CONSTRAINT.isValid(url) && new UrlValidator().isValid(url) && new URI(url).host == 'example.com' + } + + and: 'a hidden field is the page\'s own, so one that says nothing is sent saying nothing' + sent._available == '' + } + + void 'a select sends one of its options'() { + given: 'an enum, an association and a date picker are rendered as selects, and only an option binds' + TraceNativeMetadataTask task = task([], ['/select']) + + when: + task.trace() + + then: 'the first option where none is selected, as a browser sends' + Map sent = postedTo('/select-save') + sent.status == 'DRAFT' + sent.author == 'null' + + and: 'the selected one where one is' + sent.released_day == '9' + sent.tags == '2' + sent.released == 'date.struct' + + and: 'never one that cannot be chosen, nor one whose value only reads like the attribute' + sent.format == 'HARDBACK' + + and: 'the text of an option that has no value, inside a group as much as outside one' + sent.genre == 'Crime' + + and: 'and nothing for a select with no options, while the fields after it are still read' + sent.nothing == '' + sent.title =~ /^traced-\d+$/ + } + void 'the form with the most fields is the one submitted'() { given: 'a page carrying a layout search form ahead of its own' TraceNativeMetadataTask task = task([], ['/create']) @@ -252,6 +361,23 @@ exec '${new File(System.getProperty('java.home'), 'bin/java').absolutePath}' \\ requested.any { it.startsWith('POST /protected-save') && it.contains('secretTitle=') } } + void 'a form with a field too short for an address or link on example.com is not submitted, and fails the trace'() { + given: 'whatever fits is text the field rejects, answered with the form again and a 200, or another host' + TraceNativeMetadataTask task = task([], ['/typed?tinyContact=a@b.example']) + + when: + task.trace() + + then: 'the field without a value is named, with how to give it one' + GradleException e = thrown() + e.message.contains('FORM /typed -> not submitted: tinyHomepage too short') + e.message.contains('/typed?tinyHomepage=...') + !e.message.contains('tinyContact') + + and: + !requested.any { it.startsWith('POST /typed-save') } + } + void 'a form on a page that has none fails the trace'() { given: TraceNativeMetadataTask task = task([], ['/']) @@ -264,6 +390,46 @@ exec '${new File(System.getProperty('java.home'), 'bin/java').absolutePath}' \\ e.message.contains('no form on the page') } + void 'a path is not asked for while the application still answers that it is not ready'() { + given: 'the Spring Security plugin answers every request with 503 and Retry-After until the application has started' + TraceNativeMetadataTask task = task(['/'], [], 'gated') + + when: + task.trace() + + then: 'where it would otherwise record the refusal and fail' + notThrown(GradleException) + requested.contains('GET /') + } + + void 'an application whose home page answers 503 of its own accord is traced without waiting'() { + given: 'a 503 without Retry-After, which does not say it will change' + TraceNativeMetadataTask task = task(['/page'], [], 'homePageDown') + task.startTimeoutSeconds.set(60) + long began = System.nanoTime() + + when: + task.trace() + + then: 'rather than after sitting out the whole of the start timeout' + notThrown(GradleException) + requested.contains('GET /page') + Duration.ofNanos(System.nanoTime() - began) < Duration.ofSeconds(30) + } + + void 'an application whose home page always says to come back is still traced once the timeout has passed'() { + given: 'waited for no longer than the timeout' + TraceNativeMetadataTask task = task(['/page'], [], 'homePageRetrying') + task.startTimeoutSeconds.set(5) + + when: + task.trace() + + then: + notThrown(GradleException) + requested.contains('GET /page') + } + void 'a path that answers with an error fails the trace'() { given: 'because what would be recorded is the error page rather than the page' TraceNativeMetadataTask task = task(['/broken'], []) @@ -289,6 +455,83 @@ exec '${new File(System.getProperty('java.home'), 'bin/java').absolutePath}' \\ e.message.contains('did not answer with the page') } + void 'a request is made with the method, type, body and accept it names'() { + given: + TraceNativeMetadataTask task = task([], []) + task.requests.set([[method: 'query', path: '/book/search', contentType: 'application/json', + body: '{"title":"Grails"}', accept: 'application/json']]) + + when: + task.trace() + + then: 'an action restricted to QUERY answers nothing else, so nothing else records what it reaches' + requested.contains('QUERY /book/search {"title":"Grails"}') + requested.contains('HEADERS application/json application/json') + } + + void 'a request that names only its method and path sends no body, and accepts what a path does'() { + given: + TraceNativeMetadataTask task = task([], []) + task.requests.set([[method: 'DELETE', path: '/book/1']]) + + when: + task.trace() + + then: + requested.contains('DELETE /book/1') + requested.contains('HEADERS null text/html,*/*') + } + + void 'requests are made after the forms, whose session they are in'() { + given: 'a request of a signed-in client is only worth making once the login form has been submitted' + TraceNativeMetadataTask task = task(['/after'], ['/login']) + task.requests.set([[method: 'QUERY', path: '/protected']]) + + when: + task.trace() + + then: + requested.findIndexOf { it.startsWith('POST /authenticate') } < requested.indexOf('QUERY /protected') + requested.indexOf('QUERY /protected') < requested.indexOf('GET /after') + + and: 'and it was not sent on to the login page' + requested.count('GET /login') == 1 + } + + void 'a request that answers with an error fails the trace'() { + given: + TraceNativeMetadataTask task = task([], []) + task.requests.set([[method: 'QUERY', path: '/broken']]) + + when: + task.trace() + + then: + GradleException e = thrown() + e.message.contains('QUERY /broken -> 500') + } + + void 'a request that does not say what to send, or misspells it, fails the trace rather than sending something else'() { + given: 'listed after one that can be made, and alongside a path' + TraceNativeMetadataTask task = task(['/'], []) + task.requests.set([[method: 'QUERY', path: '/book/valid'], request]) + + when: + task.trace() + + then: 'the one that cannot be made is named' + GradleException e = thrown() + e.message.contains('a request names its method and path') + !e.message.contains('/book/valid') + + and: 'before the application is started, rather than once it has been started and traced' + requested.isEmpty() + !new File(task.temporaryDir, 'trace.log').exists() + + where: + request << [[path: '/book/search'], [method: 'QUERY'], [method: 'QUERY', path: '/book/search', bodyy: '{}']] + } + void 'the cookie handler the build had is put back after a trace'() { given: 'a daemon outlives the build, so what this sets is handed to every task after it' CookieHandler inherited = new CookieManager() @@ -322,6 +565,16 @@ exec '${new File(System.getProperty('java.home'), 'bin/java').absolutePath}' \\ CookieHandler.default = null } + /** The fields a form was submitted with, by name, from what the application was sent. */ + private Map postedTo(String action) { + String line = requested.find { String request -> request.startsWith("POST ${action} ") } + assert line, "nothing was posted to ${action}: ${requested}" + line.substring("POST ${action} ".length()).split('&').collectEntries { String pair -> + List parts = pair.split('=', 2) as List + [(URLDecoder.decode(parts[0], 'UTF-8')): URLDecoder.decode(parts.size() > 1 ? parts[1] : '', 'UTF-8')] + } as Map + } + void 'a java without the agent is refused before the application is started'() { given: 'the agent ships with GraalVM, so the JDK running the build is usually not one' File plain = new File(temporaryFolder, 'plain-jdk/bin/java') diff --git a/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TracedRunFixture.groovy b/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TracedRunFixture.groovy index 7785db385bb..2977c65dbef 100644 --- a/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TracedRunFixture.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TracedRunFixture.groovy @@ -61,17 +61,117 @@ class TracedRunFixture { ''' + /** What the fields plugin renders for a new instance: a value that is there and says nothing. */ + private static final String SCAFFOLDED_FORM = ''' +
+ + + + + +
''' + + /** + * A field of every type rendered with nothing in it, as the fields plugin renders a new instance's: a number + * or a date sent text fails to bind, and the page answers with the form again and a 200. + */ + private static final String TYPED_FORM = ''' +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
''' + + /** + * What the fields plugin renders as a select: an enum, an association, and a date picker's parts. Each binds + * only one of its options, so text sent for any of them fails to convert. + */ + private static final String SELECT_FORM = ''' +
+ + + + + + + + + +
''' + static void main(String[] args) { int port = Integer.parseInt(args[0]) File requested = new File(args[1]) String behaviour = args.length > 2 ? args[2] : 'ok' + long openAt = System.currentTimeMillis() + 1500 + HttpServer server = HttpServer.create(new InetSocketAddress('localhost', port), 0) server.createContext('/') { HttpExchange exchange -> String path = exchange.requestURI.path String method = exchange.requestMethod - String body = method == 'POST' ? exchange.requestBody.getText('UTF-8') : '' + // Held as the Spring Security plugin holds a request until the application has started: a 503 that says + // to come back. A home page that is down answers 503 for reasons of its own, which says nothing of the kind. + boolean holding = behaviour == 'gated' && System.currentTimeMillis() < openAt + boolean homePageRetrying = behaviour == 'homePageRetrying' && path == '/' + boolean homePageDown = behaviour == 'homePageDown' && path == '/' + if (holding || homePageRetrying || homePageDown) { + if (!homePageDown) { + exchange.responseHeaders.add('Retry-After', '1') + } + exchange.sendResponseHeaders(503, -1) + exchange.close() + return + } + String body = exchange.requestBody.getText('UTF-8') requested << "${method} ${path}${body ? ' ' + body : ''}\n" + if (!(method in ['GET', 'POST'])) { + requested << "HEADERS ${exchange.requestHeaders.getFirst('Content-Type')} ${exchange.requestHeaders.getFirst('Accept')}\n" + } boolean signedIn = exchange.requestHeaders.getFirst('Cookie')?.contains('session=in') int status = 200 @@ -86,6 +186,15 @@ class TracedRunFixture { else if (path == '/login') { response = LOGIN_FORM } + else if (path == '/scaffolded') { + response = SCAFFOLDED_FORM + } + else if (path == '/typed') { + response = TYPED_FORM + } + else if (path == '/select') { + response = SELECT_FORM + } else if (path == '/authenticate') { exchange.responseHeaders.add('Set-Cookie', 'session=in; Path=/') status = 302 diff --git a/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TrainAotCacheTaskSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TrainAotCacheTaskSpec.groovy index 6e91a595a76..1e97219133f 100644 --- a/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TrainAotCacheTaskSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TrainAotCacheTaskSpec.groovy @@ -18,6 +18,8 @@ */ package org.apache.grails.gradle.plugin.aot +import java.time.Duration + import org.gradle.api.GradleException import org.gradle.api.Project import org.gradle.testfixtures.ProjectBuilder @@ -62,7 +64,7 @@ class TrainAotCacheTaskSpec extends Specification { * {@code -XX:AOTCacheOutput}, which a JDK before 25 refuses outright, and a test of this task * should not also be a test of which JDK the build is running on.

*/ - private TrainAotCacheTask servingTask(List paths, boolean announcesItself = true) { + private TrainAotCacheTask servingTask(List paths, boolean announcesItself = true, String behaviour = 'ok') { File application = new File(temporaryFolder, 'serving') application.mkdirs() new File(application, 'served.jar').text = 'stands in for the archive, and is what is digested' @@ -81,7 +83,7 @@ for arg in "\$@"; do done exec '${new File(System.getProperty('java.home'), 'bin/java').absolutePath}' \\ -cp '${System.getProperty('java.class.path')}' \\ - ${TrainingRunFixture.name} "\$cache" "\$port" '${requestedFile.absolutePath}' ${announcesItself ? 'loud' : 'quiet'} + ${TrainingRunFixture.name} "\$cache" "\$port" '${requestedFile.absolutePath}' ${announcesItself ? 'loud' : 'quiet'} ${behaviour} """ script.setExecutable(true) @@ -309,6 +311,46 @@ exit 1 requested == ['/'] } + void 'a path is not asked for while the run still answers that it is not ready'() { + given: 'the Spring Security plugin answers every request with 503 and Retry-After until the application has started' + TrainAotCacheTask task = servingTask(['/'], true, 'gated') + + when: + task.train() + + then: 'where it would otherwise train on the refusal' + noExceptionThrown() + requested == ['/'] + } + + void 'a run whose home page answers 503 of its own accord is exercised without waiting'() { + given: 'a 503 without Retry-After, which does not say it will change' + TrainAotCacheTask task = servingTask(['/page'], true, 'homePageDown') + task.startTimeoutSeconds.set(60) + long began = System.nanoTime() + + when: + task.train() + + then: 'rather than after sitting out the whole of the start timeout' + noExceptionThrown() + requested == ['/page'] + Duration.ofNanos(System.nanoTime() - began) < Duration.ofSeconds(30) + } + + void 'a run whose home page always says to come back is still exercised once the timeout has passed'() { + given: 'waited for no longer than the timeout' + TrainAotCacheTask task = servingTask(['/page'], true, 'homePageRetrying') + task.startTimeoutSeconds.set(5) + + when: + task.train() + + then: + noExceptionThrown() + requested == ['/page'] + } + void 'a run that says nothing is found by its port'() { given: 'an application that has turned off the log line Spring Boot starts with' TrainAotCacheTask task = servingTask(['/'], false) diff --git a/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TrainingRunFixture.groovy b/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TrainingRunFixture.groovy index 90559bb3810..b5924acb24c 100644 --- a/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TrainingRunFixture.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/apache/grails/gradle/plugin/aot/TrainingRunFixture.groovy @@ -43,8 +43,24 @@ class TrainingRunFixture { File requested = new File(args[2]) HttpServer server = HttpServer.create(new InetSocketAddress('localhost', port), 0) + String behaviour = args.length > 4 ? args[4] : 'ok' + long openAt = behaviour == 'gated' ? System.currentTimeMillis() + 1500 : 0 server.createContext('/') { HttpExchange exchange -> - requested << "${exchange.requestURI.path}\n" + // Held as the Spring Security plugin holds a request until the application has started: a 503 that says + // to come back. A home page that is down answers 503 for reasons of its own, which says nothing of the kind. + boolean homePage = exchange.requestURI.path == '/' + boolean homePageDown = behaviour == 'homePageDown' && homePage + if (System.currentTimeMillis() < openAt || (behaviour == 'homePageRetrying' && homePage) || homePageDown) { + if (!homePageDown) { + exchange.responseHeaders.add('Retry-After', '1') + } + exchange.sendResponseHeaders(503, -1) + exchange.close() + return + } + if (exchange.requestMethod != 'HEAD') { + requested << "${exchange.requestURI.path}\n" + } byte[] body = 'served'.bytes int status = exchange.requestURI.path == '/missing' ? 404 : 200 exchange.sendResponseHeaders(status, body.length) diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/GrailsAotCacheArchiveSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/GrailsAotCacheArchiveSpec.groovy new file mode 100644 index 00000000000..11da40e5137 --- /dev/null +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/GrailsAotCacheArchiveSpec.groovy @@ -0,0 +1,106 @@ +/* + * 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.gradle.plugin.core + +import org.gradle.api.Project +import org.gradle.api.Task +import org.gradle.testfixtures.ProjectBuilder +import spock.lang.Specification +import spock.lang.TempDir + +import org.apache.grails.gradle.plugin.aot.ExtractApplicationTask +import org.apache.grails.gradle.plugin.aot.TrainAotCacheTask + +/** + * Covers which archive an AOT cache is trained against: the one the application ships. + * + *

That is the executable war where the war plugin is applied, as it is in a generated + * application, and the executable jar otherwise. The war plugin can be applied before or after the + * Grails plugin, and either order has to train against the war.

+ */ +class GrailsAotCacheArchiveSpec extends Specification { + + private static final String GRAILS_WEB_PLUGIN = 'org.apache.grails.gradle.grails-web' + + @TempDir + File projectDir + + private Project project + + void setup() { + project = ProjectBuilder.builder().withProjectDir(projectDir).withName('library').build() + project.version = '0.1' + project.extensions.extraProperties.set('grailsVersion', System.getProperty('projectVersion')) + } + + void 'an application without the war plugin is trained against its executable jar'() { + when: + project.pluginManager.apply(GRAILS_WEB_PLUGIN) + + then: + extractedArchive() == archiveOf('bootJar') + trainedApplication() == 'library-0.1.jar' + } + + void 'an application with the war plugin is trained against its executable war when #order'() { + when: + plugins.each { String id -> project.pluginManager.apply(id) } + + then: + extractedArchive() == archiveOf('bootWar') + extract().taskDependencies.getDependencies(extract())*.name.contains('bootWar') + + and: 'the war is run as the jar its extraction writes' + trainedApplication() == 'library-0.1.jar' + + where: + order | plugins + 'the Grails plugin is applied first' | [GRAILS_WEB_PLUGIN, 'war'] + 'the war plugin is applied first' | ['war', GRAILS_WEB_PLUGIN] + } + + void 'an application that switches its executable war off is trained against its executable jar'() { + given: + project.pluginManager.apply(GRAILS_WEB_PLUGIN) + project.pluginManager.apply('war') + + when: + project.tasks.named('bootWar').configure { Task task -> task.enabled = false } + + then: + extractedArchive() == archiveOf('bootJar') + trainedApplication() == 'library-0.1.jar' + } + + private ExtractApplicationTask extract() { + project.tasks.named('extractAotCacheApplication', ExtractApplicationTask).get() + } + + private File extractedArchive() { + extract().archiveFile.get().asFile + } + + private String trainedApplication() { + project.tasks.named('trainAotCache', TrainAotCacheTask).get().archiveFileName.get() + } + + private File archiveOf(String taskName) { + project.tasks.named(taskName).get().property('archiveFile').get().asFile + } +} diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/GrailsNativeImageDefaultsSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/GrailsNativeImageDefaultsSpec.groovy index a267eade4cc..2fc47e2dc86 100644 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/GrailsNativeImageDefaultsSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/GrailsNativeImageDefaultsSpec.groovy @@ -71,6 +71,41 @@ class GrailsNativeImageDefaultsSpec extends GradleSpecification { result.output.contains('INDY_OPTION=true') } + void 'what traceNativeMetadata records is on the classpath the image is built from'() { + given: + setupTestResourceProject('native-defaults-on') + + when: + BuildResult result = executeTask('inspectNativeClasspath') + + then: 'the entry native-image reads META-INF/native-image of, without which recording it changes nothing' + result.output.contains('TRACED_METADATA_ON_CLASSPATH=true') + } + + void 'metadata recorded under a directory of its own is read from the classpath entry above it'() { + given: + setupTestResourceProject('native-defaults-on') + + when: + BuildResult result = executeTask('inspectNativeClasspath', + ['-PfixtureTraceDirectory=recorded/META-INF/native-image/traced', '-PfixtureTraceRoot=recorded']) + + then: + result.output.contains('TRACED_METADATA_ON_CLASSPATH=true') + } + + void 'metadata recorded where an image cannot read it fails the build with the reason'() { + given: + GradleRunner runner = setupTestResourceProject('native-defaults-on') + + when: + BuildResult result = runner.withArguments( + ['inspectNativeClasspath', '-PfixtureTraceDirectory=recorded', '--stacktrace']).forwardOutput().buildAndFail() + + then: + result.output.contains('which an image cannot read metadata from') + } + void 'an application that builds an image and turns invokedynamic off fails the build with the reason'() { given: GradleRunner runner = setupTestResourceProject('native-defaults-on') diff --git a/grails-gradle/plugins/src/test/resources/test-projects/native-defaults-on/build.gradle b/grails-gradle/plugins/src/test/resources/test-projects/native-defaults-on/build.gradle index eb6be4900a4..edbade1fc1f 100644 --- a/grails-gradle/plugins/src/test/resources/test-projects/native-defaults-on/build.gradle +++ b/grails-gradle/plugins/src/test/resources/test-projects/native-defaults-on/build.gradle @@ -34,6 +34,24 @@ if (providers.gradleProperty('fixtureLateTaskIndy').present) { } } +// -PfixtureTraceDirectory records the traced metadata somewhere else, and -PfixtureTraceRoot names the classpath +// entry expected for it +if (providers.gradleProperty('fixtureTraceDirectory').present) { + grails { + nativeMetadata { + outputDirectory = layout.projectDirectory.dir(providers.gradleProperty('fixtureTraceDirectory').get()) + } + } +} + +tasks.register('inspectNativeClasspath') { + def expected = layout.projectDirectory.dir(providers.gradleProperty('fixtureTraceRoot').getOrElse('src/native/resources')) + def classpath = graalvmNative.binaries.named('main').map { it.classpath } + doLast { + println "TRACED_METADATA_ON_CLASSPATH=${classpath.get().files.contains(expected.asFile)}" + } +} + tasks.register('inspectDefaults') { def indyOption = providers.provider { Map options = project.tasks.named('compileGroovy', GroovyCompile).get().groovyOptions.optimizationOptions diff --git a/grails-gsp/plugin/build.gradle b/grails-gsp/plugin/build.gradle index a80f40fd1f8..6e4b6ed2048 100644 --- a/grails-gsp/plugin/build.gradle +++ b/grails-gsp/plugin/build.gradle @@ -175,6 +175,8 @@ dependencies { testImplementation "jakarta.servlet.jsp:jakarta.servlet.jsp-api:$jspApiVersion" testImplementation 'org.junit.jupiter:junit-jupiter-api' testImplementation 'org.springframework:spring-test' + // Test-only: ApplicationTagLibTests renders the nonce Spring Security's own nonce filter generates + testImplementation 'org.springframework.security:spring-security-web' testImplementation 'org.spockframework:spock-core' testImplementation 'org.apache.grails:grails-testing-support-datamapping' testImplementation project(':grails-testing-support-core') diff --git a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/ApplicationTagLib.groovy b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/ApplicationTagLib.groovy index 1922579b7f2..df1c971a33a 100644 --- a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/ApplicationTagLib.groovy +++ b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/ApplicationTagLib.groovy @@ -38,6 +38,7 @@ import grails.gsp.TagLib import grails.plugins.GrailsPluginManager import grails.util.GrailsStringUtils import grails.util.Metadata +import grails.web.http.ContentSecurityPolicyNonce import grails.web.mapping.LinkGenerator import grails.web.mapping.UrlMappingsHolder import org.grails.web.servlet.mvc.GrailsWebRequest @@ -51,7 +52,7 @@ import org.grails.web.servlet.mvc.GrailsWebRequest @TagLib class ApplicationTagLib implements ApplicationContextAware, InitializingBean, GrailsApplicationAware, TagLibrary { - static returnObjectForTags = ['createLink', 'resource', 'createLinkTo', 'cookie', 'header', 'img', 'join', 'meta', 'set', 'applyCodec'] + static returnObjectForTags = ['createLink', 'resource', 'createLinkTo', 'cookie', 'header', 'cspNonce', 'img', 'join', 'meta', 'set', 'applyCodec'] ApplicationContext applicationContext GrailsPluginManager pluginManager @@ -123,6 +124,16 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr attrs.name ? request.getHeader(attrs.name) : null } + /** + * Renders the nonce of the request's Content-Security-Policy, for the nonce attribute of an inline script or + * style, or nothing when the request carries none. + * + * @emptyTag + */ + def cspNonce(Map attrs) { + ContentSecurityPolicyNonce.nonceOf(request) + } + /** * Sets a variable in the pageContext or the specified scope. * The value can be specified directly or can be a bean retrieved from the applicationContext. diff --git a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/JavascriptTagLib.groovy b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/JavascriptTagLib.groovy index db20ab98eef..65813a4336c 100644 --- a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/JavascriptTagLib.groovy +++ b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/JavascriptTagLib.groovy @@ -65,6 +65,9 @@ class JavascriptTagLib implements ApplicationContextAware, TagLibrary { * * <g:javascript src="myscript.js" /> // actually imports '/app/js/myscript.js' * + * An inline script carries the nonce of the request's Content-Security-Policy when the request has one, + * so a policy that allows scripts by nonce runs it. + * * @attr src The name of the javascript file to import. Will look in web-app/js dir * @attr library The name of the library to include. e.g. "jquery", "prototype", "scriptaculous", "yahoo" or "dojo" * @attr plugin The plugin to look for the javascript in @@ -78,7 +81,9 @@ class JavascriptTagLib implements ApplicationContextAware, TagLibrary { if (hasResourceProcessor) { out << r.script(Collections.EMPTY_MAP, body) } else { - out.println('') diff --git a/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/ApplicationTagLibTests.groovy b/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/ApplicationTagLibTests.groovy index 7305d50fd7c..170deda23ee 100644 --- a/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/ApplicationTagLibTests.groovy +++ b/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/ApplicationTagLibTests.groovy @@ -26,13 +26,18 @@ import grails.util.Metadata import grails.util.MockRequestDataValueProcessor import groovy.test.NotYetImplemented import org.grails.gsp.GroovyPageBinding +import org.grails.plugins.web.controllers.GrailsSecurityHeadersFilter +import org.grails.plugins.web.controllers.GrailsSecurityHeadersProperties import org.grails.plugins.web.taglib.ApplicationTagLib import org.grails.plugins.web.taglib.FormTagLib import org.grails.taglib.GrailsTagException import org.grails.web.util.GrailsApplicationAttributes import org.grails.web.util.WebUtils +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.security.web.header.ContentSecurityPolicyNonceGeneratingFilter import spock.lang.Specification +import jakarta.servlet.FilterChain import jakarta.servlet.http.Cookie class ApplicationTagLibTests extends Specification implements TagLibUnitTest { @@ -238,6 +243,77 @@ class ApplicationTagLibTests extends Specification implements TagLibUnitTest + page = applyTemplate(directive + '') + } as FilterChain) + String nonce = request.getAttribute('_csp').nonce + + then: 'the page and the policy name the same nonce' + nonce ==~ /[A-Za-z0-9+\/]{22}/ + page == "" + response.getHeader('Content-Security-Policy') == "script-src 'nonce-${nonce}'; style-src 'nonce-${nonce}'" + + where: + compilation | directive + 'dynamic' | '' + 'static' | '<%@ page compileStatic="true" %>' + } + + void 'the nonce Spring Security generated renders in a #compilation page, and the policy names it'() { + given: 'Spring Security\'s nonce filter ahead of the security headers filter, as with its chain ordered ahead of GrailsFilters.FIRST' + def properties = new GrailsSecurityHeadersProperties() + properties.contentSecurityPolicy.enabled = true + properties.contentSecurityPolicy.value = "script-src 'nonce-{nonce}'; style-src 'nonce-{nonce}'" + def response = new MockHttpServletResponse() + String page = null + FilterChain view = { viewRequest, viewResponse -> + page = applyTemplate(directive + '') + } as FilterChain + + when: + new ContentSecurityPolicyNonceGeneratingFilter().doFilter(request, response, { securedRequest, securedResponse -> + new GrailsSecurityHeadersFilter(properties).doFilter(securedRequest, securedResponse, view) + } as FilterChain) + def carried = request.getAttribute('_csp') as org.springframework.security.web.header.ContentSecurityPolicyNonce + + then: 'the page renders the nonce Spring Security carries in its own non-public class' + carried.nonce ==~ /[A-Za-z0-9+\/]{22}/ + page == "" + + and: 'the policy names the same nonce' + response.getHeader('Content-Security-Policy') == "script-src 'nonce-${carried.nonce}'; style-src 'nonce-${carried.nonce}'" + + where: + compilation | directive + 'dynamic' | '' + 'static' | '<%@ page compileStatic="true" %>' + } + + void 'no nonce renders when the request carries none: #description'() { + given: + if (holder != null) { + request.setAttribute('_csp', holder) + } + + expect: + applyTemplate('') == '' + applyTemplate('') == '' + + where: + description | holder + 'no policy names one' | null + 'the attribute holds something else' | 'not a nonce' + } + void testClonedUrlFromVariable() { given: unRegisterRequestDataValueProcessor() diff --git a/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/JavascriptTagLibTests.groovy b/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/JavascriptTagLibTests.groovy index 958447bc104..bc9c44a3103 100644 --- a/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/JavascriptTagLibTests.groovy +++ b/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/JavascriptTagLibTests.groovy @@ -19,12 +19,17 @@ package org.grails.web.taglib +import jakarta.servlet.FilterChain + import grails.util.GrailsUtil import org.grails.core.artefact.UrlMappingsArtefactHandler import org.grails.gsp.GroovyPageBinding +import org.grails.plugins.web.controllers.GrailsSecurityHeadersFilter +import org.grails.plugins.web.controllers.GrailsSecurityHeadersProperties import org.grails.web.util.GrailsApplicationAttributes import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test +import org.springframework.mock.web.MockHttpServletResponse import static org.junit.jupiter.api.Assertions.assertEquals @@ -160,6 +165,31 @@ class TestUrlMappings { assertEquals("" + EOL, sw.toString()) } + @Test + void testInlineScriptCarriesTheNonceOfTheContentSecurityPolicy() { + def properties = new GrailsSecurityHeadersProperties() + properties.contentSecurityPolicy.enabled = true + properties.contentSecurityPolicy.value = "script-src 'nonce-{nonce}'" + def response = new MockHttpServletResponse() + String output = null + + new GrailsSecurityHeadersFilter(properties).doFilter(request, response, { viewRequest, viewResponse -> + output = applyTemplate('do.this();') + } as FilterChain) + + String nonce = request.getAttribute('_csp').nonce + assertEquals("${EOL}".toString(), + output) + assertEquals("script-src 'nonce-${nonce}'".toString(), response.getHeader('Content-Security-Policy')) + } + + @Test + void testInlineScriptCarriesNoNonceWhenTheAttributeHoldsNone() { + request.setAttribute('_csp', 'not a nonce') + assertOutputEquals('' + EOL, + 'do.this();') + } + @Test void testJSSrcWithBase() { StringWriter sw = new StringWriter() diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/DefaultMimeTypeResolver.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/DefaultMimeTypeResolver.groovy index 325367c4436..23ac2ac83a4 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/DefaultMimeTypeResolver.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/DefaultMimeTypeResolver.groovy @@ -33,6 +33,29 @@ import org.grails.web.servlet.mvc.GrailsWebRequest @CompileStatic class DefaultMimeTypeResolver implements MimeTypeResolver { + /** + * Resolve the {@link grails.web.mime.MimeType} to be used for the response of the current request. Written out, as + * is {@link #resolveRequestMimeType()}, because Groovy compiles a default argument dynamically even in a statically + * compiled class, which in a native image needs a reflection hint that binding a command object and resolving a + * view would otherwise depend on. + * + * @return the {@link grails.web.mime.MimeType} + */ + @Override + MimeType resolveResponseMimeType() { + resolveResponseMimeType(GrailsWebRequest.lookup()) + } + + /** + * Resolve the {@link MimeType} to be used for the request of the current request. + * + * @return the {@link MimeType} + */ + @Override + MimeType resolveRequestMimeType() { + resolveRequestMimeType(GrailsWebRequest.lookup()) + } + /** * Resolve the {@link grails.web.mime.MimeType} to be used for the response, typically established from the ACCEPT header * @@ -40,7 +63,7 @@ class DefaultMimeTypeResolver implements MimeTypeResolver { * @return the {@link grails.web.mime.MimeType} */ @Override - MimeType resolveResponseMimeType(GrailsWebRequest webRequest = GrailsWebRequest.lookup()) { + MimeType resolveResponseMimeType(GrailsWebRequest webRequest) { if (webRequest != null) { return HttpServletResponseExtension.getMimeType(webRequest.response) } @@ -54,7 +77,7 @@ class DefaultMimeTypeResolver implements MimeTypeResolver { * @return the {@link MimeType} */ @Override - MimeType resolveRequestMimeType(GrailsWebRequest webRequest = GrailsWebRequest.lookup()) { + MimeType resolveRequestMimeType(GrailsWebRequest webRequest) { if (webRequest != null) { final allMimeTypes = HttpServletRequestExtension.getMimeTypes(webRequest.request) if (allMimeTypes) { diff --git a/grails-mimetypes/src/test/groovy/org/grails/web/mime/DefaultMimeTypeResolverSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/web/mime/DefaultMimeTypeResolverSpec.groovy new file mode 100644 index 00000000000..88a92e9b0e5 --- /dev/null +++ b/grails-mimetypes/src/test/groovy/org/grails/web/mime/DefaultMimeTypeResolverSpec.groovy @@ -0,0 +1,69 @@ +/* + * 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.web.mime + +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.mock.web.MockServletContext +import org.springframework.web.context.request.RequestContextHolder +import spock.lang.Specification + +import org.grails.web.servlet.mvc.GrailsWebRequest + +class DefaultMimeTypeResolverSpec extends Specification { + + private final DefaultMimeTypeResolver resolver = new DefaultMimeTypeResolver() + + void cleanup() { + RequestContextHolder.resetRequestAttributes() + } + + void 'without a current request nothing is resolved'() { + expect: + resolver.resolveResponseMimeType() == null + resolver.resolveRequestMimeType() == null + resolver.resolveResponseMimeType(null) == null + resolver.resolveRequestMimeType(null) == null + } + + void 'the current request is the one resolved'() { + given: + def request = new MockHttpServletRequest(new MockServletContext()) + request.addHeader('Accept', 'application/json') + request.contentType = 'application/xml' + def webRequest = new GrailsWebRequest(request, new MockHttpServletResponse(), request.servletContext) + RequestContextHolder.setRequestAttributes(webRequest) + + expect: 'the same as naming it' + resolver.resolveResponseMimeType().name == resolver.resolveResponseMimeType(webRequest).name + resolver.resolveRequestMimeType().name == resolver.resolveRequestMimeType(webRequest).name + + and: + resolver.resolveResponseMimeType(webRequest) != null + resolver.resolveRequestMimeType(webRequest).extension == 'xml' + } + + void 'the methods that take no request are compiled to call GrailsWebRequest directly, which needs no reflection hint'() { + given: 'a default argument, which this replaces, is compiled dynamically even in a statically compiled class' + def bytes = DefaultMimeTypeResolver.getResourceAsStream('DefaultMimeTypeResolver.class').bytes + + expect: 'a direct call names the method and its descriptor in the constant pool, where a dynamic one has only the name' + new String(bytes, 'ISO-8859-1').contains('()Lorg/grails/web/servlet/mvc/GrailsWebRequest;') + } +} diff --git a/grails-openapi/src/main/groovy/org/grails/openapi/ValidationErrorsContent.groovy b/grails-openapi/src/main/groovy/org/grails/openapi/ValidationErrorsContent.groovy index 50dda100174..c6c5f1fdd58 100644 --- a/grails-openapi/src/main/groovy/org/grails/openapi/ValidationErrorsContent.groovy +++ b/grails-openapi/src/main/groovy/org/grails/openapi/ValidationErrorsContent.groovy @@ -167,7 +167,8 @@ class ValidationErrorsContent { .addProperty('title', new StringSchema().description('A summary of the problem, Validation failed')) .addProperty('status', new IntegerSchema().description('The status of the response')) .addProperty('detail', new StringSchema().description('How many errors the request has')) - .addProperty('instance', new StringSchema().format('uri-reference').description('The path the request was made to')) + .addProperty('instance', new StringSchema().format('uri-reference') + .description('Identifies the occurrence of the problem, where the application gives one')) .addProperty('errors', new ArraySchema().items(mapperError(true))) problem.setRequired(['title', 'status', 'detail', 'errors']) problem diff --git a/grails-openapi/src/test/groovy/grails/openapi/ErrorsViewSpec.groovy b/grails-openapi/src/test/groovy/grails/openapi/ErrorsViewSpec.groovy index e174b7bae67..ae2f7824720 100644 --- a/grails-openapi/src/test/groovy/grails/openapi/ErrorsViewSpec.groovy +++ b/grails-openapi/src/test/groovy/grails/openapi/ErrorsViewSpec.groovy @@ -208,7 +208,7 @@ class ErrorsViewSpec extends Specification { } void 'the problem schema describes the problem respond writes for validation errors, as Spring writes it'() { - given: 'validation errors as respond writes them, with the instance it gives them' + given: 'validation errors as respond writes them, with an instance the application gives them' def errors = new BeanPropertyBindingResult(new Kettle(name: ''), 'kettle') errors.rejectValue('name', 'blank', 'Name cannot be blank') def problem = new ValidationProblemDetailFactory().create(errors) diff --git a/grails-openapi/src/test/groovy/grails/openapi/UrlMappingsDocumentSpec.groovy b/grails-openapi/src/test/groovy/grails/openapi/UrlMappingsDocumentSpec.groovy index 5814bd21c10..3b33677baf9 100644 --- a/grails-openapi/src/test/groovy/grails/openapi/UrlMappingsDocumentSpec.groovy +++ b/grails-openapi/src/test/groovy/grails/openapi/UrlMappingsDocumentSpec.groovy @@ -162,12 +162,12 @@ class UrlMappingsDocumentSpec extends Specification { openApi.paths.size() == existing } - void 'skips a mapping declared for a method OpenAPI has no operation for'() { + void 'skips a mapping declared for the #httpMethod method, which OpenAPI has no operation for'() { given: def openApi = new OpenAPI() def customizer = OpenApiFixture.generator(holderFor { '/books'(controller: 'book', action: 'index', method: 'GET') - '/books/report'(controller: 'book', action: 'report', method: 'REPORT') + '/books/report'(controller: 'book', action: 'report', method: httpMethod) }) when: @@ -178,6 +178,9 @@ class UrlMappingsDocumentSpec extends Specification { and: !openApi.paths.containsKey('/books/report') + + where: + httpMethod << ['REPORT', 'QUERY'] } void 'names a greedy parameter for the constraint that binds it'() { diff --git a/grails-openapi/src/test/groovy/org/apache/grails/openapi/aot/OpenApiBeanFactoryInitializationAotProcessorSpec.groovy b/grails-openapi/src/test/groovy/org/apache/grails/openapi/aot/OpenApiBeanFactoryInitializationAotProcessorSpec.groovy index 58f9d2723d6..d0f834cf73e 100644 --- a/grails-openapi/src/test/groovy/org/apache/grails/openapi/aot/OpenApiBeanFactoryInitializationAotProcessorSpec.groovy +++ b/grails-openapi/src/test/groovy/org/apache/grails/openapi/aot/OpenApiBeanFactoryInitializationAotProcessorSpec.groovy @@ -21,9 +21,9 @@ package org.apache.grails.openapi.aot import io.swagger.v3.oas.annotations.media.Content import io.swagger.v3.oas.annotations.media.Schema import io.swagger.v3.oas.annotations.responses.ApiResponse -import org.springframework.aot.generate.ClassNameGenerator import org.springframework.aot.generate.DefaultGenerationContext import org.springframework.aot.generate.InMemoryGeneratedFiles +import org.springframework.aot.generate.NameGenerator import org.springframework.aot.hint.MemberCategory import org.springframework.aot.hint.RuntimeHints import org.springframework.aot.hint.RuntimeHintsRegistrar @@ -203,7 +203,7 @@ class OpenApiBeanFactoryInitializationAotProcessorSpec extends Specification { private RuntimeHints process(DefaultListableBeanFactory beanFactory) { def contribution = new OpenApiBeanFactoryInitializationAotProcessor().processAheadOfTime(beanFactory) def generationContext = new DefaultGenerationContext( - new ClassNameGenerator(ClassName.get('com.example', 'Application')), new InMemoryGeneratedFiles()) + new NameGenerator(ClassName.get('com.example', 'Application')), new InMemoryGeneratedFiles()) contribution.applyTo(generationContext, Stub(BeanFactoryInitializationCode)) generationContext.runtimeHints } diff --git a/grails-rest-transforms/src/main/groovy/org/grails/plugins/web/rest/render/ProblemDetailResponse.groovy b/grails-rest-transforms/src/main/groovy/org/grails/plugins/web/rest/render/ProblemDetailResponse.groovy index 47fffd8aacc..e630f9afe87 100644 --- a/grails-rest-transforms/src/main/groovy/org/grails/plugins/web/rest/render/ProblemDetailResponse.groovy +++ b/grails-rest-transforms/src/main/groovy/org/grails/plugins/web/rest/render/ProblemDetailResponse.groovy @@ -46,8 +46,8 @@ import org.grails.web.servlet.mvc.GrailsWebRequest /** * Writes the response to a {@link ProblemDetail} as Spring MVC writes one a handler method returns, and prepares it * where a view or a renderer of the application writes it: the response takes the status the problem carries, unless - * a {@code status} argument names another, as the status of a {@code ResponseEntity} does, and a problem without an - * {@code instance} is given the path of the resource. + * a {@code status} argument names another, as the status of a {@code ResponseEntity} does. A problem without an + * {@code instance} is written without one, as Spring MVC writes it since Spring Framework 7.1. * * @since 9.0 */ @@ -109,9 +109,6 @@ class ProblemDetailResponse { if (handler == null) { return false } - if (problem.instance == null && context.resourcePath) { - problem.instance = URI.create(context.resourcePath) - } HttpServletRequest request = webRequest.currentRequest // the type and encoding a renderer set for its format are not the problem's, which Spring MVC selects webRequest.currentResponse.setContentType(null) @@ -167,7 +164,7 @@ class ProblemDetailResponse { } request.setAttribute(PREPARED, problem) } - prepare(problem, context, requestedStatus(context) ?: problem.status) + setStatus(context, requestedStatus(context) ?: problem.status) } /** @@ -178,7 +175,7 @@ class ProblemDetailResponse { * @param context the context of the response */ static void prepareBuilt(ProblemDetail problem, RenderContext context) { - prepare(problem, context, problem.status) + setStatus(context, problem.status) } /** @@ -201,13 +198,6 @@ class ProblemDetailResponse { status.toString().toInteger() } - private static void prepare(ProblemDetail problem, RenderContext context, int status) { - setStatus(context, status) - if (problem.instance == null && context.resourcePath) { - problem.instance = URI.create(context.resourcePath) - } - } - /** * A problem may carry any status code, as in Spring MVC, while {@link RenderContext} takes an * {@link HttpStatus}; a code without a constant is written to the servlet response directly. diff --git a/grails-rest-transforms/src/test/groovy/org/grails/plugins/web/rest/render/html/HtmlRendererSpec.groovy b/grails-rest-transforms/src/test/groovy/org/grails/plugins/web/rest/render/html/HtmlRendererSpec.groovy index 6a799c74ce3..a7f6bffbda7 100644 --- a/grails-rest-transforms/src/test/groovy/org/grails/plugins/web/rest/render/html/HtmlRendererSpec.groovy +++ b/grails-rest-transforms/src/test/groovy/org/grails/plugins/web/rest/render/html/HtmlRendererSpec.groovy @@ -183,7 +183,9 @@ class HtmlRendererSpec extends Specification { modelAndView.viewName == 'conflict' modelAndView.model == [problemDetail: problem] webRequest.currentResponse.status == 409 - problem.instance == URI.create('/books/1') + + and: 'the problem is not given an instance' + problem.instance == null } void "the view respond names renders a problem with the status respond is given"() { diff --git a/grails-rest-transforms/src/test/groovy/org/grails/plugins/web/rest/render/json/DefaultJsonRendererSpec.groovy b/grails-rest-transforms/src/test/groovy/org/grails/plugins/web/rest/render/json/DefaultJsonRendererSpec.groovy index 0fc21e34a97..0856bbc32bd 100644 --- a/grails-rest-transforms/src/test/groovy/org/grails/plugins/web/rest/render/json/DefaultJsonRendererSpec.groovy +++ b/grails-rest-transforms/src/test/groovy/org/grails/plugins/web/rest/render/json/DefaultJsonRendererSpec.groovy @@ -88,7 +88,7 @@ import static java.nio.charset.StandardCharsets.UTF_8 class DefaultJsonRendererSpec extends Specification { - void 'an explicit ProblemDetail controls the status content type and instance for status #status'() { + void 'an explicit ProblemDetail controls the status and content type for status #status'() { given: def renderer = new DefaultJsonRenderer(Object) renderer.springHttpMessageConvertersSupplier = { [new JacksonJsonHttpMessageConverter()] } @@ -102,12 +102,32 @@ class DefaultJsonRendererSpec extends Specification { then: webRequest.response.status == status webRequest.response.contentType.startsWith('application/problem+json') - webRequest.response.contentAsString.contains('"instance":"/books"') + + and: 'a problem without an instance is written without one, as Spring MVC writes it' + !(new JsonSlurper().parseText(webRequest.response.contentAsString) as Map).containsKey('instance') where: 'a problem may carry a status that has no HttpStatus constant' status << [422, 499, 299, 599] } + void 'a ProblemDetail is written with the instance the application gives it'() { + given: + def renderer = new DefaultJsonRenderer(Object) + renderer.springHttpMessageConvertersSupplier = { [new JacksonJsonHttpMessageConverter()] } + def webRequest = GrailsWebMockUtil.bindMockWebRequest() + def context = new ServletRenderContext(webRequest) + context.resourcePath = '/books' + def problem = ProblemDetail.forStatus(409) + problem.instance = URI.create('urn:uuid:5c3e1d2a-8f4b-4e7a-9d61-0a2b3c4d5e6f') + + when: + renderer.render(problem, context) + + then: + (new JsonSlurper().parseText(webRequest.response.contentAsString) as Map).instance == + 'urn:uuid:5c3e1d2a-8f4b-4e7a-9d61-0a2b3c4d5e6f' + } + void 'a status argument of #argument decides the status of a problem response, as the status of a ResponseEntity does in Spring MVC'() { given: def renderer = new DefaultJsonRenderer(Object) diff --git a/grails-skills/developer/skills/grails-developer/SKILL.md b/grails-skills/developer/skills/grails-developer/SKILL.md index beb98c9b7c5..7752e415a60 100644 --- a/grails-skills/developer/skills/grails-developer/SKILL.md +++ b/grails-skills/developer/skills/grails-developer/SKILL.md @@ -31,8 +31,8 @@ Activate this skill when developing with Grails, including: ## Technology Stack Grails is built on: -- **Spring Boot**: 4.1.x -- **Spring Framework**: 7.0.x +- **Spring Boot**: 4.2.x +- **Spring Framework**: 7.1.x - **Groovy**: 6.0.x - **Gradle**: 9.7 or later - **Spock**: 2.5-groovy-6.0 diff --git a/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md b/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md index 4af226e93a8..ce4817a689f 100644 --- a/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md +++ b/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md @@ -352,6 +352,8 @@ The Grails HAL JSON renderers remain available through explicit renderer registr With `grails.exceptionresolver.springHandlers: true`, the `@ExceptionHandler` methods of the controller and of the `@ControllerAdvice` beans that apply to it, Spring Boot's `spring.mvc.problemdetails.enabled` handler among them, resolve the exceptions actions throw before the Grails exception resolver. An exception none of them handles is resolved as before. Grails 10 makes it the default, so scope `@ControllerAdvice` beans meant only for Spring MVC controllers by `assignableTypes`, `basePackages` or `annotations` before then. +Do not turn on Spring Framework 7.1's `ProblemDetailsConfigurer.renderUnhandledExceptions` in a `WebMvcConfigurer`: it answers every exception of every action with a problem, a browser's included, ahead of the Grails exception resolver, so `"500"` URL mappings no longer apply. Set `grails.exceptionresolver.problemDetails: true` instead, which writes the same problems and leaves the error page to the clients that prefer HTML and to an exception a URL mapping names. + ## Actions Can Return Spring MVC Responses - Whatever the settings, an action that returns a `ResponseEntity` or another `HttpEntity`, a `ProblemDetail`, an `ErrorResponse`, `HttpHeaders`, a `StreamingResponseBody` or a `ResponseBodyEmitter` has it written as Spring MVC writes a handler method's return value, where Grails 8 did not write it. An action that returns `null`, renders with `render`, `respond` or `redirect`, or forwards, is unaffected. Look for actions that return one of these types. @@ -447,6 +449,15 @@ Grails no longer registers a character encoding filter of its own. Spring Boot's - Code that reads a child of a string or a closure and relied on being answered with the parent reads the parent instead. - Code that read a whole list through a key ending in a whole number, such as `server.ports.0`, now gets the element; read the list as `server.ports`. +## The Spring Security User Cache Stores Copies + +With `cacheUsers`, the Spring Security plugin's user cache stores users by value: each lookup gets a serialized copy, where Grails 8 handed out the cached instance. Erasing the password of a signed-in user no longer erases the cached one, so the cache is used on the next sign-in, and the session's user holds no password. + +- A custom `UserDetails` with a field that cannot be serialized is not cached: it is loaded on every sign-in, with a warning. Make the field `transient` or hold an id. +- Code that changed a user read from `userCache` puts it back with `userCache.putUserInCache`. +- `cacheConfig` on the `userCache` bean, a `MutableConfiguration` with `setStoreByValue(false)`, restores storing by reference. +- In a native image, the plugin registers `User`, `GrailsUser` with a `Long` id and `SimpleGrantedAuthority` for serialization; a custom user class or id type needs metadata, which `traceNativeMetadata` records when the login form is in `forms`. + ## Verification Checklist - [ ] `gradle.properties` names the Grails 9 version and the wrapper is Gradle 9.8 or later diff --git a/grails-spring-security/compat/src/main/groovy/org/springframework/security/web/access/intercept/FilterSecurityInterceptor.groovy b/grails-spring-security/compat/src/main/groovy/org/springframework/security/web/access/intercept/FilterSecurityInterceptor.groovy index ac716a25670..e9a98ea8dcb 100644 --- a/grails-spring-security/compat/src/main/groovy/org/springframework/security/web/access/intercept/FilterSecurityInterceptor.groovy +++ b/grails-spring-security/compat/src/main/groovy/org/springframework/security/web/access/intercept/FilterSecurityInterceptor.groovy @@ -29,6 +29,8 @@ import jakarta.servlet.ServletResponse import org.springframework.security.access.intercept.AbstractSecurityInterceptor import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException +import org.springframework.security.core.Authentication +import org.springframework.security.core.context.SecurityContext import org.springframework.security.core.context.SecurityContextHolder import org.springframework.security.web.FilterInvocation @@ -65,16 +67,38 @@ class FilterSecurityInterceptor extends AbstractSecurityInterceptor implements F return } - def authentication = SecurityContextHolder.context?.authentication + Authentication authentication = SecurityContextHolder.context?.authentication if (authentication == null) { throw new AuthenticationCredentialsNotFoundException('An Authentication object was not found in the SecurityContext') } - accessDecisionManager?.decide(authentication, invocation, attributes) + accessDecisionManager?.decide(authenticateIfRequired(authentication), invocation, attributes) if (!invocation.response.committed) { chain.doFilter(request, response) } } + /** + * Authenticates, for a request that has security attributes, what a filter left in the context without doing so, + * as {@code DigestAuthenticationFilter} does so that the account checks of the authentication manager still run. + * A request without security attributes is passed on before this, so nothing is authenticated for it, as Spring + * Security's own {@code AbstractSecurityInterceptor} did for public invocations before Spring Security 7 removed it. + * + *

With {@code alwaysReauthenticate}, what is authenticated already is authenticated again too, but only while it + * still holds its credentials. The authentication manager erases them by default once it has authenticated it, as + * for a form login kept in the session, and such an authentication, with nothing left to authenticate with, is + * returned as it is.

+ */ + private Authentication authenticateIfRequired(Authentication authentication) { + if (authentication.authenticated && (!alwaysReauthenticate || authentication.credentials == null)) { + return authentication + } + Authentication authenticated = authenticationManager.authenticate(authentication) + SecurityContext context = SecurityContextHolder.createEmptyContext() + context.authentication = authenticated + SecurityContextHolder.context = context + authenticated + } + @Override void destroy() { } diff --git a/grails-spring-security/plugin/build.gradle b/grails-spring-security/plugin/build.gradle index d0718b9ad24..f986419c45d 100644 --- a/grails-spring-security/plugin/build.gradle +++ b/grails-spring-security/plugin/build.gradle @@ -130,6 +130,10 @@ apply { from rootProject.layout.projectDirectory.file('gradle/spring-security-test-config.gradle') } +// DefaultSecurityConfig is compiled, so that loading it finds the class rather than compiling the script at every +// startup, which a native image cannot do. +sourceSets.main.groovy.srcDir('grails-app/conf') + tasks.named('generateConfigurationMetadata') { dslConfigurationFiles.from(file('grails-app/conf/DefaultSecurityConfig.groovy')) dslRootPrefixes.put('security', 'grails.plugin.springsecurity') diff --git a/grails-spring-security/plugin/grails-app/conf/DefaultSecurityConfig.groovy b/grails-spring-security/plugin/grails-app/conf/DefaultSecurityConfig.groovy index 56ff07ebe29..0f2088e87a2 100644 --- a/grails-spring-security/plugin/grails-app/conf/DefaultSecurityConfig.groovy +++ b/grails-spring-security/plugin/grails-app/conf/DefaultSecurityConfig.groovy @@ -132,6 +132,15 @@ security { // requestCache requestCache { createSession = true + // what browsers and their tools request on their own, which Spring Security's DSL does not save either + ignoredPatterns = [ + '/favicon.*', + '/apple-touch-icon*.png', + '/manifest.json', + '/manifest.webmanifest', + '/browserconfig.xml', + '/.well-known/appspecific/com.chrome.devtools.json' + ] } // redirectStrategy diff --git a/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/SpringSecurityCoreGrailsPlugin.groovy b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/SpringSecurityCoreGrailsPlugin.groovy index 6d74ff6e174..1a27b120942 100644 --- a/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/SpringSecurityCoreGrailsPlugin.groovy +++ b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/SpringSecurityCoreGrailsPlugin.groovy @@ -22,6 +22,7 @@ import grails.plugin.springsecurity.access.NullAfterInvocationProvider import grails.plugin.springsecurity.access.intercept.NullAfterInvocationManager import grails.plugin.springsecurity.access.vote.AuthenticatedVetoableDecisionManager import grails.plugin.springsecurity.access.vote.ClosureVoter +import grails.plugin.springsecurity.authentication.DigestDaoAuthenticationProvider import grails.plugin.springsecurity.authentication.GrailsAnonymousAuthenticationProvider import grails.plugin.springsecurity.authentication.NullAuthenticationEventPublisher import grails.plugin.springsecurity.cache.SpringUserCacheFactoryBean @@ -32,6 +33,7 @@ import grails.plugin.springsecurity.userdetails.GormUserDetailsService import grails.plugin.springsecurity.userdetails.NoStackUsernameNotFoundException import grails.plugin.springsecurity.web.GrailsRedirectStrategy import grails.plugin.springsecurity.web.NullFilterChainValidator +import grails.plugin.springsecurity.web.SavedRequestMatcher import grails.plugin.springsecurity.web.SecurityRequestHolderFilter import grails.plugin.springsecurity.web.UpdateRequestContextHolderExceptionTranslationFilter import grails.plugin.springsecurity.web.access.AjaxAwareAccessDeniedHandler @@ -78,6 +80,7 @@ import org.springframework.security.access.intercept.NullRunAsManager import org.springframework.security.access.vote.AuthenticatedVoter import org.springframework.security.access.vote.RoleHierarchyVoter import org.springframework.security.authentication.AccountStatusUserDetailsChecker +import org.springframework.security.authentication.AuthenticationProvider import org.springframework.security.authentication.AuthenticationTrustResolverImpl import org.springframework.security.authentication.DefaultAuthenticationEventPublisher import org.springframework.security.authentication.ProviderManager @@ -102,6 +105,7 @@ import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder import org.springframework.security.web.FilterChainProxy import org.springframework.security.web.PortMapperImpl import org.springframework.security.web.PortResolverImpl +import org.springframework.security.web.SecurityFilterChain import org.springframework.security.web.access.AccessDeniedHandlerImpl import org.springframework.security.web.access.channel.ChannelDecisionManagerImpl import org.springframework.security.web.access.channel.ChannelProcessingFilter @@ -135,7 +139,6 @@ import org.springframework.security.web.savedrequest.HttpSessionRequestCache import org.springframework.security.web.savedrequest.NullRequestCache import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter import org.springframework.security.web.session.HttpSessionEventPublisher -import org.springframework.security.web.util.matcher.AnyRequestMatcher import jakarta.servlet.DispatcherType @@ -253,7 +256,9 @@ class SpringSecurityCoreGrailsPlugin extends Plugin { if (conf.useHttpSessionEventPublisher) { log.trace 'Configuring HttpSessionEventPublisher' - httpSessionEventPublisher(classFor('httpSessionEventPublisher', ServletListenerRegistrationBean), new HttpSessionEventPublisher()) + httpSessionEventPublisher(classFor('httpSessionEventPublisher', ServletListenerRegistrationBean)) { + listener = { HttpSessionEventPublisher publisher -> } + } } createRefList.delegate = delegate @@ -404,8 +409,9 @@ class SpringSecurityCoreGrailsPlugin extends Plugin { // TODO doc new if (conf.afterInvocationManagerProviderNames) { log.trace 'Configuring AfterInvocationProviderManager' + nullAfterInvocationProvider(NullAfterInvocationProvider) afterInvocationManager(classFor('afterInvocationManager', AfterInvocationProviderManager)) { - providers = [new NullAfterInvocationProvider()] // will be replaced in doWithApplicationContext + providers = [ref('nullAfterInvocationProvider')] // will be replaced in doWithApplicationContext } } else { @@ -447,7 +453,7 @@ class SpringSecurityCoreGrailsPlugin extends Plugin { if (securityConfigType == 'Annotation') { objectDefinitionSource(classFor('objectDefinitionSource', AnnotationFilterInvocationDefinition)) { - application = grailsApplication + application = ref('grailsApplication') grailsUrlConverter = ref('grailsUrlConverter') httpServletResponseExtension = ref('httpServletResponseExtension') if (conf.rejectIfNoRule instanceof Boolean) { @@ -490,8 +496,25 @@ class SpringSecurityCoreGrailsPlugin extends Plugin { preAuthenticationChecks(classFor('preAuthenticationChecks', DefaultPreAuthenticationChecks)) postAuthenticationChecks(classFor('postAuthenticationChecks', DefaultPostAuthenticationChecks)) - daoAuthenticationProvider(classFor('daoAuthenticationProvider', DaoAuthenticationProvider), ref('userDetailsService')) { - passwordEncoder = ref('passwordEncoder') + if (conf.useDigestAuth && !conf.digest.useCleartextPasswords) { + // what is stored is the hash a browser computes, which no PasswordEncoder can make since it is not given + // the username, so the application makes it + conf.digest.passwordAlreadyEncoded = true + } + + // with Digest authentication what is stored is what the filter needs, which passwordEncoder cannot compare + // with a password, so the provider compares it itself and passwordEncoder stays as configured + Class daoAuthenticationProviderClass = classFor('daoAuthenticationProvider', + conf.useDigestAuth ? DigestDaoAuthenticationProvider : DaoAuthenticationProvider) + boolean digestProvider = DigestDaoAuthenticationProvider.isAssignableFrom(daoAuthenticationProviderClass) + daoAuthenticationProvider(daoAuthenticationProviderClass, ref('userDetailsService')) { + if (digestProvider) { + realmName = conf.digest.realmName // 'Grails Realm' + passwordAlreadyEncoded = conf.digest.passwordAlreadyEncoded + } + else { + passwordEncoder = ref('passwordEncoder') + } userCache = ref('userCache') preAuthenticationChecks = ref('preAuthenticationChecks') postAuthenticationChecks = ref('postAuthenticationChecks') @@ -503,11 +526,21 @@ class SpringSecurityCoreGrailsPlugin extends Plugin { String algorithm = conf.password.algorithm log.trace 'Using {} algorithm', algorithm - passwordEncoder(classFor('passwordEncoder', DelegatingPasswordEncoder), algorithm, idToPasswordEncoder(conf)) + passwordEncoders(SpringSecurityCoreGrailsPlugin, conf.password.encodeHashAsBase64, + conf.password.hash.iterations, conf.password.bcrypt.logrounds) { bean -> + bean.factoryMethod = 'passwordEncoders' + } + Class passwordEncoderClass = classFor('passwordEncoder', DelegatingPasswordEncoder) + boolean defaultEncoder = passwordEncoderClass == DelegatingPasswordEncoder + passwordEncoder(defaultEncoder ? SpringSecurityCoreGrailsPlugin : passwordEncoderClass, algorithm, ref('passwordEncoders')) { bean -> + if (defaultEncoder) { + bean.factoryMethod = 'delegatingPasswordEncoder' + } + } /** userDetailsService */ userDetailsService(classFor('userDetailsService', GormUserDetailsService)) { - grailsApplication = grailsApplication + grailsApplication = ref('grailsApplication') } /** authenticationUserDetailsService */ @@ -733,13 +766,6 @@ class SpringSecurityCoreGrailsPlugin extends Plugin { applicationContext.registerAlias 'securityDebugFilter', 'springSecurityFilterChain' } - if (conf.useDigestAuth) { - def passwordEncoder = applicationContext.passwordEncoder -// TODO if (passwordEncoder instanceof DigestAuthPasswordEncoder) { -// passwordEncoder.resetInitializing() -// } - } - if (conf.rejectRequestsDuringStartup) { // the startup gate opens once this has run and the application has started applicationContext.springSecurityStartupGateFilter.markInitialized() @@ -895,17 +921,6 @@ class SpringSecurityCoreGrailsPlugin extends Plugin { private configureDigestAuth = { conf -> - if (conf.digest.useCleartextPasswords) { - passwordEncoder(classFor('passwordEncoder', DelegatingPasswordEncoder), ENCODING_ID_NOOP, idToPasswordEncoder(conf)) - } - else { - conf.digest.passwordAlreadyEncoded = true - conf.dao.reflectionSaltSourceProperty = conf.userLookup.usernamePropertyName - passwordEncoder(classFor('passwordEncoder', DigestAuthPasswordEncoder)) { - realm = conf.digest.realmName - } - } - digestAuthenticationEntryPoint(classFor('digestAuthenticationEntryPoint', DigestAuthenticationEntryPoint)) { realmName = conf.digest.realmName // 'Grails Realm' key = conf.digest.key // 'changeme' @@ -973,7 +988,12 @@ class SpringSecurityCoreGrailsPlugin extends Plugin { def providerRefs = createRefList(SpringSecurityUtils.providerNames) /** authenticationManager */ - authenticationManager(classFor('authenticationManager', ProviderManager), providerRefs) { + Class authenticationManagerClass = classFor('authenticationManager', ProviderManager) + boolean defaultManager = authenticationManagerClass == ProviderManager + authenticationManager(defaultManager ? SpringSecurityCoreGrailsPlugin : authenticationManagerClass, providerRefs) { bean -> + if (defaultManager) { + bean.factoryMethod = 'providerManager' + } authenticationEventPublisher = ref('authenticationEventPublisher') eraseCredentialsAfterAuthentication = conf.providerManager.eraseCredentialsAfterAuthentication // true } @@ -987,7 +1007,12 @@ class SpringSecurityCoreGrailsPlugin extends Plugin { securityFilterChains(classFor('securityFilterChains', ArrayList)) - springSecurityFilterChainProxy(classFor('springSecurityFilterChainProxy', FilterChainProxy), ref('securityFilterChains')) { + Class filterChainProxyClass = classFor('springSecurityFilterChainProxy', FilterChainProxy) + boolean defaultProxy = filterChainProxyClass == FilterChainProxy + springSecurityFilterChainProxy(defaultProxy ? SpringSecurityCoreGrailsPlugin : filterChainProxyClass, ref('securityFilterChains')) { bean -> + if (defaultProxy) { + bean.factoryMethod = 'filterChainProxy' + } filterChainValidator = ref('filterChainValidator') firewall = ref('httpFirewall') } @@ -1106,7 +1131,13 @@ class SpringSecurityCoreGrailsPlugin extends Plugin { authenticationDetailsSource(classFor('authenticationDetailsSource', WebAuthenticationDetailsSource)) - requestMatcher(classFor('requestMatcher', AnyRequestMatcher)) + Class requestMatcherClass = classFor('requestMatcher', SavedRequestMatcher) + requestMatcher(requestMatcherClass) { + // a class the application configures in its place need not have this property + if (SavedRequestMatcher.isAssignableFrom(requestMatcherClass)) { + ignoredPatterns = conf.requestCache.ignoredPatterns ?: [] + } + } requestCache(classFor('requestCache', HttpSessionRequestCache)) { createSessionAllowed = conf.requestCache.createSession // true @@ -1167,22 +1198,62 @@ class SpringSecurityCoreGrailsPlugin extends Plugin { beanTypeResolver.resolveType beanName, defaultType } + /** + * The default {@code passwordEncoder}. Its constructors overlap, so ahead-of-time processing cannot choose one for + * the reference a bean definition holds, and an application cannot build a native image. + * + * @since 9.0 + */ + static DelegatingPasswordEncoder delegatingPasswordEncoder(String algorithm, Map encoders) { + new DelegatingPasswordEncoder(algorithm, encoders) + } + + /** + * The default {@code authenticationManager}. Its constructors overlap, so ahead-of-time processing cannot choose one + * for the list of references a bean definition holds, and an application cannot build a native image. + * + * @since 9.0 + */ + static ProviderManager providerManager(List providers) { + new ProviderManager(providers) + } + + /** + * The default {@code springSecurityFilterChainProxy}, whose constructors overlap as {@code ProviderManager}'s do. + * + * @since 9.0 + */ + static FilterChainProxy filterChainProxy(List filterChains) { + new FilterChainProxy(filterChains) + } + static Map idToPasswordEncoder(ConfigObject conf) { + passwordEncoders((boolean) conf.password.encodeHashAsBase64, (int) conf.password.hash.iterations, + (int) conf.password.bcrypt.logrounds) + } + + /** + * The encoders {@code passwordEncoder} chooses between by id, as a bean created from plain values. Ahead-of-time + * processing cannot write out a bean definition that holds the encoders themselves, so an application with them in + * one cannot build a native image. + * + * @since 9.0 + */ + static Map passwordEncoders(boolean encodeHashAsBase64, int iterations, int bcryptLogRounds) { MessageDigestPasswordEncoder messageDigestPasswordEncoderMD5 = new MessageDigestPasswordEncoder(ENCODING_ID_MD5) - messageDigestPasswordEncoderMD5.encodeHashAsBase64 = conf.password.encodeHashAsBase64 // false - messageDigestPasswordEncoderMD5.iterations = conf.password.hash.iterations // 10000 + messageDigestPasswordEncoderMD5.encodeHashAsBase64 = encodeHashAsBase64 + messageDigestPasswordEncoderMD5.iterations = iterations MessageDigestPasswordEncoder messageDigestPasswordEncoderSHA1 = new MessageDigestPasswordEncoder(ENCODING_ID_SHA1) - messageDigestPasswordEncoderSHA1.encodeHashAsBase64 = conf.password.encodeHashAsBase64 // false - messageDigestPasswordEncoderSHA1.iterations = conf.password.hash.iterations // 10000 + messageDigestPasswordEncoderSHA1.encodeHashAsBase64 = encodeHashAsBase64 + messageDigestPasswordEncoderSHA1.iterations = iterations MessageDigestPasswordEncoder messageDigestPasswordEncoderSHA256 = new MessageDigestPasswordEncoder(ENCODING_IDSHA256) - messageDigestPasswordEncoderSHA256.encodeHashAsBase64 = conf.password.encodeHashAsBase64 // false - messageDigestPasswordEncoderSHA256.iterations = conf.password.hash.iterations // 10000 + messageDigestPasswordEncoderSHA256.encodeHashAsBase64 = encodeHashAsBase64 + messageDigestPasswordEncoderSHA256.iterations = iterations - int strength = conf.password.bcrypt.logrounds - [(ENCODING_ID_BCRYPT): new BCryptPasswordEncoder(strength), + [(ENCODING_ID_BCRYPT): new BCryptPasswordEncoder(bcryptLogRounds), (ENCODING_ID_LDAP): new LdapShaPasswordEncoder(), (ENCODING_ID_MD4): new Md4PasswordEncoder(), (ENCODING_ID_MD5): messageDigestPasswordEncoderMD5, diff --git a/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/aot/SpringSecurityRuntimeHints.groovy b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/aot/SpringSecurityRuntimeHints.groovy new file mode 100644 index 00000000000..4fbc9ea4041 --- /dev/null +++ b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/aot/SpringSecurityRuntimeHints.groovy @@ -0,0 +1,64 @@ +/* + * 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 grails.plugin.springsecurity.aot + +import groovy.transform.CompileStatic + +import org.springframework.aot.hint.MemberCategory +import org.springframework.aot.hint.RuntimeHints +import org.springframework.aot.hint.RuntimeHintsRegistrar +import org.springframework.aot.hint.TypeHint +import org.springframework.security.core.authority.SimpleGrantedAuthority +import org.springframework.security.core.userdetails.User + +import grails.plugin.springsecurity.userdetails.GrailsUser + +/** + * Registers what the plugin reaches reflectively before any bean exists. + * + *

{@code ConfigSlurper} creates the plugin's default configuration through Groovy's cached constructors, which + * {@code traceNativeMetadata} does not see, though it records everything the script goes on to call.

+ * + *

With {@code cacheUsers}, the user cache stores users by value, so Ehcache creates its copier for the key + * reflectively when the cache is created, and copies each user by Java serialization. The users the plugin creates are + * registered for that: a {@code User} or {@code GrailsUser} with a {@code Long} id, its {@code SimpleGrantedAuthority} + * roles, and the sorted set {@code User} keeps them in. A user class or id type of the application's own is recorded + * by {@code traceNativeMetadata} when a login form is listed in {@code forms}. These are registered whether or not + * {@code cacheUsers} is set, as the configuration is not known here.

+ * + * @since 9.0 + */ +@CompileStatic +class SpringSecurityRuntimeHints implements RuntimeHintsRegistrar { + + @Override + void registerHints(RuntimeHints hints, ClassLoader classLoader) { + hints.reflection().registerTypeIfPresent(classLoader, 'DefaultSecurityConfig', + MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS) + hints.reflection().registerTypeIfPresent(classLoader, 'org.ehcache.jsr107.Eh107IdentityCopier', + MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS) + [User, GrailsUser, SimpleGrantedAuthority, String, Long, Number].each { Class type -> + hints.reflection().registerJavaSerialization(type) + } + ['org.springframework.security.core.userdetails.User$AuthorityComparator', 'java.util.TreeSet', + 'java.util.Collections$UnmodifiableSet', 'java.util.Collections$UnmodifiableCollection'].each { String name -> + hints.reflection().registerTypeIfPresent(classLoader, name) { TypeHint.Builder type -> type.withJavaSerialization(true) } + } + } +} diff --git a/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/authentication/DigestDaoAuthenticationProvider.groovy b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/authentication/DigestDaoAuthenticationProvider.groovy new file mode 100644 index 00000000000..f32ff2ae782 --- /dev/null +++ b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/authentication/DigestDaoAuthenticationProvider.groovy @@ -0,0 +1,108 @@ +/* + * 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 grails.plugin.springsecurity.authentication + +import java.nio.charset.StandardCharsets +import java.security.MessageDigest + +import groovy.transform.CompileStatic + +import org.springframework.security.authentication.BadCredentialsException +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken +import org.springframework.security.authentication.dao.DaoAuthenticationProvider +import org.springframework.security.core.AuthenticationException +import org.springframework.security.core.userdetails.UserDetails +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.security.crypto.password.NoOpPasswordEncoder + +/** + * The {@code daoAuthenticationProvider} the plugin defines with {@code useDigestAuth}, which compares what is presented + * with what Digest authentication has a user store: the hash a browser computes from the username, the realm name and + * the password, or with {@code digest.useCleartextPasswords} the password as it was typed. + * + *

A password that a form or Basic authentication sends is hashed as a browser hashes it before it is compared, so a + * user signs in with their password in either mode. {@code DigestAuthenticationFilter} leaves a token whose principal + * is the {@code UserDetails} it loaded and whose credentials are what is stored, for the security interceptor to + * authenticate; only in a token whose principal is a {@code UserDetails} is what is stored accepted as it is. The + * principal of what a form or Basic authentication sends is the username, so the stored hash does not sign anyone in + * there.

+ * + *

The {@code passwordEncoder} bean is not used to compare, so it stays as configured for everything else.

+ * + * @since 9.0 + */ +@CompileStatic +class DigestDaoAuthenticationProvider extends DaoAuthenticationProvider { + + private static final String BAD_CREDENTIALS = 'AbstractUserDetailsAuthenticationProvider.badCredentials' + + /** The realm name a browser hashes the password with, {@code digest.realmName}. */ + String realmName + + /** + * Whether what is stored is the hash a browser computes rather than the password, as the filter's + * {@code passwordAlreadyEncoded} is. + */ + boolean passwordAlreadyEncoded = true + + DigestDaoAuthenticationProvider(UserDetailsService userDetailsService) { + super(userDetailsService) + // Not used to compare a password. A sign-in for a username nobody has is compared with what this encoder made, + // so that it takes about as long as one for a user, and with what is compared here as cheap as it is, a slower + // encoder would tell from the time a sign-in takes which usernames exist. + setPasswordEncoder(NoOpPasswordEncoder.getInstance()) + } + + @Override + protected void additionalAuthenticationChecks(UserDetails user, UsernamePasswordAuthenticationToken authentication) + throws AuthenticationException { + Object credentials = authentication.credentials + if (credentials == null) { + logger.debug('Failed to authenticate since no credentials provided') + throw new BadCredentialsException(messages.getMessage(BAD_CREDENTIALS, 'Bad credentials')) + } + if (!matches(user, authentication.principal, credentials.toString())) { + logger.debug('Failed to authenticate since password does not match stored value') + throw new BadCredentialsException(messages.getMessage(BAD_CREDENTIALS, 'Bad credentials')) + } + } + + private boolean matches(UserDetails user, Object principal, String presented) { + String stored = user.password + if (stored == null) { + return false + } + if (!passwordAlreadyEncoded) { + return isEqual(presented, stored) + } + if (isEqual(md5Hex("${user.username}:${realmName}:${presented}"), stored)) { + return true + } + // what the filter left, or a token authenticated before that still holds what it was authenticated with + principal instanceof UserDetails && isEqual(presented, stored) + } + + private static String md5Hex(String value) { + HexFormat.of().formatHex(MessageDigest.getInstance('MD5').digest(value.getBytes(StandardCharsets.UTF_8))) + } + + private static boolean isEqual(String presented, String stored) { + MessageDigest.isEqual(presented.getBytes(StandardCharsets.UTF_8), stored.getBytes(StandardCharsets.UTF_8)) + } +} diff --git a/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/cache/SpringUserCacheFactoryBean.groovy b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/cache/SpringUserCacheFactoryBean.groovy index 5ea0b736177..a6317b6991f 100644 --- a/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/cache/SpringUserCacheFactoryBean.groovy +++ b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/cache/SpringUserCacheFactoryBean.groovy @@ -22,15 +22,29 @@ import javax.cache.configuration.Configuration import javax.cache.configuration.MutableConfiguration import groovy.transform.CompileStatic +import groovy.util.logging.Slf4j import org.springframework.beans.factory.FactoryBean import org.springframework.beans.factory.InitializingBean import org.springframework.cache.jcache.JCacheCache import org.springframework.cache.jcache.JCacheCacheManager import org.springframework.security.core.userdetails.User +import org.springframework.security.core.userdetails.UserDetails import org.springframework.security.core.userdetails.cache.SpringCacheBasedUserCache import org.springframework.util.Assert +/** + * The {@code userCache} the plugin defines with {@code cacheUsers}. + * + *

Users are stored by value, so each lookup is given a copy. The authentication manager erases the credentials of + * the user it signs in by default, and a user stored by reference was erased in the cache too: the next sign-in, and + * with Digest authentication the next request, then missed the cache and loaded the user again. With a copy, what is + * signed in is erased and what is cached keeps what it is checked against.

+ * + *

A user is copied by serializing it, as {@code UserDetails} is {@code Serializable}. One that cannot be serialized + * is not cached, and is loaded on every sign-in, as is one a native image has no serialization metadata for. A + * {@code cacheConfig} set on the bean is used as it is.

+ */ @CompileStatic class SpringUserCacheFactoryBean implements FactoryBean, InitializingBean { @@ -56,8 +70,40 @@ class SpringUserCacheFactoryBean implements FactoryBean() .setTypes(String, User) - .setStoreByValue(false) + .setStoreByValue(true) + } + springUserCache = new CopyingUserCache(new JCacheCache(cacheManager.cacheManager.createCache(cacheName, cacheConfig))) + } + + /** Leaves a user that cannot be stored out of the cache, rather than failing the sign-in that loaded it. */ + @Slf4j + @CompileStatic + private static class CopyingUserCache extends SpringCacheBasedUserCache { + + private final JCacheCache cache + + CopyingUserCache(JCacheCache cache) { + super(cache) + this.cache = cache + } + + /** + * The cache the users are stored in, which the Spring Security UI plugin shows the contents of. The superclass + * keeps it in a private field, which Groovy reached as {@code userCache.cache} only on that class itself. + */ + JCacheCache getCache() { + cache + } + + @Override + void putUserInCache(UserDetails user) { + try { + super.putUserInCache(user) + } + catch (RuntimeException | LinkageError e) { + // a LinkageError where a native image has no serialization metadata for a class of the user + log.warn('User {} was not cached, as it could not be copied into the user cache: {}', user.username, e.message) + } } - springUserCache = new SpringCacheBasedUserCache(new JCacheCache(cacheManager.cacheManager.createCache(cacheName, cacheConfig))) } } diff --git a/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/web/SavedRequestMatcher.groovy b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/web/SavedRequestMatcher.groovy new file mode 100644 index 00000000000..beb52b83710 --- /dev/null +++ b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/web/SavedRequestMatcher.groovy @@ -0,0 +1,52 @@ +/* + * 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 grails.plugin.springsecurity.web + +import groovy.transform.CompileStatic + +import jakarta.servlet.http.HttpServletRequest + +import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher +import org.springframework.security.web.util.matcher.RequestMatcher + +/** + * Decides which requests the request cache saves as the page to redirect to after a login: all but those the + * {@code requestCache.ignoredPatterns} setting names, which browsers and their tools send on their own. Spring Security + * leaves the same paths out of the matcher its configuration DSL builds, which the plugin does not use, and that list + * is private to the DSL. + * + * @since 9.0 + */ +@CompileStatic +class SavedRequestMatcher implements RequestMatcher { + + private List ignored = [] + + /** + * @param patterns the path patterns, relative to the context path, of the requests not to save + */ + void setIgnoredPatterns(Collection patterns) { + ignored = patterns.collect { String pattern -> PathPatternRequestMatcher.pathPattern(pattern) as RequestMatcher } + } + + @Override + boolean matches(HttpServletRequest request) { + !ignored.any { RequestMatcher matcher -> matcher.matches(request) } + } +} diff --git a/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/web/filter/StartupGateFilter.groovy b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/web/filter/StartupGateFilter.groovy index 141e363e765..84a980055aa 100644 --- a/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/web/filter/StartupGateFilter.groovy +++ b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/web/filter/StartupGateFilter.groovy @@ -30,17 +30,28 @@ import jakarta.servlet.http.HttpServletResponse import org.springframework.boot.context.event.ApplicationStartedEvent import org.springframework.context.ApplicationListener +import org.springframework.http.HttpHeaders import org.springframework.web.filter.GenericFilterBean /** * Responds to every request with 503 Service Unavailable until the application has started, * that is, until the plugin has initialized and Spring Boot has published * ApplicationStartedEvent. + * + *

The response carries Retry-After, which is how a 503 says the unavailability + * is temporary (RFC 9110, section 15.6.4). A client can then tell this answer, which changes once + * the application has started, from a 503 the application gives for reasons of its own.

*/ @Slf4j @CompileStatic class StartupGateFilter extends GenericFilterBean implements ApplicationListener { + /** + * How many seconds a rejected request is asked to wait before it is made again: one, since the + * gate is closed only while the application finishes starting. + */ + private static final String RETRY_AFTER_SECONDS = '1' + private boolean initialized private boolean started private volatile boolean opened @@ -66,7 +77,9 @@ class StartupGateFilter extends GenericFilterBean implements ApplicationListener if (!opened) { log.debug('Rejecting request to {}, the application has not started yet', ((HttpServletRequest) req).requestURI) // set the status rather than calling sendError() to avoid an error dispatch - ((HttpServletResponse) res).status = HttpServletResponse.SC_SERVICE_UNAVAILABLE + HttpServletResponse response = (HttpServletResponse) res + response.status = HttpServletResponse.SC_SERVICE_UNAVAILABLE + response.setHeader(HttpHeaders.RETRY_AFTER, RETRY_AFTER_SECONDS) return } diff --git a/grails-spring-security/plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/grails-spring-security/plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 9fb9659330e..c39b0a334ac 100644 --- a/grails-spring-security/plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/grails-spring-security/plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -729,7 +729,7 @@ { "name": "grails.plugin.springsecurity.fii.alwaysReauthenticate", "type": "java.lang.Boolean", - "description": "If true, re-authenticates when there is an Authentication in the SecurityContext.", + "description": "If true, re-authenticates the Authentication in the SecurityContext for every request to a secured URL, unless its credentials were erased once it was authenticated, as providerManager.eraseCredentialsAfterAuthentication does by default.", "group": "grails.plugin.springsecurity.authentication" }, { diff --git a/grails-spring-security/plugin/src/main/resources/META-INF/spring/aot.factories b/grails-spring-security/plugin/src/main/resources/META-INF/spring/aot.factories new file mode 100644 index 00000000000..369a0ae4882 --- /dev/null +++ b/grails-spring-security/plugin/src/main/resources/META-INF/spring/aot.factories @@ -0,0 +1,2 @@ +org.springframework.aot.hint.RuntimeHintsRegistrar=\ +grails.plugin.springsecurity.aot.SpringSecurityRuntimeHints diff --git a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/DigestAuthenticationSpec.groovy b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/DigestAuthenticationSpec.groovy new file mode 100644 index 00000000000..076c3a8d6ab --- /dev/null +++ b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/DigestAuthenticationSpec.groovy @@ -0,0 +1,344 @@ +/* + * 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 grails.plugin.springsecurity + +import jakarta.servlet.FilterChain + +import org.springframework.mock.web.MockFilterChain +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.security.authentication.AuthenticationManager +import org.springframework.security.authentication.BadCredentialsException +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken +import org.springframework.security.core.Authentication +import org.springframework.security.core.authority.SimpleGrantedAuthority +import org.springframework.security.core.context.SecurityContext +import org.springframework.security.core.context.SecurityContextHolder +import org.springframework.security.core.userdetails.User +import org.springframework.security.core.userdetails.UserCache +import org.springframework.security.core.userdetails.UserDetails +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.security.core.userdetails.UsernameNotFoundException +import org.springframework.security.crypto.password.DelegatingPasswordEncoder +import org.springframework.security.crypto.password.PasswordEncoder +import org.springframework.security.web.context.HttpSessionSecurityContextRepository +import spock.lang.Specification + +import grails.core.GrailsApplication +import grails.testing.web.GrailsWebUnitTest +import grails.web.CamelCaseUrlConverter + +/** + * The handshake a browser makes, against what the plugin creates with {@code useDigestAuth}: the challenge from its + * entry point, and the response sent through its whole filter chain, which is what grants the request. And what the + * rest of the application still does with Digest authentication on: a form sign-in, a Basic one, and the user that + * {@code spring.security.user.*} describes. + */ +abstract class AbstractDigestAuthenticationSpec extends Specification implements GrailsWebUnitTest { + + protected static final String REALM = 'Test Realm' + + Set getIncludePlugins() { + ['core', 'springSecurityCore'] as Set + } + + Closure doWithSpring() { + { -> + grailsUrlConverter(CamelCaseUrlConverter) + } + } + + Closure doWithConfig() { + { config -> + config.grails.plugin.springsecurity.securityConfigType = 'InterceptUrlMap' + config.grails.plugin.springsecurity.interceptUrlMap = [[pattern: '/secure', access: ['isAuthenticated()']]] + config.grails.plugin.springsecurity.useDigestAuth = true + config.grails.plugin.springsecurity.userDetailsServiceBeanClass = StoredPasswordUserDetailsService + config.grails.plugin.springsecurity.digest = digestConfig() + config.spring.security.user.name = 'admin' + config.spring.security.user.password = 'admin-secret' + } + } + + protected abstract Map digestConfig() + + /** What the application stores for a user with this password. */ + protected abstract String storedFor(String password) + + void cleanup() { + SecurityContextHolder.clearContext() + } + + void cleanupSpec() { + SpringSecurityUtils.resetSecurityConfig() + ReflectionUtils.application = null + SpringSecurityUtils.application = null + } + + void 'a browser signs a user in with their password, and not with another'() { + given: + store(storedFor('secret')) + + expect: + signedInWith('secret') == 'user' + signedInWith('wrong') == null + } + + void 'an account that is disabled is refused, however right the password'() { + given: + store(storedFor('secret'), false) + + expect: + signedInWith('secret') == null + } + + void 'a form signs a user in with their password, and not with another'() { + given: + store(storedFor('secret')) + + expect: + signedInThroughForm('user', 'secret') == 'user' + signedInThroughForm('user', 'wrong') == null + } + + void 'what Basic authentication hands the authentication manager signs a user in with their password'() { + given: + store(storedFor('secret')) + + when: + Authentication signedIn = authenticationManager.authenticate( + UsernamePasswordAuthenticationToken.unauthenticated('user', 'secret')) + + then: + signedIn.authenticated + signedIn.name == 'user' + + when: + authenticationManager.authenticate(UsernamePasswordAuthenticationToken.unauthenticated('user', 'wrong')) + + then: + thrown(BadCredentialsException) + } + + void 'the password encoder stays as configured'() { + when: + PasswordEncoder encoder = applicationContext.getBean('passwordEncoder', PasswordEncoder) + + then: + encoder instanceof DelegatingPasswordEncoder + encoder.encode('secret').startsWith('{bcrypt}') + } + + void 'the user spring.security.user describes signs in through a form'() { + expect: + signedInThroughForm('admin', 'admin-secret') == 'admin' + signedInThroughForm('admin', 'wrong') == null + } + + protected AuthenticationManager getAuthenticationManager() { + applicationContext.getBean('authenticationManager', AuthenticationManager) + } + + protected void store(String password, boolean enabled = true) { + def users = applicationContext.getBean('userDetailsService', StoredPasswordUserDetailsService) + users.password = password + users.enabled = enabled + } + + /** Who a browser sending this password reaches the application as, if it reaches it. */ + protected String signedInWith(String password) { + def challenge = new MockHttpServletResponse() + applicationContext.getBean('digestAuthenticationEntryPoint') + .commence(new MockHttpServletRequest(), challenge, new BadCredentialsException('no credentials yet')) + String header = challenge.getHeader('WWW-Authenticate') + String realm = (header =~ /realm="([^"]*)"/)[0][1] + String nonce = (header =~ /nonce="([^"]*)"/)[0][1] + + String ha1 = "user:${realm}:${password}".md5() + String ha2 = 'GET:/secure'.md5() + String response = "${ha1}:${nonce}:00000001:cnonce:auth:${ha2}".md5() + + def request = new MockHttpServletRequest('GET', '/secure') + request.servletPath = '/secure' + request.addHeader('Authorization', "Digest username=\"user\", realm=\"${realm}\", nonce=\"${nonce}\", " + + "uri=\"/secure\", qop=auth, nc=00000001, cnonce=\"cnonce\", response=\"${response}\"") + String reached = null + FilterChain application = { req, res -> reached = SecurityContextHolder.context.authentication.name } as FilterChain + SecurityContextHolder.clearContext() + applicationContext.getBean('springSecurityFilterChainProxy').doFilter(request, new MockHttpServletResponse(), application) + reached + } + + /** Who a login form sending this username and password signs in, if anyone. */ + protected String signedInThroughForm(String username, String password) { + def request = new MockHttpServletRequest('POST', '/login/authenticate') + request.servletPath = '/login/authenticate' + request.addParameter('username', username) + request.addParameter('password', password) + SecurityContextHolder.clearContext() + applicationContext.getBean('springSecurityFilterChainProxy') + .doFilter(request, new MockHttpServletResponse(), new MockFilterChain()) + def context = request.getSession(false)?.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY) + ((SecurityContext) context)?.authentication?.name + } + + static class StoredPasswordUserDetailsService implements UserDetailsService { + + GrailsApplication grailsApplication + String password + boolean enabled = true + int loads + + @Override + UserDetails loadUserByUsername(String username) { + if (username != 'user') { + throw new UsernameNotFoundException(username) + } + loads++ + new User(username, password, enabled, true, true, true, [new SimpleGrantedAuthority('ROLE_USER')]) + } + } +} + +class EncodedDigestAuthenticationSpec extends AbstractDigestAuthenticationSpec { + + protected Map digestConfig() { + [realmName: REALM] + } + + protected String storedFor(String password) { + "user:${REALM}:${password}".md5() + } + + void 'the user signed in is erased of its password, as the authentication manager erases it by default'() { + given: + store(storedFor('secret')) + + when: + Authentication signedIn = authenticationManager.authenticate( + UsernamePasswordAuthenticationToken.unauthenticated('user', 'secret')) + + then: 'without cacheUsers nothing else holds the user' + signedIn.credentials == null + ((UserDetails) signedIn.principal).password == null + } + + void 'a password stored as it was typed does not sign anyone in'() { + given: + store('secret') + + expect: + signedInWith('secret') == null + } + + void 'the hash stored does not sign anyone in through a form'() { + given: + String hash = storedFor('secret') + store(hash) + + expect: + signedInThroughForm('user', hash) == null + } + + void 'the hash stored does not sign anyone in through Basic authentication'() { + given: + String hash = storedFor('secret') + store(hash) + + when: + authenticationManager.authenticate(UsernamePasswordAuthenticationToken.unauthenticated('user', hash)) + + then: + thrown(BadCredentialsException) + } +} + +class CleartextDigestAuthenticationSpec extends AbstractDigestAuthenticationSpec { + + protected Map digestConfig() { + [realmName: REALM, useCleartextPasswords: true] + } + + protected String storedFor(String password) { + password + } + + void 'a password stored by the password encoder does not sign anyone in'() { + given: + store(applicationContext.getBean('passwordEncoder', PasswordEncoder).encode('secret')) + + expect: + signedInWith('secret') == null + } +} + +class CachedDigestAuthenticationSpec extends AbstractDigestAuthenticationSpec { + + protected Map digestConfig() { + [realmName: REALM] + } + + protected String storedFor(String password) { + "user:${REALM}:${password}".md5() + } + + Closure doWithConfig() { + Closure digest = super.doWithConfig() + return { config -> + digest.call(config) + config.grails.plugin.springsecurity.cacheUsers = true + } + } + + /** What the cache holds was loaded before this, so it is emptied, as a change to the user empties it. */ + @Override + protected void store(String password, boolean enabled = true) { + super.store(password, enabled) + applicationContext.getBean('userCache', UserCache).removeUserFromCache('user') + applicationContext.getBean('userDetailsService', StoredPasswordUserDetailsService).loads = 0 + } + + void 'a cached user keeps its password, so a browser signing in again is answered from the cache'() { + given: + store(storedFor('secret')) + def users = applicationContext.getBean('userDetailsService', StoredPasswordUserDetailsService) + UserCache cache = applicationContext.getBean('userCache', UserCache) + + expect: + signedInWith('secret') == 'user' + cache.getUserFromCache('user').password == storedFor('secret') + signedInWith('secret') == 'user' + users.loads == 1 + } + + void 'what is signed in is erased of its password, and what is cached is not'() { + given: + store(storedFor('secret')) + + when: + Authentication signedIn = authenticationManager.authenticate( + UsernamePasswordAuthenticationToken.unauthenticated('user', 'secret')) + + then: 'the cache gives each lookup a copy, so nothing a session keeps holds the stored password' + signedIn.authenticated + signedIn.credentials == null + ((UserDetails) signedIn.principal).password == null + applicationContext.getBean('userCache', UserCache).getUserFromCache('user').password == storedFor('secret') + } +} diff --git a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/PluginBeansSpec.groovy b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/PluginBeansSpec.groovy new file mode 100644 index 00000000000..b1100afae1c --- /dev/null +++ b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/PluginBeansSpec.groovy @@ -0,0 +1,259 @@ +/* + * 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 grails.plugin.springsecurity + +import org.springframework.boot.web.servlet.ServletListenerRegistrationBean +import org.springframework.security.authentication.AuthenticationProvider +import org.springframework.security.authentication.ProviderManager +import org.springframework.security.authentication.dao.DaoAuthenticationProvider +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.security.crypto.password.DelegatingPasswordEncoder +import org.springframework.security.crypto.password.MessageDigestPasswordEncoder +import org.springframework.security.crypto.password.PasswordEncoder +import org.springframework.security.web.DefaultSecurityFilterChain +import org.springframework.security.web.FilterChainProxy +import org.springframework.security.web.SecurityFilterChain +import org.springframework.security.web.session.HttpSessionEventPublisher +import org.springframework.security.web.util.matcher.AnyRequestMatcher +import spock.lang.Specification + +import grails.plugin.springsecurity.access.NullAfterInvocationProvider +import grails.plugin.springsecurity.authentication.DigestDaoAuthenticationProvider +import grails.testing.web.GrailsWebUnitTest +import grails.web.CamelCaseUrlConverter + +/** + * What the beans the plugin declares are once created, however they are declared. + */ +abstract class AbstractPluginBeansSpec extends Specification implements GrailsWebUnitTest { + + Set getIncludePlugins() { + ['core', 'springSecurityCore'] as Set + } + + Closure doWithSpring() { + { -> + grailsUrlConverter(CamelCaseUrlConverter) + } + } + + Closure doWithConfig() { + { config -> + config.grails.plugin.springsecurity.securityConfigType = 'InterceptUrlMap' + configure(config.grails.plugin.springsecurity) + } + } + + protected abstract void configure(Object securityConfig) + + void cleanupSpec() { + SpringSecurityUtils.resetSecurityConfig() + ReflectionUtils.application = null + SpringSecurityUtils.application = null + } +} + +class DefaultPluginBeansSpec extends AbstractPluginBeansSpec { + + protected void configure(Object securityConfig) { + securityConfig.password = [algorithm: 'SHA-256', encodeHashAsBase64: true, hash: [iterations: 2]] + securityConfig.providerManager = [eraseCredentialsAfterAuthentication: false] + securityConfig.useHttpSessionEventPublisher = true + securityConfig.afterInvocationManagerProviderNames = ['nullAfterInvocationProvider'] + } + + void 'the password encoder encodes as the application configured it'() { + given: 'what the digest encoder makes of the values, which it only accepts if they all arrived' + def digest = new MessageDigestPasswordEncoder('SHA-256') + digest.encodeHashAsBase64 = true + digest.iterations = 2 + + when: + String encoded = applicationContext.getBean('passwordEncoder', PasswordEncoder).encode('secret') + + then: + applicationContext.getBean('passwordEncoder') instanceof DelegatingPasswordEncoder + encoded.startsWith('{SHA-256}') + digest.matches('secret', encoded - '{SHA-256}') + } + + void 'the authentication manager is told what the application configured'() { + given: + ProviderManager manager = applicationContext.getBean('authenticationManager', ProviderManager) + + expect: + !manager.eraseCredentialsAfterAuthentication + } + + void 'the filter chain proxy reads the list the plugin goes on to fill'() { + given: + List chains = applicationContext.getBean('securityFilterChains', List) + FilterChainProxy proxy = applicationContext.getBean('springSecurityFilterChainProxy', FilterChainProxy) + def chain = new DefaultSecurityFilterChain(AnyRequestMatcher.INSTANCE) + + when: + chains << chain + + then: + proxy.filterChains.contains(chain) + } + + void 'the session event publisher is registered as a listener'() { + expect: + applicationContext.getBean('httpSessionEventPublisher', ServletListenerRegistrationBean) + .listener instanceof HttpSessionEventPublisher + } + + void 'the after-invocation manager starts with a provider'() { + expect: + applicationContext.getBean('afterInvocationManager').providers*.getClass() == [NullAfterInvocationProvider] + } +} + +class BcryptPluginBeansSpec extends AbstractPluginBeansSpec { + + protected void configure(Object securityConfig) { + securityConfig.password = [algorithm: 'bcrypt', bcrypt: [logrounds: 5]] + } + + void 'the password encoder uses the rounds the application configured'() { + expect: + applicationContext.getBean('passwordEncoder', PasswordEncoder).encode('secret').startsWith('{bcrypt}$2a$05$') + } +} + +class CustomClassesPluginBeansSpec extends AbstractPluginBeansSpec { + + protected void configure(Object securityConfig) { + securityConfig.password = [algorithm: 'bcrypt'] + securityConfig.passwordEncoderBeanClass = CustomPasswordEncoder + securityConfig.authenticationManagerBeanClass = CustomProviderManager + securityConfig.springSecurityFilterChainProxyBeanClass = CustomFilterChainProxy + } + + void 'a class configured in place of a default one is created with what the default is given'() { + given: + CustomPasswordEncoder encoder = applicationContext.getBean('passwordEncoder', CustomPasswordEncoder) + + expect: + encoder.algorithm == 'bcrypt' + encoder.encoders.bcrypt + applicationContext.getBean('authenticationManager') instanceof CustomProviderManager + applicationContext.getBean('springSecurityFilterChainProxy') instanceof CustomFilterChainProxy + } + + static class CustomPasswordEncoder extends DelegatingPasswordEncoder { + + final String algorithm + final Map encoders + + CustomPasswordEncoder(String algorithm, Map encoders) { + super(algorithm, encoders) + this.algorithm = algorithm + this.encoders = encoders + } + } + + static class CustomProviderManager extends ProviderManager { + + /** The plugin sets the providers on whatever class is configured, and this one's are private to its parent. */ + List providers + + CustomProviderManager(List providers) { + super(providers) + } + } + + static class CustomFilterChainProxy extends FilterChainProxy { + + CustomFilterChainProxy(List chains) { + super(chains) + } + } +} + +class DigestPluginBeansSpec extends AbstractPluginBeansSpec { + + protected void configure(Object securityConfig) { + securityConfig.useDigestAuth = true + securityConfig.digest = [realmName: 'Test Realm'] + } + + void 'with Digest authentication the provider is told the realm and that what is stored is the hash'() { + given: + DigestDaoAuthenticationProvider provider = + applicationContext.getBean('daoAuthenticationProvider', DigestDaoAuthenticationProvider) + + expect: + provider.realmName == 'Test Realm' + provider.passwordAlreadyEncoded + applicationContext.getBean('passwordEncoder') instanceof DelegatingPasswordEncoder + } +} + +class CleartextDigestPluginBeansSpec extends AbstractPluginBeansSpec { + + protected void configure(Object securityConfig) { + securityConfig.useDigestAuth = true + securityConfig.digest = [realmName: 'Test Realm', useCleartextPasswords: true] + securityConfig.daoAuthenticationProviderBeanClass = CustomDigestDaoAuthenticationProvider + } + + void 'a subclass of the Digest provider configured in its place is told what it is'() { + given: + CustomDigestDaoAuthenticationProvider provider = + applicationContext.getBean('daoAuthenticationProvider', CustomDigestDaoAuthenticationProvider) + + expect: + provider.realmName == 'Test Realm' + !provider.passwordAlreadyEncoded + } + + static class CustomDigestDaoAuthenticationProvider extends DigestDaoAuthenticationProvider { + + CustomDigestDaoAuthenticationProvider(UserDetailsService userDetailsService) { + super(userDetailsService) + } + } +} + +class CustomProviderDigestPluginBeansSpec extends AbstractPluginBeansSpec { + + protected void configure(Object securityConfig) { + securityConfig.useDigestAuth = true + securityConfig.daoAuthenticationProviderBeanClass = CustomDaoAuthenticationProvider + } + + void 'a provider class of the application is given the password encoder with Digest authentication too'() { + expect: + applicationContext.getBean('daoAuthenticationProvider', CustomDaoAuthenticationProvider).encoder + .is(applicationContext.getBean('passwordEncoder')) + } + + static class CustomDaoAuthenticationProvider extends DaoAuthenticationProvider { + + CustomDaoAuthenticationProvider(UserDetailsService userDetailsService) { + super(userDetailsService) + } + + PasswordEncoder getEncoder() { + getPasswordEncoder() + } + } +} diff --git a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/RequestCacheConfigurationSpec.groovy b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/RequestCacheConfigurationSpec.groovy new file mode 100644 index 00000000000..61c3e772c58 --- /dev/null +++ b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/RequestCacheConfigurationSpec.groovy @@ -0,0 +1,132 @@ +/* + * 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 grails.plugin.springsecurity + +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.security.web.savedrequest.RequestCache +import org.springframework.security.web.util.matcher.AnyRequestMatcher +import spock.lang.Specification + +import grails.testing.web.GrailsWebUnitTest +import grails.web.CamelCaseUrlConverter + +/** + * What the plugin's {@code requestCache} bean saves, as {@code requestCache.ignoredPatterns} and + * {@code requestMatcherBeanClass} configure it. + */ +abstract class AbstractRequestCacheConfigurationSpec extends Specification implements GrailsWebUnitTest { + + Set getIncludePlugins() { + ['core', 'springSecurityCore'] as Set + } + + Closure doWithSpring() { + { -> + grailsUrlConverter(CamelCaseUrlConverter) + } + } + + Closure doWithConfig() { + { config -> + config.grails.plugin.springsecurity.securityConfigType = 'InterceptUrlMap' + configure(config.grails.plugin.springsecurity) + } + } + + protected abstract void configure(Object securityConfig) + + void cleanupSpec() { + SpringSecurityUtils.resetSecurityConfig() + ReflectionUtils.application = null + SpringSecurityUtils.application = null + } + + protected boolean saves(String path) { + def requestCache = applicationContext.getBean('requestCache', RequestCache) + def request = new MockHttpServletRequest('GET', path) + def response = new MockHttpServletResponse() + requestCache.saveRequest(request, response) + requestCache.getRequest(request, response) != null + } +} + +class DefaultRequestCacheConfigurationSpec extends AbstractRequestCacheConfigurationSpec { + + protected void configure(Object securityConfig) { + } + + void 'by default the requests a browser sends on its own are not saved'() { + expect: + !saves('/manifest.json') + !saves('/.well-known/appspecific/com.chrome.devtools.json') + saves('/book/show') + } +} + +class CustomIgnoredPatternsConfigurationSpec extends AbstractRequestCacheConfigurationSpec { + + protected void configure(Object securityConfig) { + securityConfig.requestCache.ignoredPatterns = ['/poll/**'] + } + + void 'the patterns an application configures replace the default ones'() { + expect: + !saves('/poll/status') + saves('/manifest.json') + } +} + +class NoIgnoredPatternsConfigurationSpec extends AbstractRequestCacheConfigurationSpec { + + protected void configure(Object securityConfig) { + securityConfig.requestCache.ignoredPatterns = [] + } + + void 'no patterns save every request'() { + expect: + saves('/manifest.json') + saves('/book/show') + } +} + +class EmptyStringIgnoredPatternsConfigurationSpec extends AbstractRequestCacheConfigurationSpec { + + protected void configure(Object securityConfig) { + securityConfig.requestCache.ignoredPatterns = '' + } + + void 'an empty value, which an empty YAML list flattens to, saves every request'() { + expect: + saves('/manifest.json') + } +} + +class RequestMatcherBeanClassConfigurationSpec extends AbstractRequestCacheConfigurationSpec { + + protected void configure(Object securityConfig) { + securityConfig.requestMatcherBeanClass = AnyRequestMatcher + } + + void 'a class configured in place of the default matcher is created without the patterns'() { + expect: + applicationContext.getBean('requestMatcher') instanceof AnyRequestMatcher + saves('/manifest.json') + } +} diff --git a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/aot/PluginBeansAotSpec.groovy b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/aot/PluginBeansAotSpec.groovy new file mode 100644 index 00000000000..86b989b932b --- /dev/null +++ b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/aot/PluginBeansAotSpec.groovy @@ -0,0 +1,154 @@ +/* + * 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 grails.plugin.springsecurity.aot + +import org.springframework.aot.generate.DefaultGenerationContext +import org.springframework.aot.generate.InMemoryGeneratedFiles +import org.springframework.aot.generate.NameGenerator +import org.springframework.context.aot.ApplicationContextAotGenerator +import org.springframework.context.support.GenericApplicationContext +import org.springframework.javapoet.ClassName +import spock.lang.Specification + +import grails.plugin.springsecurity.CustomClassesPluginBeansSpec +import grails.plugin.springsecurity.ReflectionUtils +import grails.plugin.springsecurity.SpringSecurityCoreGrailsPlugin +import grails.plugin.springsecurity.SpringSecurityUtils +import grails.spring.BeanBuilder +import grails.testing.web.GrailsWebUnitTest + +/** + * Runs Spring's own ahead-of-time generator over the beans the plugin declares, which an application's + * {@code processAot} does with them, and which fails where a bean holds a value it cannot write out or + * has constructors it cannot choose between. + */ +abstract class AbstractPluginBeansAotSpec extends Specification implements GrailsWebUnitTest { + + Set getIncludePlugins() { + ['core'] as Set + } + + Closure doWithConfig() { + { config -> + configure(config.grails.plugin.springsecurity) + } + } + + protected abstract void configure(Object securityConfig) + + void cleanupSpec() { + SpringSecurityUtils.resetSecurityConfig() + ReflectionUtils.application = null + SpringSecurityUtils.application = null + } + + void 'what the plugin declares can be written out ahead of time'() { + given: + def plugin = new SpringSecurityCoreGrailsPlugin(grailsApplication: grailsApplication) + def builder = new BeanBuilder(null, getClass().classLoader) + builder.beans(plugin.doWithSpring()) + def context = new GenericApplicationContext() + builder.registerBeans(context) + def generationContext = new DefaultGenerationContext( + new NameGenerator(ClassName.get('generated', 'Application')), new InMemoryGeneratedFiles()) + + when: + new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext) + + then: + noExceptionThrown() + context.getBeanDefinition('passwordEncoder') + context.getBeanDefinition('authenticationManager') + context.getBeanDefinition('springSecurityFilterChainProxy') + } +} + +class AnnotationConfigPluginBeansAotSpec extends AbstractPluginBeansAotSpec { + + protected void configure(Object securityConfig) { + securityConfig.securityConfigType = 'Annotation' + } +} + +class RequestmapPluginBeansAotSpec extends AbstractPluginBeansAotSpec { + + protected void configure(Object securityConfig) { + securityConfig.securityConfigType = 'Requestmap' + } +} + +class InterceptUrlMapPluginBeansAotSpec extends AbstractPluginBeansAotSpec { + + protected void configure(Object securityConfig) { + securityConfig.securityConfigType = 'InterceptUrlMap' + } +} + +class OptionalFiltersPluginBeansAotSpec extends AbstractPluginBeansAotSpec { + + protected void configure(Object securityConfig) { + securityConfig.securityConfigType = 'InterceptUrlMap' + securityConfig.useBasicAuth = true + securityConfig.useSwitchUserFilter = true + securityConfig.useX509 = true + securityConfig.useSecurityEventListener = true + securityConfig.useHttpSessionEventPublisher = true + securityConfig.registerLoggerListener = true + securityConfig.debug = [useFilter: true] + securityConfig.cacheUsers = true + securityConfig.secureChannel = [definition: [[pattern: '/secure/**', access: 'REQUIRES_SECURE_CHANNEL']]] + securityConfig.ipRestrictions = [[pattern: '/admin/**', access: ['127.0.0.1']]] + securityConfig.afterInvocationManagerProviderNames = ['afterInvocationProvider'] + } +} + +class PersistentRememberMePluginBeansAotSpec extends AbstractPluginBeansAotSpec { + + protected void configure(Object securityConfig) { + securityConfig.securityConfigType = 'InterceptUrlMap' + securityConfig.rememberMe = [persistent: true, persistentToken: [domainClassName: 'test.PersistentLogin']] + } +} + +class CustomClassesPluginBeansAotSpec extends AbstractPluginBeansAotSpec { + + protected void configure(Object securityConfig) { + securityConfig.securityConfigType = 'InterceptUrlMap' + securityConfig.passwordEncoderBeanClass = CustomClassesPluginBeansSpec.CustomPasswordEncoder + securityConfig.authenticationManagerBeanClass = CustomClassesPluginBeansSpec.CustomProviderManager + securityConfig.springSecurityFilterChainProxyBeanClass = CustomClassesPluginBeansSpec.CustomFilterChainProxy + } +} + +class CleartextDigestPluginBeansAotSpec extends AbstractPluginBeansAotSpec { + + protected void configure(Object securityConfig) { + securityConfig.securityConfigType = 'InterceptUrlMap' + securityConfig.useDigestAuth = true + securityConfig.digest = [useCleartextPasswords: true] + } +} + +class EncodedDigestPluginBeansAotSpec extends AbstractPluginBeansAotSpec { + + protected void configure(Object securityConfig) { + securityConfig.securityConfigType = 'InterceptUrlMap' + securityConfig.useDigestAuth = true + } +} diff --git a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/aot/SpringSecurityRuntimeHintsSpec.groovy b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/aot/SpringSecurityRuntimeHintsSpec.groovy new file mode 100644 index 00000000000..7fe7c00ea02 --- /dev/null +++ b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/aot/SpringSecurityRuntimeHintsSpec.groovy @@ -0,0 +1,91 @@ +/* + * 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 grails.plugin.springsecurity.aot + +import org.springframework.aot.hint.MemberCategory +import org.springframework.aot.hint.RuntimeHints +import org.springframework.aot.hint.RuntimeHintsRegistrar +import org.springframework.aot.hint.TypeReference +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates +import org.springframework.core.io.support.SpringFactoriesLoader +import org.springframework.security.core.authority.SimpleGrantedAuthority +import spock.lang.Specification + +import grails.plugin.springsecurity.userdetails.GrailsUser + +class SpringSecurityRuntimeHintsSpec extends Specification { + + void 'the default configuration can be created in an image'() { + given: + RuntimeHints hints = new RuntimeHints() + + when: + new SpringSecurityRuntimeHints().registerHints(hints, getClass().classLoader) + + then: 'the script the plugin loads its defaults from is compiled, and so found by name' + getClass().classLoader.loadClass('DefaultSecurityConfig').getConstructor() + RuntimeHintsPredicates.reflection() + .onType(TypeReference.of('DefaultSecurityConfig')) + .withMemberCategory(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS) + .test(hints) + } + + void 'the user cache can be created in an image, which with cacheUsers stores users by value'() { + given: + RuntimeHints hints = new RuntimeHints() + + when: + new SpringSecurityRuntimeHints().registerHints(hints, getClass().classLoader) + + then: 'Ehcache creates the copier for the key reflectively' + getClass().classLoader.loadClass('org.ehcache.jsr107.Eh107IdentityCopier').getConstructor() + RuntimeHintsPredicates.reflection() + .onType(TypeReference.of('org.ehcache.jsr107.Eh107IdentityCopier')) + .withMemberCategory(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS) + .test(hints) + } + + void 'every class a cached user is copied with is registered for serialization'() { + given: 'a user as the plugin loads one, with a role and a GORM id' + RuntimeHints hints = new RuntimeHints() + new SpringSecurityRuntimeHints().registerHints(hints, getClass().classLoader) + GrailsUser user = new GrailsUser('user', 'stored', true, true, true, true, + [new SimpleGrantedAuthority('ROLE_USER')], 42L) + + when: 'it is serialized, as the user cache copies it, noting each class written' + Set> written = new LinkedHashSet<>() + new ObjectOutputStream(new ByteArrayOutputStream()) { + @Override + protected void annotateClass(Class type) { + written << type + } + }.withCloseable { it.writeObject(user) } + + then: 'a change in what Spring Security writes fails here, rather than in an image' + written.containsAll([GrailsUser, Long, SimpleGrantedAuthority]) + written.findAll { Class type -> !RuntimeHintsPredicates.reflection().onJavaSerialization(type, true).test(hints) } == [] as Set + } + + void 'ahead-of-time processing finds the hints'() { + expect: + SpringFactoriesLoader.forResourceLocation('META-INF/spring/aot.factories') + .load(RuntimeHintsRegistrar)*.getClass() + .contains(SpringSecurityRuntimeHints) + } +} diff --git a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/cache/SpringUserCacheFactoryBeanSpec.groovy b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/cache/SpringUserCacheFactoryBeanSpec.groovy new file mode 100644 index 00000000000..e1251a1f797 --- /dev/null +++ b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/cache/SpringUserCacheFactoryBeanSpec.groovy @@ -0,0 +1,125 @@ +/* + * 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 grails.plugin.springsecurity.cache + +import javax.cache.Cache +import javax.cache.CacheManager +import javax.cache.Caching + +import org.springframework.cache.jcache.JCacheCacheManager +import org.springframework.security.core.authority.SimpleGrantedAuthority +import org.springframework.security.core.userdetails.User +import org.springframework.security.core.userdetails.UserCache +import spock.lang.Specification + +import grails.plugin.springsecurity.userdetails.GrailsUser + +class SpringUserCacheFactoryBeanSpec extends Specification { + + CacheManager jcache = Caching.cachingProvider.cacheManager + + String cacheName = "userCache-${System.nanoTime()}" + + UserCache cache = new SpringUserCacheFactoryBean(cacheManager: new JCacheCacheManager(jcache), cacheName: cacheName) + .tap { afterPropertiesSet() } + .object + + void cleanup() { + jcache.destroyCache(cacheName) + } + + void 'erasing the user a lookup returns leaves the cached one with its password'() { + given: + cache.putUserInCache(new GrailsUser('user', 'stored', true, true, true, true, + [new SimpleGrantedAuthority('ROLE_USER')], 42L)) + + when: 'what the authentication manager does to the user it signs in' + User signedIn = (User) cache.getUserFromCache('user') + signedIn.eraseCredentials() + + then: + signedIn.password == null + cache.getUserFromCache('user').password == 'stored' + + and: 'the copy is of the class that was cached, with what it carries' + GrailsUser copy = (GrailsUser) cache.getUserFromCache('user') + copy.id == 42L + copy.authorities*.authority == ['ROLE_USER'] + !copy.is(cache.getUserFromCache('user')) + } + + void 'a user that cannot be copied is left out of the cache rather than failing the sign-in'() { + when: + cache.putUserInCache(new HoldsSomethingUnserializable('user', 'stored')) + + then: + noExceptionThrown() + cache.getUserFromCache('user') == null + } + + void 'a user an image cannot copy is left out of the cache rather than failing the sign-in'() { + when: 'a native image without serialization metadata for a class throws an error rather than an exception' + cache.putUserInCache(new CannotBeCopiedInAnImage('user', 'stored')) + + then: + noExceptionThrown() + cache.getUserFromCache('user') == null + } + + void 'what the users are stored in can be read, as the Spring Security UI plugin shows its contents'() { + given: + cache.putUserInCache(new User('user', 'stored', [])) + + expect: 'read as a property, as the UI plugin\'s controller reads it' + def nativeCache = cache.cache.nativeCache + nativeCache instanceof Cache + ((Cache) nativeCache).iterator()*.key == ['user'] + } + + void 'a user that is removed is no longer cached'() { + given: + cache.putUserInCache(new User('user', 'stored', [])) + + when: + cache.removeUserFromCache('user') + + then: + cache.getUserFromCache('user') == null + } + + static class CannotBeCopiedInAnImage extends User { + + CannotBeCopiedInAnImage(String username, String password) { + super(username, password, []) + } + + private void writeObject(ObjectOutputStream out) { + throw new LinkageError('no serialization metadata in this image') + } + } + + static class HoldsSomethingUnserializable extends User { + + final Object held = new Object() + + HoldsSomethingUnserializable(String username, String password) { + super(username, password, []) + } + } +} diff --git a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/web/SavedRequestMatcherSpec.groovy b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/web/SavedRequestMatcherSpec.groovy new file mode 100644 index 00000000000..feb27f59a7a --- /dev/null +++ b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/web/SavedRequestMatcherSpec.groovy @@ -0,0 +1,94 @@ +/* + * 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 grails.plugin.springsecurity.web + +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.security.web.savedrequest.HttpSessionRequestCache +import spock.lang.Specification + +class SavedRequestMatcherSpec extends Specification { + + private static final List DEFAULT_PATTERNS = new ConfigSlurper() + .parse(new GroovyClassLoader(SavedRequestMatcherSpec.classLoader).loadClass('DefaultSecurityConfig')) + .security.requestCache.ignoredPatterns as List + + void 'the request cache does not save #path, with the default patterns'() { + expect: + savedRequest(path, new SavedRequestMatcher(ignoredPatterns: DEFAULT_PATTERNS)) == null + + where: + path << [ + '/favicon.ico', + '/favicon.png', + '/apple-touch-icon.png', + '/apple-touch-icon-precomposed.png', + '/apple-touch-icon-152x152.png', + '/manifest.json', + '/manifest.webmanifest', + '/browserconfig.xml', + '/.well-known/appspecific/com.chrome.devtools.json' + ] + } + + void 'the request cache saves #path, with the default patterns'() { + expect: + savedRequest(path, new SavedRequestMatcher(ignoredPatterns: DEFAULT_PATTERNS)).redirectUrl == "http://localhost$path" + + where: + path << ['/', '/book/show/1', '/images/favicon.ico', '/manifest', '/manifest.json/1', '/.well-known/security.txt'] + } + + void 'the patterns are relative to the context path'() { + given: + def request = request('/manifest.json') + request.contextPath = '/app' + request.requestURI = '/app/manifest.json' + + expect: + !new SavedRequestMatcher(ignoredPatterns: ['/manifest.json']).matches(request) + } + + void 'the application names its own patterns'() { + given: + def matcher = new SavedRequestMatcher(ignoredPatterns: ['/poll/**']) + + expect: + savedRequest('/poll/status', matcher) == null + savedRequest('/manifest.json', matcher) != null + } + + void 'no patterns save every request'() { + expect: + savedRequest('/manifest.json', new SavedRequestMatcher(ignoredPatterns: [])) != null + savedRequest('/manifest.json', new SavedRequestMatcher()) != null + } + + private static savedRequest(String path, SavedRequestMatcher matcher) { + def cache = new HttpSessionRequestCache(requestMatcher: matcher, matchingRequestParameterName: null) + def request = request(path) + def response = new MockHttpServletResponse() + cache.saveRequest(request, response) + cache.getRequest(request, response) + } + + private static MockHttpServletRequest request(String path) { + new MockHttpServletRequest('GET', path) + } +} diff --git a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/web/access/intercept/FilterSecurityInterceptorSpec.groovy b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/web/access/intercept/FilterSecurityInterceptorSpec.groovy index 36d8bd48f38..6ca5215768a 100644 --- a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/web/access/intercept/FilterSecurityInterceptorSpec.groovy +++ b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/web/access/intercept/FilterSecurityInterceptorSpec.groovy @@ -23,14 +23,23 @@ import jakarta.servlet.Servlet import org.springframework.http.HttpStatus import org.springframework.mock.web.MockFilterChain +import org.springframework.mock.web.MockHttpServletRequest import org.springframework.security.access.AccessDecisionManager import org.springframework.security.access.AccessDeniedException import org.springframework.security.access.SecurityConfig import org.springframework.security.access.vote.RoleVoter import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException +import org.springframework.security.authentication.AuthenticationManager +import org.springframework.security.authentication.BadCredentialsException +import org.springframework.security.authentication.ProviderManager import org.springframework.security.authentication.UsernamePasswordAuthenticationToken +import org.springframework.security.authentication.dao.DaoAuthenticationProvider +import org.springframework.security.core.Authentication import org.springframework.security.core.authority.AuthorityUtils import org.springframework.security.core.context.SecurityContextHolder +import org.springframework.security.core.userdetails.User +import org.springframework.security.core.userdetails.UserDetails +import org.springframework.security.provisioning.InMemoryUserDetailsManager import org.springframework.security.web.FilterInvocation import org.springframework.security.web.access.ExceptionTranslationFilter import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource @@ -105,6 +114,202 @@ class FilterSecurityInterceptorSpec extends AbstractUnitSpec { 0 * chain._ } + void 'what a filter left unauthenticated is authenticated before the access decision'() { + given: + def left = UsernamePasswordAuthenticationToken.unauthenticated('user', 'stored') + def authenticated = UsernamePasswordAuthenticationToken.authenticated( + 'user', 'stored', AuthorityUtils.createAuthorityList('ROLE_USER')) + SecurityContextHolder.context.authentication = left + def interceptor = interceptorFor(Mock(AuthenticationManager), Mock(AccessDecisionManager)) + FilterChain chain = Mock() + + when: + interceptor.doFilter(request, response, chain) + + then: + 1 * interceptor.authenticationManager.authenticate(left) >> authenticated + + then: + 1 * interceptor.accessDecisionManager.decide(authenticated, _ as FilterInvocation, _) + + then: + 1 * chain.doFilter(request, response) + SecurityContextHolder.context.authentication.is(authenticated) + } + + void 'what the authentication manager refuses never reaches the access decision'() { + given: + SecurityContextHolder.context.authentication = UsernamePasswordAuthenticationToken.unauthenticated('user', 'stored') + def interceptor = interceptorFor( + Stub(AuthenticationManager) { authenticate(_) >> { throw new BadCredentialsException('refused') } }, + Mock(AccessDecisionManager)) + FilterChain chain = Mock() + + when: + interceptor.doFilter(request, response, chain) + + then: + thrown(BadCredentialsException) + 0 * interceptor.accessDecisionManager._ + 0 * chain._ + } + + void 'what is authenticated already is left alone unless alwaysReauthenticate is set'() { + given: + def authentication = UsernamePasswordAuthenticationToken.authenticated( + 'user', 'password', AuthorityUtils.createAuthorityList('ROLE_USER')) + SecurityContextHolder.context.authentication = authentication + def interceptor = interceptorFor(Mock(AuthenticationManager), Mock(AccessDecisionManager)) + interceptor.alwaysReauthenticate = always + + when: + interceptor.doFilter(request, response, Stub(FilterChain)) + + then: + times * interceptor.authenticationManager.authenticate(authentication) >> authentication + + where: + always | times + false | 0 + true | 1 + } + + void 'with alwaysReauthenticate a user whose credentials were erased once signed in reaches a secured page'() { + given: 'a user signed in through an authentication manager that erases the credentials, as it does by default' + def provider = new DaoAuthenticationProvider(new InMemoryUserDetailsManager(user('secret'))) + ProviderManager manager = Spy(constructorArgs: [[provider]]) + Authentication signedIn = manager.authenticate(UsernamePasswordAuthenticationToken.unauthenticated('user', 'secret')) + SecurityContextHolder.context.authentication = signedIn + def interceptor = securedBy(manager) + FilterChain chain = Mock() + + when: + interceptor.doFilter(request, response, chain) + + then: 'what was signed in is used as it is, since it has nothing left to be authenticated with' + 0 * manager.authenticate(_) + 1 * chain.doFilter(request, response) + signedIn.credentials == null + SecurityContextHolder.context.authentication.is(signedIn) + } + + void 'with alwaysReauthenticate a user who still holds their password is authenticated again on every request'() { + given: 'a user signed in through an authentication manager that keeps the credentials' + def users = new InMemoryUserDetailsManager(user('secret')) + def manager = new ProviderManager([new DaoAuthenticationProvider(users)]) + manager.eraseCredentialsAfterAuthentication = false + Authentication signedIn = manager.authenticate(UsernamePasswordAuthenticationToken.unauthenticated('user', 'secret')) + SecurityContextHolder.context.authentication = signedIn + def interceptor = securedBy(manager) + FilterChain chain = Mock() + + when: + interceptor.doFilter(request, response, chain) + + then: + 1 * chain.doFilter(request, response) + !SecurityContextHolder.context.authentication.is(signedIn) + SecurityContextHolder.context.authentication.authenticated + + when: 'the password is changed before the next request' + users.updateUser(user('changed')) + def next = new MockHttpServletRequest('GET', '/secure') + interceptor.doFilter(next, response, chain) + + then: + thrown(BadCredentialsException) + 0 * chain._ + } + + void 'what a filter left unauthenticated is authenticated when alwaysReauthenticate is #always'() { + given: + def manager = new ProviderManager([new DaoAuthenticationProvider(new InMemoryUserDetailsManager(user('secret')))]) + SecurityContextHolder.context.authentication = UsernamePasswordAuthenticationToken.unauthenticated('user', 'secret') + def interceptor = securedBy(manager) + interceptor.alwaysReauthenticate = always + FilterChain chain = Mock() + + when: + interceptor.doFilter(request, response, chain) + + then: + 1 * chain.doFilter(request, response) + SecurityContextHolder.context.authentication.authenticated + SecurityContextHolder.context.authentication.name == 'user' + + where: + always << [false, true] + } + + void 'what a filter left unauthenticated without credentials is refused when alwaysReauthenticate is #always'() { + given: + def manager = new ProviderManager([new DaoAuthenticationProvider(new InMemoryUserDetailsManager(user('secret')))]) + SecurityContextHolder.context.authentication = UsernamePasswordAuthenticationToken.unauthenticated('user', null) + def interceptor = securedBy(manager) + interceptor.alwaysReauthenticate = always + FilterChain chain = Mock() + + when: + interceptor.doFilter(request, response, chain) + + then: + thrown(BadCredentialsException) + 0 * chain._ + + where: + always << [false, true] + } + + void 'a request without security attributes authenticates nothing, even with alwaysReauthenticate'() { + given: + def left = UsernamePasswordAuthenticationToken.unauthenticated('user', 'secret') + SecurityContextHolder.context.authentication = left + AuthenticationManager manager = Mock() + def interceptor = new FilterSecurityInterceptor( + authenticationManager: manager, + alwaysReauthenticate: true, + rejectPublicInvocations: false, + securityMetadataSource: Stub(FilterInvocationSecurityMetadataSource) { + getAttributes(_ as FilterInvocation) >> null + } + ) + FilterChain chain = Mock() + + when: + interceptor.doFilter(request, response, chain) + + then: + 0 * manager._ + 1 * chain.doFilter(request, response) + SecurityContextHolder.context.authentication.is(left) + } + + private FilterSecurityInterceptor interceptorFor(AuthenticationManager manager, AccessDecisionManager decisions) { + new FilterSecurityInterceptor( + authenticationManager: manager, + accessDecisionManager: decisions, + securityMetadataSource: Stub(FilterInvocationSecurityMetadataSource) { + getAttributes(_ as FilterInvocation) >> [new SecurityConfig('ROLE_USER')] + } + ) + } + + /** An interceptor that re-authenticates, in front of a URL a user with {@code ROLE_USER} may reach. */ + private FilterSecurityInterceptor securedBy(AuthenticationManager manager) { + new FilterSecurityInterceptor( + authenticationManager: manager, + alwaysReauthenticate: true, + accessDecisionManager: new AuthenticatedVetoableDecisionManager([new WebExpressionVoter(), new RoleVoter()]), + securityMetadataSource: Stub(FilterInvocationSecurityMetadataSource) { + getAttributes(_ as FilterInvocation) >> [new SecurityConfig('ROLE_USER')] + } + ) + } + + private static UserDetails user(String password) { + User.withUsername('user').password("{noop}${password}").roles('USER').build() + } + void 'secured filter chain responds with #status for #scenario'() { given: if (role != null) { diff --git a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/web/filter/StartupGateFilterSpec.groovy b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/web/filter/StartupGateFilterSpec.groovy index b70c99788b8..fffad8daeb5 100644 --- a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/web/filter/StartupGateFilterSpec.groovy +++ b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/web/filter/StartupGateFilterSpec.groovy @@ -45,6 +45,15 @@ class StartupGateFilterSpec extends Specification { chain.request == null } + void 'says the rejection is temporary, so it can be told from a 503 the application gives itself'() { + when: + filter.doFilter(request, response, chain) + + then: + response.status == 503 + response.getHeader('Retry-After') == '1' + } + void 'sets the status instead of sending an error'() { when: filter.doFilter(request, response, chain) @@ -85,6 +94,7 @@ class StartupGateFilterSpec extends Specification { then: filter.open response.status == 200 + !response.containsHeader('Retry-After') chain.request.is(request) chain.response.is(response) } diff --git a/grails-spring-security/rest/core/src/main/groovy/grails/plugin/springsecurity/rest/token/bearer/BearerTokenReader.groovy b/grails-spring-security/rest/core/src/main/groovy/grails/plugin/springsecurity/rest/token/bearer/BearerTokenReader.groovy index 4574a9ee77e..0fae3b0d44d 100644 --- a/grails-spring-security/rest/core/src/main/groovy/grails/plugin/springsecurity/rest/token/bearer/BearerTokenReader.groovy +++ b/grails-spring-security/rest/core/src/main/groovy/grails/plugin/springsecurity/rest/token/bearer/BearerTokenReader.groovy @@ -22,6 +22,7 @@ import groovy.util.logging.Slf4j import jakarta.servlet.http.HttpServletRequest +import org.springframework.http.InvalidMediaTypeException import org.springframework.http.MediaType import grails.plugin.springsecurity.rest.token.AccessToken @@ -64,6 +65,16 @@ class BearerTokenReader implements TokenReader { } private boolean isFormEncoded(HttpServletRequest servletRequest) { - servletRequest.contentType && MediaType.parseMediaType(servletRequest.contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED) + String contentType = servletRequest.contentType + if (!contentType) { + return false + } + try { + // Only the type matters here. Spring rejects a parameter it cannot read, and a client decides what it sends. + return MediaType.parseMediaType(contentType.split(';', 2)[0]).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED) + } + catch (InvalidMediaTypeException ignored) { + return false + } } } diff --git a/grails-spring-security/rest/core/src/test/groovy/grails/plugin/springsecurity/rest/rfc6750/BearerTokenReaderSpec.groovy b/grails-spring-security/rest/core/src/test/groovy/grails/plugin/springsecurity/rest/rfc6750/BearerTokenReaderSpec.groovy index 97366d37323..480af9616c2 100644 --- a/grails-spring-security/rest/core/src/test/groovy/grails/plugin/springsecurity/rest/rfc6750/BearerTokenReaderSpec.groovy +++ b/grails-spring-security/rest/core/src/test/groovy/grails/plugin/springsecurity/rest/rfc6750/BearerTokenReaderSpec.groovy @@ -155,4 +155,45 @@ class BearerTokenReaderSpec extends Specification { notThrown(Throwable) } + @Unroll + def "a token in a form body is read whatever parameter the client sent with its content type: #contentType"() { + given: + request.method = 'POST' + request.contentType = contentType + request.addParameter('access_token', 'mytesttokenvalue') + + expect: + tokenReader.findToken(request) == new AccessToken('mytesttokenvalue') + + where: + contentType << [ + 'application/x-www-form-urlencoded;foo', + 'application/x-www-form-urlencoded;charset=UTF-8;CHARSET=ISO-8859-1' + ] + } + + @Unroll + def "a content type that cannot be read is not a form, and does not fail the request: #contentType"() { + given: + request.method = 'POST' + request.contentType = contentType + request.addParameter('access_token', 'mytesttokenvalue') + + expect: + tokenReader.findToken(request) == null + + where: + contentType << ['not a media type', 'text/plain;foo'] + } + + def "a token in the query string is still read when the content type cannot be"() { + given: + request.method = 'POST' + request.contentType = 'not a media type' + request.queryString = 'access_token=mytesttokenvalue' + request.addParameter('access_token', 'mytesttokenvalue') + + expect: + tokenReader.findToken(request) == new AccessToken('mytesttokenvalue') + } } diff --git a/grails-test-core/src/main/groovy/org/grails/plugins/testing/GrailsMockHttpServletRequest.groovy b/grails-test-core/src/main/groovy/org/grails/plugins/testing/GrailsMockHttpServletRequest.groovy index a81842b7d17..14e8600ac9c 100644 --- a/grails-test-core/src/main/groovy/org/grails/plugins/testing/GrailsMockHttpServletRequest.groovy +++ b/grails-test-core/src/main/groovy/org/grails/plugins/testing/GrailsMockHttpServletRequest.groovy @@ -515,7 +515,7 @@ class GrailsMockHttpServletRequest extends MockHttpServletRequest implements Mul /** * Simulates Spring's {@code FormContentFilter}: parses an {@code application/x-www-form-urlencoded} - * body of a {@code PUT}, {@code PATCH} or {@code DELETE} request into request parameters, so a mock + * body of a {@code PUT}, {@code PATCH}, {@code DELETE} or {@code QUERY} request into request parameters, so a mock * request reflects what application code sees after that filter has run in a real deployment. Runs * once, the first time a non-empty form body is observed on parameter access. */ @@ -524,7 +524,7 @@ class GrailsMockHttpServletRequest extends MockHttpServletRequest implements Mul return } String httpMethod = getMethod() - if (httpMethod != 'PUT' && httpMethod != 'PATCH' && httpMethod != 'DELETE') { + if (!(httpMethod in ['PUT', 'PATCH', 'DELETE', 'QUERY'])) { return } String currentContentType = getContentType() diff --git a/grails-test-core/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletRequestFormBodySpec.groovy b/grails-test-core/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletRequestFormBodySpec.groovy new file mode 100644 index 00000000000..1f79d769683 --- /dev/null +++ b/grails-test-core/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletRequestFormBodySpec.groovy @@ -0,0 +1,79 @@ +/* + * 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.plugins.testing + +import jakarta.servlet.FilterChain + +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.web.filter.FormContentFilter +import spock.lang.Specification + +class GrailsMockHttpServletRequestFormBodySpec extends Specification { + + private static final String FORM = 'application/x-www-form-urlencoded' + + void 'a form-encoded #method body is #outcome, as Spring FormContentFilter treats it'() { + given: + def request = new GrailsMockHttpServletRequest() + request.method = method + request.contentType = FORM + request.content = 'title=Grails'.bytes + + expect: + request.getParameter('title') == title + + and: 'the filter this request stands in for agrees' + filteredTitle(method) == title + + where: + method | title + 'PUT' | 'Grails' + 'PATCH' | 'Grails' + 'DELETE' | 'Grails' + 'QUERY' | 'Grails' + 'POST' | null + 'GET' | null + + outcome = title ? 'parsed' : 'left unparsed' + } + + void 'a QUERY body of another content type is left unparsed'() { + given: + def request = new GrailsMockHttpServletRequest() + request.method = 'QUERY' + request.contentType = 'application/json' + request.content = '{"title":"Grails"}'.bytes + + expect: + request.getParameter('title') == null + new String(request.contentAsByteArray) == '{"title":"Grails"}' + } + + private static String filteredTitle(String method) { + def request = new MockHttpServletRequest(method, '/') + request.contentType = FORM + request.content = 'title=Grails'.bytes + String title = null + new FormContentFilter().doFilter(request, new MockHttpServletResponse(), { req, res -> + title = req.getParameter('title') + } as FilterChain) + title + } +} diff --git a/grails-test-examples/app1/grails-app/controllers/functionaltests/HttpQueryController.groovy b/grails-test-examples/app1/grails-app/controllers/functionaltests/HttpQueryController.groovy new file mode 100644 index 00000000000..b339535d48d --- /dev/null +++ b/grails-test-examples/app1/grails-app/controllers/functionaltests/HttpQueryController.groovy @@ -0,0 +1,43 @@ +/* + * 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 functionaltests + +import grails.converters.JSON + +class HttpQueryController { + + static allowedMethods = [restricted: 'QUERY'] + + def books(BookCriteria criteria) { + render([method: request.method, title: criteria.title, pages: criteria.pages] as JSON) + } + + def restricted() { + render 'restricted' + } + + def lookup(Book book) { + render([id: book?.id, title: book?.title, changed: book?.dirty ?: false] as JSON) + } +} + +class BookCriteria { + String title + Integer pages +} diff --git a/grails-test-examples/app1/grails-app/controllers/functionaltests/UrlMappings.groovy b/grails-test-examples/app1/grails-app/controllers/functionaltests/UrlMappings.groovy index 56115b5afc7..41431269397 100644 --- a/grails-test-examples/app1/grails-app/controllers/functionaltests/UrlMappings.groovy +++ b/grails-test-examples/app1/grails-app/controllers/functionaltests/UrlMappings.groovy @@ -34,6 +34,10 @@ class UrlMappings { // See RequestPathSpec.groovy - one request through filter chain, dispatcher, mapping and controller "/request-path"(resources: 'requestPath') + // See HttpQueryFunctionalSpec.groovy + query "/http-query/books"(controller: 'httpQuery', action: 'books') + query "/http-query/lookup/$id"(controller: 'httpQuery', action: 'lookup') + "/alphaDemo"(controller: 'demo', action: 'doit', namespace: 'alpha') "/betaDemo"(controller: 'demo', action: 'doit', namespace: 'beta') diff --git a/grails-test-examples/app1/src/integration-test/groovy/functionaltests/FormContentFunctionalSpec.groovy b/grails-test-examples/app1/src/integration-test/groovy/functionaltests/FormContentFunctionalSpec.groovy index 5c146e01fcb..c94afa69a12 100644 --- a/grails-test-examples/app1/src/integration-test/groovy/functionaltests/FormContentFunctionalSpec.groovy +++ b/grails-test-examples/app1/src/integration-test/groovy/functionaltests/FormContentFunctionalSpec.groovy @@ -57,7 +57,7 @@ class FormContentFunctionalSpec extends Specification implements HttpClientSuppo json.pages == '1153' where: - httpMethod << ['PUT', 'PATCH', 'DELETE'] + httpMethod << ['PUT', 'PATCH', 'DELETE', 'QUERY'] } void 'a form-encoded POST body is parsed into request parameters'() { diff --git a/grails-test-examples/app1/src/integration-test/groovy/functionaltests/HttpQueryFunctionalSpec.groovy b/grails-test-examples/app1/src/integration-test/groovy/functionaltests/HttpQueryFunctionalSpec.groovy new file mode 100644 index 00000000000..1daba4f0b16 --- /dev/null +++ b/grails-test-examples/app1/src/integration-test/groovy/functionaltests/HttpQueryFunctionalSpec.groovy @@ -0,0 +1,101 @@ +/* + * 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 functionaltests + +import java.net.http.HttpRequest + +import org.junit.jupiter.api.Tag +import spock.lang.Specification + +import grails.testing.mixin.integration.Integration +import org.apache.grails.testing.http.client.HttpClientSupport +import org.apache.grails.testing.http.client.TestHttpResponse + +@Integration +@Tag('http-client') +class HttpQueryFunctionalSpec extends Specification implements HttpClientSupport { + + private static final String JSON_TYPE = 'application/json' + private static final String FORM_TYPE = 'application/x-www-form-urlencoded' + + void 'the query prefix maps a URL to the QUERY method, and a JSON body is bound to the command object'() { + when: + def response = send('QUERY', '/http-query/books', '{"title":"The Stand","pages":1153}', JSON_TYPE) + + then: + response.assertStatus(200) + with(response.json()) { + method == 'QUERY' + title == 'The Stand' + pages == 1153 + } + } + + void 'a form-encoded body is bound to the command object'() { + when: + def response = send('QUERY', '/http-query/books', 'title=The%20Stand&pages=1153', FORM_TYPE) + + then: + response.assertStatus(200) + with(response.json()) { + method == 'QUERY' + title == 'The Stand' + pages == 1153 + } + } + + void 'a mapping for the QUERY method does not match another method'() { + when: + def response = send('POST', '/http-query/books', '{"title":"The Stand"}', JSON_TYPE) + + then: + response.statusCode() == 404 + } + + void 'allowedMethods restricts an action to the QUERY method'() { + expect: + send('QUERY', '/httpQuery/restricted', '{}', JSON_TYPE).statusCode() == 200 + send('POST', '/httpQuery/restricted', '{}', JSON_TYPE).statusCode() == 405 + } + + void 'a domain class is looked up by its identifier and not bound to the body'() { + given: + Long id = Book.withNewTransaction { new Book(title: 'Original').save(failOnError: true, flush: true).id } + + when: + def response = send('QUERY', "/http-query/lookup/$id", '{"title":"Changed"}', JSON_TYPE) + + then: + response.assertStatus(200) + with(response.json()) { + title == 'Original' + changed == false + } + + cleanup: + Book.withNewTransaction { Book.get(id)?.delete(flush: true) } + } + + private TestHttpResponse send(String httpMethod, String path, String body, String contentType) { + sendHttpRequest(newHttpRequestWith(path) { + header('Content-Type', contentType) + method(httpMethod, HttpRequest.BodyPublishers.ofString(body)) + }) + } +} diff --git a/grails-test-examples/enable-mvc-check/src/integration-test/groovy/enablemvccheck/WebMvcDefaultsFunctionalSpec.groovy b/grails-test-examples/enable-mvc-check/src/integration-test/groovy/enablemvccheck/WebMvcDefaultsFunctionalSpec.groovy index 2ab72472e72..a51190b1090 100644 --- a/grails-test-examples/enable-mvc-check/src/integration-test/groovy/enablemvccheck/WebMvcDefaultsFunctionalSpec.groovy +++ b/grails-test-examples/enable-mvc-check/src/integration-test/groovy/enablemvccheck/WebMvcDefaultsFunctionalSpec.groovy @@ -71,7 +71,7 @@ class WebMvcDefaultsFunctionalSpec extends Specification implements HttpClientSu json.pages == '1153' where: - httpMethod << ['PUT', 'PATCH', 'DELETE'] + httpMethod << ['PUT', 'PATCH', 'DELETE', 'QUERY'] } void 'a form-encoded POST body is parsed into request parameters'() { diff --git a/grails-test-examples/hibernate7/spring-json-respond/grails-app/conf/application.yml b/grails-test-examples/hibernate7/spring-json-respond/grails-app/conf/application.yml index 7a2e02a9d88..b71378e43fa 100644 --- a/grails-test-examples/hibernate7/spring-json-respond/grails-app/conf/application.yml +++ b/grails-test-examples/hibernate7/spring-json-respond/grails-app/conf/application.yml @@ -106,6 +106,12 @@ grails: --- # The settings whose defaults later releases change, as an application that opts into them sets them grails: + mime: + disable: + accept: + header: + userAgents: + - WebKit web: rendering: json: @@ -118,6 +124,7 @@ grails: jackson: true exceptionresolver: springHandlers: true + problemDetails: true controllers: responseBody: true spring: diff --git a/grails-test-examples/hibernate7/spring-json-respond/grails-app/controllers/UrlMappings.groovy b/grails-test-examples/hibernate7/spring-json-respond/grails-app/controllers/UrlMappings.groovy index 2dc628cc93a..74358688fb3 100644 --- a/grails-test-examples/hibernate7/spring-json-respond/grails-app/controllers/UrlMappings.groovy +++ b/grails-test-examples/hibernate7/spring-json-respond/grails-app/controllers/UrlMappings.groovy @@ -17,9 +17,13 @@ * under the License. */ +import springjson.BookArchivedException + class UrlMappings { static mappings = { "/$controller/$action?/$id?(.$format)?"() + "500"(controller: 'errors', action: 'serverError') + "500"(controller: 'errors', action: 'archived', exception: BookArchivedException) } } diff --git a/grails-test-examples/hibernate7/spring-json-respond/grails-app/controllers/springjson/BookController.groovy b/grails-test-examples/hibernate7/spring-json-respond/grails-app/controllers/springjson/BookController.groovy index 1546ae7cd4d..2462dfa91d5 100644 --- a/grails-test-examples/hibernate7/spring-json-respond/grails-app/controllers/springjson/BookController.groovy +++ b/grails-test-examples/hibernate7/spring-json-respond/grails-app/controllers/springjson/BookController.groovy @@ -32,6 +32,7 @@ import org.springframework.web.ErrorResponseException import org.springframework.web.bind.MissingServletRequestParameterException import org.springframework.web.bind.annotation.ResponseBody import org.springframework.web.bind.annotation.ResponseStatus +import org.springframework.web.server.ResponseStatusException import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody import grails.gorm.transactions.Transactional @@ -75,6 +76,14 @@ class BookController { throw new IllegalStateException('Not handled by any exception handler') } + def gone() { + throw new ResponseStatusException(HttpStatus.GONE, 'The book was withdrawn') + } + + def archived() { + throw new BookArchivedException('Handled by the URL mapping that names it') + } + def search() { throw new MissingServletRequestParameterException('title', 'String') } diff --git a/grails-test-examples/hibernate7/spring-json-respond/grails-app/controllers/springjson/ErrorsController.groovy b/grails-test-examples/hibernate7/spring-json-respond/grails-app/controllers/springjson/ErrorsController.groovy new file mode 100644 index 00000000000..16d20fffcdd --- /dev/null +++ b/grails-test-examples/hibernate7/spring-json-respond/grails-app/controllers/springjson/ErrorsController.groovy @@ -0,0 +1,30 @@ +/* + * 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 springjson + +class ErrorsController { + + def serverError() { + render status: 500, text: 'the error page' + } + + def archived() { + render status: 500, text: 'the mapping for an archived book' + } +} diff --git a/grails-test-examples/hibernate7/spring-json-respond/src/integration-test/groovy/springjson/SpringJsonRespondSpec.groovy b/grails-test-examples/hibernate7/spring-json-respond/src/integration-test/groovy/springjson/SpringJsonRespondSpec.groovy index 1123523a0de..8665c841507 100644 --- a/grails-test-examples/hibernate7/spring-json-respond/src/integration-test/groovy/springjson/SpringJsonRespondSpec.groovy +++ b/grails-test-examples/hibernate7/spring-json-respond/src/integration-test/groovy/springjson/SpringJsonRespondSpec.groovy @@ -30,7 +30,8 @@ import org.apache.grails.testing.http.client.HttpClientSupport * grails.databinding.json.jackson, the defaults later releases change to, as application.yml sets them: respond writes JSON * through Spring MVC's converter with the Grails mapper, binds JSON bodies with Jackson, and Spring MVC writes the * problems it responds with, with the application's advice. With grails.exceptionresolver.springHandlers, the exception - * handlers of the application and of spring.mvc.problemdetails.enabled answer the exceptions an action throws. What an + * handlers of the application and of spring.mvc.problemdetails.enabled answer the exceptions an action throws, and with + * grails.exceptionresolver.problemDetails, a problem answers one they do not, unless the error page is for it. What an * action returns, where Spring MVC writes what a handler method returns as the response, such as a ResponseEntity, Spring * MVC writes, and with grails.controllers.responseBody, so is the value of an action annotated @ResponseBody. */ @@ -38,6 +39,10 @@ import org.apache.grails.testing.http.client.HttpClientSupport @Tag('http-client') class SpringJsonRespondSpec extends Specification implements HttpClientSupport { + private static final String HTML = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' + + private static final String BROWSER = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0 Safari/537.36' + Long bookId Long authorId @@ -118,7 +123,7 @@ class SpringJsonRespondSpec extends Specification implements HttpClientSupport { response.assertStatus(409) response.contentType == 'application/problem+json' response.json().detail == 'The title is taken' - response.json().instance == '/book/conflict' + !response.json().containsKey('instance') response.json().advisedBy == 'ProblemAdvice' } @@ -163,12 +168,67 @@ class SpringJsonRespondSpec extends Specification implements HttpClientSupport { response.json().bookId == 42 } - void 'an exception no exception handler answers is resolved as without the setting'() { + void 'an exception no exception handler answers is written as a problem that does not expose its message to #client'() { + when: + def response = http(headers, '/book/broken') + + then: + response.assertStatus(500) + response.contentType == 'application/problem+json' + response.headerValue('X-Advised-By') == 'ProblemAdvice' + response.json().status == 500 + response.json().title == 'Internal Server Error' + response.json().advisedBy == 'ProblemAdvice' + !response.body().contains('Not handled by any exception handler') + + where: + client | headers + 'a client without an Accept header' | [:] + 'a client that accepts any type' | [Accept: '*/*'] + 'a client that asks for JSON' | [Accept: 'application/json'] + 'a browser that asks for JSON' | [Accept: 'application/json', 'User-Agent': BROWSER] + } + + void 'an action named with the json extension is answered with a problem, whatever the Accept header prefers'() { + when: + def response = http([Accept: HTML], '/book/broken.json') + + then: + response.assertStatus(500) + response.contentType == 'application/problem+json' + } + + void 'the status and detail of a ResponseStatusException are those of the problem'() { + when: + def response = http('/book/gone') + + then: + response.assertStatus(410) + response.contentType == 'application/problem+json' + response.json().detail == 'The book was withdrawn' + } + + void 'an exception no exception handler answers renders the error page for #client'() { + when: + def response = http(headers, '/book/broken') + + then: + response.assertStatus(500) + response.body() == 'the error page' + + where: + client | headers + 'a client that asks for HTML' | [Accept: 'text/html'] + 'a browser' | [Accept: HTML, 'User-Agent': BROWSER] + } + + void 'an exception a URL mapping names is answered by that mapping, whatever the client asks for'() { when: - def response = http('/book/broken') + def response = http([Accept: 'application/json'], '/book/archived') then: response.assertStatus(500) + response.body() == 'the mapping for an archived book' } void 'a ResponseEntity an action returns is written by Spring MVC, with the advice of the application'() { diff --git a/grails-test-examples/hibernate7/spring-json-respond/src/main/groovy/springjson/BookArchivedException.groovy b/grails-test-examples/hibernate7/spring-json-respond/src/main/groovy/springjson/BookArchivedException.groovy new file mode 100644 index 00000000000..fb34dcc8e2e --- /dev/null +++ b/grails-test-examples/hibernate7/spring-json-respond/src/main/groovy/springjson/BookArchivedException.groovy @@ -0,0 +1,26 @@ +/* + * 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 springjson + +class BookArchivedException extends RuntimeException { + + BookArchivedException(String message) { + super(message) + } +} diff --git a/grails-test-examples/spring-security/core/functional-test-app/grails-app/controllers/com/testapp/TestFormParamsController.groovy b/grails-test-examples/spring-security/core/functional-test-app/grails-app/controllers/com/testapp/TestFormParamsController.groovy index 94ec7346f8d..215d07b7fed 100644 --- a/grails-test-examples/spring-security/core/functional-test-app/grails-app/controllers/com/testapp/TestFormParamsController.groovy +++ b/grails-test-examples/spring-security/core/functional-test-app/grails-app/controllers/com/testapp/TestFormParamsController.groovy @@ -23,13 +23,13 @@ import grails.plugin.springsecurity.annotation.Secured import grails.validation.Validateable /** - * This controller is used to verify that form parameters on PUT and PATCH requests are available + * This controller is used to verify that form parameters on PUT, PATCH and QUERY requests are available */ class TestFormParamsController { static allowedMethods = [ - permitAll : ["PUT", "PATCH"], - permitAdmin: ["PUT", "PATCH"] + permitAll : ["PUT", "PATCH", "QUERY"], + permitAdmin: ["PUT", "PATCH", "QUERY"] ] @Secured(['permitAll']) diff --git a/grails-test-examples/spring-security/core/functional-test-app/src/integration-test/groovy/specs/SavedRequestSpec.groovy b/grails-test-examples/spring-security/core/functional-test-app/src/integration-test/groovy/specs/SavedRequestSpec.groovy new file mode 100644 index 00000000000..b70dc3d393c --- /dev/null +++ b/grails-test-examples/spring-security/core/functional-test-app/src/integration-test/groovy/specs/SavedRequestSpec.groovy @@ -0,0 +1,95 @@ +/* + * 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 specs + +import java.net.http.HttpClient + +import org.apache.grails.testing.http.client.HttpClientSupport +import org.apache.grails.testing.http.client.TestHttpResponse +import org.springframework.beans.factory.annotation.Autowired +import spock.lang.IgnoreIf +import spock.lang.Shared +import spock.lang.Specification + +import com.testapp.TestDataService +import grails.testing.mixin.integration.Integration + +/** + * A login redirects to the page whose request was denied, but not to a request the browser sent on its own. It runs in + * a configuration that adds no other specification's data to the roles and users the browser specifications count. + */ +@IgnoreIf({ System.getProperty('TESTCONFIG') != 'oncePerRequest' }) +@Integration(applicationClass = functional.test.app.Application) +class SavedRequestSpec extends Specification implements HttpClientSupport { + + private static final String USERNAME = 'saved_request_user' + + @Autowired + TestDataService testDataService + + @Shared + private boolean userAdded + + void setup() { + if (!userAdded) { + testDataService.addTestUser(USERNAME) + userAdded = true + } + } + + void 'a login redirects to the page whose request was denied'() { + when: + def response = loginAfterRequestOf('/secureAnnotated') + + then: + response.statusCode() == 302 + response.headerValue('Location').contains('/secureAnnotated') + } + + void 'a login does not redirect to #path, which a browser requests on its own'() { + when: + def response = loginAfterRequestOf(path) + + then: + response.statusCode() == 302 + response.headerValue('Location').endsWith('/') + !response.headerValue('Location').contains(path) + + where: + path << [ + '/apple-touch-icon.png', + '/apple-touch-icon-152x152.png', + '/manifest.json', + '/manifest.webmanifest', + '/browserconfig.xml', + '/.well-known/appspecific/com.chrome.devtools.json' + ] + } + + private TestHttpResponse loginAfterRequestOf(String path) { + def client = newHttpClientWith { + followRedirects(HttpClient.Redirect.NEVER) + cookieHandler(new CookieManager()) + } + def denied = http([:], path, client) + assert denied.statusCode() == 302 + assert denied.headerValue('Location').endsWith('/login/auth') + httpPostForm([:], '/login/authenticate', [username: USERNAME, password: 'password'], client) + } +} diff --git a/grails-test-examples/spring-security/core/functional-test-app/src/integration-test/groovy/specs/TestFormParamsControllerSpec.groovy b/grails-test-examples/spring-security/core/functional-test-app/src/integration-test/groovy/specs/TestFormParamsControllerSpec.groovy index 0113b5c6174..906f4addbc3 100644 --- a/grails-test-examples/spring-security/core/functional-test-app/src/integration-test/groovy/specs/TestFormParamsControllerSpec.groovy +++ b/grails-test-examples/spring-security/core/functional-test-app/src/integration-test/groovy/specs/TestFormParamsControllerSpec.groovy @@ -127,6 +127,15 @@ class TestFormParamsControllerSpec extends Specification implements HttpClientSu response.body() == "username: ${USERNAME}, password: ${PASSWORD}" } + void 'QUERY request with parameters as x-www-form-urlencoded'() { + when: "A QUERY request with form params is made" + TestHttpResponse response = exchange('QUERY', '/testFormParams/permitAll', "username=${USERNAME}&password=${PASSWORD}", FORM) + + then: "the firewall lets it through, and the controller responds with the correct status and parameters are extracted" + response.statusCode() == 200 + response.body() == "username: ${USERNAME}, password: ${PASSWORD}" + } + void 'PUT request to secured endpoint with parameters as x-www-form-urlencoded'() { when: "A PUT request with form params is made to a secured endpoint" TestHttpResponse response = exchange('PUT', '/testFormParams/permitAdmin', "username=${USERNAME}&password=${PASSWORD}", FORM) @@ -145,6 +154,15 @@ class TestFormParamsControllerSpec extends Specification implements HttpClientSu response.body() != "username: ${USERNAME}, password: ${PASSWORD}" } + void 'QUERY request to secured endpoint with parameters as x-www-form-urlencoded'() { + when: "A QUERY request with form params is made to a secured endpoint" + TestHttpResponse response = exchange('QUERY', '/testFormParams/permitAdmin', "username=${USERNAME}&password=${PASSWORD}", FORM) + + then: "the request is not processed by the controller (redirected to login)" + response.statusCode() == 200 + response.body() != "username: ${USERNAME}, password: ${PASSWORD}" + } + void 'PATCH request with NULL Content-Type and parameters in the URL'() { when: TestHttpResponse response = exchange('PATCH', "/testFormParams/permitAll?username=${USERNAME}&password=${PASSWORD}", '', null) diff --git a/grails-test-suite-base/src/main/groovy/org/grails/support/MockApplicationContext.java b/grails-test-suite-base/src/main/groovy/org/grails/support/MockApplicationContext.java index d0e7ce29dff..4283b29d075 100644 --- a/grails-test-suite-base/src/main/groovy/org/grails/support/MockApplicationContext.java +++ b/grails-test-suite-base/src/main/groovy/org/grails/support/MockApplicationContext.java @@ -50,6 +50,7 @@ import org.springframework.context.MessageSource; import org.springframework.context.MessageSourceResolvable; import org.springframework.context.NoSuchMessageException; +import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ResolvableType; import org.springframework.core.env.Environment; import org.springframework.core.io.AbstractResource; @@ -281,6 +282,12 @@ public T getBean(String name, Class requiredType) throws BeansException { return (T) beans.get(name); } + @Override + @SuppressWarnings("unchecked") + public T getBean(String name, ParameterizedTypeReference requiredType) throws BeansException { + return (T) getBean(name, ResolvableType.forType(requiredType.getType()).toClass()); + } + public T getBean(Class tClass) throws BeansException { final Map map = getBeansOfType(tClass); if (map.isEmpty()) { @@ -513,7 +520,7 @@ public T getObject() throws BeansException { } @Override - public ObjectProvider getBeanProvider(org.springframework.core.ParameterizedTypeReference requiredType) { + public ObjectProvider getBeanProvider(ParameterizedTypeReference requiredType) { return getBeanProvider(ResolvableType.forType(requiredType.getType())); } } diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/support/MockApplicationContextSpec.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/support/MockApplicationContextSpec.groovy new file mode 100644 index 00000000000..9cb1532a000 --- /dev/null +++ b/grails-test-suite-uber/src/test/groovy/org/grails/support/MockApplicationContextSpec.groovy @@ -0,0 +1,62 @@ +/* + * 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.support + +import org.springframework.beans.factory.NoSuchBeanDefinitionException +import org.springframework.core.ParameterizedTypeReference +import spock.lang.Specification + +class MockApplicationContextSpec extends Specification { + + void 'getBean with a ParameterizedTypeReference returns the named bean'() { + given: + def context = new MockApplicationContext() + def names = ['one', 'two'] + context.registerMockBean('names', names) + + when: + List bean = context.getBean('names', new ParameterizedTypeReference>() {}) + + then: + bean.is(names) + } + + void 'getBean with a ParameterizedTypeReference rejects a bean of another type'() { + given: + def context = new MockApplicationContext() + context.registerMockBean('names', 'not a list') + + when: + context.getBean('names', new ParameterizedTypeReference>() {}) + + then: + thrown(NoSuchBeanDefinitionException) + } + + void 'getBean with a ParameterizedTypeReference rejects an unknown bean name'() { + given: + def context = new MockApplicationContext() + + when: + context.getBean('missing', new ParameterizedTypeReference>() {}) + + then: + thrown(NoSuchBeanDefinitionException) + } +} diff --git a/grails-test-suite-web/src/test/groovy/grails/rest/web/ControllerJsonSerializationSpec.groovy b/grails-test-suite-web/src/test/groovy/grails/rest/web/ControllerJsonSerializationSpec.groovy index d0ad23cf58d..eae9e9edb42 100644 --- a/grails-test-suite-web/src/test/groovy/grails/rest/web/ControllerJsonSerializationSpec.groovy +++ b/grails-test-suite-web/src/test/groovy/grails/rest/web/ControllerJsonSerializationSpec.groovy @@ -315,7 +315,7 @@ class ControllerJsonSerializationSpec extends Specification implements Controlle response.status == 422 response.contentType == 'application/problem+json' response.json.status == 422 - response.json.instance == '/books' + !response.json.containsKey('instance') } void 'a status respond is given decides the status of a problem response, and the problem keeps its own'() { @@ -369,7 +369,7 @@ class ControllerJsonSerializationSpec extends Specification implements Controlle response.status == 409 response.contentType == 'application/problem+json' response.json.status == 409 - response.json.instance == '/books' + !response.json.containsKey('instance') where: format << ['html', 'csv'] @@ -786,7 +786,7 @@ class ControllerProblemDefaultsSpec extends Specification implements ControllerU response.status == 409 response.contentType == 'application/problem+json' response.json.status == 409 - response.json.instance == '/books' + !response.json.containsKey('instance') where: format << ['json', 'html', 'csv'] @@ -973,7 +973,7 @@ class ControllerProblemPageSpec extends Specification implements ControllerUnitT view == '/errors/problem' model.problem.is(problem) response.status == 409 - problem.instance == URI.create('/books') + problem.instance == null } } diff --git a/grails-test-suite-web/src/test/groovy/grails/rest/web/ControllerProblemSpringMvcSpec.groovy b/grails-test-suite-web/src/test/groovy/grails/rest/web/ControllerProblemSpringMvcSpec.groovy index c92556563c1..f28c86c3ce9 100644 --- a/grails-test-suite-web/src/test/groovy/grails/rest/web/ControllerProblemSpringMvcSpec.groovy +++ b/grails-test-suite-web/src/test/groovy/grails/rest/web/ControllerProblemSpringMvcSpec.groovy @@ -129,7 +129,7 @@ class ControllerProblemSpringMvcSpec extends Specification implements Controller response.contentType == 'application/problem+json' response.getHeader(HttpHeaders.RETRY_AFTER) == '30' response.json.status == 503 - response.json.instance == '/books/1' + !response.json.containsKey('instance') and: 'the interceptors are given the ErrorResponse' response.json.source == 'ErrorResponseException' diff --git a/grails-views-gson/src/test/groovy/grails/plugin/json/renderer/JsonViewJsonRendererSpec.groovy b/grails-views-gson/src/test/groovy/grails/plugin/json/renderer/JsonViewJsonRendererSpec.groovy index 8426b8d4f26..ed84c7fc423 100644 --- a/grails-views-gson/src/test/groovy/grails/plugin/json/renderer/JsonViewJsonRendererSpec.groovy +++ b/grails-views-gson/src/test/groovy/grails/plugin/json/renderer/JsonViewJsonRendererSpec.groovy @@ -93,7 +93,7 @@ class JsonViewJsonRendererSpec extends Specification { 0 * defaultRenderer.render(_, _) modelAndView.view.is(problemView) context.webRequest.currentResponse.status == 422 - problem.instance == URI.create('/books/1') + problem.instance == null } void 'a ProblemDetail rendered with the view respond names, for a client that accepts no JSON type, is sent as JSON'() { diff --git a/grails-web-common/build.gradle b/grails-web-common/build.gradle index cb166e24fb4..30e72b7ed17 100644 --- a/grails-web-common/build.gradle +++ b/grails-web-common/build.gradle @@ -62,6 +62,10 @@ dependencies { testImplementation 'jakarta.servlet:jakarta.servlet-api' testImplementation 'org.springframework:spring-test' + // Test-only: ContentSecurityPolicyNonceSpec reads the nonce Spring Security's own nonce filter + // generates. Not a main/runtime dependency - the nonce is read through Spring Security's public + // interface without depending on it. + testImplementation 'org.springframework.security:spring-security-web' testImplementation 'org.apache.groovy:groovy-test-junit5' testImplementation 'org.junit.jupiter:junit-jupiter-api' diff --git a/grails-web-common/src/main/groovy/grails/web/http/ContentSecurityPolicyNonce.java b/grails-web-common/src/main/groovy/grails/web/http/ContentSecurityPolicyNonce.java new file mode 100644 index 00000000000..265974ec55f --- /dev/null +++ b/grails-web-common/src/main/groovy/grails/web/http/ContentSecurityPolicyNonce.java @@ -0,0 +1,113 @@ +/* + * 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 grails.web.http; + +import java.lang.reflect.Method; +import java.security.SecureRandom; +import java.util.Base64; +import java.util.function.Supplier; + +import jakarta.servlet.http.HttpServletRequest; + +import org.jspecify.annotations.Nullable; + +import org.springframework.util.ClassUtils; +import org.springframework.util.ReflectionUtils; +import org.springframework.util.StringUtils; +import org.springframework.util.function.SingletonSupplier; + +/** + * The nonce of a nonce-based Content-Security-Policy, which a request carries under the + * {@value #ATTRIBUTE} attribute. + * + *

The Grails security headers filter puts one on each request when + * {@code grails.security.headers.content-security-policy.value} names the nonce with the + * {@code {nonce}} placeholder. {@value #ATTRIBUTE} is also the attribute Spring Security's + * {@code ContentSecurityPolicyNonceGeneratingFilter} sets by default, so + * {@link #nonceOf(HttpServletRequest)} reads the nonce whichever of the two generated it.

+ * + * @since 9.0 + */ +public final class ContentSecurityPolicyNonce { + + /** + * The request attribute that carries the nonce. + */ + public static final String ATTRIBUTE = "_csp"; + + /** + * The public type of the nonce Spring Security generates, whose implementation is a private + * class. It is read through this type, the one registered for reflection, since this module + * does not depend on Spring Security. + */ + private static final String SPRING_SECURITY_NONCE = + "org.springframework.security.web.header.ContentSecurityPolicyNonce"; + + private static final SecureRandom RANDOM = new SecureRandom(); + + private static final int NONCE_BYTES = 16; + + private final Supplier generated = SingletonSupplier.of(ContentSecurityPolicyNonce::generate); + + /** + * The nonce, generated the first time it is read: 128 bits from a {@link SecureRandom}, + * Base64-encoded without padding, as Spring Security generates one. + * + * @return the nonce + */ + public String getNonce() { + return this.generated.get(); + } + + /** + * The nonce the request carries: one the Grails security headers filter generated, or one + * Spring Security's {@code ContentSecurityPolicyNonceGeneratingFilter} generated. + * + * @param request the request + * @return the nonce, or {@code null} when the request carries neither + */ + public static @Nullable String nonceOf(HttpServletRequest request) { + Object holder = request.getAttribute(ATTRIBUTE); + if (holder instanceof ContentSecurityPolicyNonce nonce) { + return nonce.getNonce(); + } + Method getNonce = springSecurityGetNonce(holder); + Object nonce = (getNonce != null) ? ReflectionUtils.invokeMethod(getNonce, holder) : null; + return (nonce instanceof String value && StringUtils.hasText(value)) ? value : null; + } + + private static @Nullable Method springSecurityGetNonce(@Nullable Object holder) { + if (holder == null) { + return null; + } + // the loader of the holder's own class, which loaded the interface it implements + ClassLoader classLoader = holder.getClass().getClassLoader(); + if (!ClassUtils.isPresent(SPRING_SECURITY_NONCE, classLoader)) { + return null; + } + Class nonceType = ClassUtils.resolveClassName(SPRING_SECURITY_NONCE, classLoader); + return nonceType.isInstance(holder) ? ClassUtils.getMethodIfAvailable(nonceType, "getNonce") : null; + } + + private static String generate() { + byte[] bytes = new byte[NONCE_BYTES]; + RANDOM.nextBytes(bytes); + return Base64.getEncoder().withoutPadding().encodeToString(bytes); + } +} diff --git a/grails-web-common/src/main/groovy/grails/web/servlet/mvc/GrailsParameterMap.java b/grails-web-common/src/main/groovy/grails/web/servlet/mvc/GrailsParameterMap.java index a950d66bf2f..ad32623c228 100644 --- a/grails-web-common/src/main/groovy/grails/web/servlet/mvc/GrailsParameterMap.java +++ b/grails-web-common/src/main/groovy/grails/web/servlet/mvc/GrailsParameterMap.java @@ -64,8 +64,8 @@ public class GrailsParameterMap extends TypeConvertingMap implements Cloneable { private final HttpServletRequest request; /** - * Retained for binary compatibility. Request-body parsing for form-encoded {@code PUT}, {@code PATCH} - * and {@code DELETE} requests is now handled at the servlet layer by Spring's {@code FormContentFilter} + * Retained for binary compatibility. Request-body parsing for form-encoded {@code PUT}, {@code PATCH}, + * {@code DELETE} and {@code QUERY} requests is now handled at the servlet layer by Spring's {@code FormContentFilter} * (registered by Boot's {@code WebMvcAutoConfiguration}), not by this map. */ public static final String REQUEST_BODY_PARSED = "org.codehaus.groovy.grails.web.REQUEST_BODY_PARSED"; @@ -89,7 +89,7 @@ public GrailsParameterMap(Map values, HttpServletRequest request) { */ public GrailsParameterMap(HttpServletRequest request) { this.request = request; - // Request parameters (including form-encoded PUT/PATCH/DELETE bodies parsed at the servlet + // Request parameters (including form-encoded PUT/PATCH/DELETE/QUERY bodies parsed at the servlet // layer by Spring's FormContentFilter) are read straight from the request parameter map. // updateNestedKeys only reads this map, so the servlet's own is used directly and copied only when // there is something to merge into it. Read tolerantly because a multipart body the container diff --git a/grails-web-common/src/main/groovy/org/apache/grails/web/http/aot/ContentSecurityPolicyRuntimeHints.java b/grails-web-common/src/main/groovy/org/apache/grails/web/http/aot/ContentSecurityPolicyRuntimeHints.java new file mode 100644 index 00000000000..4498b0ca5fa --- /dev/null +++ b/grails-web-common/src/main/groovy/org/apache/grails/web/http/aot/ContentSecurityPolicyRuntimeHints.java @@ -0,0 +1,62 @@ +/* + * 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.apache.grails.web.http.aot; + +import org.jspecify.annotations.Nullable; + +import org.springframework.aot.hint.MemberCategory; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; + +import grails.web.http.ContentSecurityPolicyNonce; + +/** + * Registers what reading the nonce of a nonce-based Content-Security-Policy reflects on. + * + *

Spring Security's nonce is a private class behind a public interface, and + * {@link ContentSecurityPolicyNonce#nonceOf} reads it through that interface. An image has no + * metadata for the private class, so only the interface can be registered, and the read must not + * reflect on anything else.

+ * + * @since 9.0 + */ +public class ContentSecurityPolicyRuntimeHints implements RuntimeHintsRegistrar { + + /** What a hinted type is registered for: the members Groovy reads to choose an overload. */ + private static final MemberCategory[] DISPATCHED_MEMBERS = { + MemberCategory.INVOKE_DECLARED_METHODS, + MemberCategory.INVOKE_PUBLIC_METHODS, + MemberCategory.INVOKE_DECLARED_CONSTRUCTORS + }; + + /** + * The nonce Spring Security's nonce filter sets. Named as a string and registered only when + * present, since an application need not use Spring Security. + */ + private static final String SPRING_SECURITY_NONCE = + "org.springframework.security.web.header.ContentSecurityPolicyNonce"; + + @Override + public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) { + // a controller, a service or a dynamically compiled page reads the nonce through Groovy's + // dynamic dispatch + hints.reflection().registerType(ContentSecurityPolicyNonce.class, DISPATCHED_MEMBERS); + hints.reflection().registerTypeIfPresent(classLoader, SPRING_SECURITY_NONCE, DISPATCHED_MEMBERS); + } +} diff --git a/grails-web-common/src/main/groovy/org/grails/web/config/http/GrailsFilters.java b/grails-web-common/src/main/groovy/org/grails/web/config/http/GrailsFilters.java index 742282f91df..c2e8d2840a1 100644 --- a/grails-web-common/src/main/groovy/org/grails/web/config/http/GrailsFilters.java +++ b/grails-web-common/src/main/groovy/org/grails/web/config/http/GrailsFilters.java @@ -73,7 +73,7 @@ public enum GrailsFilters { CAS_SINGLE_SIGN_OUT_FILTER(Ordered.HIGHEST_PRECEDENCE + 1), /** - * Spring's {@code FormContentFilter}, which parses the form of a PUT, PATCH or DELETE request into request + * Spring's {@code FormContentFilter}, which parses the form of a PUT, PATCH, DELETE or QUERY request into request * parameters: Spring Boot's, at its default order, or Grails' at the same order when Spring Boot registers none. */ FORM_CONTENT_FILTER(-9900), diff --git a/grails-web-common/src/main/resources/META-INF/spring/aot.factories b/grails-web-common/src/main/resources/META-INF/spring/aot.factories index b3eeaefec08..f0ffe83ea41 100644 --- a/grails-web-common/src/main/resources/META-INF/spring/aot.factories +++ b/grails-web-common/src/main/resources/META-INF/spring/aot.factories @@ -1,2 +1,3 @@ org.springframework.aot.hint.RuntimeHintsRegistrar=\ +org.apache.grails.web.http.aot.ContentSecurityPolicyRuntimeHints,\ org.apache.grails.web.common.aot.ServletContextHolderRuntimeHints diff --git a/grails-web-common/src/test/groovy/grails/web/http/ContentSecurityPolicyNonceSpec.groovy b/grails-web-common/src/test/groovy/grails/web/http/ContentSecurityPolicyNonceSpec.groovy new file mode 100644 index 00000000000..6af059f7fa4 --- /dev/null +++ b/grails-web-common/src/test/groovy/grails/web/http/ContentSecurityPolicyNonceSpec.groovy @@ -0,0 +1,106 @@ +/* + * 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 grails.web.http + +import java.lang.reflect.Modifier + +import jakarta.servlet.FilterChain +import jakarta.servlet.http.HttpServletRequest + +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.security.web.header.ContentSecurityPolicyNonceGeneratingFilter +import spock.lang.Specification +import spock.lang.Unroll + +class ContentSecurityPolicyNonceSpec extends Specification { + + MockHttpServletRequest request = new MockHttpServletRequest('GET', '/') + + void 'a nonce is 128 random bits in Base64 without padding, generated once'() { + given: + def nonce = new ContentSecurityPolicyNonce() + + expect: + nonce.nonce ==~ /[A-Za-z0-9+\/]{22}/ + nonce.nonce == nonce.nonce + nonce.nonce != new ContentSecurityPolicyNonce().nonce + } + + void 'the nonce the Grails security headers filter put on the request is read'() { + given: + def nonce = new ContentSecurityPolicyNonce() + request.setAttribute('_csp', nonce) + + expect: + ContentSecurityPolicyNonce.ATTRIBUTE == '_csp' + ContentSecurityPolicyNonce.nonceOf(request) == nonce.nonce + } + + void 'the nonce Spring Security generated is read through its public interface'() { + given: 'a view behind Spring Security\'s own nonce filter' + String read = null + Object carried = null + FilterChain view = { viewRequest, viewResponse -> + read = ContentSecurityPolicyNonce.nonceOf(viewRequest as HttpServletRequest) + carried = viewRequest.getAttribute('_csp') + } as FilterChain + + when: + new ContentSecurityPolicyNonceGeneratingFilter().doFilter(request, new MockHttpServletResponse(), view) + + then: 'the filter carries it in a class that is not public, behind the interface' + !Modifier.isPublic(carried.getClass().modifiers) + carried instanceof org.springframework.security.web.header.ContentSecurityPolicyNonce + + and: + read ==~ /[A-Za-z0-9+\/]{22}/ + read == (carried as org.springframework.security.web.header.ContentSecurityPolicyNonce).nonce + } + + @Unroll + void 'a request carries no nonce when #description'() { + given: + if (holder != null) { + request.setAttribute('_csp', holder) + } + + expect: + ContentSecurityPolicyNonce.nonceOf(request) == null + + where: + description | holder + 'nothing is under the attribute' | null + 'something else is under the attribute' | 'not a nonce' + 'Spring Security\'s nonce is blank' | blankSpringSecurityNonce() + 'another type with a nonce getter is under the attribute' | new LookalikeNonce() + } + + private static org.springframework.security.web.header.ContentSecurityPolicyNonce blankSpringSecurityNonce() { + { -> ' ' } as org.springframework.security.web.header.ContentSecurityPolicyNonce + } + + /** Has a public nonce getter, but is neither the Grails nonce nor Spring Security's. */ + static class LookalikeNonce { + + String getNonce() { + 'abc' + } + } +} diff --git a/grails-web-common/src/test/groovy/org/apache/grails/web/http/aot/ContentSecurityPolicyRuntimeHintsSpec.groovy b/grails-web-common/src/test/groovy/org/apache/grails/web/http/aot/ContentSecurityPolicyRuntimeHintsSpec.groovy new file mode 100644 index 00000000000..ff82bdf6e72 --- /dev/null +++ b/grails-web-common/src/test/groovy/org/apache/grails/web/http/aot/ContentSecurityPolicyRuntimeHintsSpec.groovy @@ -0,0 +1,71 @@ +/* + * 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.apache.grails.web.http.aot + +import org.springframework.aot.hint.RuntimeHints +import org.springframework.aot.hint.RuntimeHintsRegistrar +import org.springframework.aot.hint.TypeReference +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates +import org.springframework.core.io.support.SpringFactoriesLoader +import spock.lang.Specification + +import grails.web.http.ContentSecurityPolicyNonce + +/** + * Covers reading the nonce of a nonce-based Content-Security-Policy in an ahead-of-time image, where + * only registered types can be reflected on. + */ +class ContentSecurityPolicyRuntimeHintsSpec extends Specification { + + private static final String SPRING_SECURITY_NONCE = 'org.springframework.security.web.header.ContentSecurityPolicyNonce' + + RuntimeHints hints = new RuntimeHints() + + void 'Spring Security\'s nonce can be read through its public interface'() { + when: + new ContentSecurityPolicyRuntimeHints().registerHints(hints, getClass().classLoader) + + then: + RuntimeHintsPredicates.reflection().onMethodInvocation(SPRING_SECURITY_NONCE, 'getNonce').test(hints) + } + + void 'the Grails nonce can be read by Groovy\'s dynamic dispatch'() { + when: + new ContentSecurityPolicyRuntimeHints().registerHints(hints, getClass().classLoader) + + then: 'a controller or a dynamically compiled page calling the reader, or the getter' + RuntimeHintsPredicates.reflection().onMethodInvocation(ContentSecurityPolicyNonce, 'nonceOf').test(hints) + RuntimeHintsPredicates.reflection().onMethodInvocation(ContentSecurityPolicyNonce, 'getNonce').test(hints) + } + + void 'an application without Spring Security still registers the Grails nonce'() { + when: 'a class loader that resolves nothing, as an application without Spring Security has' + new ContentSecurityPolicyRuntimeHints().registerHints(hints, new URLClassLoader(new URL[0], null)) + + then: + hints.reflection().getTypeHint(TypeReference.of(SPRING_SECURITY_NONCE)) == null + hints.reflection().getTypeHint(TypeReference.of(ContentSecurityPolicyNonce)) != null + } + + void 'the hints are registered for an ahead-of-time build'() { + expect: + SpringFactoriesLoader.forResourceLocation('META-INF/spring/aot.factories') + .load(RuntimeHintsRegistrar)*.class.contains(ContentSecurityPolicyRuntimeHints) + } +} diff --git a/grails-web-url-mappings/src/main/groovy/org/apache/grails/web/mapping/aot/UrlMappingRuntimeHints.java b/grails-web-url-mappings/src/main/groovy/org/apache/grails/web/mapping/aot/UrlMappingRuntimeHints.java index 08eade865a2..5cf042789db 100644 --- a/grails-web-url-mappings/src/main/groovy/org/apache/grails/web/mapping/aot/UrlMappingRuntimeHints.java +++ b/grails-web-url-mappings/src/main/groovy/org/apache/grails/web/mapping/aot/UrlMappingRuntimeHints.java @@ -18,8 +18,11 @@ */ package org.apache.grails.web.mapping.aot; +import java.util.List; + import org.jspecify.annotations.Nullable; +import org.springframework.aot.hint.ExecutableMode; import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHintsRegistrar; @@ -56,6 +59,12 @@ public class UrlMappingRuntimeHints implements RuntimeHintsRegistrar { */ private static final String MAPPINGS_BUILDER = "org.grails.web.mapping.DefaultUrlMappingEvaluator$UrlMappingBuilder"; + /** + * Whose method an error response is written as the return value of, which is looked up reflectively to tell Spring + * MVC the type of the value when {@code grails.exceptionresolver.problemDetails} writes one. + */ + private static final String ACTION_RETURN_VALUES = "org.grails.web.mapping.mvc.ActionReturnValues"; + @Override public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) { for (String type : DISPATCHED_TYPES) { @@ -68,5 +77,7 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) MemberCategory.INVOKE_DECLARED_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.ACCESS_DECLARED_FIELDS); + hints.reflection().registerTypeIfPresent(classLoader, ACTION_RETURN_VALUES, + type -> type.withMethod("returnedErrorResponse", List.of(), ExecutableMode.INVOKE)); } } diff --git a/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/DefaultUrlMappingEvaluator.java b/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/DefaultUrlMappingEvaluator.java index 5aa34a648ad..8b02c101461 100644 --- a/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/DefaultUrlMappingEvaluator.java +++ b/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/DefaultUrlMappingEvaluator.java @@ -675,6 +675,43 @@ public UrlMapping options(RegexUrlMapping regexUrlMapping) { return regexUrlMapping; } + /** + * Matches the QUERY method of RFC 10008 + * + * @param arguments The arguments + * @param uri The URI + * @return the UrlMapping + * @since 9.0 + */ + public UrlMapping query(Map arguments, String uri, Closure callable) { + arguments.put(HTTP_METHOD, HttpMethod.QUERY); + return (UrlMapping) _invoke(uri, new Object[]{ arguments, callable }, this); + } + + /** + * Matches the QUERY method of RFC 10008 + * + * @param arguments The arguments + * @param uri The URI + * @return the UrlMapping + * @since 9.0 + */ + public UrlMapping query(Map arguments, String uri) { + return query(arguments, uri, null); + } + + /** + * Matches the QUERY method of RFC 10008 + * + * @param regexUrlMapping The mapping + * @return the UrlMapping + * @since 9.0 + */ + public UrlMapping query(RegexUrlMapping regexUrlMapping) { + regexUrlMapping.httpMethod = HttpMethod.QUERY.toString(); + return regexUrlMapping; + } + /** * Define Url mapping collections that are nested directly below the parent resource (without the id) * diff --git a/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/ActionExceptionHandlerResolver.groovy b/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/ActionExceptionHandlerResolver.groovy index 3055b9e564a..68d8fceb3c8 100644 --- a/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/ActionExceptionHandlerResolver.groovy +++ b/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/ActionExceptionHandlerResolver.groovy @@ -19,22 +19,47 @@ package org.grails.web.mapping.mvc import groovy.transform.CompileStatic +import groovy.util.logging.Slf4j +import org.codehaus.groovy.runtime.InvokerInvocationException +import jakarta.servlet.ServletException import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse +import io.micrometer.observation.Observation + import org.springframework.context.ApplicationContext import org.springframework.context.ApplicationContextAware import org.springframework.core.Ordered +import org.springframework.http.HttpStatus +import org.springframework.http.HttpStatusCode +import org.springframework.http.MediaType +import org.springframework.http.server.observation.ServerRequestObservationContext +import org.springframework.web.ErrorResponse +import org.springframework.web.HttpMediaTypeNotAcceptableException +import org.springframework.web.accept.ContentNegotiationManager +import org.springframework.web.context.request.ServletWebRequest +import org.springframework.web.context.request.async.AsyncRequestTimeoutException +import org.springframework.web.filter.ServerHttpObservationFilter import org.springframework.web.method.HandlerMethod +import org.springframework.web.method.annotation.ProblemDetailExceptionHandler import org.springframework.web.servlet.HandlerExceptionResolver +import org.springframework.web.servlet.HandlerMapping import org.springframework.web.servlet.ModelAndView import org.springframework.web.servlet.handler.HandlerExceptionResolverComposite import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver +import org.springframework.web.servlet.support.RequestContextUtils +import org.springframework.web.util.DisconnectedClientHelper import grails.config.Settings +import grails.web.mapping.UrlMappingsHolder +import grails.web.mime.MimeType +import grails.web.mime.MimeTypeResolver +import org.grails.exceptions.ExceptionUtils import org.grails.web.servlet.mvc.GrailsWebRequest +import org.grails.web.servlet.mvc.exceptions.GrailsMVCException import org.grails.web.util.GrailsApplicationAttributes +import org.grails.web.util.WebUtils /** * Resolves an exception a controller action throws with the {@code @ExceptionHandler} methods Spring MVC resolves those @@ -44,43 +69,230 @@ import org.grails.web.util.GrailsApplicationAttributes * ordered ahead of Spring MVC's resolvers, as that resolver is ahead of Spring MVC's others, so that an exception none of * those methods handles is resolved by them and by the Grails exception resolver, as without it. * + *

With {@code grails.exceptionresolver.problemDetails}, it writes an exception none of those methods handles as an + * RFC 9457 problem, as Spring MVC writes one with {@code ProblemDetailsConfigurer.renderUnhandledExceptions}, except to + * a client that prefers HTML, or where a URL mapping names the exception. Those are left to the Grails exception + * resolver and the application's error page, which turning on that switch of Spring MVC would take every exception from. + * * @since 9.0 */ @CompileStatic +@Slf4j class ActionExceptionHandlerResolver implements HandlerExceptionResolver, Ordered, ApplicationContextAware { + /** + * Where {@link ServerHttpObservationFilter} keeps the observation of a request. The filter only offers its context, + * which cannot tell the handlers of an error, and a registry cannot be relied on to find it, because a filter can be + * built with a registry the application does not know. A public way to get it is requested in + * spring-framework#37658. + */ + private static final String SERVER_OBSERVATION_ATTRIBUTE = ServerHttpObservationFilter.name + '.observation' + + /** The name of the bean Spring MVC's content negotiation manager is. */ + private static final String MVC_CONTENT_NEGOTIATION_MANAGER = 'mvcContentNegotiationManager' + + /** Logs a client that went away while a problem was written, in a category of its own, as Spring MVC does. */ + private static final DisconnectedClientHelper DISCONNECTED_CLIENTS = + new DisconnectedClientHelper(ActionExceptionHandlerResolver.name + '.DisconnectedClient') + /** Ahead of Spring MVC's resolvers, which its configuration orders at 0, behind Spring Boot's error attributes. */ int order = -1 /** * Whether it resolves exceptions, as {@code grails.exceptionresolver.springHandlers} says, which it reads from the - * environment of its application context where it is not set. + * environment of its application context where it is not set, and which {@code grails.exceptionresolver.problemDetails} + * turns on unless the application sets it. */ Boolean enabled + /** + * Whether it writes an exception no handler method handles as a problem, as + * {@code grails.exceptionresolver.problemDetails} says, which it reads from the environment of its application + * context where it is not set. + * + * @since 9.0 + */ + Boolean problemDetails + private ApplicationContext applicationContext private volatile ExceptionHandlerExceptionResolver resolver + private ActionReturnValues returnValues + + private ProblemDetailExceptionHandler problemDetailHandler + @Override void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext + if (problemDetails == null) { + problemDetails = applicationContext.environment.getProperty(Settings.SETTING_EXCEPTION_RESOLVER_PROBLEM_DETAILS, + Boolean, false) + } if (enabled == null) { + // a problem is the last resort after the exception handler methods, as it is in Spring MVC enabled = applicationContext.environment.getProperty(Settings.SETTING_EXCEPTION_RESOLVER_SPRING_HANDLERS, Boolean, - Boolean.valueOf(Settings.SETTING_EXCEPTION_RESOLVER_SPRING_HANDLERS_DEFAULT)) + problemDetails || Boolean.valueOf(Settings.SETTING_EXCEPTION_RESOLVER_SPRING_HANDLERS_DEFAULT)) } + returnValues = new ActionReturnValues(applicationContext) + problemDetailHandler = new ProblemDetailExceptionHandler(applicationContext) } @Override ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { - if (!enabled || !(handler instanceof GrailsControllerUrlMappingInfo)) { + if (!(handler instanceof GrailsControllerUrlMappingInfo)) { return null } - HandlerMethod action = action(request, (GrailsControllerUrlMappingInfo) handler) + ModelAndView resolved = enabled ? resolveWithHandlerMethods(request, response, (GrailsControllerUrlMappingInfo) handler, ex) : null + return resolved == null && problemDetails ? + resolveAsProblem(request, response, (GrailsControllerUrlMappingInfo) handler, ex) : resolved + } + + private ModelAndView resolveWithHandlerMethods(HttpServletRequest request, HttpServletResponse response, + GrailsControllerUrlMappingInfo handler, Exception ex) { + HandlerMethod action = action(request, handler) ExceptionHandlerExceptionResolver springResolver = action != null ? exceptionHandlerResolver() : null return springResolver?.resolveException(request, response, action, ex) } + /** + * Writes an exception as Spring MVC writes one no handler method handles, with Spring MVC's translation of it, which + * logs an unexpected one and never exposes its message. A failure to write it leaves the exception to the other + * resolvers, as a failure of an exception handler method leaves it in Spring MVC. An action a page includes is left + * to them too, since a problem written into the page is no answer to the client. + */ + private ModelAndView resolveAsProblem(HttpServletRequest request, HttpServletResponse response, + GrailsControllerUrlMappingInfo handler, Exception thrown) { + Exception ex = unwrap(thrown) + try { + if (response.committed || WebUtils.isIncludeRequest(request) || !returnValues.writesErrorResponses() || + prefersHtml(request) || isMappedToErrorHandler(ex)) { + return null + } + ErrorResponse errorResponse = problemDetailHandler.handleException(ex, RequestContextUtils.getLocale(request)) + // what an exception handler method that failed produces, as Spring MVC removes it for its own last resort + request.removeAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE) + ModelAndView written = errorResponse == null ? new ModelAndView() : returnValues.writeErrorResponse(errorResponse, + handler.controllerClass?.clazz, request, response) + if (isServerError(response)) { + recordServerError(request, reportedFailure(thrown)) + // Spring MVC's translation logs an unexpected exception without the request it failed + if (!(ex instanceof ErrorResponse) && !DisconnectedClientHelper.isClientDisconnectedException(ex)) { + log.error('{} occurred when processing request: [{}] {}', ex.class.simpleName, request.method, requestUri(request)) + } + } + return written + } + catch (Exception e) { + if (DISCONNECTED_CLIENTS.checkAndLogClientDisconnectedException(e)) { + if (isServerError(response)) { + recordServerError(request, reportedFailure(thrown)) + } + return new ModelAndView() + } + log.warn('Could not write a problem for an exception of [{}], so leaving it to the other exception resolvers', + requestUri(request), e) + return null + } + } + + private static boolean isServerError(HttpServletResponse response) { + return HttpStatusCode.valueOf(response.status).is5xxServerError() + } + + /** The URI the client asked for, where the action was forwarded to, as the Grails exception resolver logs it. */ + private static Object requestUri(HttpServletRequest request) { + return request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE) ?: request.requestURI + } + + /** + * The exception an action threw, out of what Groovy and Grails wrap it in. Only those come off: what that exception + * wraps itself is its own cause, and the status it answers with is its own. + */ + private static Exception unwrap(Exception ex) { + Throwable found = ex + while ((found instanceof InvokerInvocationException || found instanceof GrailsMVCException) && found.cause != null) { + found = found.cause + } + return found instanceof Exception ? (Exception) found : ex + } + + /** + * Whether the application's error page is what the client asks for: the format Grails resolves for the response, + * from a {@code format} parameter or the {@code Accept} header, or else, where that is any format, as for a browser + * whose {@code Accept} header {@code grails.mime.disable.accept.header.userAgents} disregards, whether the first + * concrete type of the {@code Accept} header is HTML, as Spring MVC decides to render a problem as a view. + */ + private boolean prefersHtml(HttpServletRequest request) { + GrailsWebRequest webRequest = GrailsWebRequest.lookup(request) + MimeType format = webRequest != null ? applicationContext.getBeanProvider(MimeTypeResolver).getIfAvailable() + ?.resolveResponseMimeType(webRequest) : null + if (format != null && format.extension != MimeType.ALL.extension) { + return format.extension == MimeType.HTML.extension + } + try { + MediaType first = contentNegotiationManager().resolveMediaTypes(new ServletWebRequest(request)) + .find { MediaType type -> type.concrete } + return first != null && MediaType.TEXT_HTML.isCompatibleWith(first) + } + catch (HttpMediaTypeNotAcceptableException ignored) { + return false + } + } + + private ContentNegotiationManager contentNegotiationManager() { + return applicationContext.containsBean(MVC_CONTENT_NEGOTIATION_MANAGER) ? + applicationContext.getBean(MVC_CONTENT_NEGOTIATION_MANAGER, ContentNegotiationManager) : + new ContentNegotiationManager() + } + + /** + * Whether the application maps the exception, as a {@code "500"} mapping with an {@code exception} names it, under + * the status the Grails exception resolver looks for it with, which for an asynchronous timeout is its own. + */ + private boolean isMappedToErrorHandler(Exception ex) { + if (!applicationContext.containsBean(UrlMappingsHolder.BEAN_ID)) { + return false + } + UrlMappingsHolder urlMappings = applicationContext.getBean(UrlMappingsHolder.BEAN_ID, UrlMappingsHolder) + int status = ex instanceof AsyncRequestTimeoutException ? + ((AsyncRequestTimeoutException) ex).statusCode.value() : HttpStatus.INTERNAL_SERVER_ERROR.value() + return urlMappings.matchStatusCode(status, ex) != null || urlMappings.matchStatusCode(status, ExceptionUtils.getRootCause(ex)) != null + } + + /** + * What the action threw, as the observation of the request reports it. {@code DispatcherServlet} wraps an + * {@link Error}, such as a failed {@code assert}, in a {@link ServletException}, which + * {@link ServerHttpObservationFilter} takes off an exception that reaches it, and {@link #unwrap} leaves a Groovy + * wrapper around an {@code Error}, since a problem is written for an exception. + */ + private static Throwable reportedFailure(Exception thrown) { + Throwable failure = thrown + if (failure instanceof ServletException && failure.cause != null) { + failure = failure.cause + } + while ((failure instanceof InvokerInvocationException || failure instanceof GrailsMVCException) && failure.cause != null) { + failure = failure.cause + } + return failure + } + + /** + * As Spring MVC records an exception it translates to a server error, which the observation would not see otherwise. + * Only the observation tells its handlers, one of which marks the span. + */ + private static void recordServerError(HttpServletRequest request, Throwable ex) { + Object observation = request.getAttribute(SERVER_OBSERVATION_ATTRIBUTE) + if (observation instanceof Observation) { + ((Observation) observation).error(ex) + } + else { + ServerHttpObservationFilter.findObservationContext(request).ifPresent { ServerRequestObservationContext context -> + context.error = ex + } + } + } + /** The action as a handler method: the controller that handles the request, and its method of the action. */ private static HandlerMethod action(HttpServletRequest request, GrailsControllerUrlMappingInfo info) { String actionName = GrailsWebRequest.lookup(request)?.actionName ?: info.actionName ?: info.controllerClass.defaultAction diff --git a/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/ActionReturnValues.groovy b/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/ActionReturnValues.groovy index a95be909958..035063a9f62 100644 --- a/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/ActionReturnValues.groovy +++ b/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/ActionReturnValues.groovy @@ -29,6 +29,7 @@ import jakarta.servlet.http.HttpServletResponse import org.springframework.context.ApplicationContext import org.springframework.context.MessageSource +import org.springframework.core.MethodParameter import org.springframework.core.annotation.AnnotatedElementUtils import org.springframework.core.task.AsyncTaskExecutor import org.springframework.http.HttpEntity @@ -178,6 +179,47 @@ class ActionReturnValues { return write(value, action, request, response) } + /** + * Whether Spring MVC's handler adapter is there to write an error response. + * + * @return whether it is + * @since 9.0 + */ + boolean writesErrorResponses() { + return adapter() != null + } + + /** + * Writes an error response with the return value handler Spring MVC's handler adapter selects for one, as Spring MVC + * writes what an exception handler method returns: with its content negotiation, its {@code ErrorResponse.Interceptor} + * beans and the {@code ResponseBodyAdvice} beans that apply to the controller. Those are found by the controller's + * class, since the request may not have got as far as creating the controller, as when an interceptor's + * {@code before()} fails, or may still hold the one that forwarded to it. + * + * @param errorResponse the error response + * @param controllerType the class of the controller the request is mapped to, or {@code null} + * @param request the request + * @param response the response + * @return the view a handler selected, or an empty one where it wrote the response + * @since 9.0 + */ + ModelAndView writeErrorResponse(ErrorResponse errorResponse, Class controllerType, HttpServletRequest request, + HttpServletResponse response) { + // looked up here rather than when the class loads, so that an application that writes none never needs it + MethodParameter returnType = new MethodParameter(ActionReturnValues.getDeclaredMethod('returnedErrorResponse'), -1) + .withContainingClass(controllerType ?: ActionReturnValues) + ModelAndViewContainer container = new ModelAndViewContainer() + adapter().returnValueHandlers.handleReturnValue(errorResponse, returnType, container, new ServletWebRequest(request, response)) + // only once it is written: a failure leaves the response to the error page, which an action can answer + GrailsWebRequest.lookup(request)?.renderView = false + return container.requestHandled ? new ModelAndView() : viewOf(container) + } + + /** The method an error response is written as the return value of, where it is not the value of an action. */ + private static ErrorResponse returnedErrorResponse() { + return null + } + private ModelAndView write(Object value, ActionHandlerMethod action, HttpServletRequest request, HttpServletResponse response) { MvcAdapter adapter = adapter() GrailsWebRequest.lookup(request)?.renderView = false @@ -210,7 +252,11 @@ class ActionReturnValues { if (container.requestHandled) { return null } - // a value Spring MVC renders a view for, such as a ModelAndView an action annotated @ResponseBody returns + return viewOf(container) + } + + /** A value Spring MVC renders a view for, such as a ModelAndView an action annotated @ResponseBody returns. */ + private static ModelAndView viewOf(ModelAndViewContainer container) { ModelAndView modelAndView = container.viewReference ? new ModelAndView(container.viewName, container.model) : new ModelAndView((View) container.view, container.model) if (container.status != null) { diff --git a/grails-web-url-mappings/src/test/groovy/org/apache/grails/web/mapping/aot/UrlMappingRuntimeHintsSpec.groovy b/grails-web-url-mappings/src/test/groovy/org/apache/grails/web/mapping/aot/UrlMappingRuntimeHintsSpec.groovy index ac709cbb0ce..020f6c60077 100644 --- a/grails-web-url-mappings/src/test/groovy/org/apache/grails/web/mapping/aot/UrlMappingRuntimeHintsSpec.groovy +++ b/grails-web-url-mappings/src/test/groovy/org/apache/grails/web/mapping/aot/UrlMappingRuntimeHintsSpec.groovy @@ -27,6 +27,7 @@ import spock.lang.Specification import grails.web.mapping.LinkGenerator import grails.web.mapping.UrlMapping import grails.web.mapping.UrlMappingInfo +import org.grails.web.mapping.mvc.ActionReturnValues /** * Covers the URL mappings being evaluated and used in an image: the types Groovy dispatches on, and the @@ -49,6 +50,14 @@ class UrlMappingRuntimeHintsSpec extends Specification { } } + void 'the method a problem is written as the return value of can be found'() { + given: 'the method the class looks up' + def method = ActionReturnValues.getDeclaredMethod('returnedErrorResponse') + + expect: + hints.reflection().getTypeHint(TypeReference.of(ActionReturnValues)).methods().anyMatch { it.name == method.name } + } + void 'the builder a URL mappings closure is delegated to can be called and read'() { expect: 'each mapping is a call the closure resolves against the builder, which reads its own fields too' RuntimeHintsPredicates.reflection() diff --git a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/UrlMappingsWithHttpMethodSpec.groovy b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/UrlMappingsWithHttpMethodSpec.groovy index 2d0e0e66977..2c7fa78e143 100644 --- a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/UrlMappingsWithHttpMethodSpec.groovy +++ b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/UrlMappingsWithHttpMethodSpec.groovy @@ -29,6 +29,7 @@ import org.grails.web.mapping.DefaultLinkGenerator import org.grails.web.mapping.DefaultUrlMappingEvaluator import org.grails.web.mapping.DefaultUrlMappingsHolder import org.grails.web.util.WebUtils +import org.springframework.http.HttpMethod import org.springframework.mock.web.MockServletContext import org.springframework.web.context.request.RequestContextHolder import spock.lang.IgnoreIf @@ -58,6 +59,68 @@ class UrlMappingsWithHttpMethodSpec extends Specification{ } + void 'the query prefix maps a URL to the QUERY method of RFC 10008 alone'() { + given: + def holder = urlMappingsHolder { + query "/books/search"(controller: 'book', action: 'search') + query "/books/find"(controller: 'book') { + action = 'find' + } + } + + expect: + holder.matchAll('/books/search', 'QUERY').size() == 1 + holder.matchAll('/books/search', 'QUERY')[0].actionName == 'search' + holder.matchAll('/books/find', 'QUERY')[0].actionName == 'find' + !holder.matchAll('/books/search', 'POST') + !holder.matchAll('/books/search', 'GET') + } + + void 'the method argument maps a URL to the QUERY method of RFC 10008, in any case'() { + given: + def holder = urlMappingsHolder { + "/books/search"(controller: 'book', action: 'search', method: 'QUERY') + } + + expect: + holder.matchAll('/books/search', 'QUERY')[0].actionName == 'search' + holder.matchAll('/books/search', 'query')[0].actionName == 'search' + !holder.matchAll('/books/search', 'GET') + } + + void 'a mapping for any method matches the QUERY method, and offers it among the methods of its URL'() { + given: + def holder = urlMappingsHolder { + "/books/any"(controller: 'book', action: 'any') + query "/books/search"(controller: 'book', action: 'search') + } + + expect: + holder.matchAll('/books/any', 'QUERY')[0].actionName == 'any' + holder.allowedMethods('/books/any').contains(HttpMethod.QUERY) + holder.allowedMethods('/books/search') == [HttpMethod.QUERY] as Set + } + + void 'a link to a QUERY mapping is generated with the method it is mapped to'() { + given: + def holder = urlMappingsHolder { + query "/books/search"(controller: 'book', action: 'search') + "/$controller/$action?"() + } + def linkGenerator = new DefaultLinkGenerator('http://localhost', '') + linkGenerator.urlMappingsHolder = holder + + expect: + linkGenerator.link(controller: 'book', action: 'search', method: 'QUERY') == '/books/search' + linkGenerator.link(controller: 'book', action: 'search') == '/book/search' + } + + private static UrlMappingsHolder urlMappingsHolder(Closure mappings) { + def ctx = new MockApplicationContext() + ctx.registerMockBean(GrailsApplication.APPLICATION_ID, new DefaultGrailsApplication()) + new DefaultUrlMappingsHolder(new DefaultUrlMappingEvaluator(ctx).evaluateMappings(mappings)) + } + void "Test that the http method can be used as a prefix to URL mappings"() { given:"A URL mapping evaluator" def ctx = new MockApplicationContext() diff --git a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/ActionExceptionProblemDetailsSpec.groovy b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/ActionExceptionProblemDetailsSpec.groovy new file mode 100644 index 00000000000..1caf9777b0e --- /dev/null +++ b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/ActionExceptionProblemDetailsSpec.groovy @@ -0,0 +1,636 @@ +/* + * 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.web.mapping.mvc + +import groovy.json.JsonSlurper +import org.codehaus.groovy.runtime.InvokerInvocationException + +import jakarta.servlet.FilterChain +import jakarta.servlet.ServletException +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse + +import ch.qos.logback.classic.Level +import io.micrometer.observation.Observation +import io.micrometer.observation.ObservationHandler +import io.micrometer.observation.ObservationRegistry + +import org.springframework.core.MethodParameter +import org.springframework.core.env.MapPropertySource +import org.springframework.http.HttpHeaders +import org.springframework.http.HttpStatus +import org.springframework.http.MediaType +import org.springframework.http.ProblemDetail +import org.springframework.http.converter.HttpMessageConverter +import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter +import org.springframework.http.server.ServerHttpRequest +import org.springframework.http.server.ServerHttpResponse +import org.springframework.http.server.observation.ServerRequestObservationContext +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.mock.web.MockServletContext +import org.springframework.web.ErrorResponse +import org.springframework.web.ErrorResponseException +import org.springframework.web.bind.annotation.ControllerAdvice +import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.bind.annotation.ResponseStatus +import org.springframework.web.context.request.async.AsyncRequestNotUsableException +import org.springframework.web.context.request.async.AsyncRequestTimeoutException +import org.springframework.web.context.support.GenericWebApplicationContext +import org.springframework.web.filter.ServerHttpObservationFilter +import org.springframework.web.server.ResponseStatusException +import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice + +import grails.core.DefaultGrailsApplication +import grails.core.GrailsApplication +import grails.web.mapping.UrlMappingInfo +import grails.web.mapping.UrlMappingsHolder +import org.apache.grails.core.testing.support.LogCapture +import org.grails.core.DefaultGrailsControllerClass +import org.grails.support.MockApplicationContext +import org.grails.web.mapping.DefaultUrlMappingEvaluator +import org.grails.web.mapping.DefaultUrlMappingsHolder +import org.grails.web.servlet.mvc.GrailsWebRequest +import org.grails.web.servlet.mvc.exceptions.ControllerExecutionException +import org.grails.web.util.GrailsApplicationAttributes +import org.grails.web.util.WebUtils +import spock.lang.Specification + +/** + * An exception a controller action throws is written as a problem where {@code grails.exceptionresolver.problemDetails} + * is set, by Spring MVC's translation and its handler adapter, unless the application's error page is for it. + */ +class ActionExceptionProblemDetailsSpec extends Specification { + + private GenericWebApplicationContext context + + private MockHttpServletRequest request + + private MockHttpServletResponse response + + private GrailsWebRequest webRequest + + private List interceptors = [] + + void setup() { + request = new MockHttpServletRequest(new MockServletContext()) + request.requestURI = '/problem/index' + response = new MockHttpServletResponse() + webRequest = new GrailsWebRequest(request, response, request.servletContext) + request.setAttribute(GrailsApplicationAttributes.WEB_REQUEST, webRequest) + } + + void cleanup() { + context?.close() + } + + void 'with the setting, an exception is written as a problem that does not expose its message, to a client that #client'() { + given: + def resolver = resolver([problemDetails: true]) + if (accept) { + request.addHeader(HttpHeaders.ACCEPT, accept) + } + + when: + def modelAndView = resolver.resolveException(request, response, action(new ProblemController()), new IllegalStateException('secret')) + + then: + modelAndView != null + modelAndView.empty + response.status == 500 + response.contentType == 'application/problem+json' + problem() == [title: 'Internal Server Error', status: 500] + !response.contentAsString.contains('secret') + + and: 'no view is rendered after it' + !webRequest.renderView + + where: + client | accept + 'names no type' | null + 'accepts any type' | '*/*' + 'asks for JSON' | 'application/json' + 'asks for text' | 'text/plain' + 'asks for JSON first' | 'application/json, text/html;q=0.9' + } + + void 'an exception is left to the other resolvers for a client that prefers HTML: #accept'() { + given: + def resolver = resolver([problemDetails: true]) + request.addHeader(HttpHeaders.ACCEPT, accept) + + expect: + resolver.resolveException(request, response, action(new ProblemController()), new IllegalStateException('secret')) == null + response.status == 200 + !response.contentAsString + + where: + accept << ['text/html', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'application/json;q=0.5, text/html'] + } + + void 'without the setting, an exception is left to the other resolvers'() { + given: + def resolver = resolver(setting) + + expect: + resolver.resolveException(request, response, action(new ProblemController()), new IllegalStateException('secret')) == null + response.status == 200 + + where: + setting << [[:], [problemDetails: false]] + } + + void 'a handler that is not an action is left to the other resolvers'() { + given: + def resolver = resolver([problemDetails: true]) + + expect: + resolver.resolveException(request, response, new Object(), new IllegalStateException('secret')) == null + } + + void 'an exception after the response is committed is left to the other resolvers'() { + given: + def resolver = resolver([problemDetails: true]) + response.committed = true + + expect: + resolver.resolveException(request, response, action(new ProblemController()), new IllegalStateException('secret')) == null + } + + void 'the problem has the status and detail #description declares'() { + given: + def resolver = resolver([problemDetails: true]) + + when: + resolver.resolveException(request, response, action(new ProblemController()), exception) + + then: + response.status == status + problem().detail == detail + + where: + description | exception | status | detail + 'a ResponseStatusException' | new ResponseStatusException(HttpStatus.GONE, 'Withdrawn') | 410 | 'Withdrawn' + '@ResponseStatus' | new TeapotException() | 418 | 'Short and stout' + '@ResponseStatus of a cause' | new RuntimeException(new TeapotException()) | 418 | 'Short and stout' + 'an ErrorResponse' | new ErrorResponseException(HttpStatus.SERVICE_UNAVAILABLE, ProblemDetail.forStatusAndDetail(HttpStatus.SERVICE_UNAVAILABLE, 'Later'), null) | 503 | 'Later' + } + + void 'an exception wrapped by a dynamic invocation is written as the exception the action threw'() { + given: + def resolver = resolver([problemDetails: true]) + def thrown = new ErrorResponseException(HttpStatus.CONFLICT, ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT, 'Taken'), null) + + when: + resolver.resolveException(request, response, action(new ProblemController()), new InvokerInvocationException(thrown)) + + then: + response.status == 409 + problem().detail == 'Taken' + } + + void 'an exception wrapped by #wrapper keeps its own status, whatever caused it'() { + given: + def resolver = resolver([problemDetails: true]) + def thrown = new ResponseStatusException(HttpStatus.GONE, 'Gone for good', new IllegalStateException('why')) + + when: + resolver.resolveException(request, response, action(new ProblemController()), wrap.call(thrown)) + + then: + response.status == 410 + problem().status == 410 + + where: + wrapper | wrap + 'a dynamic invocation' | { Throwable it -> new InvokerInvocationException(it) } + 'an action execution' | { Throwable it -> new ControllerExecutionException('Failed', it) } + 'both' | { Throwable it -> new ControllerExecutionException('Failed', new InvokerInvocationException(it)) } + } + + void 'a request that can no longer be answered writes nothing'() { + given: + def resolver = resolver([problemDetails: true]) + + when: + def modelAndView = resolver.resolveException(request, response, action(new ProblemController()), new AsyncRequestNotUsableException('gone')) + + then: + modelAndView.empty + !response.contentAsString + } + + void 'an exception a URL mapping names, or whose cause it names, is left to the Grails exception resolver'() { + given: + def resolver = resolver([problemDetails: true]) { + "500"(controller: 'errors', action: 'denied', exception: SecurityException) + "500"(controller: 'errors', action: 'serverError') + } + + expect: + resolver.resolveException(request, response, action(new ProblemController()), new SecurityException('denied')) == null + resolver.resolveException(request, response, action(new ProblemController()), new RuntimeException(new SecurityException('denied'))) == null + + and: 'a mapping for the status alone is the error page of a client that prefers HTML' + resolver.resolveException(request, response, action(new ProblemController()), new IllegalStateException('secret')) != null + response.status == 500 + } + + void 'the exception handlers of the advice go first, with #settings'() { + given: + def resolver = resolver(settings) + + when: + resolver.resolveException(request, response, action(new ProblemController()), new IllegalArgumentException('advised')) + + then: + response.status == 422 + problem().detail == 'advised' + + when: 'none of them handles it' + response = new MockHttpServletResponse() + resolver.resolveException(request, response, action(new ProblemController()), new IllegalStateException('secret')) + + then: + response.status == 500 + problem() == [title: 'Internal Server Error', status: 500] + + where: + settings << [[problemDetails: true], [springHandlers: true, problemDetails: true]] + } + + void 'an application that turns the exception handlers off gets the problem without them'() { + given: + def resolver = resolver([springHandlers: false, problemDetails: true]) + + when: + resolver.resolveException(request, response, action(new ProblemController()), new IllegalArgumentException('advised')) + + then: + response.status == 500 + problem() == [title: 'Internal Server Error', status: 500] + } + + void 'a controller the request has not created yet does not stop an exception being written'() { + given: + def resolver = resolver([problemDetails: true]) + def info = new GrailsControllerUrlMappingInfo(new DefaultGrailsControllerClass(ProblemController), Stub(UrlMappingInfo) { + getActionName() >> 'index' + }) + + when: + resolver.resolveException(request, response, info, new IllegalStateException('secret')) + + then: + response.status == 500 + problem().status == 500 + } + + void 'a server error is recorded on the HTTP server observation, and a client error is not'() { + given: + def resolver = resolver([problemDetails: true]) + def observation = new ServerRequestObservationContext(request, response) + request.setAttribute(ServerHttpObservationFilter.CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE, observation) + def serverError = new IllegalStateException('secret') + + when: + resolver.resolveException(request, response, action(new ProblemController()), new ResponseStatusException(HttpStatus.GONE)) + + then: + observation.error == null + + when: + response = new MockHttpServletResponse() + resolver.resolveException(request, response, action(new ProblemController()), serverError) + + then: + observation.error.is(serverError) + } + + void 'the handlers of the observation, one of which marks the span, are told of a server error and not of a client error'() { + given: 'the filter a running application has around its requests' + List notified = [] + def registry = ObservationRegistry.create() + registry.observationConfig().observationHandler(new ObservationHandler() { + @Override + boolean supportsContext(Observation.Context context) { + context instanceof ServerRequestObservationContext + } + + @Override + void onError(Observation.Context context) { + notified << context.error + } + }) + def resolver = resolver([problemDetails: true]) + def handler = action(new ProblemController()) + FilterChain application = { req, res -> + resolver.resolveException((HttpServletRequest) req, (HttpServletResponse) res, handler, thrown) + } as FilterChain + + when: + new ServerHttpObservationFilter(registry).doFilter(request, response, application) + + then: + notified == (told ? [thrown] : []) + + where: + thrown | told + new IllegalStateException('secret') | true + new ResponseStatusException(HttpStatus.BAD_REQUEST) | false + } + + void 'an Error an action threw is recorded on the HTTP server observation as itself, when #wrapper wrapped it'() { + given: + def resolver = resolver([problemDetails: true]) + def observation = new ServerRequestObservationContext(request, response) + request.setAttribute(ServerHttpObservationFilter.CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE, observation) + + when: + resolver.resolveException(request, response, action(new ProblemController()), thrown) + + then: 'as the filter that observes the request reports one that reaches it' + response.status == 500 + problem().status == 500 + observation.error.is(error) + + where: + wrapper | error | thrown + 'Spring MVC' | new AssertionError('failed') | new ServletException("Handler dispatch failed: $error", error) + 'a dynamic invocation' | new StackOverflowError('deep') | new InvokerInvocationException(error) + } + + void 'the request of an unexpected exception is logged, and that of an exception raised on purpose is not'() { + given: + def resolver = resolver([problemDetails: true]) + def logCapture = new LogCapture(ActionExceptionHandlerResolver) + request.method = 'GET' + + when: + resolver.resolveException(request, response, action(new ProblemController()), + new ErrorResponseException(HttpStatus.SERVICE_UNAVAILABLE)) + + then: + response.status == 503 + logCapture.events.empty + + when: + response = new MockHttpServletResponse() + resolver.resolveException(request, response, action(new ProblemController()), new IllegalStateException('secret')) + + then: + logCapture.events*.formattedMessage == ['IllegalStateException occurred when processing request: [GET] /problem/index'] + + cleanup: + logCapture.close() + } + + void 'the advice for the controller the request is mapped to applies to the problem when the request holds #held'() { + given: + def resolver = resolver([problemDetails: true]) + if (controller != null) { + request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller) + } + def info = new GrailsControllerUrlMappingInfo(new DefaultGrailsControllerClass(ProblemController), Stub(UrlMappingInfo) { + getActionName() >> 'index' + }) + + when: + resolver.resolveException(request, response, info, new IllegalStateException('secret')) + + then: + problem().status == 500 + response.getHeader(ProblemResponseAdvice.HEADER) == ProblemController.simpleName + + where: + held | controller + 'no controller, as when an interceptor fails before creating it' | null + 'the controller that forwarded to it' | new ForwardingProblemController() + 'that controller' | new ProblemController() + } + + void 'an ErrorResponse.Interceptor sees the problem, as it sees one a handler method returns'() { + given: + interceptors = [new ErrorResponse.Interceptor() { + @Override + void handleError(ProblemDetail detail, ErrorResponse errorResponse) { + detail.instance = URI.create('urn:problem:1') + } + }] + def resolver = resolver([problemDetails: true]) + + when: + resolver.resolveException(request, response, action(new ProblemController()), new IllegalStateException('secret')) + + then: + problem().instance == 'urn:problem:1' + } + + void 'a failure to write the problem leaves the exception to the other resolvers, which can still render a view'() { + given: + def resolver = resolver([problemDetails: true]) + def failing = Spy(response) { + getOutputStream() >> { throw new IOException('closed') } + } + + expect: + resolver.resolveException(request, failing, action(new ProblemController()), new IllegalStateException('secret')) == null + webRequest.renderView + } + + void 'a client that disconnects while the problem is written gets nothing more, and nothing is logged as a failure'() { + given: + def resolver = resolver([problemDetails: true]) + def logCapture = new LogCapture(ActionExceptionHandlerResolver) + def disconnected = Spy(response) { + getOutputStream() >> { throw new IOException('Broken pipe') } + } + + when: + def modelAndView = resolver.resolveException(request, disconnected, action(new ProblemController()), new IllegalStateException('secret')) + + then: + modelAndView.empty + !logCapture.events.any { it.level.isGreaterOrEqual(Level.WARN) && !it.formattedMessage.contains('occurred when processing request') } + + cleanup: + logCapture.close() + } + + void 'a disconnected client is not logged as a request that failed'() { + given: + def resolver = resolver([problemDetails: true]) + def logCapture = new LogCapture(ActionExceptionHandlerResolver) + + when: + resolver.resolveException(request, response, action(new ProblemController()), new IOException('Broken pipe')) + + then: + response.status == 500 + !logCapture.events.any { it.level.isGreaterOrEqual(Level.WARN) } + + cleanup: + logCapture.close() + } + + void 'the request of a forwarded action is logged by the URI the client asked for'() { + given: + def resolver = resolver([problemDetails: true]) + def logCapture = new LogCapture(ActionExceptionHandlerResolver) + request.method = 'GET' + request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, '/asked/for') + + when: + resolver.resolveException(request, response, action(new ProblemController()), new IllegalStateException('secret')) + + then: + logCapture.events*.formattedMessage == ['IllegalStateException occurred when processing request: [GET] /asked/for'] + + cleanup: + logCapture.close() + } + + void 'an action a page includes is left to the other resolvers, so that a problem is not written into the page'() { + given: + def resolver = resolver([problemDetails: true]) + request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, '/problem/index') + + expect: + resolver.resolveException(request, response, action(new ProblemController()), new IllegalStateException('secret')) == null + !response.contentAsString + } + + void 'an exception handler method that fails does not stop the problem being written in another type'() { + given: + def resolver = resolver([problemDetails: true]) + + when: + resolver.resolveException(request, response, action(new ProblemController()), new UnsupportedOperationException('csv')) + + then: + response.status == 500 + response.contentType == 'application/problem+json' + } + + void 'an asynchronous timeout a URL mapping names under its own status is left to the Grails exception resolver'() { + given: + def resolver = resolver([problemDetails: true]) { + "503"(controller: 'errors', action: 'timedOut', exception: AsyncRequestTimeoutException) + } + + expect: + resolver.resolveException(request, response, action(new ProblemController()), new AsyncRequestTimeoutException()) == null + } + + void 'an asynchronous timeout no URL mapping names is written as a problem'() { + given: + def resolver = resolver([problemDetails: true]) + + when: + resolver.resolveException(request, response, action(new ProblemController()), new AsyncRequestTimeoutException()) + + then: + response.status == 503 + problem().status == 503 + } + + private Map problem() { + new JsonSlurper().parseText(response.contentAsString) as Map + } + + private ActionExceptionHandlerResolver resolver(Map settings, Closure mappings = null) { + context = new GenericWebApplicationContext() + context.environment.propertySources.addFirst(new MapPropertySource('test', + settings.collectEntries { key, value -> ["grails.exceptionresolver.$key".toString(), value] } as Map)) + List> converters = [new JacksonJsonHttpMessageConverter()] + context.registerBean(ProblemControllerAdvice) + context.registerBean(ProblemResponseAdvice) + context.registerBean(ExceptionHandlerExceptionResolver, { new ExceptionHandlerExceptionResolver(messageConverters: converters) }) + context.registerBean('requestMappingHandlerAdapter', RequestMappingHandlerAdapter, { + new RequestMappingHandlerAdapter(messageConverters: converters, errorResponseInterceptors: interceptors) + }) + if (mappings != null) { + def grailsContext = new MockApplicationContext() + grailsContext.registerMockBean(GrailsApplication.APPLICATION_ID, new DefaultGrailsApplication()) + def holder = new DefaultUrlMappingsHolder(new DefaultUrlMappingEvaluator(grailsContext).evaluateMappings(mappings)) + context.registerBean(UrlMappingsHolder.BEAN_ID, UrlMappingsHolder, { holder }) + } + context.registerBean(ActionExceptionHandlerResolver) + context.refresh() + context.getBean(ActionExceptionHandlerResolver) + } + + private GrailsControllerUrlMappingInfo action(Object controller) { + request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller) + new GrailsControllerUrlMappingInfo(new DefaultGrailsControllerClass(controller.getClass()), Stub(UrlMappingInfo) { + getActionName() >> 'index' + }) + } +} + +class ProblemController { + + def index() { + } +} + +class ForwardingProblemController { + + def index() { + } +} + +@ResponseStatus(code = HttpStatus.I_AM_A_TEAPOT, reason = 'Short and stout') +class TeapotException extends RuntimeException { +} + +@ControllerAdvice(assignableTypes = ProblemController) +class ProblemControllerAdvice { + + @ExceptionHandler(IllegalArgumentException) + ProblemDetail unprocessable(IllegalArgumentException e) { + ProblemDetail.forStatusAndDetail(HttpStatus.UNPROCESSABLE_CONTENT, e.message) + } + + @ExceptionHandler(value = UnsupportedOperationException, produces = 'text/csv') + String failing(UnsupportedOperationException e) { + throw e + } +} + +@ControllerAdvice(assignableTypes = ProblemController) +class ProblemResponseAdvice implements ResponseBodyAdvice { + + static final String HEADER = 'X-Advised-For' + + @Override + boolean supports(MethodParameter returnType, Class> converterType) { + true + } + + @Override + Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, + Class> selectedConverterType, ServerHttpRequest request, + ServerHttpResponse response) { + response.headers.set(HEADER, ProblemController.simpleName) + body + } +} diff --git a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/ActionReturnValuesSpec.groovy b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/ActionReturnValuesSpec.groovy index 6b6260de5a7..9456b7668f0 100644 --- a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/ActionReturnValuesSpec.groovy +++ b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/ActionReturnValuesSpec.groovy @@ -21,6 +21,8 @@ package org.grails.web.mapping.mvc import java.util.concurrent.Callable import java.util.concurrent.CompletableFuture +import jakarta.servlet.DispatcherType + import com.fasterxml.jackson.annotation.JsonView import groovy.json.JsonSlurper import jakarta.servlet.DispatcherType @@ -144,7 +146,9 @@ class ActionReturnValuesSpec extends AbstractUrlMappingsSpec { response.status == 422 response.contentType == MediaType.APPLICATION_PROBLEM_JSON_VALUE json().detail == 'invalid' - json().instance == '/returnValues/action' + + and: 'Spring MVC does not give it an instance' + !json().containsKey('instance') } void 'a ProblemDetail an action of an error page returns is written, whatever the status of the response'() {