Class PathValidator
- Namespace
- SharpConsoleUI.Helpers
- Assembly
- SharpConsoleUI.dll
Centralised path validation for preventing path traversal, symlink following, and access to sensitive system directories. All file-access paths in the framework that accept external input should validate through this class.
public static class PathValidator
- Inheritance
-
PathValidator
- Inherited Members
Methods
IsPathWithinDirectory(string, string)
Returns true if filePath is contained within
allowedDirectory after full path canonicalization.
Uses case-insensitive comparison for cross-platform correctness.
public static bool IsPathWithinDirectory(string filePath, string allowedDirectory)
Parameters
filePathstringThe file path to validate.
allowedDirectorystringThe directory that must contain the file.
Returns
- bool
True if the file is within the allowed directory.
IsSensitiveSystemPath(string)
Returns true if the path targets a sensitive system directory that should not be written to by application code. Checks common Unix system paths; on Windows, returns false (system paths are ACL-protected).
public static bool IsSensitiveSystemPath(string path)
Parameters
pathstringThe file or directory path to check.
Returns
- bool
True if the path is within a sensitive system directory.
IsSymlink(string)
Returns true if the given path points to a symlink (reparse point).
public static bool IsSymlink(string path)
Parameters
pathstringThe file or directory path to check.
Returns
- bool
True if the path is a symbolic link or junction.
ValidatePath(string, string, out string)
Validates that a file path is safe to access: it must be within the allowed directory, must not be a symlink, and must not target a sensitive system path.
public static bool ValidatePath(string filePath, string allowedDirectory, out string reason)
Parameters
filePathstringThe file path to validate.
allowedDirectorystringThe directory that must contain the file.
reasonstringIf validation fails, the reason why.
Returns
- bool
True if the path passes all validation checks.