Skip to content

QueryUtils.checkSortExpression rejects valid property names containing Unicode combining marks (Devanagari and other Indic scripts, Thai, decomposed Latin, …) #4364

Closed

Description

@vinodrenu

Summary

Since Spring Data JPA 4.0.7, sorting on an entity property is rejected whenever the property name contains a Unicode combining mark (general category M: Mn, Mc or Me). The name can still be a perfectly valid Java identifier. The call fails with:

org.springframework.dao.InvalidDataAccessApiUsageException: Sort expression 'निर्माणतिथि: DESC' must only
contain property references or aliases used in the select clause; If you really want to use something other
than that for sorting, please use JpaSort.unsafe(…)

Combining marks are not optional in Indic scripts. Devanagari (Hindi, Marathi, Nepali, Sanskrit), Bengali, Tamil, Telugu, Kannada, Malayalam, Gujarati, Gurmukhi and Odia write almost every word with vowel signs (matras) and the virama, all of which are combining marks. As a result, virtually every identifier in these scripts is rejected. Thai, Hebrew with vowel points, Arabic with harakat, and Latin text in Unicode NFD form (e + U+0301) are affected in the same way.

Both of these ordinary, non-injectable sort paths are broken:

API Path
Derived query: findAllByOrderByनिर्माणतिथिDesc() PartTreeJpaQuery → JpaQueryCreator.buildQuery → QueryUtils.checkSortExpression
findAll(Sort.by(DESC, "निर्माणतिथि")) SimpleJpaRepository.getQuery → QueryUtils.toOrders → toJpaOrder → checkSortExpression

The derived query is the more serious case. The property name comes straight from a compiled method name and was already resolved against the entity metamodel, so it cannot carry user input at all. An application that worked on 4.0.6 now returns HTTP 500 on any page backed by such a query, after nothing more than a patch-level upgrade (Spring Boot 4.0.6 → 4.0.8).

Affected versions

Spring Data JPA Release train Spring Boot Result
4.0.5 2025.1.5 4.0.6 ✅ works
4.0.6 2025.1.6 4.0.7 ✅ works (last good)
4.0.7 2025.1.7 4.0.8 ❌ broken (first bad)
4.1.0 / 4.1.1 2026.0.x 4.1.x ❌ broken
main, 4.1.x, 4.0.x branches (as of 2026-09-25) ❌ broken

Environment used for this reproducer: Java 21 (Eclipse Temurin 21.0.8+9), macOS, H2 (in memory), Spring Boot 4.1.1. The problem does not depend on the database: the exception is thrown before any SQL is generated.

Steps to reproduce

./mvnw test

Expected: all 8 tests pass.
Actual: 2 tests fail:

[ERROR] DevanagariSortTest.derivedOrderByOnDevanagariProperty:48 » InvalidDataAccessApiUsage Sort expression 'निर्माणतिथि: DESC' must only contain property references ...
[ERROR] DevanagariSortTest.sortByOnDevanagariProperty:53 » InvalidDataAccessApiUsage Sort expression 'निर्माणतिथि: DESC' must only contain property references ...
[ERROR] Tests run: 8, Failures: 0, Errors: 2, Skipped: 0

Stack trace for the derived query:

org.springframework.dao.InvalidDataAccessApiUsageException: Sort expression 'निर्माणतिथि: DESC' must only contain property references ...
	at org.springframework.data.jpa.repository.query.QueryUtils.checkSortExpression(QueryUtils.java:804)
	at org.springframework.data.jpa.repository.query.JpaQueryCreator.buildQuery(JpaQueryCreator.java:242)
	at org.springframework.data.jpa.repository.query.JpaQueryCreator.createQuery(JpaQueryCreator.java:214)
	at org.springframework.data.jpa.repository.query.JpaQueryCreator.complete(JpaQueryCreator.java:208)
	at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:96)
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer$CacheableJpqlQueryCreator.<init>(PartTreeJpaQuery.java:335)
	...

Stack trace for findAll(Sort):

	at org.springframework.data.jpa.repository.query.QueryUtils.checkSortExpression(QueryUtils.java:804)
	at org.springframework.data.jpa.repository.query.QueryUtils.toJpaOrder(QueryUtils.java:756)
	at org.springframework.data.jpa.repository.query.QueryUtils.toOrders(QueryUtils.java:707)
	at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:867)
	...
	at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:449)

To show the regression, run the same tests against the last good release. All 8 pass:

./mvnw test -Dspring-data-bom.version=2025.1.6   # Spring Data JPA 4.0.6 → Tests run: 8, Failures: 0, Errors: 0
./mvnw test -Dspring-data-bom.version=2025.1.7   # Spring Data JPA 4.0.7 → Tests run: 8, Errors: 2

What does the sample project contains

spring-data-jpa-devanagari-sort-repro.zip

File Purpose
Notice.java Entity with two identical timestamps: निर्माणतिथि (Hindi for "creation date") and the ASCII control createdAt.
NoticeRepository.java findAllByOrderByनिर्माणतिथिDesc() (bug), findAllByOrderByCreatedAtDesc() (control), and an @Query workaround.
DevanagariSortTest.java @DataJpaTest with 8 tests: the 2 reproductions, the ASCII control, 2 workarounds, and 3 tests that pin down the root cause and check the suggested fix against upstream's own test inputs.

