Skip to content

Commit 111e0fd

Browse files
authored
Merge pull request #923 from AdoptOpenJDK/fix_URL_as_map_key
Fix url as map key
2 parents cb751bb + 292a153 commit 111e0fd

File tree

4 files changed

+55
-40
lines changed

4 files changed

+55
-40
lines changed

core/src/main/java/net/adoptopenjdk/icedteaweb/manifest/ManifestAttributesChecker.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -331,20 +331,20 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
331331
final URL codebase = file.getCodeBase();
332332

333333
//cases
334-
final Map<URL, Set<URL>> usedUrls = new HashMap<>();
334+
final Map<String, Set<URL>> usedUrls = new HashMap<>();
335335
final URL sourceLocation = file.getSourceLocation();
336336
final ResourcesDesc[] resourcesDescs = file.getResourcesDescs();
337337
if ((sourceLocation != null) && !FILE_PROTOCOL.equals(sourceLocation.getProtocol())) {
338338
final URL urlWithoutFileName = UrlUtils.removeFileName(sourceLocation);
339-
usedUrls.computeIfAbsent(urlWithoutFileName, url -> new HashSet<>()).add(sourceLocation);
339+
usedUrls.computeIfAbsent(urlWithoutFileName.toString(), url -> new HashSet<>()).add(sourceLocation);
340340
}
341341
for (ResourcesDesc resourcesDesc : resourcesDescs) {
342342
ExtensionDesc[] ex = resourcesDesc.getExtensions();
343343
if (ex != null) {
344344
for (ExtensionDesc extensionDesc : ex) {
345345
if (extensionDesc != null) {
346346
final URL urlWithoutFileName = UrlUtils.removeFileName(extensionDesc.getLocation());
347-
usedUrls.computeIfAbsent(urlWithoutFileName, url -> new HashSet<>()).add(extensionDesc.getLocation());
347+
usedUrls.computeIfAbsent(urlWithoutFileName.toString(), url -> new HashSet<>()).add(extensionDesc.getLocation());
348348
}
349349
}
350350
}
@@ -353,7 +353,7 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
353353
for (JARDesc jarDesc : jars) {
354354
if (jarDesc != null) {
355355
final URL urlWithoutFileName = UrlUtils.removeFileName(jarDesc.getLocation());
356-
usedUrls.computeIfAbsent(urlWithoutFileName, url -> new HashSet<>()).add(jarDesc.getLocation());
356+
usedUrls.computeIfAbsent(urlWithoutFileName.toString(), url -> new HashSet<>()).add(jarDesc.getLocation());
357357
}
358358
}
359359
}
@@ -367,14 +367,19 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
367367
}
368368
final Set<URL> notOkUrls = new HashSet<>();
369369
final boolean skipResourcesFromFileSystem = Boolean.parseBoolean(JNLPRuntime.getConfiguration().getProperty(ConfigurationConstants.KEY_ASSUME_FILE_STEM_IN_CODEBASE));
370-
for (URL u : usedUrls.keySet()) {
371-
if (UrlUtils.urlRelativeTo(u, codebase)) {
372-
LOG.debug("OK - '{}' is from codebase '{}'.", u, codebase);
373-
} else if (skipResourcesFromFileSystem && FILE_PROTOCOL.equals(u.getProtocol())) {
374-
LOG.debug("OK - '{}' is from file system", u);
375-
} else {
376-
notOkUrls.add(u);
377-
LOG.warn("Warning! '{}' is NOT from codebase '{}'.", u, codebase);
370+
for (String urlString : usedUrls.keySet()) {
371+
try {
372+
final URL u = new URL(urlString);
373+
if (UrlUtils.urlRelativeTo(u, codebase)) {
374+
LOG.debug("OK - '{}' is from codebase '{}'.", u, codebase);
375+
} else if (skipResourcesFromFileSystem && FILE_PROTOCOL.equals(u.getProtocol())) {
376+
LOG.debug("OK - '{}' is from file system", u);
377+
} else {
378+
notOkUrls.add(u);
379+
LOG.warn("Warning! '{}' is NOT from codebase '{}'.", u, codebase);
380+
}
381+
} catch (MalformedURLException mue) {
382+
LOG.debug("Malformed URL checkApplicationLibraryAllowableCodebaseAttribute '{}'.", urlString);
378383
}
379384
}
380385
if (notOkUrls.isEmpty()) {
@@ -392,7 +397,7 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
392397
}
393398

394399
final Set<URL> notOkResources = notOkUrls.stream()
395-
.flatMap(notOk -> usedUrls.get(notOk).stream())
400+
.flatMap(notOk -> usedUrls.get(notOk.toString()).stream())
396401
.collect(Collectors.toSet());
397402

398403
notOkResources.forEach(url -> LOG.warn("The resource '{}' is not from codebase '{}'", url, codebase));
@@ -406,11 +411,16 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
406411
return;
407412
}
408413
} else {
409-
for (URL foundUrl : usedUrls.keySet()) {
410-
if (!att.matches(foundUrl)) {
411-
throw new LaunchException("The resources " + usedUrls.get(foundUrl) + " do not match the location in Application-Library-Allowable-Codebase Attribute " + att + ". Blocking the application from running.");
412-
} else {
413-
LOG.debug("The resources from {} do match the location in Application-Library-Allowable-Codebase Attribute {}. Continuing.", foundUrl, att);
414+
for (String foundUrlString : usedUrls.keySet()) {
415+
try {
416+
URL foundUrl = new URL(foundUrlString);
417+
if (!att.matches(foundUrl)) {
418+
throw new LaunchException("The resources " + usedUrls.get(foundUrlString) + " do not match the location in Application-Library-Allowable-Codebase Attribute " + att + ". Blocking the application from running.");
419+
} else {
420+
LOG.debug("The resources from {} do match the location in Application-Library-Allowable-Codebase Attribute {}. Continuing.", foundUrl, att);
421+
}
422+
} catch (MalformedURLException mue) {
423+
throw new LaunchException("Malformed URL " + foundUrlString + ". Resources do not match the location in Application-Library-Allowable-Codebase Attribute " + att + ". Blocking the application from running.");
414424
}
415425
}
416426
}

core/src/main/java/net/adoptopenjdk/icedteaweb/resources/ResourceTracker.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public class ResourceTracker {
9797
/**
9898
* the resources known about by this resource tracker
9999
*/
100-
private final Map<URL, Resource> resources = new HashMap<>();
100+
private final Map<String, Resource> resources = new HashMap<>();
101101

102102
/**
103103
* whether to download parts before requested
@@ -166,10 +166,10 @@ public void addResource(URL location, final VersionString version, final UpdateP
166166
*/
167167
private boolean addToResources(Resource resource) {
168168
synchronized (resources) {
169-
final Resource existingResource = resources.get(resource.getLocation());
169+
final Resource existingResource = resources.get(resource.getLocation().toString());
170170

171171
if (existingResource == null) {
172-
resources.put(resource.getLocation(), resource);
172+
resources.put(resource.getLocation().toString(), resource);
173173
return true;
174174
}
175175

@@ -203,7 +203,7 @@ private void startDownloadingIfPrefetch(Resource resource) {
203203
public void removeResource(URL location) {
204204
synchronized (resources) {
205205
Resource resource = getResource(location);
206-
resources.remove(resource.getLocation());
206+
resources.remove(resource.getLocation().toString());
207207
}
208208
}
209209

@@ -362,7 +362,7 @@ private Resource[] getResources(URL[] urls) {
362362
private Resource getResource(URL location) {
363363
final URL normalizedLocation = normalizeUrlQuietly(location);
364364
synchronized (resources) {
365-
final Resource result = resources.get(normalizedLocation);
365+
final Resource result = resources.get(normalizedLocation.toString());
366366
if (result == null) {
367367
throw new IllegalResourceDescriptorException("Location " + location + " does not specify a resource being tracked.");
368368
}

core/src/main/java/net/sourceforge/jnlp/runtime/CachedJarFileCallback.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,29 @@ public synchronized static CachedJarFileCallback getInstance() {
7373
}
7474

7575
/* our managed cache */
76-
private final Map<URL, URL> mapping;
76+
private final Map<String, URL> mapping;
7777

7878
private CachedJarFileCallback() {
79-
mapping = new ConcurrentHashMap<URL, URL>();
79+
mapping = new ConcurrentHashMap<String, URL>();
8080
}
8181

8282
public void addMapping(URL remoteUrl, URL localUrl) {
83-
mapping.put(remoteUrl, localUrl);
83+
LOG.debug("CachedJarFileCallback.addMapping : {} -> {} ", remoteUrl, localUrl);
84+
mapping.put(remoteUrl.toString(), localUrl);
8485
}
8586

8687
@Override
8788
public java.util.jar.JarFile retrieve(URL url) throws IOException {
88-
URL localUrl = mapping.get(url);
89+
URL localUrl = mapping.get(url.toString());
8990
if (localUrl == null) {
9091
if (url.getRef() != null) {
9192
url = new URL(url.toString().substring(0, url.toString().lastIndexOf(url.getRef()) - 1));
92-
localUrl = mapping.get(url);
93+
localUrl = mapping.get(url.toString());
9394
}
9495
}
9596

9697
if (localUrl == null) {
97-
LOG.info("could not find mapping for {} - falling back to downloading without caching", url);
98+
LOG.info("CachedJarFileCallback.retrieve : could not find mapping for {} - falling back to downloading without caching", url);
9899
/*
99100
* If the jar url is not known, treat it as it would be treated in
100101
* general by URLJarFile.
@@ -112,7 +113,7 @@ public java.util.jar.JarFile retrieve(URL url) throws IOException {
112113
// 2) For the plug-in, we want to cache files from class-path so we do it manually
113114
returnFile.getManifest().getMainAttributes().putValue(Attributes.Name.CLASS_PATH.toString(), "");
114115

115-
LOG.debug("Class-Path attribute cleared for {}", returnFile.getName());
116+
LOG.debug("Class-Path attribute cleared for Cached Jar {}", returnFile.getName());
116117

117118
} catch (NullPointerException npe) {
118119
// Discard NPE here. Maybe there was no manifest, maybe there were no attributes, etc.

core/src/main/java/net/sourceforge/jnlp/runtime/classloader/JNLPClassLoader.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public enum SigningState {
256256
* classloading threads. See loadClass(String) and
257257
* CodebaseClassLoader.findClassNonRecursive(String).
258258
*/
259-
final Map<URL, SecurityDesc> jarLocationSecurityMap = Collections.synchronizedMap(new HashMap<>());
259+
final Map<String, SecurityDesc> jarLocationSecurityMap = Collections.synchronizedMap(new HashMap<>());
260260

261261
/*Set to prevent once tried-to-get resources to be tried again*/
262262
private final Set<URL> alreadyTried = Collections.synchronizedSet(new HashSet<>());
@@ -822,7 +822,7 @@ private void initializeResources() throws LaunchException {
822822
for (JARDesc jarDesc : validJars) {
823823
final URL codebase = getJnlpFileCodebase();
824824
final SecurityDesc jarSecurity = securityDelegate.getCodebaseSecurityDesc(jarDesc, codebase);
825-
jarLocationSecurityMap.put(jarDesc.getLocation(), jarSecurity);
825+
jarLocationSecurityMap.put(jarDesc.getLocation().toString(), jarSecurity);
826826
}
827827

828828
activateJars(initialJars);
@@ -1254,7 +1254,7 @@ private Void doActivateJars(List<JARDesc> jars) {
12541254
CachedJarFileCallback.getInstance().addMapping(fakeRemote, fileURL);
12551255
addURL(fakeRemote);
12561256

1257-
jarLocationSecurityMap.put(fakeRemote, jarSecurity);
1257+
jarLocationSecurityMap.put(fakeRemote.toString(), jarSecurity);
12581258

12591259
} catch (MalformedURLException mfue) {
12601260
LOG.error("Unable to add extracted nested jar to classpath", mfue);
@@ -1566,7 +1566,7 @@ private void addNewJar(final JARDesc desc, UpdatePolicy updatePolicy) {
15661566

15671567
final SecurityDesc security = securityDelegate.getJarPermissions(file.getCodeBase());
15681568

1569-
jarLocationSecurityMap.put(remoteURL, security);
1569+
jarLocationSecurityMap.put(remoteURL.toString(), security);
15701570

15711571
return null;
15721572
});
@@ -1846,7 +1846,7 @@ public SecurityDesc getSecurity() {
18461846
* @return The SecurityDescriptor for that source
18471847
*/
18481848
private SecurityDesc getCodeSourceSecurity(URL source) {
1849-
SecurityDesc sec = jarLocationSecurityMap.get(source);
1849+
SecurityDesc sec = jarLocationSecurityMap.get(source.toString());
18501850
synchronized (alreadyTried) {
18511851
if (sec == null && !alreadyTried.contains(source)) {
18521852
alreadyTried.add(source);
@@ -1855,7 +1855,7 @@ private SecurityDesc getCodeSourceSecurity(URL source) {
18551855
try {
18561856
JARDesc des = new JARDesc(source, null, null, false, false, false, false);
18571857
addNewJar(des);
1858-
sec = jarLocationSecurityMap.get(source);
1858+
sec = jarLocationSecurityMap.get(source.toString());
18591859
} catch (Throwable t) {
18601860
LOG.error("Error while getting security", t);
18611861
sec = null;
@@ -1900,7 +1900,7 @@ private void merge(JNLPClassLoader extLoader) {
19001900

19011901
// security descriptors
19021902
synchronized (jarLocationSecurityMap) {
1903-
for (URL key : extLoader.jarLocationSecurityMap.keySet()) {
1903+
for (String key : extLoader.jarLocationSecurityMap.keySet()) {
19041904
jarLocationSecurityMap.put(key, extLoader.jarLocationSecurityMap.get(key));
19051905
}
19061906
}
@@ -2102,9 +2102,13 @@ AccessControlContext getAccessControlContextForClassLoading() {
21022102

21032103
// Permissions for all remote hosting urls
21042104
synchronized (jarLocationSecurityMap) {
2105-
for (URL u : jarLocationSecurityMap.keySet()) {
2106-
permissions.add(new SocketPermission(UrlUtils.getHostAndPort(u),
2107-
"connect, accept"));
2105+
for (String urlString : jarLocationSecurityMap.keySet()) {
2106+
try {
2107+
URL u = new URL(urlString);
2108+
permissions.add(new SocketPermission(UrlUtils.getHostAndPort(u), "connect, accept"));
2109+
} catch (MalformedURLException mue) {
2110+
LOG.debug("Could not add SocketPermission for url : {}", urlString);
2111+
}
21082112
}
21092113
}
21102114

0 commit comments

Comments
 (0)