Skip to content

Commit 0298ee3

Browse files
authored
Follow up on GH-16622 - handle also string with URL encoding characters (#16631)
1 parent 56fca16 commit 0298ee3

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

h2o-core/src/main/java/water/jdbc/SQLManager.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import water.fvec.*;
55
import water.parser.ParseDataset;
66
import water.util.Log;
7+
import water.util.StringUtils;
78

89
import java.io.UnsupportedEncodingException;
910
import java.net.URI;
@@ -614,11 +615,29 @@ public static void validateJdbcUrl(String jdbcUrl) throws IllegalArgumentExcepti
614615
throw new IllegalArgumentException("JDBC URL is null or empty");
615616
}
616617

617-
if (!jdbcUrl.toLowerCase().startsWith("jdbc:")) {
618+
String previous = null;
619+
String jdbcUrlDecode = jdbcUrl;
620+
try {
621+
for (int i = 0; i < 10; i++) {
622+
previous = jdbcUrlDecode;
623+
jdbcUrlDecode = URLDecoder.decode(jdbcUrlDecode, "UTF-8");
624+
if (previous.equals(jdbcUrlDecode)) {
625+
break;
626+
}
627+
}
628+
} catch (UnsupportedEncodingException e) {
629+
throw new IllegalArgumentException("JDBC URL has wrong encoding");
630+
}
631+
632+
if (!previous.equals(jdbcUrlDecode)) {
633+
throw new IllegalArgumentException("JDBC URL contains invalid characters");
634+
}
635+
636+
if (!jdbcUrlDecode.toLowerCase().startsWith("jdbc:")) {
618637
throw new IllegalArgumentException("JDBC URL must start with 'jdbc:'");
619638
}
620639

621-
Matcher matcher = JDBC_PARAMETERS_REGEX_PATTERN.matcher(jdbcUrl);
640+
Matcher matcher = JDBC_PARAMETERS_REGEX_PATTERN.matcher(jdbcUrlDecode);
622641
String property = System.getProperty(DISALLOWED_JDBC_PARAMETERS_PARAM);
623642
List<String> disallowedParameters = property == null ?
624643
DEFAULT_JDBC_DISALLOWED_PARAMETERS :

h2o-core/src/test/java/water/jdbc/SQLManagerTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,26 @@ public void testValidateJdbcConnectionStringMysqlOneParameter() {
226226
SQLManager.validateJdbcUrl(jdbcConnection);
227227
}
228228

229+
@Test
230+
public void testValidateJdbcConnectionStringMysqlDoubleEncodedString() {
231+
exception.expect(IllegalArgumentException.class);
232+
exception.expectMessage("Potentially dangerous JDBC parameter found: allowLoadLocalInfile");
233+
234+
String jdbcConnection = "jdbc%3Amysql%3A%2F%2F127.0.0.1%3A3308%2Ftest%3F+%2561%256c%256c%256f%2577%254c%256f%2561%2564%254c%256f%2563%2561%256c%2549%256e%2566%2569%256c%2565%3Dtrue%26+%2561%256c%256c%256f%2577%2555%2572%256c%2549%256e%254c%256f%2563%2561%256c%2549%256e%2566%2569%256c%2565%3Dtrue&table=a&username=fileread_/etc/passwd&password=123123&fetch_mode=SINGLE";
235+
236+
SQLManager.validateJdbcUrl(jdbcConnection);
237+
}
238+
239+
@Test
240+
public void testValidateJdbcConnectionStringMysqlMultipleEncodedString() {
241+
exception.expect(IllegalArgumentException.class);
242+
exception.expectMessage("JDBC URL contains invalid characters");
243+
244+
String jdbcConnection = "jdbc%2525252525252525253Amysql%2525252525252525253A%2525252525252525252F%2525252525252525252F127.0.0.1%2525252525252525253A3308%2525252525252525252Ftest%2525252525252525253F%25252525252525252B%2525252525252525252561%252525252525252525256c%252525252525252525256c%252525252525252525256f%2525252525252525252577%252525252525252525254c%252525252525252525256f%2525252525252525252561%2525252525252525252564%252525252525252525254c%252525252525252525256f%2525252525252525252563%2525252525252525252561%252525252525252525256c%2525252525252525252549%252525252525252525256e%2525252525252525252566%2525252525252525252569%252525252525252525256c%2525252525252525252565%2525252525252525253Dtrue%25252525252525252526%25252525252525252B%2525252525252525252561%252525252525252525256c%252525252525252525256c%252525252525252525256f%2525252525252525252577%2525252525252525252555%2525252525252525252572%252525252525252525256c%2525252525252525252549%252525252525252525256e%252525252525252525254c%252525252525252525256f%2525252525252525252563%2525252525252525252561%252525252525252525256c%2525252525252525252549%252525252525252525256e%2525252525252525252566%2525252525252525252569%252525252525252525256c%2525252525252525252565%2525252525252525253Dtrue%252525252525252526table%25252525252525253Da%252525252525252526username%25252525252525253Dfileread_%25252525252525252Fetc%25252525252525252Fpasswd%252525252525252526password%25252525252525253D123123%252525252525252526fetch_mode%25252525252525253DSINGLE";
245+
246+
SQLManager.validateJdbcUrl(jdbcConnection);
247+
}
248+
229249
/**
230250
* Test fail if any exception is thrown therefore no assert
231251
*/

0 commit comments

Comments
 (0)