Skip to content

Commit f0a26bf

Browse files
wy471xclaude
andcommitted
fix: add null/blank guard to parseHostPort and normalizeUrl
- IpUtils.parseHostPort: throw IllegalArgumentException for null/blank input instead of NPE - CommonUpstreamUtils.normalizeUrl: same guard for a clear error at the public API boundary - DiscoveryUpstreamServiceTest.testNativeCreate: set a URL on the DTO so the test exercises the actual create path Co-Authored-By: Claude Opus 4.7 <[email protected]>
1 parent 50ec9a9 commit f0a26bf

3 files changed

Lines changed: 7 additions & 0 deletions

File tree

‎shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/CommonUpstreamUtils.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ public static String[] parseHostPort(final String upstreamUrl) {
296296
* @return canonical URL string
297297
*/
298298
public static String normalizeUrl(final String url) {
299+
if (StringUtils.isBlank(url)) {
300+
throw new IllegalArgumentException("Invalid upstream URL, null or blank");
301+
}
299302
String[] parts = parseHostPort(url);
300303
return buildUrl(parts[0], Integer.parseInt(parts[1]));
301304
}

‎shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DiscoveryUpstreamServiceTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ private void testNativeUpdate() {
202202

203203
private void testNativeCreate() {
204204
DiscoveryUpstreamDTO discoveryUpstreamDTO = buildDiscoveryUpstreamDTO("");
205+
discoveryUpstreamDTO.setUrl("192.168.1.1:8080");
205206
discoveryUpstreamService.nativeCreateOrUpdate(discoveryUpstreamDTO);
206207
}
207208

‎shenyu-common/src/main/java/org/apache/shenyu/common/utils/IpUtils.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ public static String getZookeeperHost(final String zookeeperUrl) {
426426
* @return string array with [host, port], port defaults to "80" if not present
427427
*/
428428
public static String[] parseHostPort(final String upstreamUrl) {
429+
if (StringUtils.isBlank(upstreamUrl)) {
430+
throw new IllegalArgumentException("Invalid upstream URL, null or blank");
431+
}
429432
if (upstreamUrl.startsWith("[")) {
430433
int closingBracket = upstreamUrl.lastIndexOf(']');
431434
if (closingBracket < 0) {

0 commit comments

Comments
 (0)