checkPathSafety function

({bool isDangerous, String? reason}) checkPathSafety(
  1. String command
)

Check if a command attempts dangerous path operations.

Implementation

({bool isDangerous, String? reason}) checkPathSafety(String command) {
  // Check rm/rmdir commands
  final rmMatch = RegExp(
    r'\b(rm|rmdir)\s+((-[rfvd]+\s+)*)(.*)',
  ).firstMatch(command);

  if (rmMatch != null) {
    final path = rmMatch.group(4)?.trim() ?? '';

    // Check system paths
    for (final dangerous in _dangerousRemovalPaths) {
      if (path == dangerous || path.startsWith('$dangerous/')) {
        return (
          isDangerous: true,
          reason: 'Cannot remove system path: $dangerous',
        );
      }
    }

    // Check framework paths
    for (final framework in _protectedFrameworkPaths) {
      if (path == framework || path.endsWith('/$framework')) {
        return (
          isDangerous: true,
          reason: 'Removing framework directory: $framework',
        );
      }
    }
  }

  return (isDangerous: false, reason: null);
}