@@ -25,9 +25,6 @@ type PathValidationOptions struct {
2525 Existence ExistenceType
2626}
2727
28- // Common dangerous characters that could be used for injection attacks
29- var dangerousChars = []string {";" , "&" , "|" , "`" , "$" , "\" " , "'" , "\n " , "\r " , "\t " , "*" }
30-
3128// ValidatePath validates any path for security with customizable requirements
3229func ValidatePath (path types.FilePath , options PathValidationOptions ) error {
3330 pathStr := strings .TrimSpace (string (path ))
@@ -38,12 +35,12 @@ func ValidatePath(path types.FilePath, options PathValidationOptions) error {
3835 return fmt .Errorf ("path cannot be empty, got: '%s'" , string (path ))
3936 }
4037
41- // 1. Check for dangerous characters
42- if err := validateDangerousCharacters (pathStr ); err != nil {
38+ // Validate Windows UNC admin share paths
39+ if err := validateWindowsUNCAdminShare (pathStr ); err != nil {
4340 return err
4441 }
4542
46- // 2. Validate path existence based on requirements
43+ // Validate path existence based on requirements
4744 if err := validatePathExistence (pathStr , options .Existence ); err != nil {
4845 return err
4946 }
@@ -78,8 +75,7 @@ func ValidatePathStrict(path types.FilePath) error {
7875// ValidatePathForStorage validates a path for storage purposes without requiring the path to exist.
7976// This function is used when storing paths where the path may not exist yet
8077// (e.g., user-configured paths for future use, paths during data migration, or storage keys).
81- // It performs security validation (dangerous characters, path traversal) but allows empty paths
82- // and doesn't check if the path actually exists on the filesystem.
78+ // It allows empty paths and doesn't check if the path actually exists on the filesystem.
8379func ValidatePathForStorage (path types.FilePath ) error {
8480 options := PathValidationOptions {
8581 AllowEmpty : true ,
@@ -103,30 +99,27 @@ func PathKey(p types.FilePath) types.FilePath {
10399 return ""
104100 }
105101
106- if err := validateDangerousCharacters (s ); err != nil {
107- return ""
108- }
109-
110102 // Normalize the path using filepath.Clean()
111103 s = filepath .Clean (s )
112104
113105 return types .FilePath (s )
114106}
115107
116- // validateDangerousCharacters checks for dangerous characters in a string
117- func validateDangerousCharacters ( input string ) error {
118- for _ , char := range dangerousChars {
119- if ! strings . Contains ( input , char ) {
120- continue
121- }
122-
123- // Special case: $ is allowed in Windows UNC administrative share paths (e.g., \\server\C$\path)
124- if char == "$" && isWindowsUNCAdminShare ( input ) {
125- continue
108+ // validateWindowsUNCAdminShare validates Windows UNC admin share paths
109+ // These paths have the format \\server\C$\... where C$ is the administrative share
110+ func validateWindowsUNCAdminShare ( path string ) error {
111+ // Check if path looks like a UNC path (starts with \\ or //)
112+ if strings . HasPrefix ( path , " \\ \\ " ) || strings . HasPrefix ( path , "//" ) {
113+ // If it's a UNC path, verify it's a valid admin share format
114+ if ! isWindowsUNCAdminShare ( path ) {
115+ // If it looks like UNC but isn't a valid admin share, that's okay
116+ // We just want to ensure admin shares are properly recognized
117+ return nil
126118 }
127-
128- return fmt . Errorf ( "dangerous character detected in '%s': %s" , input , char )
119+ // Valid admin share path - no error
120+ return nil
129121 }
122+ // Not a UNC path - no validation needed
130123 return nil
131124}
132125
0 commit comments