T09 · Insecure Skill Coding Practices
- Location
scripts/search.py:49- Finding
Automatic cleanup can delete unrelated CSV files from an arbitrary output directory
- Content
View full analysis
int: """ Delete CSV files older than specified age. Returns the number of files deleted. """ if not output_dir.exists(): return 0 deleted_count = 0 cutoff_time = datetime.now() - timedelta(hours=max_age_hours) for file in output_dir.glob("*.csv"): try: mtime = datetime.fromtimestamp(file.stat().st_mtime) if mtime < cutoff_time: file.unlink() deleted_count += 1 except Exception: # Skip files that can't be accessed pass return deleted_count def cleanup_excess_files(output_dir: Path, max_files: int = 100) -> int: """ Delete oldest CSV files when file count exceeds limit. Returns the number of files deleted. """ if not output_dir.exists(): return 0 # Get all CSV files with their modification times files = [] for file in output_dir.glob("*.csv"): try: files.append((file, file.stat().st_mtime)) except Exception: pass # Sort by modification time (newest first) files.sort(key=lambda x: x[1], reverse=True) # Delete files exceeding the limit deleted_count = 0 if len(files) > max_files: for file, _ in files[max_files:]: try: file.unlink() deleted_count += 1 except Exception: # Continue on error pass return deleted_count ``` The cleanup is enabled automatically when saving results: ```python # Create output directory if it doesn't exist output_dir.mkdir(parents=True, exist_ok=True) ...[truncated 3010 chars]- Remediation
View remediation
