1717
1818package org .apache .linkis .manager .engineplugin .jdbc .utils ;
1919
20+ import org .apache .linkis .common .conf .CommonVars ;
21+ import org .apache .linkis .common .conf .CommonVars$ ;
2022import org .apache .linkis .manager .engineplugin .jdbc .JDBCPropertiesParser ;
2123import org .apache .linkis .manager .engineplugin .jdbc .constant .JDBCEngineConnConstant ;
2224import org .apache .linkis .manager .engineplugin .jdbc .exception .JDBCParamsIllegalException ;
2325
2426import org .apache .commons .lang3 .StringUtils ;
2527
28+ import java .util .HashMap ;
2629import java .util .Map ;
30+ import java .util .stream .Collectors ;
2731
2832import org .slf4j .Logger ;
2933import org .slf4j .LoggerFactory ;
@@ -43,6 +47,9 @@ public class JdbcParamUtils {
4347 private static final String APPEND_PARAMS =
4448 "allowLoadLocalInfile=false&autoDeserialize=false&allowLocalInfile=false&allowUrlInLocalInfile=false" ;
4549
50+ public static final CommonVars <String > MYSQL_STRONG_SECURITY_ENABLE =
51+ CommonVars$ .MODULE$ .apply ("linkis.mysql.strong.security.enable" , "false" );
52+
4653 private static final char AND_SYMBOL = '&' ;
4754
4855 private static final String QUOTATION_MARKS = "\" " ;
@@ -64,20 +71,62 @@ public static void validateJdbcUrl(String url) {
6471 }
6572
6673 public static String filterJdbcUrl (String url ) {
74+ if (StringUtils .isBlank (url )) {
75+ return url ;
76+ }
6777 // temporarily filter only mysql jdbc url. & Handles cases that start with JDBC
68- if (!url .startsWith ( JDBC_MYSQL_PROTOCOL ) && ! url . toLowerCase ().contains (JDBC_MYSQL_PROTOCOL )) {
78+ if (!url .toLowerCase ().contains (JDBC_MYSQL_PROTOCOL )) {
6979 return url ;
7080 }
71- if (url .contains (SENSITIVE_PARAM )) {
72- int index = url .indexOf (SENSITIVE_PARAM );
73- String tmp = SENSITIVE_PARAM ;
74- if (url .charAt (index - 1 ) == AND_SYMBOL ) {
75- tmp = AND_SYMBOL + tmp ;
76- } else if (url .charAt (index + 1 ) == AND_SYMBOL ) {
77- tmp = tmp + AND_SYMBOL ;
81+
82+ // no params
83+ if (!url .contains (String .valueOf (QUESTION_MARK ))) {
84+ return url + QUESTION_MARK + APPEND_PARAMS ;
85+ }
86+
87+ // enable strong security
88+ if (Boolean .valueOf (MYSQL_STRONG_SECURITY_ENABLE .getValue ())) {
89+ LOG .info ("mysql engine use strong security configuration. Remove all connection parameters." );
90+ return url + QUESTION_MARK + APPEND_PARAMS ;
91+ }
92+
93+ // deal with params
94+ String [] items = url .split ("\\ ?" );
95+ // params error: multiple question marks
96+ if (items .length != 2 ) {
97+ LOG .warn ("JDBC params error, the url is : " + url );
98+ return items [0 ];
99+ }
100+
101+ String [] params = items [1 ].split ("&" );
102+ Map <String , String > paramsMap = new HashMap <>(params .length );
103+ for (String param : params ) {
104+ String [] keyAndValues = param .split ("=" );
105+ // params error: key and value error
106+ if (keyAndValues .length != 2 ) {
107+ continue ;
78108 }
79- LOG .warn ("Sensitive param: {} in jdbc url is filtered." , tmp );
80- url = url .replace (tmp , "" );
109+ String key = keyAndValues [0 ];
110+ String value = keyAndValues [1 ];
111+ // key and value is blank
112+ if (StringUtils .isBlank (key ) || StringUtils .isBlank (value )) {
113+ continue ;
114+ }
115+ if (isSecurity (key , value )) {
116+ paramsMap .put (key , value );
117+ } else {
118+ LOG .warn ("Sensitive param : key={} and value={}" , key , value );
119+ }
120+ }
121+ String extraParamString =
122+ paramsMap .entrySet ().stream ()
123+ .map (e -> String .join ("=" , e .getKey (), String .valueOf (e .getValue ())))
124+ .collect (Collectors .joining ("&" ));
125+
126+ if (StringUtils .isBlank (extraParamString )) {
127+ url = items [0 ];
128+ } else {
129+ url = items [0 ] + String .valueOf (QUESTION_MARK ) + extraParamString ;
81130 }
82131 if (url .endsWith (String .valueOf (QUESTION_MARK ))) {
83132 url = url + APPEND_PARAMS ;
@@ -90,6 +139,18 @@ public static String filterJdbcUrl(String url) {
90139 return url ;
91140 }
92141
142+ private static boolean isSecurity (String key , String value ) {
143+ return !(isNotSecurity (key ) || isNotSecurity (value ));
144+ }
145+
146+ private static boolean isNotSecurity (String key ) {
147+ return key .toLowerCase ().contains ("allowLoadLocalInfile" .toLowerCase ())
148+ || key .toLowerCase ().contains ("autoDeserialize" .toLowerCase ())
149+ || key .toLowerCase ().contains ("allowLocalInfile" .toLowerCase ())
150+ || key .toLowerCase ().contains ("allowUrlInLocalInfile" .toLowerCase ())
151+ || key .toLowerCase ().contains ("#" .toLowerCase ());
152+ }
153+
93154 public static String getJdbcUsername (Map <String , String > properties )
94155 throws JDBCParamsIllegalException {
95156 String username =
0 commit comments