File size: 22,822 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { parse as shellParse } from 'shell-quote';
import {
  extractStringFromParseEntry,
  initializeShellParsers,
  splitCommands,
  stripShellWrapper,
} from '../../utils/shell-utils.js';
import {
  isSubpath,
  isTrustedSystemPath,
  resolveToRealPath,
} from '../../utils/paths.js';

function isRipgrepCommand(cmd: string): boolean {
  const cmdBasename = path.basename(cmd);
  return cmdBasename === 'rg' || cmdBasename === 'rg.exe';
}

function isTrustedCommandPath(cmd: string): boolean {
  if (!path.isAbsolute(cmd)) {
    return false;
  }
  try {
    const realPath = resolveToRealPath(cmd);
    return isTrustedSystemPath(realPath);
  } catch {
    return false;
  }
}

/**
 * Determines if a command is strictly approved for execution on macOS.
 * A command is approved if it's composed entirely of tools explicitly listed in `approvedTools`
 * OR if it's composed of known safe, read-only POSIX commands.
 *
 * @param command - The full command string to execute.
 * @param args - The arguments for the command.
 * @param approvedTools - A list of explicitly approved tool names (e.g., ['npm', 'git']).
 * @returns true if the command is strictly approved, false otherwise.
 */
export async function isStrictlyApproved(
  command: string,
  args: string[],
  approvedTools?: string[],
  cwd?: string,
  workspaceRoot?: string,
): Promise<boolean> {
  const tools = approvedTools ?? [];

  await initializeShellParsers();

  const fullCmd = [command, ...args].join(' ');
  const stripped = stripShellWrapper(fullCmd);

  const pipelineCommands = splitCommands(stripped);

  // Fallback for simple commands or parsing failures
  if (pipelineCommands.length === 0) {
    // For simple commands, we check the root command.
    // If it's explicitly approved OR it's a known safe POSIX command, we allow it.
    return (
      tools.includes(command) ||
      isKnownSafeCommand([command, ...args], cwd, workspaceRoot)
    );
  }

  // Check every segment of the pipeline
  return pipelineCommands.every((cmdString) => {
    const trimmed = cmdString.trim();
    if (!trimmed) return true;

    const parsedArgs = shellParse(trimmed).map(extractStringFromParseEntry);
    if (parsedArgs.length === 0) return true;

    const root = parsedArgs[0];
    // The segment is approved if the root tool is in the allowlist OR if the whole segment is safe.
    return (
      tools.includes(root) || isKnownSafeCommand(parsedArgs, cwd, workspaceRoot)
    );
  });
}

/**
 * Checks whether an argument provided to a file-reading command points outside the workspace
 * or resolves via symbolic link to a target outside the workspace.
 */
function isPathEscapingWorkspace(
  arg: string,
  workspaceRoot: string,
  cwd: string,
): boolean {
  if (!arg || typeof arg !== 'string') return false;

  // Unresolved shell variables or backticks cannot be statically verified
  if (arg.includes('$') || arg.includes('`')) {
    return true;
  }

  let target: string;
  if (arg === '~' || arg.startsWith('~/') || arg.startsWith('~\\')) {
    const homeDir = os.homedir();
    target = path.resolve(homeDir, arg.slice(2));
    if (!isSubpath(workspaceRoot, target)) {
      return true;
    }
  } else if (arg.startsWith('~')) {
    return true;
  } else if (path.isAbsolute(arg)) {
    target = path.resolve(arg);
    if (!isSubpath(workspaceRoot, target)) {
      return true;
    }
  } else {
    target = path.resolve(cwd, arg);
    if (!isSubpath(workspaceRoot, target)) {
      return true;
    }
  }

  // Check if target or any parent directory resolves through a symlink to outside the workspace
  let curr = target;
  while (
    curr &&
    isSubpath(workspaceRoot, curr) &&
    curr !== path.dirname(curr)
  ) {
    try {
      const stat = fs.lstatSync(curr, { throwIfNoEntry: false });
      if (stat?.isSymbolicLink()) {
        const real = fs.realpathSync(curr);
        if (!isSubpath(workspaceRoot, real)) {
          return true;
        }
      }
    } catch {
      // ignore
    }
    curr = path.dirname(curr);
  }

  return false;
}

