Class PathSecurity

java.lang.Object
fr.pacdu.pacpackquests.util.PathSecurity

public final class PathSecurity extends Object
Utility class providing path validation, canonicalization, and security checks against path traversal attacks and reserved filesystem names.
  • Method Details

    • isValidIdentifier

      public static boolean isValidIdentifier(String id)
      Checks if the given identifier is safe to be used in file and directory names. Must be 1-64 characters matching [a-z0-9_], and not a reserved Windows device name.
      Parameters:
      id - The identifier string to check.
      Returns:
      true if valid and safe; false otherwise.
    • getQuestsRootDir

      public static Path getQuestsRootDir()
      Returns the absolute, normalized root directory for quests.
      Returns:
      The canonical Path to the quests root directory.
    • isPathContained

      public static boolean isPathContained(Path parent, Path child)
      Checks if child path is strictly inside parent path (not equal, no traversal escaping).
      Parameters:
      parent - The trusted base directory path.
      child - The candidate target path to verify.
      Returns:
      true if child is strictly inside parent; false otherwise.
    • getSafeCategoryDir

      public static Optional<Path> getSafeCategoryDir(String category)
      Returns the safe category directory Path if valid, or empty Optional if unsafe or invalid.
      Parameters:
      category - The category identifier to resolve.
      Returns:
      An Optional holding the validated directory Path, or empty if invalid.
    • getSafeQuestFile

      public static Optional<Path> getSafeQuestFile(String category, String questId)
      Returns the safe quest JSON file Path if both category and questId are valid, or empty Optional if unsafe or invalid.
      Parameters:
      category - The category identifier.
      questId - The quest identifier.
      Returns:
      An Optional holding the validated JSON Path, or empty if invalid.
    • formatStringToValidPath

      public static String formatStringToValidPath(String str)
      Formats an arbitrary string to be safe for filenames and filesystem paths: converts to lowercase, decomposes accents/diacritics, and removes invalid characters.
      Parameters:
      str - The raw input string.
      Returns:
      The sanitized, path-safe string.