Skip to content

Commit 1d28e6f

Browse files
committed
8367049: URL.openConnection throws StringIndexOutOfBoundsException in avm mode
1 parent 0db4702 commit 1d28e6f

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/java.base/share/classes/java/net/HostPortrange.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ public int hashCode() {
6060
}
6161

6262
HostPortrange(String scheme, String host) {
63+
// Defensive validation first
64+
if (host == null || host.isEmpty()) {
65+
throw new IllegalArgumentException("empty authority");
66+
}
67+
68+
// A leading ':' means missing host, which is invalid for URLPermission authorities
69+
if (host.charAt(0) == ':') {
70+
throw new IllegalArgumentException("missing host in authority: " + host);
71+
}
6372
// Parse the host name. A name has up to three components, the
6473
// hostname, a port number, or two numbers representing a port
6574
// range. "www.example.com:8080-9090" is a valid host name.

src/java.base/share/classes/java/net/URLPermission.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,9 @@ static class Authority {
527527
HostPortrange p;
528528

529529
Authority(String scheme, String authority) {
530+
if (authority == null || authority.isEmpty()) {
531+
throw new IllegalArgumentException("Invalid URL authority: empty host");
532+
}
530533
int at = authority.indexOf('@');
531534
if (at == -1) {
532535
p = new HostPortrange(scheme, authority);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8367049
27+
* @summary URLPermission must reject empty/missing host authority with IAE (no SIOOBE)
28+
* @run testng EmptyAuthorityTest
29+
*/
30+
31+
import java.net.URLPermission;
32+
import org.testng.Assert;
33+
import org.testng.annotations.DataProvider;
34+
import org.testng.annotations.Test;
35+
36+
public class EmptyAuthorityTest {
37+
38+
@DataProvider(name = "badUrls")
39+
public Object[][] badUrls() {
40+
return new Object[][]{
41+
{ "http:///path" }, // empty authority
42+
{ "https:///x" }, // empty authority
43+
{ "http://@/x" }, // userinfo + empty host
44+
{ "http://user@/x" }, // userinfo + empty host
45+
{ "http://:80/x" }, // port with no host
46+
{ "http://[]/x" } // empty IPv6 literal
47+
};
48+
}
49+
50+
@DataProvider(name = "goodUrls")
51+
public Object[][] goodUrls() {
52+
return new Object[][]{
53+
{ "http://example.com/x" },
54+
{ "http://example.com:80/x" },
55+
{ "http://[::1]/x" },
56+
{ "http://[::1]:8080/x" }
57+
};
58+
}
59+
60+
@Test(dataProvider = "badUrls")
61+
public void rejectsEmptyOrMalformedAuthority(String url) {
62+
Assert.expectThrows(IllegalArgumentException.class, () -> new URLPermission(url));
63+
}
64+
65+
@Test(dataProvider = "goodUrls")
66+
public void acceptsValidAuthorities(String url) {
67+
new URLPermission(url); // should not throw
68+
}
69+
}

0 commit comments

Comments
 (0)