Skip to content

Commit

Permalink
Merge pull request #930 from AdoptOpenJDK/urlKeyForCacheKey
Browse files Browse the repository at this point in the history
Url key for cache key
  • Loading branch information
janakmulani authored Dec 19, 2023
2 parents d96e19d + ebc8895 commit e28f8d4
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void put(URL url, List<URL> where) {
if (url != null) {
if (where.isEmpty()) {
where.add(0, url);
} else if (!where.get(0).toString().equals(url.toString())) {
} else if (!UrlUtils.equalUrls(where.get(0), url)) {
where.add(0, url);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;

/**
* http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/security/manifest.html#app_library
Expand Down Expand Up @@ -151,7 +151,7 @@ public void hyperlinkUpdate(HyperlinkEvent e) {
}

public static void main(String[] args) throws MalformedURLException {
Set<URL> s = new HashSet<>();
List<URL> s = new ArrayList<>();
s.add(new URL("http:/blah.com/blah"));
s.add(new URL("http:/blah.com/blah/blah"));
MissingALACAttributePanel w = new MissingALACAttributePanel(null, "HelloWorld", "http://nbblah.url", UrlUtils.setOfUrlsToHtmlList(s));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Set;
import java.util.List;
import java.util.concurrent.Semaphore;

/**
Expand Down Expand Up @@ -221,7 +221,7 @@ public static NamePassword showAuthenticationPrompt(String host, int port, Strin
return (NamePassword) response;
}

public static boolean showMissingALACAttributePanel(JNLPFile file, URL codeBase, Set<URL> remoteUrls) {
public static boolean showMissingALACAttributePanel(JNLPFile file, URL codeBase, List<URL> remoteUrls) {

SecurityDialogMessage message = new SecurityDialogMessage(file);
message.dialogType = DialogType.MISSING_ALACA;
Expand All @@ -242,7 +242,7 @@ public static boolean showMissingALACAttributePanel(JNLPFile file, URL codeBase,
return selectedValue.toBoolean();
}

public static boolean showMatchingALACAttributePanel(JNLPFile file, URL documentBase, Set<URL> remoteUrls) {
public static boolean showMatchingALACAttributePanel(JNLPFile file, URL documentBase, List<URL> remoteUrls) {

SecurityDialogMessage message = new SecurityDialogMessage(file);
message.dialogType = DialogType.MATCHING_ALACA;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import net.sourceforge.jnlp.runtime.classloader.JNLPClassLoader.SigningState;
import net.sourceforge.jnlp.runtime.classloader.SecurityDelegate;
import net.sourceforge.jnlp.util.ClasspathMatcher.ClasspathMatchers;
import net.sourceforge.jnlp.util.UrlKey;
import net.sourceforge.jnlp.util.UrlUtils;

import java.net.MalformedURLException;
Expand All @@ -77,7 +78,7 @@ public class ManifestAttributesChecker {
private final SecurityDelegate securityDelegate;

public ManifestAttributesChecker(final SecurityDesc security, final JNLPFile file,
final SigningState signing, final SecurityDelegate securityDelegate) {
final SigningState signing, final SecurityDelegate securityDelegate) {
this.security = security;
this.file = file;
this.signing = signing;
Expand Down Expand Up @@ -122,7 +123,7 @@ public void checkAll() throws LaunchException {
}

if (attributesCheck.contains(MANIFEST_ATTRIBUTES_CHECK.ALAC) ||
attributesCheck.contains(MANIFEST_ATTRIBUTES_CHECK.ALL)) {
attributesCheck.contains(MANIFEST_ATTRIBUTES_CHECK.ALL)) {
checkApplicationLibraryAllowableCodebaseAttribute();
} else {
LOG.warn("check on {} skipped because property of deployment.manifest.attributes.check was not set to ALL or includes {} in the combination of options", "Application Library Allowable Codebase", "ALAC");
Expand All @@ -142,7 +143,7 @@ public static List<MANIFEST_ATTRIBUTES_CHECK> getAttributesCheck() {
final List<String> configs = JNLPRuntime.getConfiguration().getPropertyAsList(ConfigurationConstants.KEY_ENABLE_MANIFEST_ATTRIBUTES_CHECK);
List<MANIFEST_ATTRIBUTES_CHECK> manifestAttributesCheckList = new ArrayList<>();
for (String attribute : configs) {
for (MANIFEST_ATTRIBUTES_CHECK manifestAttribute : MANIFEST_ATTRIBUTES_CHECK.values()) {
for (MANIFEST_ATTRIBUTES_CHECK manifestAttribute : MANIFEST_ATTRIBUTES_CHECK.values()) {
if (manifestAttribute.toString().equals(attribute)) {
manifestAttributesCheckList.add(manifestAttribute);
}
Expand Down Expand Up @@ -331,20 +332,20 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
final URL codebase = file.getCodeBase();

//cases
final Map<String, Set<URL>> usedUrls = new HashMap<>();
final Map<UrlKey, Set<UrlKey>> usedUrls = new HashMap<>();
final URL sourceLocation = file.getSourceLocation();
final ResourcesDesc[] resourcesDescs = file.getResourcesDescs();
if ((sourceLocation != null) && !FILE_PROTOCOL.equals(sourceLocation.getProtocol())) {
final URL urlWithoutFileName = UrlUtils.removeFileName(sourceLocation);
usedUrls.computeIfAbsent(urlWithoutFileName.toString(), url -> new HashSet<>()).add(sourceLocation);
usedUrls.computeIfAbsent(new UrlKey(urlWithoutFileName), url -> new HashSet<>()).add(new UrlKey(sourceLocation));
}
for (ResourcesDesc resourcesDesc : resourcesDescs) {
ExtensionDesc[] ex = resourcesDesc.getExtensions();
if (ex != null) {
for (ExtensionDesc extensionDesc : ex) {
if (extensionDesc != null) {
final URL urlWithoutFileName = UrlUtils.removeFileName(extensionDesc.getLocation());
usedUrls.computeIfAbsent(urlWithoutFileName.toString(), url -> new HashSet<>()).add(extensionDesc.getLocation());
usedUrls.computeIfAbsent(new UrlKey(urlWithoutFileName), url -> new HashSet<>()).add(new UrlKey(extensionDesc.getLocation()));
}
}
}
Expand All @@ -353,7 +354,7 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
for (JARDesc jarDesc : jars) {
if (jarDesc != null) {
final URL urlWithoutFileName = UrlUtils.removeFileName(jarDesc.getLocation());
usedUrls.computeIfAbsent(urlWithoutFileName.toString(), url -> new HashSet<>()).add(jarDesc.getLocation());
usedUrls.computeIfAbsent(new UrlKey(urlWithoutFileName), url -> new HashSet<>()).add(new UrlKey(jarDesc.getLocation()));
}
}
}
Expand All @@ -365,21 +366,17 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
LOG.debug("The application is not using any url resources, skipping Application-Library-Allowable-Codebase Attribute check.");
return;
}
final Set<URL> notOkUrls = new HashSet<>();
final Set<UrlKey> notOkUrls = new HashSet<>();
final boolean skipResourcesFromFileSystem = Boolean.parseBoolean(JNLPRuntime.getConfiguration().getProperty(ConfigurationConstants.KEY_ASSUME_FILE_STEM_IN_CODEBASE));
for (String urlString : usedUrls.keySet()) {
try {
final URL u = new URL(urlString);
if (UrlUtils.urlRelativeTo(u, codebase)) {
LOG.debug("OK - '{}' is from codebase '{}'.", u, codebase);
} else if (skipResourcesFromFileSystem && FILE_PROTOCOL.equals(u.getProtocol())) {
LOG.debug("OK - '{}' is from file system", u);
} else {
notOkUrls.add(u);
LOG.warn("Warning! '{}' is NOT from codebase '{}'.", u, codebase);
}
} catch (MalformedURLException mue) {
LOG.debug("Malformed URL checkApplicationLibraryAllowableCodebaseAttribute '{}'.", urlString);
for (UrlKey urlKey : usedUrls.keySet()) {
final URL u = urlKey.getUrl();
if (UrlUtils.urlRelativeTo(u, codebase)) {
LOG.debug("OK - '{}' is from codebase '{}'.", u, codebase);
} else if (skipResourcesFromFileSystem && FILE_PROTOCOL.equals(u.getProtocol())) {
LOG.debug("OK - '{}' is from file system", u);
} else {
notOkUrls.add(urlKey);
LOG.warn("Warning! '{}' is NOT from codebase '{}'.", u, codebase);
}
}
if (notOkUrls.isEmpty()) {
Expand All @@ -396,9 +393,11 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
att = null;
}

final Set<URL> notOkResources = notOkUrls.stream()
.flatMap(notOk -> usedUrls.get(notOk.toString()).stream())
.collect(Collectors.toSet());
final List<URL> notOkResources = notOkUrls.stream()
.flatMap(notOk -> usedUrls.get(notOk).stream())
.collect(Collectors.toSet()).stream()
.map(UrlKey::getUrl)
.collect(Collectors.toList());

notOkResources.forEach(url -> LOG.warn("The resource '{}' is not from codebase '{}'", url, codebase));

Expand All @@ -411,16 +410,12 @@ private void checkApplicationLibraryAllowableCodebaseAttribute() throws LaunchEx
return;
}
} else {
for (String foundUrlString : usedUrls.keySet()) {
try {
URL foundUrl = new URL(foundUrlString);
if (!att.matches(foundUrl)) {
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.");
} else {
LOG.debug("The resources from {} do match the location in Application-Library-Allowable-Codebase Attribute {}. Continuing.", foundUrl, att);
}
} catch (MalformedURLException mue) {
throw new LaunchException("Malformed URL " + foundUrlString + ". Resources do not match the location in Application-Library-Allowable-Codebase Attribute " + att + ". Blocking the application from running.");
for (UrlKey foundUrlKey : usedUrls.keySet()) {
URL foundUrl = foundUrlKey.getUrl();
if (!att.matches(foundUrl)) {
throw new LaunchException("The resources " + usedUrls.get(foundUrlKey) + " do not match the location in Application-Library-Allowable-Codebase Attribute " + att + ". Blocking the application from running.");
} else {
LOG.debug("The resources from {} do match the location in Application-Library-Allowable-Codebase Attribute {}. Continuing.", foundUrl, att);
}
}
}
Expand All @@ -446,7 +441,7 @@ static URL stripDocbase(URL documentBase) {
if (i <= 8 || i >= s.length()) {
return documentBase;
}
s = s.substring(0, i+1);
s = s.substring(0, i + 1);
try {
documentBase = new URL(s);
} catch (MalformedURLException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.sourceforge.jnlp.cache.CacheUtil;
import net.sourceforge.jnlp.config.ConfigurationConstants;
import net.sourceforge.jnlp.runtime.JNLPRuntime;
import net.sourceforge.jnlp.util.UrlKey;
import net.sourceforge.jnlp.util.UrlUtils;

import javax.jnlp.DownloadServiceListener;
Expand Down Expand Up @@ -97,7 +98,7 @@ public class ResourceTracker {
/**
* the resources known about by this resource tracker
*/
private final Map<String, Resource> resources = new HashMap<>();
private final Map<UrlKey, Resource> resources = new HashMap<>();

/**
* whether to download parts before requested
Expand Down Expand Up @@ -166,10 +167,11 @@ public void addResource(URL location, final VersionString version, final UpdateP
*/
private boolean addToResources(Resource resource) {
synchronized (resources) {
final Resource existingResource = resources.get(resource.getLocation().toString());
final UrlKey urlKey = new UrlKey(resource.getLocation());
final Resource existingResource = resources.get(urlKey);

if (existingResource == null) {
resources.put(resource.getLocation().toString(), resource);
resources.put(urlKey, resource);
return true;
}

Expand Down Expand Up @@ -203,7 +205,7 @@ private void startDownloadingIfPrefetch(Resource resource) {
public void removeResource(URL location) {
synchronized (resources) {
Resource resource = getResource(location);
resources.remove(resource.getLocation().toString());
resources.remove(new UrlKey(resource.getLocation()));
}
}

Expand Down Expand Up @@ -362,7 +364,7 @@ private Resource[] getResources(URL[] urls) {
private Resource getResource(URL location) {
final URL normalizedLocation = normalizeUrlQuietly(location);
synchronized (resources) {
final Resource result = resources.get(normalizedLocation.toString());
final Resource result = resources.get(new UrlKey(normalizedLocation));
if (result == null) {
throw new IllegalResourceDescriptorException("Location " + location + " does not specify a resource being tracked.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.adoptopenjdk.icedteaweb.resources.cache;

import net.adoptopenjdk.icedteaweb.jnlp.version.VersionId;
import net.sourceforge.jnlp.util.UrlKey;

import java.net.URL;
import java.util.Objects;
Expand All @@ -10,12 +11,12 @@
class CacheKey {

private final URL location;
private final String locationString;
private final UrlKey urlKey;
private final VersionId version;

public CacheKey(final URL location, final VersionId version) {
this.location = requireNonNull(location, "location");
this.locationString = location.toString();
this.urlKey = new UrlKey(location);
this.version = version;
}

Expand All @@ -30,7 +31,7 @@ public VersionId getVersion() {
@Override
public String toString() {
return "CacheKey{" +
"location=" + locationString +
"location=" + location +
", version=" + version +
'}';
}
Expand All @@ -40,15 +41,15 @@ public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CacheKey cacheKey = (CacheKey) o;
return Objects.equals(locationString, cacheKey.locationString) && Objects.equals(version, cacheKey.version);
return Objects.equals(urlKey, cacheKey.urlKey) && Objects.equals(version, cacheKey.version);
}

@Override
public int hashCode() {
return Objects.hash(locationString, version);
return Objects.hash(urlKey, version);
}

public boolean matches(URL resource) {
return resource != null && locationString.equals(resource.toString());
return resource != null && urlKey.equals(new UrlKey(resource));
}
}
3 changes: 2 additions & 1 deletion core/src/main/java/net/sourceforge/jnlp/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.sourceforge.jnlp.runtime.classloader.JNLPClassLoader;
import net.sourceforge.jnlp.services.InstanceExistsException;
import net.sourceforge.jnlp.services.ServiceUtil;
import net.sourceforge.jnlp.util.UrlUtils;

import javax.imageio.ImageIO;
import javax.swing.text.html.parser.ParserDelegator;
Expand Down Expand Up @@ -309,7 +310,7 @@ private JNLPFile fromUrl(URL location) throws LaunchException {
}
if (!isLocal && haveHref) {
//this is case when remote file have href to different file
if (file.getSourceLocation() == null || !location.toString().equals(file.getSourceLocation().toString())) {
if (!UrlUtils.equalUrls(location, file.getSourceLocation())) {
//mark local true, so the following condition will be true and
//new jnlp file will be downloaded
isLocal = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import net.adoptopenjdk.icedteaweb.logging.Logger;
import net.adoptopenjdk.icedteaweb.logging.LoggerFactory;
import net.sourceforge.jnlp.util.JarFile;
import net.sourceforge.jnlp.util.UrlKey;
import net.sourceforge.jnlp.util.UrlUtils;
import sun.net.www.protocol.jar.URLJarFile;
import sun.net.www.protocol.jar.URLJarFileCallBack;
Expand Down Expand Up @@ -73,24 +74,24 @@ public synchronized static CachedJarFileCallback getInstance() {
}

/* our managed cache */
private final Map<String, URL> mapping;
private final Map<UrlKey, URL> mapping;

private CachedJarFileCallback() {
mapping = new ConcurrentHashMap<String, URL>();
mapping = new ConcurrentHashMap<>();
}

public void addMapping(URL remoteUrl, URL localUrl) {
LOG.debug("CachedJarFileCallback.addMapping : {} -> {} ", remoteUrl, localUrl);
mapping.put(remoteUrl.toString(), localUrl);
mapping.put(new UrlKey(remoteUrl), localUrl);
}

@Override
public java.util.jar.JarFile retrieve(URL url) throws IOException {
URL localUrl = mapping.get(url.toString());
URL localUrl = mapping.get(new UrlKey(url));
if (localUrl == null) {
if (url.getRef() != null) {
url = new URL(url.toString().substring(0, url.toString().lastIndexOf(url.getRef()) - 1));
localUrl = mapping.get(url.toString());
localUrl = mapping.get(new UrlKey(url));
}
}

Expand Down
Loading

0 comments on commit e28f8d4

Please sign in to comment.