Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7418 Corrected Ghidra URL encode/decode issues

ghidra1bb474169b236
diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/protocol/ghidra/DefaultLocalGhidraProtocolConnector.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/protocol/ghidra/DefaultLocalGhidraProtocolConnector.java
index 26c7c1461b..c8d03ede35 100644
--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/protocol/ghidra/DefaultLocalGhidraProtocolConnector.java
+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/protocol/ghidra/DefaultLocalGhidraProtocolConnector.java
@@ -65,6 +65,13 @@ public class DefaultLocalGhidraProtocolConnector extends GhidraProtocolConnector
}
}
+ @Override
+ protected String parseRepositoryName() {
+ // The URL path is a local storage path (possibly UNC), not a server repository path.
+ // The project name is obtained from the locator after superclass construction.
+ return null;
+ }
+
@Override
public String getRepositoryName() {
return localStorageLocator.getName();
@@ -73,7 +80,9 @@ public class DefaultLocalGhidraProtocolConnector extends GhidraProtocolConnector
@Override
protected String parseItemPath() throws MalformedURLException {
- String path = url.getQuery();
+ // Content names must be decoded before project lookup or folder creation.
+ // URI decoding preserves literal '+' characters and decodes percent escapes only once.
+ String path = uri.getQuery();
initFolderItemPath(path);
diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/protocol/ghidra/GhidraProtocolConnector.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/protocol/ghidra/GhidraProtocolConnector.java
index 6db7dc5b6c..846f1c30b0 100644
--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/protocol/ghidra/GhidraProtocolConnector.java
+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/protocol/ghidra/GhidraProtocolConnector.java
@@ -107,11 +107,12 @@ public abstract class GhidraProtocolConnector {
}
/**
- * Parse repository name from URL
+ * Parse repository name from URL. Invoked by the constructor; overrides must not depend
+ * on subclass fields initialized after the superclass constructor returns.
* @return repository name or null if not specified
* @throws MalformedURLException if URL is invalid
*/
- private String parseRepositoryName() throws MalformedURLException {
+ protected String parseRepositoryName() throws MalformedURLException {
String path = uri.getPath();
diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/protocol/ghidra/GhidraURL.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/protocol/ghidra/GhidraURL.java
index 79297c4cd8..7a4c3a480e 100644
--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/protocol/ghidra/GhidraURL.java
+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/protocol/ghidra/GhidraURL.java
@@ -236,8 +236,7 @@ public class GhidraURL {
return "/";
}
if (!path.startsWith("/")) {
- throw new URISyntaxException(uri.toString(),
- "Missing absolute project content path");
+ throw new URISyntaxException(uri.toString(), "Missing absolute project content path");
}
return path;
}
@@ -427,26 +426,34 @@ public class GhidraURL {
}
/**
- * Check for valid project path. See {@link NamingUtilities#checkName(String, String)}).
+ * Check for valid project path. Empty path elements are prohibited, except for a single
+ * trailing separator identifying a folder. See {@link NamingUtilities#checkName(String, String)}).
*
* @param path project path to check (using '/' path separator).
* @param startIndex index at which to start checking immediately after root path separator
- * @throws IllegalArgumentException if path contains invalid character
+ * @throws IllegalArgumentException if path contains an invalid or empty element
*/
private static void checkValidProjectPath(String path, int startIndex) {
String str = path.substring(startIndex);
- if (str.length() != 0) {
- for (String s : str.split("/")) {
- NamingUtilities.checkName(s, null);
- }
+ if (str.isEmpty()) {
+ return; // root path
+ }
+ if (str.endsWith("/")) {
+ str = str.substring(0, str.length() - 1);
+ }
+ // Preserve empty elements so repeated separators cannot be silently discarded.
+ for (String s : str.split("/", -1)) {
+ NamingUtilities.checkName(s, null);
}
}
/**
* Create a Ghidra URL from a string form of a Ghidra URL or local project path.
* This method can consume strings produced by the getDisplayString method.
+ * Parsed URLs are checked for the same decoded name and path restrictions as constructed URLs.
+ * A local project path must omit the project marker and directory extensions.
* <p>
- * NOTE: If a Ghidra URL string is improperly encode (e.g., had been made by simple string
+ * NOTE: If a Ghidra URL string is improperly encoded (e.g., had been made by simple string
* concatentaion) an exception may be thrown. In such situations, a URL re-encoding will be
* attempted but may produce an incorrect URL if partial encoding was used. The following
* characters present in an improperly encoded URL may not handled properly:
@@ -457,7 +464,7 @@ public class GhidraURL {
* @return local Ghidra project URL
* @throws IllegalArgumentException invalid path or URL specified
*/
- public static URL toURL(String projectPathOrURL) {
+ public static URL toURL(String projectPathOrURL) throws IllegalArgumentException {
if (!isGhidraURL(projectPathOrURL)) {
if (projectPathOrURL.endsWith(ProjectLocator.PROJECT_DIR_SUFFIX) ||
projectPathOrURL.endsWith(ProjectLocator.PROJECT_FILE_SUFFIX)) {
@@ -465,7 +472,8 @@ public class GhidraURL {
throw new IllegalArgumentException("Project path must omit extension: " + ext);
}
projectPathOrURL = checkLocalAbsolutePath(projectPathOrURL, false);
- int minSplitIndex = projectPathOrURL.charAt(2) == ':' ? 3 : 0;
+ int minSplitIndex =
+ (projectPathOrURL.length() > 2 && projectPathOrURL.charAt(2) == ':') ? 3 : 0;
int splitIndex = projectPathOrURL.lastIndexOf('/');
if (splitIndex < minSplitIndex || projectPathOrURL.length() == (splitIndex + 1)) {
throw new IllegalArgumentException("Absolute project path is missing project name");
@@ -489,12 +497,53 @@ public class GhidraURL {
throw new IllegalArgumentException("Invalid Ghidra URL: " + projectPathOrURL, e);
}
}
- if (!isLocalURL(url) && url.getQuery() != null) {
- throw new IllegalArgumentException("Invalid Ghidra URL: " + projectPathOrURL);
+ try {
+ validateParsedURL(url);
+ }
+ catch (URISyntaxException e) {
+ throw new IllegalArgumentException("Invalid Ghidra URL: " + projectPathOrURL, e);
}
return url;
}
+ /**
+ * Validate decoded components without rebuilding the URL or changing its encoding.
+ */
+ private static void validateParsedURL(URL url) throws URISyntaxException {
+ URI uri = url.toURI();
+ if (isLocalURL(url)) {
+ String path = uri.getPath();
+ if (path == null || path.endsWith("/") || path.contains("\\")) {
+ throw new IllegalArgumentException("Invalid local project path: " + path);
+ }
+ // UNC URLs have four leading slashes; the storage path has two.
+ String storagePath = path.startsWith("////") ? path.substring(2) : path;
+ checkLocalAbsolutePath(storagePath, false);
+ NamingUtilities.checkName(path.substring(path.lastIndexOf('/') + 1), "Project name");
+ String contentPath = getLocalProjectContentPath(uri);
+ checkValidProjectPath(contentPath, 1);
+ return;
+ }
+
+ if (uri.isOpaque()) {
+ // The outer URI escapes the embedded URL. Decode that layer once, then validate
+ // the embedded hierarchical URI's decoded repository/content path.
+ uri = new URI(uri.getSchemeSpecificPart());
+ }
+ if (uri.isOpaque() || uri.getScheme() == null || StringUtils.isBlank(uri.getHost()) ||
+ uri.getQuery() != null) {
+ throw new IllegalArgumentException("Invalid server URL: " + url);
+ }
+ String path = uri.getPath();
+ if (path == null || path.isEmpty() || "/".equals(path)) {
+ return; // server-only URL
+ }
+ if (!path.startsWith("/")) {
+ throw new IllegalArgumentException("Absolute repository path required");
+ }
+ checkValidProjectPath(path, 1);
+ }
+
private static URL getReformedGhidraURL(String invalidGhidraUrl) {
if (!invalidGhidraUrl.startsWith(PROTOCOL_URL_START + "/")) {
@@ -503,9 +552,16 @@ public class GhidraURL {
}
try {
- // Assume old code was used to directly for URL without the use of URI
+ // Assume old code was used to directly create URL without the use of URI
URL url = new URL(invalidGhidraUrl);
+ if (isLocalURL(url)) {
+ // An empty URL host would add an authority delimiter to a UNC path that
+ // already has four leading slashes. Local URLs have no authority.
+ return new URI(PROTOCOL, null, url.getPath(), url.getQuery(), url.getRef())
+ .toURL();
+ }
+
URI repairedUri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(),
url.getPort(), url.getPath(), url.getQuery(), url.getRef());
return repairedUri.toURL();
@@ -521,16 +577,16 @@ public class GhidraURL {
*
* @param ghidraUrl the base Ghidra project or repository URL which will be used as the basis
* for forming a new Ghidra URL.
- * @param projectFilePath an absolute folder or file path within the project (e.g., /a/b/c,
+ * @param projectFilePath a decoded absolute folder or file path within the project (e.g., /a/b/c,
* may be null for root folder). Folder paths should end with a '/' character.
- * @param ref optional location reference (may be null) which is appended to URL with a '#'
+ * @param ref optional decoded location reference (may be null) which is appended to URL with a '#'
* delimiter.
* @return new resolved URL
* @throws IllegalArgumentException if an invalid Ghidra project or repository URL is specified
* or an invalid folder/file path is specified.
*/
public static URL resolve(URL ghidraUrl, String projectFilePath, String ref) {
-
+
if (!StringUtils.isBlank(projectFilePath)) {
if (!projectFilePath.startsWith("/") || projectFilePath.contains("\\")) {
throw new IllegalArgumentException("Absolute path required using '/' delimiter");
@@ -567,17 +623,14 @@ public class GhidraURL {
"Invalid Ghidra URL");
}
URI extURI = URI.create(subPart);
- URI revisedExtURI = extURI.resolve(path);
- return new URI(PROTOCOL, revisedExtURI.toURL().toExternalForm(),
- ref).toURL();
+ URI revisedExtURI =
+ new URI(extURI.getScheme(), extURI.getAuthority(), path, null, null);
+ return new URI(PROTOCOL, revisedExtURI.toURL().toExternalForm(), ref).toURL();
}
- // Resolve normal Ghidra server URL
- uri = uri.resolve(path);
- if (ref != null) {
- uri = uri.resolve("#" + ref);
- }
- return uri.toURL();
+ // Encode decoded path and reference components, as makeURL does. Resolving a
+ // string would interpret literal '%' and '#' characters as URI syntax.
+ return new URI(PROTOCOL, uri.getAuthority(), path, null, ref).toURL();
}
}
catch (URISyntaxException | MalformedURLException e) {
@@ -788,6 +841,7 @@ public class GhidraURL {
* @param dirPath absolute path of project location directory
* @param projectName name of project
* @return local Ghidra project URL
+ * @throws IllegalArgumentException if an absolute dirPath is not specified
*/
public static URL makeURL(String dirPath, String projectName) {
return makeURL(dirPath, projectName, null, null);
@@ -856,8 +910,7 @@ public class GhidraURL {
}
try {
- return new URI(GhidraURL.PROTOCOL, null, path, projectFilePath, ref)
- .toURL();
+ return new URI(GhidraURL.PROTOCOL, null, path, projectFilePath, ref).toURL();
}
catch (URISyntaxException | MalformedURLException e) {
throw new IllegalArgumentException("Unable to form local project URL", e);
@@ -964,13 +1017,7 @@ public class GhidraURL {
if (!repositoryPath.startsWith("/") || repositoryPath.indexOf('\\') >= 0) {
throw new IllegalArgumentException("Invalid repository path");
}
- if (repositoryPath.length() != 1) {
- String checkPath = repositoryPath.substring(1);
- if (checkPath.endsWith("/")) {
- checkPath = checkPath.substring(0, checkPath.length() - 1);
- }
- checkValidProjectPath(checkPath, 0);
- }
+ checkValidProjectPath(repositoryPath, 1);
path += repositoryPath;
}
diff --git a/Ghidra/Framework/Project/src/test/java/ghidra/framework/protocol/ghidra/DefaultLocalGhidraProtocolConnectorParseTest.java b/Ghidra/Framework/Project/src/test/java/ghidra/framework/protocol/ghidra/DefaultLocalGhidraProtocolConnectorParseTest.java
index 94c7a60d18..a856a57791 100644
--- a/Ghidra/Framework/Project/src/test/java/ghidra/framework/protocol/ghidra/DefaultLocalGhidraProtocolConnectorParseTest.java
+++ b/Ghidra/Framework/Project/src/test/java/ghidra/framework/protocol/ghidra/DefaultLocalGhidraProtocolConnectorParseTest.java
@@ -19,6 +19,7 @@ import static org.junit.Assert.*;
import java.net.MalformedURLException;
import java.net.URI;
+import java.net.URL;
import org.junit.Test;
@@ -79,4 +80,89 @@ public class DefaultLocalGhidraProtocolConnectorParseTest extends AbstractGeneri
}
}
+
+ @Test
+ public void testParseUNCProjectFileURL() throws Exception {
+
+ URL url = GhidraURL.makeURL("//server/share", "Project", "/folder/file", null);
+ assertEquals("ghidra:////server/share/Project?/folder/file", url.toExternalForm());
+ assertTrue(GhidraURL.isLocalURL(url));
+ assertEquals("//server/share/", GhidraURL.getProjectStorageLocator(url).getLocation());
+ assertEquals("Project", GhidraURL.getProjectStorageLocator(url).getName());
+ assertEquals("/folder/file", GhidraURL.getProjectPathname(url));
+
+ DefaultLocalGhidraProtocolConnector connector =
+ new DefaultLocalGhidraProtocolConnector(url);
+ assertEquals("Project", connector.getRepositoryName());
+ assertEquals("//server/share/", connector.getLocalProjectLocator().getLocation());
+ assertEquals("/folder", connector.getFolderPath());
+ assertEquals("file", connector.getFolderItemName());
+ assertEquals("/folder/file", getInstanceField("itemPath", connector));
+ }
+
+ @Test
+ public void testParseUNCProjectRootAndFolderURLs() throws Exception {
+ for (String location : new String[] { "//server/share", "\\\\server\\share" }) {
+ for (String path : new String[] { null, "/", "/folder [a]+/東京/" }) {
+ URL url = GhidraURL.makeURL(location, "Project [a]+", path, "ref");
+ url = URI.create(url.toURI().toASCIIString()).toURL();
+ DefaultLocalGhidraProtocolConnector connector =
+ new DefaultLocalGhidraProtocolConnector(url);
+ assertEquals("Project [a]+", connector.getRepositoryName());
+ assertEquals("//server/share/", connector.getLocalProjectLocator().getLocation());
+ assertEquals(GhidraURL.getProjectStorageLocator(url),
+ connector.getLocalProjectLocator());
+ String expectedPath = path == null ? "/" : path;
+ assertEquals(expectedPath, getInstanceField("itemPath", connector));
+ assertEquals(expectedPath.equals("/") ? "/"
+ : expectedPath.substring(0, expectedPath.length() - 1),
+ connector.getFolderPath());
+ assertNull(connector.getFolderItemName());
+ }
+ }
+ }
+
+ @Test
+ public void testParseEncodedFileNames() throws Exception {
+ String[] names = { "with space", "with[brackets]", "plus+name", "東京", "\uD801\uDC00" };
+ for (String folder : names) {
+ for (String file : names) {
+ String path = "/" + folder + "/" + file;
+ URL url = GhidraURL.makeURL("/projects", "Project", path, "ref # %20 +");
+ // Also exercise percent-encoded Unicode, as received in an external URL.
+ url = URI.create(url.toURI().toASCIIString()).toURL();
+ DefaultLocalGhidraProtocolConnector connector =
+ new DefaultLocalGhidraProtocolConnector(url);
+ assertEquals("/" + folder, connector.getFolderPath());
+ assertEquals(file, connector.getFolderItemName());
+ assertEquals(path, getInstanceField("itemPath", connector));
+ assertEquals(GhidraURL.getProjectPathname(url),
+ getInstanceField("itemPath", connector));
+ assertEquals("Project", connector.getRepositoryName());
+ }
+ }
+ }
+
+ @Test
+ public void testParseEncodedFolderPath() throws Exception {
+ String folderPath = "/folder with spaces/東京 [a]+";
+ URL url = GhidraURL.makeURL("/projects", "Project", folderPath + "/", "ref");
+ url = URI.create(url.toURI().toASCIIString()).toURL();
+ DefaultLocalGhidraProtocolConnector connector =
+ new DefaultLocalGhidraProtocolConnector(url);
+ assertEquals(folderPath, connector.getFolderPath());
+ assertNull(connector.getFolderItemName());
+ assertEquals(folderPath + "/", getInstanceField("itemPath", connector));
+ }
+
+ @Test
+ public void testParseEncodedFileAtRoot() throws Exception {
+ String name = "file + [a]";
+ URL url = GhidraURL.makeURL("/projects", "Project", "/" + name, null);
+ DefaultLocalGhidraProtocolConnector connector =
+ new DefaultLocalGhidraProtocolConnector(url);
+ assertEquals("/", connector.getFolderPath());
+ assertEquals(name, connector.getFolderItemName());
+ assertEquals("/" + name, getInstanceField("itemPath", connector));
+ }
}
diff --git a/Ghidra/Framework/Project/src/test/java/ghidra/framework/protocol/ghidra/GhidraURLTest.java b/Ghidra/Framework/Project/src/test/java/ghidra/framework/protocol/ghidra/GhidraURLTest.java
index bdcd29883e..5596da1ed6 100644
--- a/Ghidra/Framework/Project/src/test/java/ghidra/framework/protocol/ghidra/GhidraURLTest.java
+++ b/Ghidra/Framework/Project/src/test/java/ghidra/framework/protocol/ghidra/GhidraURLTest.java
@@ -18,7 +18,9 @@ package ghidra.framework.protocol.ghidra;
import static org.junit.Assert.*;
import java.io.IOException;
+import java.lang.reflect.Field;
import java.net.*;
+import java.util.List;
import org.junit.Before;
import org.junit.Test;
@@ -635,6 +637,25 @@ public class GhidraURLTest extends AbstractGenericTest {
}
}
+ @Test
+ public void testToURLShortProjectPaths() {
+ assertEquals(GhidraURL.makeURL("/", "a").toExternalForm(),
+ GhidraURL.toURL("/a").toExternalForm());
+ assertEquals(GhidraURL.makeURL("/", "a").toExternalForm(),
+ GhidraURL.toURL("\\a").toExternalForm());
+ assertEquals(GhidraURL.makeURL("C:/", "a").toExternalForm(),
+ GhidraURL.toURL("C:/a").toExternalForm());
+ }
+
+ @Test
+ public void testToURLRejectsMissingProjectName() {
+ for (String path : new String[] { "/", "\\", "C:", "C:/", "/C:/", "/projects/" }) {
+ IllegalArgumentException exception = assertThrows(path, IllegalArgumentException.class,
+ () -> GhidraURL.toURL(path));
+ assertEquals("Absolute project path is missing project name", exception.getMessage());
+ }
+ }
+
// toURL(String)
@Test
public void testToURL() throws Exception {
@@ -705,6 +726,91 @@ public class GhidraURLTest extends AbstractGenericTest {
assertEquals(ghidraUrl, GhidraURL.toURL(GhidraURL.getDisplayString(ghidraUrl)));
}
+ @Test
+ public void testLocalProjectMarkerCompatibility() throws Exception {
+ for (String location : new String[] { "/projects", "C:/projects", "//server/share" }) {
+ // This branch accepts marker filenames in ProjectLocator, but not in toURL paths.
+ ProjectLocator locator = new ProjectLocator(location, "Test.gpr");
+ assertEquals("Test", locator.getName());
+ URL url = GhidraURL.makeURL(location, "Test");
+ assertEquals(url.toExternalForm(), locator.getURL().toExternalForm());
+ assertEquals(locator, GhidraURL.getProjectStorageLocator(url));
+ assertEquals(url.toExternalForm(),
+ GhidraURL.toURL(location + "/Test").toExternalForm());
+ assertEquals(url.toExternalForm(),
+ GhidraURL.toURL(GhidraURL.getDisplayString(url)).toExternalForm());
+ for (String name : new String[] { "Test.gpr", "Test.rep", "Test.gpr.gpr", ".gpr" }) {
+ assertThrows(IllegalArgumentException.class,
+ () -> GhidraURL.toURL(location + "/" + name));
+ }
+
+ // Explicit URL parsing retains its encoding; locator interpretation strips the marker.
+ URL markerURL = GhidraURL.makeURL(location, "Test.gpr", "/folder/file", "ref");
+ assertEquals(markerURL.toExternalForm(),
+ GhidraURL.toURL(markerURL.toExternalForm()).toExternalForm());
+ assertEquals(locator, GhidraURL.getProjectStorageLocator(markerURL));
+ assertEquals("Test",
+ new DefaultLocalGhidraProtocolConnector(markerURL).getRepositoryName());
+ }
+ }
+
+ @Test
+ public void testToURLRejectsInvalidDecodedComponents() {
+ String[] invalid = { "bad%3Fname", "bad%2Aname", "bad%3Aname", "bad%5Cname",
+ "bad%00name", "%20", "%2E", "%2E%2E", "%2Ehidden", "bad%23name",
+ "bad%2520name", "bad%252Fname", "bad%26name", "bad%C2%A9name", "a//b", "a//" };
+ for (String component : invalid) {
+ for (String text : new String[] {
+ "ghidra:/projects/" + component,
+ "ghidra:/" + component + "/Project",
+ "ghidra:/projects/Project?/" + component,
+ "ghidra://localhost/" + component,
+ "ghidra://localhost/Repo/" + component }) {
+ assertThrows(text, IllegalArgumentException.class, () -> GhidraURL.toURL(text));
+ }
+ }
+ for (String text : new String[] { "ghidra:/", "ghidra:/projects/",
+ "ghidra:/projects/Project?relative", "ghidra:/projects/Project?",
+ "ghidra:/projects/Project?//", "ghidra://localhost//Repo",
+ "ghidra:/projects/Project?/bad name*", "ghidra://localhost/Repo/bad name*" }) {
+ assertThrows(text, IllegalArgumentException.class, () -> GhidraURL.toURL(text));
+ }
+ }
+
+ @Test
+ public void testToURLPreservesEncodedComponents() throws Exception {
+ String name = "name + [a] 東京 \uD801\uDC00";
+ for (String path : new String[] { null, "/", "/" + name, "/" + name + "/" }) {
+ for (URL url : new URL[] {
+ GhidraURL.makeURL("/projects", name, path, "ref #?%20+"),
+ GhidraURL.makeURL("C:/projects", name, path, "ref #?%20+"),
+ GhidraURL.makeURL("//server/share", name, path, "ref #?%20+"),
+ GhidraURL.makeURL("localhost", 123, name, path, "ref #?%20+") }) {
+ String text = url.toURI().toASCIIString();
+ URL parsed = GhidraURL.toURL(text);
+ assertEquals(text, parsed.toExternalForm());
+ assertEquals(path == null ? "/" : path, GhidraURL.getProjectPathname(parsed));
+ assertEquals("ref #?%20+", GhidraURL.getDecodedReference(parsed));
+ }
+ }
+ for (String text : new String[] { "ghidra://localhost", "ghidra://localhost:123/" }) {
+ assertEquals(text, GhidraURL.toURL(text).toExternalForm());
+ }
+ }
+
+ @Test
+ public void testToURLValidatesExtensionComponents() throws Exception {
+ for (String path : new String[] { "/Repo/bad*name", "/Repo/../file", "/Repo/a//" }) {
+ URI inner = new URI("https", null, "localhost", 123, path, null, null);
+ String text = new URI("ghidra", inner.toString(), null).toString();
+ assertThrows(text, IllegalArgumentException.class, () -> GhidraURL.toURL(text));
+ }
+ URI inner = new URI("https", null, "localhost", 123, "/Repo [a]/folder +/",
+ null, null);
+ String text = new URI("ghidra", inner.toString(), "ref #?").toString();
+ assertEquals(text, GhidraURL.toURL(text).toExternalForm());
+ }
+
@Test
public void testRelaxedToURL() throws Exception {
String path = "/Test/my foo/bar";
@@ -859,6 +965,229 @@ public class GhidraURLTest extends AbstractGenericTest {
}
+ @Test
+ public void testBuildersRejectEmptyContentPathElements() throws Exception {
+ ProjectLocator locator = new ProjectLocator("/projects", "Test");
+ URL local = locator.getURL();
+ URL server = GhidraURL.makeURL("localhost", 123, "Test");
+ for (String path : new String[] { "//", "///", "/a//", "/a///", "/a/b//",
+ "//a", "/a//b", "/a//b/" }) {
+ assertThrows(path, IllegalArgumentException.class,
+ () -> GhidraURL.makeURL("/projects", "Test", path, null));
+ assertThrows(path, IllegalArgumentException.class,
+ () -> GhidraURL.makeURL(locator, path, null));
+ assertThrows(path, IllegalArgumentException.class,
+ () -> GhidraURL.makeURL("localhost", 123, "Test", path));
+ assertThrows(path, IllegalArgumentException.class,
+ () -> GhidraURL.makeURL("localhost", 123, "Test", path, "ref"));
+ assertThrows(path, IllegalArgumentException.class,
+ () -> GhidraURL.resolve(local, path, null));
+ assertThrows(path, IllegalArgumentException.class,
+ () -> GhidraURL.resolve(server, path, null));
+
+ // The connectors reject these same structures when supplied as raw URLs.
+ URL rawLocal = toGhidraLocalURL("/projects/Test", path);
+ URL rawServer = toGhidraServerURL("localhost", 123, "Test", path);
+ assertThrows(path, MalformedURLException.class,
+ () -> new DefaultLocalGhidraProtocolConnector(rawLocal));
+ assertThrows(path, MalformedURLException.class,
+ () -> new DefaultGhidraProtocolConnector(rawServer));
+ }
+ assertThrows(IllegalArgumentException.class,
+ () -> GhidraURL.makeURL("localhost", 123, "Test", "/a//", "file", null));
+ assertThrows(IllegalArgumentException.class,
+ () -> GhidraURL.makeURL("localhost", 123, "Test", "/a", "file//", null));
+ }
+
+ @Test
+ public void testBuildersPreserveValidContentPathStructure() throws Exception {
+ ProjectLocator locator = new ProjectLocator("/projects", "Test");
+ URL server = GhidraURL.makeURL("localhost", 123, "Test");
+ for (String path : new String[] { null, "", "/", "/a", "/a/", "/a/b", "/a/b/" }) {
+ String expectedPath = path == null || path.isEmpty() ? "/" : path;
+ URL localURL = GhidraURL.makeURL("/projects", "Test", path, null);
+ URL serverURL = GhidraURL.makeURL("localhost", 123, "Test", path);
+ assertEquals(localURL.toExternalForm(),
+ GhidraURL.makeURL(locator, path, null).toExternalForm());
+ assertEquals(serverURL.toExternalForm(),
+ GhidraURL.resolve(server, path, null).toExternalForm());
+ for (URL url : new URL[] { localURL, serverURL }) {
+ assertEquals(expectedPath, GhidraURL.getProjectPathname(url));
+ GhidraProtocolConnector connector = GhidraURL.isLocalURL(url)
+ ? new DefaultLocalGhidraProtocolConnector(url)
+ : new DefaultGhidraProtocolConnector(url);
+ assertEquals(expectedPath, getInstanceField("itemPath", connector));
+ if (expectedPath.endsWith("/")) {
+ assertNull(connector.getFolderItemName());
+ }
+ }
+ }
+ }
+
+ @Test
+ public void testLocalStoragePathsRejectRepeatedSeparators() {
+ for (String path : new String[] { "/projects//", "/projects///", "/a//b",
+ "C:/projects//", "//server/share//" }) {
+ assertThrows(path, IllegalArgumentException.class,
+ () -> GhidraURL.checkLocalAbsolutePath(path, true));
+ }
+ assertEquals("/", GhidraURL.checkLocalAbsolutePath("/", true));
+ assertEquals("/C:/", GhidraURL.checkLocalAbsolutePath("C:/", true));
+ assertEquals("//server/share/",
+ GhidraURL.checkLocalAbsolutePath("//server/share/", true));
+ }
+
+ @Test
+ public void testResolveSpacesInServerPath() throws Exception {
+ // Reproduce getSharedProjectURL: resolve a decoded file path against its folder URL.
+ URL base = GhidraURL.makeURL("localhost", 123, "Repository", "/");
+ URL url = GhidraURL.resolve(base, "/Sample Program.exe", null);
+ String expected = "ghidra://localhost:123/Repository/Sample%20Program.exe";
+ assertEquals(expected, url.toExternalForm());
+ assertEquals("/Repository/Sample%20Program.exe", url.getPath());
+ assertEquals("/Repository/Sample Program.exe", url.toURI().getPath());
+ assertEquals("/Sample Program.exe", GhidraURL.getProjectPathname(url));
+ assertNull(url.getQuery());
+ assertEquals(expected, GhidraURL.toURL(expected).toExternalForm());
+ assertEquals(expected,
+ GhidraURL.toURL(expected.replace("%20", " ")).toExternalForm());
+
+ DefaultGhidraProtocolConnector connector = new DefaultGhidraProtocolConnector(url);
+ assertEquals("Repository", connector.getRepositoryName());
+ assertEquals("/", connector.getFolderPath());
+ assertEquals("Sample Program.exe", connector.getFolderItemName());
+ }
+
+ @Test
+ public void testResolveSpacesInLocalPathAndQuery() throws Exception {
+ String path = "/folder/Sample Program.exe";
+ for (String location : new String[] { "/my projects", "C:/my projects",
+ "//server/my share" }) {
+ ProjectLocator locator = new ProjectLocator(location, "My Project");
+ URL base = locator.getURL();
+ URL url = GhidraURL.resolve(base, path, null);
+ String expected = base.toExternalForm() + "?/folder/Sample%20Program.exe";
+ assertEquals(expected, url.toExternalForm());
+ assertFalse(url.getPath().contains(" "));
+ assertTrue(url.getPath().contains("%20"));
+ assertTrue(url.toURI().getPath().endsWith("/My Project"));
+ assertEquals("/folder/Sample%20Program.exe", url.getQuery());
+ assertEquals(path, url.toURI().getQuery());
+ assertEquals(path, GhidraURL.getProjectPathname(url));
+ assertEquals(expected, GhidraURL.makeURL(locator, path, null).toExternalForm());
+ assertEquals(expected, GhidraURL.toURL(expected).toExternalForm());
+ assertEquals(expected,
+ GhidraURL.toURL(expected.replace("%20", " ")).toExternalForm());
+
+ DefaultLocalGhidraProtocolConnector connector =
+ new DefaultLocalGhidraProtocolConnector(url);
+ assertEquals(locator, connector.getLocalProjectLocator());
+ assertEquals("/folder", connector.getFolderPath());
+ assertEquals("Sample Program.exe", connector.getFolderItemName());
+ }
+ }
+
+ @Test
+ public void testResolveServerEncodedNames() throws Exception {
+ String[] names = { "plain", "with space", "with[brackets]", "plus+name",
+ "symbols.-=@_()~", "東京", "\uD801\uDC00" };
+ for (String repository : names) {
+ URL base = GhidraURL.makeURL("localhost", 123, repository, "/old", "old ref");
+ for (String name : names) {
+ for (String suffix : new String[] { "", "/" }) {
+ String path = "/" + name + "/child" + suffix;
+ URL resolved = GhidraURL.resolve(base, path, "new ref");
+ URL expected = GhidraURL.makeURL("localhost", 123, repository, path,
+ "new ref");
+ assertEquals(expected.toExternalForm(), resolved.toExternalForm());
+ assertEquals(repository, GhidraURL.getRepositoryName(resolved));
+ assertEquals(path, GhidraURL.getProjectPathname(resolved));
+ assertEquals("new ref", GhidraURL.getDecodedReference(resolved));
+ assertNull(resolved.getQuery());
+ }
+ }
+ }
+ }
+
+ @Test
+ public void testResolveServerEncodedReferences() throws Exception {
+ URL base = GhidraURL.makeURL("localhost", 123, "Repo", "/old", "old ref");
+ String[] refs = { null, "", "ref with spaces", "literal%20escape", "a+b",
+ "ref#?[]%&/\\", "東京" };
+ for (String ref : refs) {
+ URL resolved = GhidraURL.resolve(base, "/folder/file", ref);
+ URL expected = GhidraURL.makeURL("localhost", 123, "Repo", "/folder/file", ref);
+ assertEquals(expected.toExternalForm(), resolved.toExternalForm());
+ assertEquals(ref, GhidraURL.getDecodedReference(resolved));
+ assertEquals("/folder/file", GhidraURL.getProjectPathname(resolved));
+ }
+ }
+
+ @Test
+ public void testServerProjectAndFolderURLsWithEncodedNames() throws Exception {
+ String repository = "Repo [a]+";
+ String path = "/folder [b]+/file";
+ String ref = "ref # %20 +";
+ URL fileURL = GhidraURL.makeURL("localhost", 123, repository, path, ref);
+ URL rootURL = GhidraURL.makeURL("localhost", 123, repository);
+ assertEquals(rootURL.toExternalForm(), GhidraURL.getProjectURL(fileURL).toExternalForm());
+ assertEquals(rootURL.toExternalForm(), GhidraURL.resolve(fileURL, null, null).toExternalForm());
+ assertEquals(rootURL.toExternalForm(), GhidraURL.resolve(fileURL, "", null).toExternalForm());
+ assertEquals(GhidraURL.makeURL("localhost", 123, repository, "/").toExternalForm(),
+ GhidraURL.resolve(fileURL, "/", null).toExternalForm());
+
+ URL folderURL = GhidraURL.getFolderURL(fileURL);
+ assertEquals(GhidraURL.makeURL("localhost", 123, repository, path + "/", ref)
+ .toExternalForm(), folderURL.toExternalForm());
+ assertEquals(path + "/", GhidraURL.getProjectPathname(folderURL));
+ assertEquals(ref, GhidraURL.getDecodedReference(folderURL));
+ assertEquals(folderURL.toExternalForm(), GhidraURL.getFolderURL(folderURL).toExternalForm());
+ }
+
+ @Test
+ public void testResolveExtensionEncodedNamesAndReferences() throws Exception {
+ // Install a parsing-only extension handler; no network connection is needed.
+ Field handlers = Handler.class.getDeclaredField("protocolHandlers");
+ handlers.setAccessible(true);
+ Object originalHandlers = handlers.get(null);
+ GhidraProtocolHandler handler = new GhidraProtocolHandler() {
+ @Override
+ public boolean isExtensionSupported(String protocol) {
+ return "https".equals(protocol);
+ }
+
+ @Override
+ public GhidraProtocolConnector getConnector(URL url) {
+ throw new UnsupportedOperationException("Parsing only");
+ }
+ };
+ try {
+ handlers.set(null, List.of(handler));
+ String repository = "Repo [a]+";
+ URI inner = new URI("https", null, "localhost", 123, "/" + repository + "/old",
+ null, null);
+ URL base = new URI(GhidraURL.PROTOCOL, inner.toString(), "old ref").toURL();
+ for (String path : new String[] { null, "/", "/folder [b]+/東京",
+ "/folder [b]+/東京/" }) {
+ for (String ref : new String[] { null, "", "ref #?[]%20+ /\\" }) {
+ String fullPath = "/" + repository + (path == null ? "" : path);
+ URI expectedInner = new URI("https", null, "localhost", 123, fullPath,
+ null, null);
+ URL expected = new URI(GhidraURL.PROTOCOL, expectedInner.toString(), ref)
+ .toURL();
+ URL resolved = GhidraURL.resolve(base, path, ref);
+ assertEquals(expected.toExternalForm(), resolved.toExternalForm());
+ assertEquals(repository, GhidraURL.getRepositoryName(resolved));
+ assertEquals(path == null ? "/" : path, GhidraURL.getProjectPathname(resolved));
+ assertEquals(ref, GhidraURL.getDecodedReference(resolved));
+ }
+ }
+ }
+ finally {
+ handlers.set(null, originalHandlers);
+ }
+ }
+
private static class DummyGhidraProtocolConnector extends GhidraProtocolConnector {
private URL repositoryURL;