@@ -380,24 +380,26 @@ def extract_unique_errors(pytest_output: str) -> set[str]:
380380
381381def validate_relative_directory_path (path : str ) -> tuple [bool , str ]:
382382 """Validate that a path is a safe relative directory path.
383-
383+
384384 Prevents path traversal attacks and invalid paths.
385385 Works cross-platform (Windows, Linux, macOS).
386-
386+
387387 Args:
388388 path: The path string to validate
389-
389+
390390 Returns:
391391 tuple[bool, str]: (is_valid, error_message)
392392 - is_valid: True if path is valid, False otherwise
393393 - error_message: Empty string if valid, error description if invalid
394+
394395 """
396+ # Check for empty path
395397 if not path or not path .strip ():
396398 return False , "Path cannot be empty"
397-
399+
398400 # Normalize whitespace
399401 path = path .strip ()
400-
402+
401403 # Check for shell commands or dangerous patterns
402404 dangerous_patterns = [
403405 "cd " ,
@@ -423,36 +425,26 @@ def validate_relative_directory_path(path: str) -> tuple[bool, str]:
423425 for pattern in dangerous_patterns :
424426 if pattern in path_lower :
425427 return False , f"Path contains invalid characters or commands: { pattern .strip ()} "
426-
428+
427429 # Check for path traversal attempts (cross-platform)
428- # Normalize path separators for checking
429430 normalized = path .replace ("\\ " , "/" )
430431 if ".." in normalized :
431432 return False , "Path cannot contain '..' (parent directory traversal)"
432-
433- # Check for absolute paths (Windows and Unix)
434- if os .path .isabs (path ):
435- return False , "Path must be relative, not absolute"
436-
437- # Check for invalid characters (OS-specific)
438- invalid_chars = set ()
439- if os .name == "nt" : # Windows
440- invalid_chars = {'<' , '>' , ':' , '"' , '|' , '?' , '*' }
441- else : # Unix-like
442- invalid_chars = {'\0 ' }
443-
444- if any (char in path for char in invalid_chars ):
445- return False , f"Path contains invalid characters for this operating system"
446-
447- # Validate using pathlib to ensure it's a valid path structure
448- try :
449- path_obj = Path (path )
450- # Check if path would resolve outside the current directory
451- # This is a safety check for edge cases
452- parts = path_obj .parts
453- if any (part == ".." for part in parts ):
454- return False , "Path cannot contain '..' (parent directory traversal)"
455- except (ValueError , OSError ) as e :
456- return False , f"Invalid path format: { str (e )} "
457-
458- return True , ""
433+
434+ # Check for absolute paths and invalid characters
435+ invalid_chars = {"<" , ">" , ":" , '"' , "|" , "?" , "*" } if os .name == "nt" else {"\0 " }
436+ error_msg = ""
437+ if Path (path ).is_absolute ():
438+ error_msg = "Path must be relative, not absolute"
439+ elif any (char in path for char in invalid_chars ):
440+ error_msg = "Path contains invalid characters for this operating system"
441+ else :
442+ # Validate using pathlib to ensure it's a valid path structure
443+ try :
444+ Path (path )
445+ except (ValueError , OSError ) as e :
446+ error_msg = f"Invalid path format: { e !s} "
447+
448+ if error_msg :
449+ return False , error_msg
450+ return True , ""
0 commit comments