Skip to content

Commit

Permalink
Add security check for jdbc url in SecurityUtils.java (#5164)
Browse files Browse the repository at this point in the history
* Add security check for jdbc url in SecurityUtils.java

* Update SecurityUtils.java

* Update SecurityUtils.java
  • Loading branch information
Le1a committed Sep 4, 2024
1 parent 5a5a95f commit 9560905
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public abstract class SecurityUtils {

private static final String JDBC_MYSQL_PROTOCOL = "jdbc:mysql";

private static final String BLACKLIST_REGEX =
"autodeserialize|allowloadlocalinfile|allowurlinlocalinfile|allowloadlocalinfileinpath";

/**
* check mysql connection params
*
Expand Down Expand Up @@ -118,6 +121,10 @@ public static void checkJdbcConnParams(

// 3. Check params. Mainly vulnerability parameters. Note the url encoding
checkParams(extraParams);

// 4. Check url security, especially for the possibility of malicious characters appearing on
// the host
checkUrlIsSafe(url);
}

/** @param url */
Expand Down Expand Up @@ -283,6 +290,35 @@ private static void checkParams(Map<String, Object> paramsMap) {
}
}

/**
* check url is safe
*
* @param url
*/
public static void checkUrlIsSafe(String url) {
try {
String lowercaseURL = url.toLowerCase();

Pattern pattern = Pattern.compile(BLACKLIST_REGEX);
Matcher matcher = pattern.matcher(lowercaseURL);

StringBuilder foundKeywords = new StringBuilder();
while (matcher.find()) {
if (foundKeywords.length() > 0) {
foundKeywords.append(", ");
}
foundKeywords.append(matcher.group());
}

if (foundKeywords.length() > 0) {
throw new LinkisSecurityException(
35000, "url contains blacklisted characters: " + foundKeywords);
}
} catch (Exception e) {
throw new LinkisSecurityException(35000, "error occurred during url security check: " + e);
}
}

private static Map<String, Object> parseMysqlUrlParamsToMap(String paramsUrl) {
if (StringUtils.isBlank(paramsUrl)) {
return new LinkedHashMap<>();
Expand Down

0 comments on commit 9560905

Please sign in to comment.