Root cause

Commit 68395c1 (GH-4325) replaced the denylist in QueryUtils

private static final Pattern PUNCTATION_PATTERN = compile(".*((?![._])[\\p{Punct}|\\s])");

with an allowlist:

private static final Pattern PUNCTATION_PATTERN = compile("[^\\p{L}\\p{N}._]", UNICODE_CHARACTER_CLASS);

The allowlist admits letters (\p{L}), numbers (\p{N}), . and _. It leaves out marks (\p{M}), although marks are a required part of words in many scripts. Here is how निर्माणतिथि breaks down (printed by the test rootCauseCombiningMarksAreNotAllowedByCurrentPattern):

Code point Char Unicode category Character.isJavaIdentifierPart Rejected by current pattern
U+0928 न Lo (OTHER_LETTER) true no
U+093F ि Mc (COMBINING_SPACING_MARK) true yes
U+0930 र Lo true no
U+094D ् Mn (NON_SPACING_MARK), virama true yes
U+092E म Lo true no
U+093E ा Mc true yes
U+0923 ण Lo true no
U+0924 त Lo true no
U+093F ि Mc true yes
U+0925 थ Lo true no
U+093F ि Mc true yes

The Java Language Specification explicitly allows Mn and Mc characters in identifiers (Character.isJavaIdentifierPart). So Java accepts the field, Hibernate maps it, and the derived-query parser resolves it as a PropertyPath. Only the sort check rejects it.

The upstream test added with GH-4325 (allowsUtf8ColumnNames) covers Chinese, Greek, Cyrillic, Turkish and José. It uses precomposed é (U+00E9, category Ll), and none of the other inputs contain combining marks, which is why the gap went unnoticed. The same José written in NFD form (e + U+0301) is rejected today (test notOnlyIndicScriptsLatinInDecomposedFormIsRejectedToo).

Before 4.0.7, the old denylist looked only at \p{Punct} and whitespace. It never matched combining marks, so these names worked.

Suggested fix

Add \p{M} to the allowed character classes in QueryUtils:

private static final Pattern PUNCTATION_PATTERN = compile("[^\\p{L}\\p{M}\\p{N}._]", UNICODE_CHARACTER_CLASS);

Why this is safe:

  • Combining marks can't form JPQL/SQL syntax. They are not operators, quotes, parentheses, separators, whitespace or format controls, and they only change the base character before them.
  • Every input that Refine validation #4325 set out to reject is still rejected, because none of them is in category M: sum(foo), full-width parentheses (), ', /, NBSP U+00A0, U+2028/U+2029, zero-width space U+200B, full-width </=, BOM U+FEFF, word joiner U+2060, minus sign U+2212, and the bidi controls U+202C/U+202E. Those are categories Cf, Zs, Zl, Zp, Sm, Ps/Pe and Po. The test suggestedFixStillRejectsEveryInputRejectedByUpstreamTest runs the fixed pattern against upstream's exact inputs from QueryUtilsUnitTests.doesNotPrefixSortsIfFunction and allowsUtf8ColumnNames. All rejected inputs stay rejected, and all accepted inputs, plus Devanagari and NFD Latin, are accepted.

Another option is to base the check on Java's own identifier rules: accept .-separated segments where each code point satisfies Character.isJavaIdentifierPart but is not Character.isIdentifierIgnorable, since the latter excludes the Cf format controls. That matches exactly the set of names a JPA property can have.

Suggested addition to QueryUtilsUnitTests.allowsUtf8ColumnNames:

@ValueSource(strings = { "员工编号", "员工.姓名", "Αναγνωριστικό_εργαζομένου", "Співробітники", "Çalışanlar", "José",
		"निर्माणतिथि",      // Devanagari (Hindi): vowel signs U+093F/U+093E (Mc), virama U+094D (Mn)
		"தேதி",            // Tamil
		"วันที่",            // Thai: U+0E31, U+0E35, U+0E48 (Mn)
		"José" })     // Latin in NFD: e + COMBINING ACUTE ACCENT (Mn)

Workarounds (until fixed)

  1. Put the ORDER BY in JPQL inside @Query (see NoticeRepository.workaroundOrderedByनिर्माणतिथि()). A static ORDER BY doesn't go through checkSortExpression. The method name can stay the same, so callers don't change.
  2. Use JpaSort.unsafe(...) for runtime sorts (see test workaroundJpaSortUnsafe). This works, but it drops the protection for all characters. That is a poor trade-off when the sort property can come from a request parameter.
  3. Pin Spring Data to release train 2025.1.6 (spring-data-bom.version). This works only on Spring Boot 4.0.x.

Activity

  1. added
    type: regression
    and removed
    status: waiting-for-triage
    on Sep 28, 2026
  2. christophstrobl commented on Sep 29, 2026

    @christophstrobl
    Member

    Thank you @vinodrenu for reporting - seems we're a bit too restrictive.

    For future reports please make sure to keep things short and as concise as possible. A, what seems to be an AI generated, wall of text does not add much value.

  3. added 2 commits that reference this issue on Sep 30, 2026
    d39ddfa
    cfbad76
  4. added a commit that references this issue on Oct 1, 2026
    5cf2dd8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

type: regression

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions