Skip to content

Commit 8e804ff

Browse files
authored
deal with url encode (#4113)
1 parent a652276 commit 8e804ff

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

linkis-commons/linkis-common/src/main/java/org/apache/linkis/common/utils/SecurityUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import org.apache.commons.lang3.StringUtils;
2525

26+
import java.io.UnsupportedEncodingException;
27+
import java.net.URLDecoder;
2628
import java.util.HashMap;
2729
import java.util.Iterator;
2830
import java.util.LinkedHashMap;
@@ -93,6 +95,12 @@ public static String checkJdbcSecurity(String url) {
9395
if (StringUtils.isBlank(url)) {
9496
throw new LinkisSecurityException(35000, "Invalid mysql connection cul, url is empty");
9597
}
98+
// deal with url encode
99+
try {
100+
url = URLDecoder.decode(url, "UTF-8");
101+
} catch (UnsupportedEncodingException e) {
102+
throw new LinkisSecurityException(35000, "mysql connection cul decode error: " + e);
103+
}
96104
if (url.endsWith(QUESTION_MARK) || !url.contains(QUESTION_MARK)) {
97105
logger.info("checkJdbcSecurity target url: {}", url);
98106
return url;
@@ -126,6 +134,18 @@ public static Map<String, Object> checkJdbcSecurity(Map<String, Object> paramsMa
126134
return paramsMap;
127135
}
128136

137+
// deal with url encode
138+
String paramUrl = parseParamsMapToMysqlParamUrl(paramsMap);
139+
try {
140+
paramUrl = URLDecoder.decode(paramUrl, "UTF-8");
141+
} catch (UnsupportedEncodingException e) {
142+
throw new LinkisSecurityException(35000, "mysql connection cul decode error: " + e);
143+
}
144+
145+
Map<String, Object> newParamsMap = parseMysqlUrlParamsToMap(paramUrl);
146+
paramsMap.clear();
147+
paramsMap.putAll(newParamsMap);
148+
129149
Iterator<Map.Entry<String, Object>> iterator = paramsMap.entrySet().iterator();
130150
while (iterator.hasNext()) {
131151
Map.Entry<String, Object> entry = iterator.next();

linkis-commons/linkis-common/src/test/java/org/apache/linkis/common/utils/SecurityUtilsTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ public void testCheckJdbcSecurityUrl() throws Exception {
9191
SecurityUtils.checkJdbcSecurity(atomUrl.get());
9292
});
9393

94+
// url encode
95+
url = "jdbc:mysql://127.0.0.1:10000/db_name?allowLocalInfil%65=true";
96+
atomUrl.set(url);
97+
Assertions.assertThrows(
98+
LinkisSecurityException.class,
99+
() -> {
100+
SecurityUtils.checkJdbcSecurity(atomUrl.get());
101+
});
102+
94103
// value is not security
95104
url = "jdbc:mysql://127.0.0.1:10000/db_name?p1=allowLocalInfile";
96105
atomUrl.set(url);
@@ -117,6 +126,11 @@ public void testCheckJdbcSecurityParamsMap() throws Exception {
117126
Map<String, Object> newMap = SecurityUtils.checkJdbcSecurity(paramsMap);
118127
Assertions.assertEquals("v1", newMap.get("p1"));
119128

129+
// key not security
130+
paramsMap.put("allowLocalInfil%67", "true");
131+
SecurityUtils.checkJdbcSecurity(paramsMap);
132+
Assertions.assertEquals("true", newMap.get("allowLocalInfilg"));
133+
120134
// key not security
121135
paramsMap.put("allowLocalInfile", "false");
122136
Assertions.assertThrows(
@@ -134,6 +148,15 @@ public void testCheckJdbcSecurityParamsMap() throws Exception {
134148
SecurityUtils.checkJdbcSecurity(paramsMap);
135149
});
136150

151+
// value not security
152+
paramsMap.clear();
153+
paramsMap.put("p1", "allowLocalInfil%65");
154+
Assertions.assertThrows(
155+
LinkisSecurityException.class,
156+
() -> {
157+
SecurityUtils.checkJdbcSecurity(paramsMap);
158+
});
159+
137160
// contains #
138161
paramsMap.clear();
139162
paramsMap.put("p1#", "v1");

0 commit comments

Comments
 (0)