You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by ja...@apache.org on 2026/07/01 22:59:34 UTC
(grails-core) 04/04: Add Hibernate 7 secure binding examples
This is an automated email from the ASF dual-hosted git repository.
jamesfredley pushed a commit to branch feat/secure-bind-data
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit e6c318ea11394ee43c62ea509e70ec14c7d067da
Author: James Fredley <ja...@users.noreply.github.com>
AuthorDate: Wed Jul 1 11:54:25 2026 -0400
Add Hibernate 7 secure binding examples
Assisted-by: opencode:gpt-5.5
---
.../binding/AdvancedDataBindingController.groovy | 70 ++++++++++++++++-
.../binding/AdvancedDataBindingSpec.groovy | 89 ++++++++++++++++++++++
2 files changed, 158 insertions(+), 1 deletion(-)
diff --git a/grails-test-examples/hibernate7/app1/grails-app/controllers/functionaltests/binding/AdvancedDataBindingController.groovy b/grails-test-examples/hibernate7/app1/grails-app/controllers/functionaltests/binding/AdvancedDataBindingController.groovy
index 78ed103c0a..97eb43b927 100644
--- a/grails-test-examples/hibernate7/app1/grails-app/controllers/functionaltests/binding/AdvancedDataBindingController.groovy
+++ b/grails-test-examples/hibernate7/app1/grails-app/controllers/functionaltests/binding/AdvancedDataBindingController.groovy
@@ -109,7 +109,75 @@ class AdvancedDataBindingController {
contributors: contributorsMap
] as JSON)
}
-
+
+ def secureBindEmployee() {
+ def employee = new Employee()
+ secureBindData(employee, params, ['firstName', 'homeAddress.street', 'homeAddress.city'])
+ render([
+ firstName: employee.firstName,
+ email: employee.email,
+ homeAddress: employee.homeAddress ? [
+ street: employee.homeAddress.street,
+ city: employee.homeAddress.city,
+ state: employee.homeAddress.state
+ ] : null,
+ workAddress: employee.workAddress ? [
+ street: employee.workAddress.street,
+ city: employee.workAddress.city,
+ state: employee.workAddress.state
+ ] : null
+ ] as JSON)
+ }
+
+ def secureBindTeamWithMembers() {
+ def team = new Team()
+ secureBindData(team, params, ['name', 'members.name', 'members.role'])
+ render([
+ name: team.name,
+ members: team.members?.findAll { it != null }?.collect { [name: it.name, role: it.role] } ?: []
+ ] as JSON)
+ }
+
+ def secureBindProjectWithContributors() {
+ def project = new Project()
+ secureBindData(project, params, ['name', 'contributors.name', 'contributors.expertise'])
+ def contributorsMap = project.contributors?.collectEntries { k, v ->
+ [k, [name: v?.name, expertise: v?.expertise]]
+ } ?: [:]
+ render([
+ name: project.name,
+ contributors: contributorsMap
+ ] as JSON)
+ }
+
+ def secureBindWithPrefix() {
+ def employee = new Employee()
+ secureBindData(employee, params, ['firstName', 'lastName'], 'employee')
+ render([
+ firstName: employee.firstName,
+ lastName: employee.lastName,
+ email: employee.email
+ ] as JSON)
+ }
+
+ def secureBindWithNullMissing() {
+ def employee = new Employee(firstName: 'Existing', email: 'existing@example.com')
+ secureBindData(employee, params, ['firstName', 'email'], nullMissing: true)
+ render([
+ firstName: employee.firstName,
+ email: employee.email
+ ] as JSON)
+ }
+
+ def secureBindWithEmptyAllowlist() {
+ def employee = new Employee(firstName: 'Existing', email: 'existing@example.com')
+ secureBindData(employee, params, [])
+ render([
+ firstName: employee.firstName,
+ email: employee.email
+ ] as JSON)
+ }
+
/**
* Test binding with @RequestParameter annotation.
*/
diff --git a/grails-test-examples/hibernate7/app1/src/integration-test/groovy/functionaltests/binding/AdvancedDataBindingSpec.groovy b/grails-test-examples/hibernate7/app1/src/integration-test/groovy/functionaltests/binding/AdvancedDataBindingSpec.groovy
index c90ee6c764..e0f9f9929d 100644
--- a/grails-test-examples/hibernate7/app1/src/integration-test/groovy/functionaltests/binding/AdvancedDataBindingSpec.groovy
+++ b/grails-test-examples/hibernate7/app1/src/integration-test/groovy/functionaltests/binding/AdvancedDataBindingSpec.groovy
@@ -233,6 +233,95 @@ class AdvancedDataBindingSpec extends Specification implements HttpClientSupport
}
}
+ def "test secureBindData binds nested allowed properties and blocks unlisted properties"() {
+ when:
+ def response = http(
+ '/advancedDataBinding/secureBindEmployee?firstName=Secure&email=blocked%40example.com&homeAddress.street=123+Secure+St&homeAddress.city=Portland&workAddress.street=999+Blocked+Ave',
+ )
+
+ then:
+ response.assertStatus(200)
+ with(response.json()) {
+ firstName == 'Secure'
+ email == null
+ homeAddress.street == '123 Secure St'
+ homeAddress.city == 'Portland'
+ homeAddress.state == null
+ workAddress == null
+ }
+ }
+
+ def "test secureBindData binds allowed List collection properties"() {
+ when:
+ def response = http(
+ '/advancedDataBinding/secureBindTeamWithMembers?name=Security&members%5B0%5D.name=Alice&members%5B0%5D.role=Lead&members%5B1%5D.name=Bob&members%5B1%5D.role=Developer',
+ )
+
+ then:
+ response.assertStatus(200)
+ with(response.json()) {
+ name == 'Security'
+ members.size() == 2
+ members[0].name == 'Alice'
+ members[0].role == 'Lead'
+ members[1].name == 'Bob'
+ members[1].role == 'Developer'
+ }
+ }
+
+ def "test secureBindData binds allowed Map collection properties"() {
+ when:
+ def response = http(
+ '/advancedDataBinding/secureBindProjectWithContributors?name=SecureCore&contributors%5Blead%5D.name=John&contributors%5Blead%5D.expertise=Architecture&contributors%5Bdev%5D.name=Jane&contributors%5Bdev%5D.expertise=Testing',
+ )
+
+ then:
+ response.assertStatus(200)
+ with(response.json()) {
+ name == 'SecureCore'
+ contributors.lead.name == 'John'
+ contributors.lead.expertise == 'Architecture'
+ contributors.dev.name == 'Jane'
+ contributors.dev.expertise == 'Testing'
+ }
+ }
+
+ def "test secureBindData supports prefix filtering"() {
+ when:
+ def response = http(
+ '/advancedDataBinding/secureBindWithPrefix?employee.firstName=Prefix&employee.lastName=Allowed&employee.email=blocked%40example.com&other.firstName=Ignored',
+ )
+
+ then:
+ response.assertJson(200, [
+ firstName: 'Prefix',
+ lastName : 'Allowed',
+ email : null
+ ])
+ }
+
+ def "test secureBindData nullMissing clears missing allowed properties"() {
+ when:
+ def response = http('/advancedDataBinding/secureBindWithNullMissing?firstName=Updated')
+
+ then:
+ response.assertJson(200, [
+ firstName: 'Updated',
+ email : null
+ ])
+ }
+
+ def "test secureBindData with empty allowlist binds no properties"() {
+ when:
+ def response = http('/advancedDataBinding/secureBindWithEmptyAllowlist?firstName=Blocked&email=blocked%40example.com')
+
+ then:
+ response.assertJson(200, [
+ firstName: 'Existing',
+ email : 'existing@example.com'
+ ])
+ }
+
// ========== Selective Property Binding Tests ==========
def "test selective property binding using subscript operator"() {