2222import org .slf4j .LoggerFactory ;
2323
2424import java .net .URLDecoder ;
25+ import java .nio .charset .StandardCharsets ;
2526import java .util .Arrays ;
2627import java .util .Collections ;
2728import java .util .HashSet ;
@@ -34,6 +35,7 @@ public final class JdbcUrlValidator {
3435 "socketfactory" , "socketfactoryarg" , "sslfactory" , "sslfactoryarg" ,
3536 "sslhostnameverifier" , "authenticationpluginclassname" , "loggerclassname" ,
3637 "kerberosservername" , "gssdelegatecred" , "sslpasswordcallback" )));
38+ private static final String [] DANGEROUS_PATTERNS = {"socketfactory" , "sslfactory" , "autodeserialize" };
3739
3840 private JdbcUrlValidator () {
3941 }
@@ -46,10 +48,7 @@ public static void validate(String jdbcUrl) throws HadoopException {
4648 throw e ;
4749 }
4850 String trimmed = jdbcUrl .trim ();
49- int queryStart = trimmed .indexOf ('?' );
50- if (queryStart == -1 ) {
51- queryStart = trimmed .indexOf (';' );
52- }
51+ int queryStart = findQueryStart (trimmed );
5352 if (queryStart != -1 ) {
5453 String queryString = trimmed .substring (queryStart + 1 );
5554 validateQueryString (queryString , trimmed );
@@ -58,7 +57,7 @@ public static void validate(String jdbcUrl) throws HadoopException {
5857 }
5958
6059 private static void validateQueryString (String queryString , String fullUrl ) throws HadoopException {
61- String [] tokens = queryString .split ("[&;]" );
60+ String [] tokens = queryString .split ("[&;? ]" );
6261 for (String token : tokens ) {
6362 if (token .trim ().isEmpty ()) {
6463 continue ;
@@ -67,7 +66,7 @@ private static void validateQueryString(String queryString, String fullUrl) thro
6766 String paramName = (eqIdx >= 0 ? token .substring (0 , eqIdx ) : token ).trim ();
6867 String decodedParamName = paramName ;
6968 try {
70- decodedParamName = URLDecoder .decode (paramName , "UTF-8" );
69+ decodedParamName = URLDecoder .decode (paramName , StandardCharsets . UTF_8 );
7170 } catch (Exception e ) {
7271 LOG .warn ("Failed to decode parameter name: {}" , paramName );
7372 }
@@ -76,8 +75,7 @@ private static void validateQueryString(String queryString, String fullUrl) thro
7675 if (BLOCKED_PARAMS .contains (normalized )) {
7776 logAndThrow ("blocked parameter" , normalized , paramName , fullUrl );
7877 }
79- String [] dangerPatterns = {"socketfactory" , "sslfactory" , "autodeserialize" };
80- for (String danger : dangerPatterns ) {
78+ for (String danger : DANGEROUS_PATTERNS ) {
8179 if (normalized .contains (danger )) {
8280 logAndThrow ("dangerous pattern '" + danger + "'" , normalized , paramName , fullUrl );
8381 }
@@ -94,7 +92,21 @@ static String sanitizeForLog(String url) {
9492 if (url == null ) {
9593 return "<null>" ;
9694 }
97- return url .replaceAll ("[?;].*" , "?<params_redacted>" );
95+ int idx = findQueryStart (url );
96+ return idx >= 0 ? url .substring (0 , idx ) + "?<params_redacted>" : url ;
97+ }
98+
99+ private static int findQueryStart (String url ) {
100+ int qIdx = url .indexOf ('?' );
101+ int sIdx = url .indexOf (';' );
102+ if (qIdx >= 0 && sIdx >= 0 ) {
103+ return Math .min (qIdx , sIdx );
104+ } else if (qIdx >= 0 ) {
105+ return qIdx ;
106+ } else if (sIdx >= 0 ) {
107+ return sIdx ;
108+ }
109+ return -1 ;
98110 }
99111
100112 private static void logAndThrow (String reason , String normalized , String originalParam , String fullUrl ) {
0 commit comments