/**
 * Checks if a command with its arguments is known to be safe to execute
 * without requiring user confirmation. This is primarily used to allow
 * harmless, read-only commands to run silently in the macOS sandbox.
 *
 * It handles raw command execution as well as wrapped commands like `bash -c "..."` or `bash -lc "..."`.
 * For wrapped commands, it parses the script and ensures all individual
 * sub-commands are in the known-safe list and no dangerous shell operators
 * (like subshells or redirection) are used.
 *
 * @param args - The command and its arguments (e.g., ['ls', '-la'])
 * @param cwd - Optional working directory for resolving relative paths
 * @param workspaceRoot - Optional workspace root directory
 * @returns true if the command is considered safe, false otherwise.
 */
export function isKnownSafeCommand(
  args: string[],
  cwd?: string,
  workspaceRoot?: string,
): boolean {
  if (!args || args.length === 0) {
    return false;
  }

  // Normalize zsh to bash
  const normalizedArgs = args.map((a) => (a === 'zsh' ? 'bash' : a));

  if (isSafeToCallWithExec(normalizedArgs, cwd, workspaceRoot)) {
    return true;
  }

  // Support `bash -lc "..."`
  if (
    normalizedArgs.length === 3 &&
    normalizedArgs[0] === 'bash' &&
    (normalizedArgs[1] === '-lc' || normalizedArgs[1] === '-c')
  ) {
    try {
      const script = normalizedArgs[2];

      // Basic check for dangerous operators that could spawn subshells or redirect output
      // We allow &&, ||, |, ; but explicitly block subshells () and redirection >, >>, <
      if (/[()<>]/g.test(script)) {
        return false;
      }

      const commands = splitCommands(script);
      if (commands.length === 0) return false;

      return commands.every((cmd) => {
        const trimmed = cmd.trim();
        if (!trimmed) return true;

        const parsed = shellParse(trimmed).map(extractStringFromParseEntry);
        if (parsed.length === 0) return true;

        return isSafeToCallWithExec(parsed, cwd, workspaceRoot);
      });
    } catch {
      return false;
    }
  }

  return false;
}

/**
 * Core validation logic that checks a single command and its arguments
 * against an allowlist of known safe operations. It performs deep validation
 * for specific tools like `base64`, `find`, `rg`, `git`, and `sed` to ensure
 * unsafe flags (like `--output`, `-exec`, or mutating options) are not used,
 * and ensures all path arguments remain strictly within the workspace.
 *
 * @param args - The command and its arguments.
 * @param cwd - Optional working directory for relative path resolution.
 * @param workspaceRoot - Optional workspace boundary.
 * @returns true if the command is strictly read-only and safe.
 */
