@@ -486,9 +486,34 @@ def save_ehsa_visualization_tool(file_path, ehsa_results_df=None, include_data=T
486486 ... include_data=False
487487 ... )
488488 """
489+ # Validate and sanitize file_path to prevent path traversal attacks
490+ if not file_path :
491+ raise ValueError ("file_path cannot be empty" )
492+
493+ # Convert to string and check for path traversal attempts
494+ file_path_str = str (file_path )
495+
496+ # Block absolute paths to sensitive system directories
497+ sensitive_paths = ['/etc' , '/var' , '/usr' , '/bin' , '/sbin' , '/boot' , '/sys' , '/proc' ]
498+ if any (file_path_str .startswith (p ) for p in sensitive_paths ):
499+ raise ValueError (f"Cannot write to system directory: { file_path_str } " )
500+
501+ # Detect path traversal attempts
502+ if '..' in file_path_str :
503+ raise ValueError (f"Path traversal detected in file_path: { file_path_str } " )
504+
505+ # Ensure file has .html extension for safety
506+ if not file_path_str .endswith ('.html' ):
507+ raise ValueError (f"File must have .html extension, got: { file_path_str } " )
508+
489509 # Convert file_path to Path object and get absolute path
490510 save_path = Path (file_path ).resolve ()
491511
512+ # Final safety check: ensure resolved path doesn't escape to system directories
513+ save_path_str = str (save_path )
514+ if any (save_path_str .startswith (p ) for p in sensitive_paths ):
515+ raise ValueError (f"Resolved path points to system directory: { save_path_str } " )
516+
492517 # Ensure the directory exists
493518 save_path .parent .mkdir (parents = True , exist_ok = True )
494519
0 commit comments