You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@grails.apache.org by "jdaugherty (via GitHub)" <gi...@apache.org> on 2026/10/01 16:39:40 UTC
[PR] Recognise constant HQL fragments and declaration-level suppression in the query safety check [grails-core]
jdaugherty opened a new pull request, #16463:
URL: https://github.com/apache/grails-core/pull/16463
The GormUnsafeQueryString check flagged every GString coerced to a String
before reaching a GORM query method, including the common pattern of
assembling query text from fixed HQL fragments chosen at runtime:
String restriction = shouldIncludeTitle ? " and b.title = :title" : ""
String query = "from Book b where 1 = 1 ${restriction}"
Book.executeQuery(query, queryParams)
The documented fix - keep the query a GString - does not apply there: GORM
binds every interpolation as a parameter, so the fragment would be bound as
a value and the query would stop working.
The check now tells query text from values. An interpolation that is
constant text (a literal, a static final field initialised from constant
text, a ternary or Elvis over constant text, a +/GString/cast/toString of
constant text, or a local that only ever held constant text on every path)
cannot carry user input and is no longer a finding. Compound assignment
with += is tracked like x = x + y, so a constant query stays constant when
constant text is appended, and a live GString or already-flattened operand
anywhere in a + concatenation makes the result flattened (an error) rather
than the lower-confidence concatenation warning.
@SuppressWarnings("GormUnsafeQueryString") can now also go on a local
variable or field declaration. It marks that one variable as reviewed for
the rest of the method, whatever it is later assigned, treats it as
constant text wherever it is interpolated, and leaves every other variable
checked.
The error and warning messages, the security guide and the Grails 8
upgrade notes explain the query-text case and no longer recommend a GString
for fragments.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Recognise constant HQL fragments and declaration-level suppression in the query safety check [grails-core]
Posted by "jdaugherty (via GitHub)" <gi...@apache.org>.
jdaugherty merged PR #16463:
URL: https://github.com/apache/grails-core/pull/16463
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Recognise constant HQL fragments and declaration-level suppression in the query safety check [grails-core]
Posted by "jdaugherty (via GitHub)" <gi...@apache.org>.
jdaugherty commented on PR #16463:
URL: https://github.com/apache/grails-core/pull/16463#issuecomment-5939361082
@sbglasius good catch - that was a real regression. Fixed in https://github.com/apache/grails-core/pull/16463/commits/9ebd139cab0e692cdef4c9923634665e002c7a34 by applying the snapshot/merge model `visitIfElse` already had to every control-flow construct, rather than the blunt "non-constant for the rest of the method" option, so the dynamic-restriction idioms keep working inside loops and switches:
- **`switch`**: every case starts from the pre-switch state merged with the previous case's fall-through state (none when that case ends in `break`/`return`/`throw`/`continue`); the state after the statement is merged from the normal exit and every `break`.
- **`try`/`catch`/`finally`**: a `catch` starts from the merge of every state the `try` block passed through, since an exception can leave it after any statement - so a data assignment that is overwritten later in the same block still reaches the handler. The `finally` block is checked against every path into it, but only the paths that complete the `try` or a `catch` normally continue past the statement.
- **Loops and closure bodies**: re-walked silently from the merged loop-head state until it stops changing, then walked once more with reporting on, so the use-before-write loop case is caught and each finding is reported exactly once. `break`, `continue` and a closure `return` carry their state to where they jump, including labelled jumps.
A side effect of modelling jumps: a branch that `return`s after flattening a query no longer poisons the state after the `if` (previously a false positive), and a `case` that `break`s no longer leaks its state into the next case.
Your spec is in verbatim (the `if/else` row as the control), extended with `do`/`while`, loop `break`/`continue`, closures, and a `try` block that overwrites the data assignment before the `catch`, plus dedicated cases for the use-before-write loop, `continue`, labelled `break`, fall-through and `finally`, and no-warning counterparts for constant text appended in a loop, chosen in every `switch` case, or assigned in both `try` and `catch`. Against dd71f8e the `try/catch`, `switch`, loop, `continue`, labelled-break and `finally` cases fail as you predicted, and so do the two "returns before the query" no-warning cases. Docs and the class Javadoc's limitations list are updated accordingly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Recognise constant HQL fragments and declaration-level suppression in the query safety check [grails-core]
Posted by "testlens-app[bot] (via GitHub)" <gi...@apache.org>.
testlens-app[bot] commented on PR #16463:
URL: https://github.com/apache/grails-core/pull/16463#issuecomment-5939939305
## 🚨 All tests passed but jobs failed 🚨
### Failed Jobs without Test Failures
❌ [CI / Spring Security Tests \(Java 25\)](https://github.com/apache/grails-core/actions/runs/36916533000/job/110551777460?pr=16463)
🏷️ Commit: 9ebd139cab0e692cdef4c9923634665e002c7a34
▶️ Tests: 68924 executed
🟡 Checks: 56/86 completed
### Rerun Controls
Click the checkbox to trigger a rerun:
🔲 **Rerun jobs**
---
_Learn more about TestLens at [testlens.app/docs](https://testlens.app/docs/features/pr-comment/)._
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Recognise constant HQL fragments and declaration-level suppression in the query safety check [grails-core]
Posted by "testlens-app[bot] (via GitHub)" <gi...@apache.org>.
testlens-app[bot] commented on PR #16463:
URL: https://github.com/apache/grails-core/pull/16463#issuecomment-5937697957
## ✅ All tests passed ✅
🏷️ Commit: dd71f8e81a37edd75da8b55ee00b7671dc3e83f6
▶️ Tests: 90622 executed
⚪️ Checks: 85/85 completed
---
_Learn more about TestLens at [testlens.app/docs](https://testlens.app/docs/features/pr-comment/)._
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Recognise constant HQL fragments and declaration-level suppression in the query safety check [grails-core]
Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #16463:
URL: https://github.com/apache/grails-core/pull/16463#issuecomment-5936689739
## [Codecov](https://app.codecov.io/gh/apache/grails-core/pull/16463?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:x: Patch coverage is `86.08696%` with `16 lines` in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 58.9519%. Comparing base ([`d13aa33`](https://app.codecov.io/gh/apache/grails-core/commit/d13aa3328220eba6c517d49a9569e4b091d646e0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`dd71f8e`](https://app.codecov.io/gh/apache/grails-core/commit/dd71f8e81a37edd75da8b55ee00b7671dc3e83f6?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
| [Files with missing lines](https://app.codecov.io/gh/apache/grails-core/pull/16463?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [...rm/query/transform/GormQuerySafetyTransformer.java](https://app.codecov.io/gh/apache/grails-core/pull/16463?src=pr&el=tree&filepath=grails-datamapping-core%2Fsrc%2Fmain%2Fgroovy%2Forg%2Fgrails%2Fdatastore%2Fgorm%2Fquery%2Ftransform%2FGormQuerySafetyTransformer.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Z3JhaWxzLWRhdGFtYXBwaW5nLWNvcmUvc3JjL21haW4vZ3Jvb3Z5L29yZy9ncmFpbHMvZGF0YXN0b3JlL2dvcm0vcXVlcnkvdHJhbnNmb3JtL0dvcm1RdWVyeVNhZmV0eVRyYW5zZm9ybWVyLmphdmE=) | 86.0870% | [5 Missing and 11 partials :warning: ](https://app.codecov.io/gh/apache/grails-core/pull/16463?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
<details><summary>Additional details and impacted files</summary>
[](https://app.codecov.io/gh/apache/grails-core/pull/16463?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
```diff
@@ Coverage Diff @@
## 8.0.x #16463 +/- ##
==================================================
+ Coverage 58.9274% 58.9519% +0.0245%
- Complexity 24440 24482 +42
==================================================
Files 2174 2174
Lines 107366 107452 +86
Branches 19557 19584 +27
==================================================
+ Hits 63268 63345 +77
Misses 35235 35235
- Partials 8863 8872 +9
```
| [Files with missing lines](https://app.codecov.io/gh/apache/grails-core/pull/16463?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [...rm/query/transform/GormQuerySafetyTransformer.java](https://app.codecov.io/gh/apache/grails-core/pull/16463?src=pr&el=tree&filepath=grails-datamapping-core%2Fsrc%2Fmain%2Fgroovy%2Forg%2Fgrails%2Fdatastore%2Fgorm%2Fquery%2Ftransform%2FGormQuerySafetyTransformer.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Z3JhaWxzLWRhdGFtYXBwaW5nLWNvcmUvc3JjL21haW4vZ3Jvb3Z5L29yZy9ncmFpbHMvZGF0YXN0b3JlL2dvcm0vcXVlcnkvdHJhbnNmb3JtL0dvcm1RdWVyeVNhZmV0eVRyYW5zZm9ybWVyLmphdmE=) | `85.8357% <86.0870%> (+2.3151%)` | :arrow_up: |
... and [2 files with indirect coverage changes](https://app.codecov.io/gh/apache/grails-core/pull/16463/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
</details>
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Recognise constant HQL fragments and declaration-level suppression in the query safety check [grails-core]
Posted by "sbglasius (via GitHub)" <gi...@apache.org>.
sbglasius commented on PR #16463:
URL: https://github.com/apache/grails-core/pull/16463#issuecomment-5938011231
**Constant-text tracking is last-write-wins outside `if`/`else`, which creates a new false negative**
Only `visitIfElse` merges state per branch. In `try`/`catch`, `switch` and loops the AST is walked in source order, so a data assignment followed by a constant one leaves the variable classified as constant text:
```groovy
String frag = ""
try { frag = sort } catch (Exception ex) { frag = "" }
String q = "from Book order by ${frag}"
executeQuery(q) // not flagged: frag is in constantTextVars
```
Before this PR any GString with an interpolation assigned to a `String` was a build error, so this is a regression rather than the already-documented limitation (that limitation only used to hide unsafe-to-safe transitions for the `flattened` state, not the direct-interpolation check). `GormQuerySafetyTransformer.isConstantTextVariable` ends in `constantTextVars.contains(...)`.
I found this by reading the code and have not run the spec below; the `try/catch` and `switch` rows should currently fail with "Expected exception ... but no exception was thrown", and the `if/else` row is the control that should pass.
```groovy
@Unroll
void "test variable assigned from data on one path of #construct is not treated as constant text"() {
when:
new GroovyClassLoader().parseClass("""
import grails.gorm.annotation.Entity
@Entity
class Book {
String title
static List sorted(String sort) {
String frag = ""
$body
String q = "from Book order by \${frag}"
executeQuery(q)
}
}
""")
then:
def e = thrown(MultipleCompilationErrorsException)
e.message.contains('GormUnsafeQueryString')
e.message.contains("passed to 'executeQuery'")
where:
construct | body
'if/else' | 'if (sort) { frag = sort } else { frag = " title" }'
'try/catch' | 'try { frag = sort } catch (Exception ex) { frag = "" }'
'switch' | 'switch (sort) { case "a": frag = sort; break; default: frag = " title" }'
}
```
A use-before-write loop (`frag` interpolated, then `frag = sort` later in the same loop body) has the same problem and is worth a row too.
Possible fix: treat any assignment inside a `try`, `switch` or loop as making the variable non-constant for the rest of the method, or override those visitors to merge pessimistically like `visitIfElse` does.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Recognise constant HQL fragments and declaration-level suppression in the query safety check [grails-core]
Posted by "matrei (via GitHub)" <gi...@apache.org>.
matrei commented on PR #16463:
URL: https://github.com/apache/grails-core/pull/16463#issuecomment-5954420234
Post-merge review: the constant-text idea works well for the dynamic-restriction idioms. While reviewing, though, I found a few gaps that I've written up in #16481 and fixed in #16482:
- **Some locals holding a value still pass as constant text.** This happens when a local is assigned somewhere the walk cannot place in order: a multiple assignment, a ternary or `&&`/`||` operand, a method argument, a `do`/`while` condition, or a closure called later. It also happens when a loop variable or closure parameter reuses the name of an earlier constant local. Each case was a build error before this PR.
- **Compile time grows exponentially with nesting.** Every loop and closure body is walked at least twice, inside each walk of its enclosing body. 24 nested closures take about 33 s to compile, and this check runs on every class compiled with `grails-datamapping-core` on the classpath.
- **The docs and the error message overstate what is reported.** Text joined from a `List` or returned from a helper method is not reported when it is passed to the query method on its own. The error message also recommends `StringBuilder`, which the check cannot follow.
#16482 tracks locals per declaration and settles the locals the walk cannot order with a per-method pre-scan. It walks a body that assigns nothing outside it only once, and corrects the message and the guide. Reviews welcome.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org