function isSafeToCallWithExec(
  args: string[],
  cwd?: string,
  workspaceRoot?: string,
): boolean {
  if (!args || args.length === 0) return false;
  const cmd = args[0];

  let effectiveWorkspace = workspaceRoot
    ? path.resolve(workspaceRoot)
    : cwd
      ? path.resolve(cwd)
      : process.cwd();
  try {
    effectiveWorkspace = resolveToRealPath(effectiveWorkspace);
  } catch {
    // Keep resolved path on failure
  }

  let effectiveCwd = cwd ? path.resolve(cwd) : effectiveWorkspace;
  try {
    effectiveCwd = resolveToRealPath(effectiveCwd);
  } catch {
    // Keep resolved path on failure
  }

  if (
    effectiveCwd !== effectiveWorkspace &&
    !isSubpath(effectiveWorkspace, effectiveCwd)
  ) {
    return false;
  }

  const safeCommands = new Set([
    '__read',
    '__write',
    'cat',
    'cd',
    'cut',
    'echo',
    'expr',
    'false',
    'grep',
    'head',
    'id',
    'ls',
    'nl',
    'paste',
    'pwd',
    'rev',
    'seq',
    'stat',
    'tail',
    'tr',
    'true',
    'uname',
    'uniq',
    'wc',
    'which',
    'whoami',
    'numfmt',
    'tac',
  ]);

  if (safeCommands.has(cmd)) {
    if (cmd === 'cd') {
      let hasPath = false;
      for (let i = 1; i < args.length; i++) {
        const arg = args[i];
        if (arg.startsWith('-')) {
          continue;
        }
        hasPath = true;
        if (isPathEscapingWorkspace(arg, effectiveWorkspace, effectiveCwd)) {
          return false;
        }
      }
      if (!hasPath) {
        if (isPathEscapingWorkspace('~', effectiveWorkspace, effectiveCwd)) {
          return false;
        }
      }
      return true;
    }

    const fileReadingCommands = new Set([
      'cat',
      'head',
      'tail',
      'tac',
      'nl',
      'stat',
      'wc',
      'cut',
      'paste',
      'rev',
      'uniq',
      'numfmt',
      'ls',
      '__read',
    ]);

    if (fileReadingCommands.has(cmd)) {
      let passedDoubleDash = false;
      for (let i = 1; i < args.length; i++) {
        const arg = args[i];
        if (!passedDoubleDash) {
          if (arg === '--') {
            passedDoubleDash = true;
            continue;
          }
          if (arg === '-') {
            continue;
          }
          if (arg.startsWith('-')) {
            if (arg.includes('=')) {
              const val = arg.slice(arg.indexOf('=') + 1);
              if (
                isPathEscapingWorkspace(val, effectiveWorkspace, effectiveCwd)
              ) {
                return false;
              }
              continue;
            }

            try {
              const stat = fs.lstatSync(path.resolve(effectiveCwd, arg), {
                throwIfNoEntry: false,
              });
              if (!stat) {
                continue;
              }
            } catch {
              continue;
            }
          }
        }
        if (isPathEscapingWorkspace(arg, effectiveWorkspace, effectiveCwd)) {
          return false;
        }
      }
      return true;
    }

    if (cmd === 'grep') {
      let passedDoubleDash = false;
      for (let i = 1; i < args.length; i++) {
        const arg = args[i];
        if (!passedDoubleDash) {
          if (arg === '--') {
            passedDoubleDash = true;
            continue;
          }
          if (arg === '-') {
            continue;
          }
          if (arg.startsWith('-f') || arg.startsWith('--file')) {
            let fileArg: string | undefined;
            if (arg.startsWith('--file=')) {
              fileArg = arg.slice(7);
            } else if (arg === '--file') {
              fileArg = args[++i];
            } else {
              fileArg = arg.length > 2 ? arg.slice(2) : args[++i];
            }
            if (
              fileArg &&
              isPathEscapingWorkspace(fileArg, effectiveWorkspace, effectiveCwd)
            ) {
              return false;
            }
            continue;
          }
          if (arg.startsWith('-')) {
            if (arg.includes('=')) {
              const val = arg.slice(arg.indexOf('=') + 1);
              if (
                isPathEscapingWorkspace(val, effectiveWorkspace, effectiveCwd)
              ) {
                return false;
              }
              continue;
            }

            try {
              const stat = fs.lstatSync(path.resolve(effectiveCwd, arg), {
                throwIfNoEntry: false,
              });
              if (!stat) {
                continue;
              }
            } catch {
              continue;
            }
          }
        }
        if (isPathEscapingWorkspace(arg, effectiveWorkspace, effectiveCwd)) {
          return false;
        }
      }
      return true;
    }

    return true;
  }

  if (cmd === 'base64') {
    const unsafeOptions = new Set(['-o', '--output']);
    if (
      args
        .slice(1)
        .some(
          (arg) =>
            unsafeOptions.has(arg) ||
            arg.startsWith('--output=') ||
            (arg.startsWith('-o') && arg !== '-o'),
        )
    ) {
      return false;
    }
    for (let i = 1; i < args.length; i++) {
      const arg = args[i];
      if (arg.startsWith('-')) continue;
      if (isPathEscapingWorkspace(arg, effectiveWorkspace, effectiveCwd)) {
        return false;
      }
    }
    return true;
  }

  if (cmd === 'find') {
    const unsafeOptions = new Set([
      '-exec',
      '-execdir',
      '-ok',
      '-okdir',
      '-delete',
      '-fls',
      '-fprint',
      '-fprint0',
      '-fprintf',
    ]);
    if (args.some((arg) => unsafeOptions.has(arg))) {
      return false;
    }
    for (let i = 1; i < args.length; i++) {
      const arg = args[i];
      if (arg.startsWith('-')) continue;
      if (isPathEscapingWorkspace(arg, effectiveWorkspace, effectiveCwd)) {
        return false;
      }
    }
    return true;
  }

  if (isRipgrepCommand(cmd)) {
    if (!isTrustedCommandPath(cmd)) return false;

    const unsafeWithArgs = new Set(['--pre', '--hostname-bin']);
    const unsafeWithoutArgs = new Set(['--search-zip', '-z']);

    for (let i = 1; i < args.length; i++) {
      const arg = args[i];
      if (unsafeWithoutArgs.has(arg)) return false;
      for (const opt of unsafeWithArgs) {
        if (arg === opt || arg.startsWith(opt + '=')) return false;
      }
    }

    let passedDoubleDash = false;
    for (let i = 1; i < args.length; i++) {
      const arg = args[i];
      if (!passedDoubleDash) {
        if (arg === '--') {
          passedDoubleDash = true;
          continue;
        }
        if (arg.startsWith('-f') || arg.startsWith('--file')) {
          let fileArg: string | undefined;
          if (arg.startsWith('--file=')) {
            fileArg = arg.slice(7);
          } else if (arg === '--file') {
            fileArg = args[++i];
          } else {
            fileArg = arg.length > 2 ? arg.slice(2) : args[++i];
          }
          if (
            fileArg &&
            isPathEscapingWorkspace(fileArg, effectiveWorkspace, effectiveCwd)
          ) {
            return false;
          }
          continue;
        }
        if (arg.startsWith('-')) {
          if (arg.includes('=')) {
            const val = arg.slice(arg.indexOf('=') + 1);
            if (
              isPathEscapingWorkspace(val, effectiveWorkspace, effectiveCwd)
            ) {
              return false;
            }
            continue;
          }

          try {
            const stat = fs.lstatSync(path.resolve(effectiveCwd, arg), {
              throwIfNoEntry: false,
            });
            if (!stat) {
              continue;
            }
          } catch {
            continue;
          }
        }
      }
      if (isPathEscapingWorkspace(arg, effectiveWorkspace, effectiveCwd)) {
        return false;
      }
    }

    return true;
  }

  if (cmd === 'git') {
    if (gitHasConfigOverrideGlobalOption(args)) {
      return false;
    }

    const { idx, subcommand } = findGitSubcommand(args, [
      'status',
      'log',
      'diff',
      'show',
      'branch',
    ]);
    if (!subcommand) {
      return false;
    }

    const subcommandArgs = args.slice(idx + 1);

    if (['status', 'log', 'diff', 'show'].includes(subcommand)) {
      return gitSubcommandArgsAreReadOnly(subcommandArgs);
    }

    if (subcommand === 'branch') {
      return (
        gitSubcommandArgsAreReadOnly(subcommandArgs) &&
        gitBranchIsReadOnly(subcommandArgs)
      );
    }

    return false;
  }

  if (cmd === 'sed') {
    // Special-case sed -n {N|M,N}p
    if (args.length <= 4 && args[1] === '-n' && isValidSedNArg(args[2])) {
      if (args[3]) {
        if (
          isPathEscapingWorkspace(args[3], effectiveWorkspace, effectiveCwd)
        ) {
          return false;
        }
      }
      return true;
    }
    return false;
  }

  return false;
}

