Skip to content

Commit 1961d39

Browse files
Vitaliy Baschlykoffholly-cummins
Vitaliy Baschlykoff
authored andcommitted
Escape column names with backticks in order by clause of generated hibernate query to prevent potential hql injection
1 parent 6f5a775 commit 1961d39

File tree

2 files changed

+37
-6
lines changed
  • extensions/panache
    • hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test
    • panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime

2 files changed

+37
-6
lines changed

extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/JpaOperationsSortTest.java

+20-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5+
import org.junit.jupiter.api.Assertions;
56
import org.junit.jupiter.api.Test;
67

78
import io.quarkus.panache.common.Sort;
9+
import io.quarkus.panache.common.exception.PanacheQueryException;
810
import io.quarkus.panache.hibernate.common.runtime.PanacheJpaUtil;
911

1012
public class JpaOperationsSortTest {
@@ -18,7 +20,7 @@ public void testEmptySortByYieldsEmptyString() {
1820
@Test
1921
public void testSortBy() {
2022
Sort sort = Sort.by("foo", "bar");
21-
assertEquals(" ORDER BY foo , bar", PanacheJpaUtil.toOrderBy(sort));
23+
assertEquals(" ORDER BY `foo` , `bar`", PanacheJpaUtil.toOrderBy(sort));
2224
}
2325

2426
@Test
@@ -29,14 +31,27 @@ public void testEmptySortEmptyYieldsEmptyString() {
2931

3032
@Test
3133
public void testSortByNullsFirst() {
32-
Sort emptySort = Sort.by("foo", Sort.Direction.Ascending, Sort.NullPrecedence.NULLS_FIRST);
33-
assertEquals(" ORDER BY foo NULLS FIRST", PanacheJpaUtil.toOrderBy(emptySort));
34+
Sort sort = Sort.by("foo", Sort.Direction.Ascending, Sort.NullPrecedence.NULLS_FIRST);
35+
assertEquals(" ORDER BY `foo` NULLS FIRST", PanacheJpaUtil.toOrderBy(sort));
3436
}
3537

3638
@Test
3739
public void testSortByNullsLast() {
38-
Sort emptySort = Sort.by("foo", Sort.Direction.Descending, Sort.NullPrecedence.NULLS_LAST);
39-
assertEquals(" ORDER BY foo DESC NULLS LAST", PanacheJpaUtil.toOrderBy(emptySort));
40+
Sort sort = Sort.by("foo", Sort.Direction.Descending, Sort.NullPrecedence.NULLS_LAST);
41+
assertEquals(" ORDER BY `foo` DESC NULLS LAST", PanacheJpaUtil.toOrderBy(sort));
42+
}
43+
44+
@Test
45+
public void testSortByColumnWithBacktick() {
46+
Sort sort = Sort.by("jeanne", "d`arc");
47+
Assertions.assertThrowsExactly(PanacheQueryException.class, () -> PanacheJpaUtil.toOrderBy(sort),
48+
"Sort column name cannot have backticks");
49+
}
50+
51+
@Test
52+
public void testSortByQuotedColumn() {
53+
Sort sort = Sort.by("`foo`", "bar");
54+
assertEquals(" ORDER BY `foo` , `bar`", PanacheJpaUtil.toOrderBy(sort));
4055
}
4156

4257
}

extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public static String toOrderBy(Sort sort) {
175175
Sort.Column column = sort.getColumns().get(i);
176176
if (i > 0)
177177
sb.append(" , ");
178-
sb.append(column.getName());
178+
sb.append('`').append(unquoteColumnName(column)).append('`');
179179
if (column.getDirection() != Sort.Direction.Ascending) {
180180
sb.append(" DESC");
181181
}
@@ -191,4 +191,20 @@ public static String toOrderBy(Sort sort) {
191191
}
192192
return sb.toString();
193193
}
194+
195+
private static String unquoteColumnName(Sort.Column column) {
196+
String columnName = column.getName();
197+
String unquotedColumnName;
198+
//Note HQL uses backticks to escape/quote special words that are used as identifiers
199+
if (columnName.charAt(0) == '`' && columnName.charAt(columnName.length() - 1) == '`') {
200+
unquotedColumnName = columnName.substring(1, columnName.length() - 1);
201+
} else {
202+
unquotedColumnName = columnName;
203+
}
204+
// Note we're not dealing with columns but with entity attributes so no backticks expected in unquoted column name
205+
if (unquotedColumnName.indexOf('`') >= 0) {
206+
throw new PanacheQueryException("Sort column name cannot have backticks");
207+
}
208+
return unquotedColumnName;
209+
}
194210
}

0 commit comments

Comments
 (0)