Skip to content

Commit d22bbb5

Browse files
dreab8Sanne
authored andcommitted
HHH-14225 CVE-2020-25638 Potential for SQL injection on use_sql_comments logging enabled
1 parent d48e19d commit d22bbb5

File tree

12 files changed

+217
-9
lines changed

12 files changed

+217
-9
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Map;
2525
import java.util.Properties;
2626
import java.util.Set;
27+
import java.util.regex.Pattern;
2728

2829
import org.hibernate.HibernateException;
2930
import org.hibernate.LockMode;
@@ -140,6 +141,9 @@ public abstract class Dialect implements ConversionContext {
140141
*/
141142
public static final String CLOSED_QUOTE = "`\"]";
142143

144+
private static final Pattern ESCAPE_CLOSING_COMMENT_PATTERN = Pattern.compile( "\\*/" );
145+
private static final Pattern ESCAPE_OPENING_COMMENT_PATTERN = Pattern.compile( "/\\*" );
146+
143147
private final TypeNames typeNames = new TypeNames();
144148
private final TypeNames hibernateTypeNames = new TypeNames();
145149

@@ -3002,6 +3006,14 @@ public String addSqlHintOrComment(
30023006
}
30033007

30043008
protected String prependComment(String sql, String comment) {
3005-
return "/* " + comment + " */ " + sql;
3009+
return "/* " + escapeComment( comment ) + " */ " + sql;
3010+
}
3011+
3012+
public static String escapeComment(String comment) {
3013+
if ( StringHelper.isNotEmpty( comment ) ) {
3014+
final String escaped = ESCAPE_CLOSING_COMMENT_PATTERN.matcher( comment ).replaceAll( "*\\\\/" );
3015+
return ESCAPE_OPENING_COMMENT_PATTERN.matcher( escaped ).replaceAll( "/\\\\*" );
3016+
}
3017+
return comment;
30063018
}
30073019
}

hibernate-core/src/main/java/org/hibernate/loader/plan/exec/query/internal/SelectStatementBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public String toStatementString() {
187187
StringBuilder buf = new StringBuilder( guesstimatedBufferSize );
188188

189189
if ( StringHelper.isNotEmpty( comment ) ) {
190-
buf.append( "/* " ).append( comment ).append( " */ " );
190+
buf.append( "/* " ).append( Dialect.escapeComment( comment ) ).append( " */ " );
191191
}
192192

193193
buf.append( "select " )

hibernate-core/src/main/java/org/hibernate/sql/Delete.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.LinkedHashMap;
1010
import java.util.Map;
1111

12+
import org.hibernate.dialect.Dialect;
13+
1214
/**
1315
* An SQL <tt>DELETE</tt> statement
1416
*
@@ -36,7 +38,7 @@ public Delete setTableName(String tableName) {
3638
public String toStatementString() {
3739
StringBuilder buf = new StringBuilder( tableName.length() + 10 );
3840
if ( comment!=null ) {
39-
buf.append( "/* " ).append(comment).append( " */ " );
41+
buf.append( "/* " ).append( Dialect.escapeComment( comment ) ).append( " */ " );
4042
}
4143
buf.append( "delete from " ).append(tableName);
4244
if ( where != null || !primaryKeyColumns.isEmpty() || versionColumnName != null ) {

hibernate-core/src/main/java/org/hibernate/sql/Insert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public Insert setTableName(String tableName) {
9090
public String toStatementString() {
9191
StringBuilder buf = new StringBuilder( columns.size()*15 + tableName.length() + 10 );
9292
if ( comment != null ) {
93-
buf.append( "/* " ).append( comment ).append( " */ " );
93+
buf.append( "/* " ).append( Dialect.escapeComment( comment ) ).append( " */ " );
9494
}
9595
buf.append("insert into ")
9696
.append(tableName);

hibernate-core/src/main/java/org/hibernate/sql/InsertSelect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public String toStatementString() {
6565

6666
StringBuilder buf = new StringBuilder( (columnNames.size() * 15) + tableName.length() + 10 );
6767
if ( comment!=null ) {
68-
buf.append( "/* " ).append( comment ).append( " */ " );
68+
buf.append( "/* " ).append( Dialect.escapeComment( comment ) ).append( " */ " );
6969
}
7070
buf.append( "insert into " ).append( tableName );
7171
if ( !columnNames.isEmpty() ) {

hibernate-core/src/main/java/org/hibernate/sql/QuerySelect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void addOrderBy(String orderByString) {
126126
public String toQueryString() {
127127
StringBuilder buf = new StringBuilder( 50 );
128128
if ( comment != null ) {
129-
buf.append( "/* " ).append( comment ).append( " */ " );
129+
buf.append( "/* " ).append( Dialect.escapeComment( comment ) ).append( " */ " );
130130
}
131131
buf.append( "select " );
132132
if ( distinct ) {

hibernate-core/src/main/java/org/hibernate/sql/Select.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Select(Dialect dialect) {
4040
public String toStatementString() {
4141
StringBuilder buf = new StringBuilder(guesstimatedBufferSize);
4242
if ( StringHelper.isNotEmpty(comment) ) {
43-
buf.append("/* ").append(comment).append(" */ ");
43+
buf.append( "/* " ).append( Dialect.escapeComment( comment ) ).append( " */ " );
4444
}
4545

4646
buf.append("select ").append(selectClause)

hibernate-core/src/main/java/org/hibernate/sql/SimpleSelect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public String toStatementString() {
148148
);
149149

150150
if ( comment != null ) {
151-
buf.append( "/* " ).append( comment ).append( " */ " );
151+
buf.append( "/* " ).append( Dialect.escapeComment( comment ) ).append( " */ " );
152152
}
153153

154154
buf.append( "select " );

hibernate-core/src/main/java/org/hibernate/sql/Update.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public Update setWhere(String where) {
166166
public String toStatementString() {
167167
StringBuilder buf = new StringBuilder( (columns.size() * 15) + tableName.length() + 10 );
168168
if ( comment!=null ) {
169-
buf.append( "/* " ).append( comment ).append( " */ " );
169+
buf.append( "/* " ).append( Dialect.escapeComment( comment ) ).append( " */ " );
170170
}
171171
buf.append( "update " ).append( tableName ).append( " set " );
172172
boolean assignmentsAppended = false;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.comments;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.Id;
11+
12+
/**
13+
* @author Andrea Boriero
14+
*/
15+
@Entity
16+
public class TestEntity {
17+
@Id
18+
private String id;
19+
20+
private String value;
21+
22+
public TestEntity() {
23+
24+
}
25+
26+
public TestEntity(String id, String value) {
27+
this.id = id;
28+
this.value = value;
29+
}
30+
31+
public String getId() {
32+
return id;
33+
}
34+
35+
public void setId(String id) {
36+
this.id = id;
37+
}
38+
39+
public String getValue() {
40+
return value;
41+
}
42+
43+
public void setValue(String value) {
44+
this.value = value;
45+
}
46+
}

0 commit comments

Comments
 (0)