8353278: Consolidate local file URL checks in jar: and file: URL schemes

Reviewed-by: dfuchs, jpai
This commit is contained in:
Eirik Bjørsnøs 2025-04-07 15:23:18 +00:00
parent 402103331b
commit 9a391f44e0
5 changed files with 30 additions and 29 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -507,6 +507,22 @@ public final class ParseUtil {
}
}
/**
* {@return true if the url is a file: URL for a 'local file' as defined by RFC 8089, Section 2}
*
* For unknown historical reasons, this method deviates from RFC 8089
* by allowing "~" as an alias for 'localhost'
*
* @param url the URL which may be a local file URL
*/
public static boolean isLocalFileURL(URL url) {
if (url.getProtocol().equalsIgnoreCase("file")) {
String host = url.getHost();
return host == null || host.isEmpty() || host.equals("~") ||
host.equalsIgnoreCase("localhost");
}
return false;
}
// -- Character classes for parsing --

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -34,6 +34,7 @@ import java.util.jar.JarFile;
import jdk.internal.util.OperatingSystem;
import sun.net.util.URLUtil;
import sun.net.www.ParseUtil;
/* A factory for cached JAR file. This class is used to both retrieve
* and cache Jar files.
@ -92,7 +93,7 @@ class JarFileFactory implements URLJarFile.URLJarFileCloseController {
return get(url, false);
}
URL patched = urlFor(url);
if (!URLJarFile.isFileURL(patched)) {
if (!ParseUtil.isLocalFileURL(patched)) {
// A temporary file will be created, we can prepopulate
// the cache in this case.
return get(url, useCaches);
@ -158,9 +159,10 @@ class JarFileFactory implements URLJarFile.URLJarFileCloseController {
// Deal with UNC pathnames specially. See 4180841
String host = url.getHost();
if (host != null && !host.isEmpty() &&
!host.equalsIgnoreCase("localhost")) {
// Subtly different from ParseUtil.isLocalFileURL, for historical reasons
boolean isLocalFile = ParseUtil.isLocalFileURL(url) && !"~".equals(host);
// For remote hosts, change 'file://host/folder/data.xml' to 'file:////host/folder/data.xml'
if (!isLocalFile) {
@SuppressWarnings("deprecation")
var _unused = url = new URL("file", "", "//" + host + url.getPath());
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -49,7 +49,7 @@ public class URLJarFile extends JarFile {
private Map<String, Attributes> superEntries;
static JarFile getJarFile(URL url, URLJarFileCloseController closeController) throws IOException {
if (isFileURL(url)) {
if (ParseUtil.isLocalFileURL(url)) {
Runtime.Version version = "runtime".equals(url.getRef())
? JarFile.runtimeVersion()
: JarFile.baseVersion();
@ -71,20 +71,6 @@ public class URLJarFile extends JarFile {
this.closeController = closeController;
}
static boolean isFileURL(URL url) {
if (url.getProtocol().equalsIgnoreCase("file")) {
/*
* Consider this a 'file' only if it's a LOCAL file, because
* 'file:' URLs can be accessible through ftp.
*/
String host = url.getHost();
if (host == null || host.isEmpty() || host.equals("~") ||
host.equalsIgnoreCase("localhost"))
return true;
}
return false;
}
/**
* Returns the <code>ZipEntry</code> for the given entry name or
* <code>null</code> if not found.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -64,8 +64,7 @@ public class Handler extends URLStreamHandler {
public URLConnection openConnection(URL u, Proxy p)
throws IOException {
String host = u.getHost();
if (host == null || host.isEmpty() || host.equals("~") ||
host.equalsIgnoreCase("localhost")) {
if (ParseUtil.isLocalFileURL(u)) {
File file = new File(ParseUtil.decode(u.getPath()));
return createFileURLConnection(u, file);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -72,9 +72,7 @@ public class Handler extends URLStreamHandler {
path = path.replace('/', '\\');
path = path.replace('|', ':');
if ((host == null) || host.isEmpty() ||
host.equalsIgnoreCase("localhost") ||
host.equals("~")) {
if (ParseUtil.isLocalFileURL(url)) {
return createFileURLConnection(url, new File(path));
}