/**
 * Helper to identify which git subcommand is being executed, skipping over
 * global git options like `-c` or `--git-dir`.
 *
 * @param args - The full git command arguments.
 * @param subcommands - A list of subcommands to look for.
 * @returns An object containing the index of the subcommand and its name.
 */
function findGitSubcommand(
  args: string[],
  subcommands: string[],
): { idx: number; subcommand: string | null } {
  let skipNext = false;

  for (let idx = 1; idx < args.length; idx++) {
    if (skipNext) {
      skipNext = false;
      continue;
    }

    const arg = args[idx];

    if (
      arg.startsWith('--config-env=') ||
      arg.startsWith('--exec-path=') ||
      arg.startsWith('--git-dir=') ||
      arg.startsWith('--namespace=') ||
      arg.startsWith('--super-prefix=') ||
      arg.startsWith('--work-tree=') ||
      ((arg.startsWith('-C') || arg.startsWith('-c')) && arg.length > 2)
    ) {
      continue;
    }

    if (
      arg === '-C' ||
      arg === '-c' ||
      arg === '--config-env' ||
      arg === '--exec-path' ||
      arg === '--git-dir' ||
      arg === '--namespace' ||
      arg === '--super-prefix' ||
      arg === '--work-tree'
    ) {
      skipNext = true;
      continue;
    }

    if (arg === '--' || arg.startsWith('-')) {
      continue;
    }

    if (subcommands.includes(arg)) {
      return { idx, subcommand: arg };
    }

    return { idx: -1, subcommand: null };
  }

  return { idx: -1, subcommand: null };
}

