From df0de49b8c19cae9b9d1d1e4f6e3a37111c6f7ec Mon Sep 17 00:00:00 2001 From: huazhongming Date: Sat, 13 Jan 2024 01:58:58 +0800 Subject: [PATCH] Fix URL parse pwd failed when the pwd contains '#' (#13631) * Fix URL parse pwd failed when the pwd contains # Signed-off-by: crazyhzm * Parse url pwd Signed-off-by: crazyhzm * Fix npe Signed-off-by: crazyhzm * Remove username and pwd from parameter Signed-off-by: crazyhzm * Add ut Signed-off-by: crazyhzm --------- Signed-off-by: crazyhzm --- .../org/apache/dubbo/common/URLStrParser.java | 21 +++++++++++++++---- .../apache/dubbo/common/URLStrParserTest.java | 1 + 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URLStrParser.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URLStrParser.java index 3b1761e2ff..c8ee6337e4 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/URLStrParser.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URLStrParser.java @@ -18,12 +18,15 @@ package org.apache.dubbo.common; import org.apache.dubbo.common.url.component.ServiceConfigURL; import org.apache.dubbo.common.url.component.URLItemCache; +import org.apache.dubbo.common.utils.StringUtils; import java.util.Collections; import java.util.HashMap; import java.util.Map; import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY_PREFIX; +import static org.apache.dubbo.common.constants.CommonConstants.PASSWORD_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.USERNAME_KEY; import static org.apache.dubbo.common.utils.StringUtils.EMPTY_STRING; import static org.apache.dubbo.common.utils.StringUtils.decodeHexByte; import static org.apache.dubbo.common.utils.Utf8Utils.decodeUtf8; @@ -42,8 +45,8 @@ public final class URLStrParser { } /** - * @param decodedURLStr : after {@link URL#decode} string - * decodedURLStr format: protocol://username:password@host:port/path?k1=v1&k2=v2 + * @param decodedURLStr : after {@link URL#decode} string decodedURLStr format: + * protocol://username:password@host:port/path?k1=v1&k2=v2 * [protocol://][username:password@][host:port]/[path][?k1=v1&k2=v2] */ public static URL parseDecodedStr(String decodedURLStr) { @@ -103,6 +106,7 @@ public final class URLStrParser { int starIdx = 0, endIdx = decodedBody.length(); // ignore the url content following '#' int poundIndex = decodedBody.indexOf('#'); + if (poundIndex != -1) { endIdx = poundIndex; } @@ -147,6 +151,15 @@ public final class URLStrParser { } starIdx = pwdEndIdx + 1; } + if (username == null && parameters != null && StringUtils.isNotEmpty(parameters.get(USERNAME_KEY))) { + username = parameters.get(USERNAME_KEY); + parameters.remove(USERNAME_KEY); + } + + if (password == null && parameters != null && StringUtils.isNotEmpty(parameters.get(PASSWORD_KEY))) { + password = parameters.get(PASSWORD_KEY); + parameters.remove(PASSWORD_KEY); + } String host = null; int port = 0; @@ -203,8 +216,8 @@ public final class URLStrParser { } /** - * @param encodedURLStr : after {@link URL#encode(String)} string - * encodedURLStr after decode format: protocol://username:password@host:port/path?k1=v1&k2=v2 + * @param encodedURLStr : after {@link URL#encode(String)} string encodedURLStr after decode format: + * protocol://username:password@host:port/path?k1=v1&k2=v2 * [protocol://][username:password@][host:port]/[path][?k1=v1&k2=v2] */ public static URL parseEncodedStr(String encodedURLStr) { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/URLStrParserTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/URLStrParserTest.java index 4c4c218c35..01364faab9 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/URLStrParserTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/URLStrParserTest.java @@ -48,6 +48,7 @@ class URLStrParserTest { testCases.add("nacos://192.168.1.1:8848?username=&password="); testCases.add("dubbo://127.0.0.1?timeout=1234&default.timeout=5678"); testCases.add("dubbo://127.0.0.1?default.timeout=5678"); + testCases.add("zookeeper://test10.301.216.302:2181?username=t1#&password=t2#"); errorDecodedCases.add("dubbo:192.168.1.1"); errorDecodedCases.add("://192.168.1.1");