You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by bo...@apache.org on 2026/06/17 20:59:42 UTC
(grails-core) 01/01: Fix HQL injection: reject plain-String single-arg query overloads
This is an automated email from the ASF dual-hosted git repository.
borinquenkid pushed a commit to branch bug/hibernate-8.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 6e71eb580f9d0eebd6c0aa92496157d5f027ab9c
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Wed Jun 17 15:58:43 2026 -0500
Fix HQL injection: reject plain-String single-arg query overloads
The single-argument CharSequence overloads of find, findAll, executeQuery,
and executeUpdate accepted plain String values without bound parameters,
leaving callers open to HQL/SQL injection via string concatenation.
HqlQueryContext.guardAgainstUnsafeString() now throws
UnsupportedOperationException when a non-GString is passed to any of those
overloads, directing callers to the safe parameterized overloads or Groovy
GString interpolation. Five new Spock features in HqlQueryContextSpec cover
all four method names plus the GString-accepted path.
Co-Authored-By: Claude Sonnet 4.6 <no...@anthropic.com>
---
.../orm/hibernate/HibernateGormStaticApi.groovy | 14 ++++++----
.../orm/hibernate/query/HqlQueryContext.java | 22 +++++++++++++++
.../orm/hibernate/query/HqlQueryContextSpec.groovy | 31 ++++++++++++++++++++++
3 files changed, 62 insertions(+), 5 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy
index d0c8ead287..89469ab716 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy
@@ -295,28 +295,32 @@ class HibernateGormStaticApi<D> extends GormStaticApi<D> {
findAllWithNativeSql(query, args)
}
- // The single-argument CharSequence overloads accept a plain String (executed as written, as
- // on Hibernate 5) or a Groovy GString. A GString is never interpolated into the query text:
- // HqlQueryContext expands every ${value} into a bound named parameter (see
- // buildNamedParameterQueryFromGString), so the idiomatic Groovy form
- // executeQuery("from Foo where bar = ${userInput}") is injection-safe by binding, not escaping.
+ // The single-argument CharSequence overloads require a Groovy GString so that every
+ // interpolated value is automatically extracted into a bound named parameter by
+ // HqlQueryContext.buildNamedParameterQueryFromGString, making the idiomatic form
+ // executeQuery("from Foo where bar = ${userInput}") injection-safe by binding.
+ // Passing a plain String is rejected to prevent silent injection vulnerabilities.
@Override
List<D> findAll(CharSequence query) {
+ HqlQueryContext.guardAgainstUnsafeString(query, 'findAll')
doListInternal(query, [:], [], [:], false)
}
@Override
List executeQuery(CharSequence query) {
+ HqlQueryContext.guardAgainstUnsafeString(query, 'executeQuery')
doListInternal(query, [:], [], [:], false)
}
@Override
Integer executeUpdate(CharSequence query) {
+ HqlQueryContext.guardAgainstUnsafeString(query, 'executeUpdate')
doInternalExecuteUpdate(query, [:], [], [:])
}
@Override
D find(CharSequence query) {
+ HqlQueryContext.guardAgainstUnsafeString(query, 'find')
doSingleInternal(query, [:], [], [:], false)
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HqlQueryContext.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HqlQueryContext.java
index bf38f30742..3367cb0f31 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HqlQueryContext.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HqlQueryContext.java
@@ -71,6 +71,28 @@ public record HqlQueryContext(
// ─── Factory ─────────────────────────────────────────────────────────────
+ /**
+ * Guards the no-params single-argument query overloads (find, findAll, executeQuery,
+ * executeUpdate) against plain-String calls that have no bound parameters and are therefore
+ * vulnerable to HQL/SQL injection.
+ *
+ * <p>Pass the method name so the error message can point the caller to the correct fix.
+ *
+ * @throws UnsupportedOperationException when {@code query} is a plain {@link String} rather
+ * than a Groovy {@link GString}
+ */
+ public static void guardAgainstUnsafeString(CharSequence query, String method) {
+ if (!(query instanceof GString)) {
+ throw new UnsupportedOperationException(
+ method + "(CharSequence) only accepts a Groovy GString with interpolated parameters " +
+ "(e.g. " + method + "(\"from Foo where bar = ${value}\")). " +
+ "Use the parameterized overload " + method + "(CharSequence, Map) or " +
+ method + "(CharSequence, Collection, Map) " +
+ "to pass a plain String query safely."
+ );
+ }
+ }
+
/**
* Resolves the final HQL string, the result target class, and expands any {@link GString} into
* named parameters. No {@code Session} is required.
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/query/HqlQueryContextSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/query/HqlQueryContextSpec.groovy
index c69c7e3cba..a4743e2bfe 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/query/HqlQueryContextSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/query/HqlQueryContextSpec.groovy
@@ -195,6 +195,37 @@ class HqlQueryContextSpec extends Specification {
"select b from Book b" | false
"select count(b.id) from Book b"| true
}
+
+ @Unroll
+ void "guardAgainstUnsafeString rejects plain String for #method"() {
+ given:
+ String userInput = "'; DROP TABLE books; --"
+ String unsafeQuery = "from HqlQueryContextSpecBook where title = '" + userInput + "'"
+
+ when:
+ HqlQueryContext.guardAgainstUnsafeString(unsafeQuery, method)
+
+ then:
+ def ex = thrown(UnsupportedOperationException)
+ ex.message.startsWith("${method}(CharSequence)")
+ ex.message.contains("Groovy GString")
+ ex.message.contains("${method}(CharSequence, Map)")
+
+ where:
+ method << ["find", "findAll", "executeQuery", "executeUpdate"]
+ }
+
+ void "guardAgainstUnsafeString accepts GString safely"() {
+ given:
+ String title = "The Hobbit"
+ GString safeQuery = "from HqlQueryContextSpecBook where title = ${title}"
+
+ when:
+ HqlQueryContext.guardAgainstUnsafeString(safeQuery, "find")
+
+ then:
+ noExceptionThrown()
+ }
}
@Entity