Skip to content

Ghidra is a software reverse engineering (SRE) framework

Merge remote-tracking branch 'origin/patch' into Ghidra_12.1

ghidra1cd5291168aa3
diff --cc Ghidra/Framework/FileSystem/src/main/java/ghidra/util/NamingUtilities.java
index d14538af13,2f218d0e0a..73a26638d7
--- a/Ghidra/Framework/FileSystem/src/main/java/ghidra/util/NamingUtilities.java
+++ b/Ghidra/Framework/FileSystem/src/main/java/ghidra/util/NamingUtilities.java
@@@ -27,91 -25,86 +27,91 @@@ import org.apache.commons.lang3.StringU
*/
public final class NamingUtilities {
- /**
- * Max length for a name.
- */
- public final static int MAX_NAME_LENGTH = 60;
-
private final static char MANGLE_CHAR = '_';
+ // Restricted character set for Ghidra related file paths.
+ //
+ // NOTE: When adding additional characters great care must be taken with Ghidra URI/URL encode/decode
+ // to ensure that proper roundtrip for path, query and ref/fragment fields work as expected.
+ // This is particularly a concern with the '+' character.
+
public final static Set<Character> VALID_NAME_CHARSET =
Collections.unmodifiableSet(
- Set.of('.', '-', '=', '@', ' ', '_', '(', ')', '[', ']'));
+ Set.of('.', '-', '=', '@', ' ', '_', '(', ')', '[', ']', '~'));
private NamingUtilities() {
}
/**
- * Tests whether the given string is a valid.
+ * Tests whether the given string is a valid project name.
+ * <p>
* Rules:
* <ul>
- * <li>All characters must be a letter, digit (0..9), period, hyphen, underscore or space</li>
- * <li>May not exceed a length of 60 characters</li>
+ * <li>Name may not be blank (i.e., no characters or all space characters)</li>
+ * <li>Name may not start with period</li>
+ * <li>All characters must be a letter, digit (0..9), or within the allowed character set:
- * '.', '-', '=', '@', ' ', '_', '(', ')', '[', ']' </li>
++ * '.', '-', '=', '@', ' ', '_', '(', ')', '[', ']', '~' </li>
* </ul>
+ *
* @param name name to validate
* @return true if specified name is valid, else false
- * @deprecated method has been deprecated due to improper and widespread use.
- * New methods include {@link NamingUtilities#isValidProjectName(String)} and
- * {@link LocalFileSystem#testValidName(String,boolean)}.
*/
- @Deprecated
- public static boolean isValidName(String name) {
-
- if (name == null) {
- return false;
+ public static boolean isValidProjectName(String name) {
+ try {
+ checkProjectName(name);
+ return true;
}
-
- if ((name.length() < 1) || (name.length() > MAX_NAME_LENGTH)) {
+ catch (Exception e) {
return false;
}
+ }
- for (int i = 0; i < name.length(); i++) {
- char c = name.charAt(i);
- if (!Character.isLetterOrDigit(c) && !VALID_NAME_CHARSET.contains(c)) {
- return false;
- }
- }
-
- return true;
+ /**
+ * Check the specified project name for character restrictions.
+ *
+ * @param name project name
+ * @throws IllegalArgumentException if name restrictions are violated
+ */
+ public static void checkProjectName(String name) throws IllegalArgumentException {
+ checkName(name, "Project name");
}
/**
- * Tests whether the given string is a valid project name.
- * Rules:
+ * Check the specified project or file path element name for character restrictions.
+ * The specified path element must exclude any path separators and must nut include any Windows
+ * drive specification (e.g., {@code C:}). If this naming restriction needs to be imposed on
+ * an entire path, it must be invoked on each path element separately.
+ * <p>
+ * Restrictions include:
* <ul>
- * <li>Name may not start with period</li>
- * <li>All characters must be a letter, digit (0..9), period, hyphen, underscore or space</li>
- * <li>May not exceed a length of 60 characters</li>
+ * <li>Path element may not be blank (i.e., no characters or all space characters).</li>
+ * <li>Path element may not start with a '.' which may result in path traversal or hidden
+ * file/folder use.</li>
+ * <li>Path element may only contain the letters, numbers, or the following characters:
- * '.', '-', '=', '@', ' ', '_', '(', ')', '[', ']'</li>
++ * '.', '-', '=', '@', ' ', '_', '(', ')', '[', ']', '~' </li>
* </ul>
- * @param name name to validate
- * @return true if specified name is valid, else false
+ *
+ * @param pathElement project or file path element (use of leading and trailing spaces should be
+ * avoided but is not prohibited).
+ * @param elementType descriptive name for type of path element or null for default: "Path element"
+ * @throws IllegalArgumentException if name restrictions are violated
*/
- public static boolean isValidProjectName(String name) {
- if (name == null) {
- return false;
- }
+ public static void checkName(String pathElement, String elementType)
+ throws IllegalArgumentException {
- if (name.startsWith(".")) {
- return false;
- }
+ String type = StringUtils.isBlank(elementType) ? "Path element" : elementType;
- if ((name.length() < 1) || (name.length() > MAX_NAME_LENGTH)) {
- return false;
+ if (StringUtils.isBlank(pathElement)) {
+ throw new IllegalArgumentException("A blank " + type + " is not allowed");
}
-
- for (int i = 0; i < name.length(); i++) {
- char c = name.charAt(i);
- if (!Character.isLetterOrDigit(c) && !VALID_NAME_CHARSET.contains(c)) {
- return false;
- }
+ if (pathElement.startsWith(".")) { // also prevents '.' and '..' path elements
+ throw new IllegalArgumentException(type + " starting with '.' is not permitted");
+ }
+ String invalidChar = findInvalidChar(pathElement);
+ if (invalidChar != null) {
+ throw new IllegalArgumentException(
+ type + " contains invalid character: '" + invalidChar + "'");
}
-
- return true;
}
/**