/**
 * Checks if a git command contains global configuration override flags
 * (e.g., `-c` or `--config-env`) which could be used maliciously to
 * execute arbitrary code via git config.
 *
 * @param args - The git command arguments.
 * @returns true if config overrides are present.
 */
function gitHasConfigOverrideGlobalOption(args: string[]): boolean {
  return args.some(
    (arg) =>
      arg === '-c' ||
      arg === '--config-env' ||
      (arg.startsWith('-c') && arg.length > 2) ||
      arg.startsWith('--config-env='),
  );
}

/**
 * Validates that the arguments for safe git subcommands (like `status`, `log`,
 * `diff`, `show`) do not contain flags that could cause mutations or execute
 * arbitrary commands (e.g., `--output`, `--exec`).
 *
 * @param args - Arguments passed to the git subcommand.
 * @returns true if the arguments only represent read-only operations.
 */
function gitSubcommandArgsAreReadOnly(args: string[]): boolean {
  const unsafeFlags = new Set([
    '--output',
    '--ext-diff',
    '--textconv',
    '--exec',
    '--paginate',
  ]);

  return !args.some(
    (arg) =>
      unsafeFlags.has(arg) ||
      arg.startsWith('--output=') ||
      arg.startsWith('--exec='),
  );
}

/**
 * Validates that `git branch` is only used for read operations
 * (e.g., listing branches) rather than creating, deleting, or renaming branches.
 *
 * @param args - Arguments passed to `git branch`.
 * @returns true if it's purely a listing/read-only branch command.
 */
function gitBranchIsReadOnly(args: string[]): boolean {
  if (args.length === 0) return true;

  let sawReadOnlyFlag = false;
  for (const arg of args) {
    if (
      [
        '--list',
        '-l',
        '--show-current',
        '-a',
        '--all',
        '-r',
        '--remotes',
        '-v',
        '-vv',
        '--verbose',
      ].includes(arg)
    ) {
      sawReadOnlyFlag = true;
    } else if (arg.startsWith('--format=')) {
      sawReadOnlyFlag = true;
    } else {
      return false;
    }
  }
  return sawReadOnlyFlag;
}

/**
 * Ensures that a `sed` command argument is a valid line-printing instruction
 * (e.g., `10p` or `5,10p`), preventing unsafe script execution in `sed`.
 *
 * @param arg - The script argument passed to `sed -n`.
 * @returns true if it's a valid, safe print command.
 */
function isValidSedNArg(arg: string | undefined): boolean {
  if (!arg) return false;

  if (!arg.endsWith('p')) return false;
  const core = arg.slice(0, -1);

  const parts = core.split(',');
  if (parts.length === 1) {
    const num = parts[0];
    return num.length > 0 && /^\d+$/.test(num);
  } else if (parts.length === 2) {
    const a = parts[0];
    const b = parts[1];
    return a.length > 0 && b.length > 0 && /^\d+$/.test(a) && /^\d+$/.test(b);
  }

  return false;
}

/**
 * Checks if a command with its arguments is explicitly known to be dangerous
 * and should be blocked or require strict user confirmation. This catches
 * destructive commands like `rm -rf`, `sudo`, and commands with execution
 * flags like `find -exec`.
 *
 * @param args - The command and its arguments.
 * @returns true if the command is identified as dangerous, false otherwise.
 */
export function isDangerousCommand(
  args: string[],
  _cwd?: string,
  _workspaceRoot?: string,
): boolean {
  if (!args || args.length === 0) {
    return false;
  }

  const cmd = args[0];

  if (cmd === 'ln') {
    const isSymbolic = args.some(
      (arg) =>
        arg === '-s' ||
        arg === '--symbolic' ||
        (arg.startsWith('-') && !arg.startsWith('--') && arg.includes('s')),
    );
    if (isSymbolic) {
      return true;
    }
  }

  if (cmd === 'rm') {
    return args[1] === '-f' || args[1] === '-rf' || args[1] === '-fr';
  }

  if (cmd === 'sudo') {
    return isDangerousCommand(args.slice(1));
  }

  if (cmd === 'find') {
    const unsafeOptions = new Set([
      '-exec',
      '-execdir',
      '-ok',
      '-okdir',
      '-delete',
      '-fls',
      '-fprint',
      '-fprint0',
      '-fprintf',
    ]);
    return args.some((arg) => unsafeOptions.has(arg));
  }

  if (isRipgrepCommand(cmd)) {
    const unsafeWithArgs = new Set(['--pre', '--hostname-bin']);
    const unsafeWithoutArgs = new Set(['--search-zip', '-z']);

    return args.some((arg) => {
      if (unsafeWithoutArgs.has(arg)) return true;
      for (const opt of unsafeWithArgs) {
        if (arg === opt || arg.startsWith(opt + '=')) return true;
      }
      return false;
    });
  }

  if (cmd === 'git') {
    if (gitHasConfigOverrideGlobalOption(args)) {
      return true;
    }

    const { idx, subcommand } = findGitSubcommand(args, [
      'status',
      'log',
      'diff',
      'show',
      'branch',
    ]);
    if (!subcommand) {
      // It's a git command we don't recognize as explicitly safe.
      return false;
    }

    const subcommandArgs = args.slice(idx + 1);

    if (['status', 'log', 'diff', 'show'].includes(subcommand)) {
      return !gitSubcommandArgsAreReadOnly(subcommandArgs);
    }

    if (subcommand === 'branch') {
      return !(
        gitSubcommandArgsAreReadOnly(subcommandArgs) &&
        gitBranchIsReadOnly(subcommandArgs)
      );
    }

    return false;
  }

  if (cmd === 'base64') {
    const unsafeOptions = new Set(['-o', '--output']);
    return args
      .slice(1)
      .some(
        (arg) =>
          unsafeOptions.has(arg) ||
          arg.startsWith('--output=') ||
          (arg.startsWith('-o') && arg !== '-o'),
      );
  }

  return false;
}