shellfirm 0.4.2

`shellfirm` will intercept any risky patterns (default or defined by you) and prompt you a small challenge for double verification, kinda like a captcha for your terminal.
Documentation
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
- from: fs
  # Operands are normalized by the parser first (`//` → `/`, `/home/..` → `/`,
  # `~/` → `~`, `${HOME}` → `$HOME`, `./*` → `*`, trailing `/` dropped).
  test: '^rm\s+(?:\S+\s+)*?(?:-[a-zA-Z]*[rRf][a-zA-Z]*|--recursive|--force)\s+(?:\S+\s+)*?(/\*?|~(?:/\*)?|\$HOME(?:/\*)?|\.\.?(?:/\.\.)*(?:/\*)?|\*|\.git(?:/(?:\*|objects|refs|logs|index|HEAD)(?:/\S*)?)?|/(?:home|Users)/[^/\s*]+(?:/\*)?|/System/Volumes/Data(?:/[^\s]*)?|/(?:home|etc|usr|var|bin|sbin|lib|lib64|boot|opt|root|srv|sys|proc|dev|System|Users|Library|Applications)(?:/\*)?)(?:\s|$)'
  description: "This deletes a critical location: the filesystem root, your home directory, a system directory, the repository, or everything in the current directory."
  id: fs:recursively_delete
  severity: Critical
  filters:
    - type: PathExists
      value: 1
  alternative: "trash <path>"
  alternative_info: "Moves to trash instead of permanent deletion. Install: brew install trash (macOS) or trash-cli (Linux)."
- from: fs
  # Credentials and keys in the home directory: losing them locks you out
  # (SSH, GPG, cloud and cluster credentials, saved git credentials). The
  # names are matched in any case: macOS and Windows file systems ignore it.
  test: '^(?:rm|unlink|shred|srm)\s+(?:\S+\s+)*?((?:~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)/\.(?i:ssh|gnupg|aws|kube|azure|docker|password-store|config/gcloud|git-credentials)(?:/\S*)?)(?:\s|$)'
  description: "This deletes SSH keys, GPG keys, git or cloud / cluster credentials from your home directory."
  id: fs:delete_credentials
  severity: Critical
  filters:
    - type: PathExists
      value: 1
  alternative: "trash <path>"
  alternative_info: "Moves to trash instead of permanent deletion, so the keys can be restored."
- from: fs
  # macOS: erasing, zeroing or repartitioning a disk or volume.
  test: '^diskutil\s+(?:\S+\s+)*?(?:eraseDisk|eraseVolume|zeroDisk|randomDisk|secureErase|partitionDisk|reformat|apfs\s+(?:deleteContainer|deleteVolume|eraseVolume|eraseDisk))(?:\s|$)'
  description: "This erases a disk or volume with diskutil: all data on it is lost."
  id: fs:diskutil_erase
  severity: Critical
  filters:
    # `diskutil secureErase freespace <mount>` erases only the free space, not
    # the files on the volume.
    - type: NotMatches
      value: '(?:^|\s)secureErase\s+freespace(?:\s|$)'
- from: fs
  # Output redirected straight onto a block device (`cat /dev/urandom > /dev/sda`),
  # or `tee` / `cp` writing onto one.
  test: '(?:(?:^|\s)\d*>>?\|?\s*|(?:^|\s)tee\s+(?:-\S+\s+)*|(?:^|\s)cp\s+(?:-\S+\s+)*\S+\s+)(/dev/(?:[hsv]d[a-z]+[0-9]*|r?disk[0-9]+(?:s[0-9]+)*|xvd[a-z]+[0-9]*|mmcblk[0-9]+(?:p[0-9]+)?|nvme[0-9]+n[0-9]+(?:p[0-9]+)?|md[0-9]+(?:p[0-9]+)?|dm-[0-9]+|mapper/[^\s/]+|disk/by-[a-z-]+/[^\s/]+))(?:\s|$)'
  description: "This writes straight onto a disk device, overwriting the partition table and file systems on it."
  id: fs:write_to_block_device
  severity: Critical
- from: fs
  # `nvme format` / `nvme sanitize` erase an NVMe device at the controller.
  test: '(?:^|\s)nvme\s+(?:format|sanitize)\s+(?:\S+\s+)*?/dev/(?:nvme[0-9]+(?:n[0-9]+)?(?:p[0-9]+)?|ng[0-9]+n[0-9]+)(?:\s|$)'
  description: "This erases or sanitizes an NVMe device at the controller level: all data on it is lost."
  id: fs:nvme_format
  severity: Critical
- from: fs
  # Windows `cmd`: `rd` / `rmdir` / `del` / `erase` with `/s` (whole tree; also
  # glued, `/s/q`) on a drive root, the profiles folder, one profile or one of
  # its top folders (Documents, Desktop, AppData, .ssh, ...), the Windows
  # directory, or a `%VAR%` naming one of them. The parser adds a `/`-separated
  # reading of lines with Windows paths, so `C:\Users\dev` is seen as `C:/Users/dev`.
  test: '(?i)(?:^|\s)(?:rmdir|rd|del|erase)(?:(?:\s+\S+)*?\s+)?(?:/[a-z])*/s(?:/[a-z])*(?:\s+\S+)*?\s+((?:[a-z]:/?|[a-z]:/Users(?:/[^/\s*]+(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?)?/?|[a-z]:/Windows(?:/System32)?/?|%(?:USERPROFILE|HOMEDRIVE|HOMEPATH|SystemDrive|SystemRoot|windir|APPDATA|LOCALAPPDATA|ProgramData)%(?:%HOMEPATH%)?(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?/?)(?:\*(?:\.\*)?)?)(?:\s|$)|(?:^|\s)(?:rmdir|rd|del|erase)\s+(?:\S+\s+)*?((?:[a-z]:/?|[a-z]:/Users(?:/[^/\s*]+(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?)?/?|[a-z]:/Windows(?:/System32)?/?|%(?:USERPROFILE|HOMEDRIVE|HOMEPATH|SystemDrive|SystemRoot|windir|APPDATA|LOCALAPPDATA|ProgramData)%(?:%HOMEPATH%)?(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?/?)(?:\*(?:\.\*)?)?)\s+(?:\S+\s+)*?(?:/[a-z])*/s(?:/[a-z])*(?:\s|$)'
  description: "This deletes a whole Windows drive, the user profiles or the Windows directory, with every file and folder in it."
  id: fs:windows_recursive_delete
  severity: Critical
- from: fs
  # PowerShell: `Remove-Item -Recurse` (`-r`, `-rec`, `-Recurse:$true`; `ri`,
  # `rm`, `rd`, `rmdir`, `del`, `erase` are aliases) on the same targets,
  # `$env:USERPROFILE` and friends (also `${env:…}`, `-Path:<target>`). `~`
  # only for `Remove-Item` / `ri` (`rm -r ~` is fs:recursively_delete).
  test: '(?i)(?:^|\s)(?:Remove-Item|ri|rm|rmdir|rd|del|erase)\s+(?:\S+\s+)*?-r[a-z]*(?::\$true)?(?:\s+\S+)*?\s+(?:-(?:Path|LiteralPath):)?((?:[a-z]:/?|[a-z]:/Users(?:/[^/\s*]+(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?)?/?|[a-z]:/Windows(?:/System32)?/?|\$\{?env:(?:USERPROFILE|HOMEDRIVE|HOMEPATH|SystemDrive|SystemRoot|windir|APPDATA|LOCALAPPDATA|ProgramData)\}?(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?/?)(?:\*(?:\.\*)?)?)(?:\s|$)|(?:^|\s)(?:Remove-Item|ri|rm|rmdir|rd|del|erase)\s+(?:\S+\s+)*?(?:-(?:Path|LiteralPath):)?((?:[a-z]:/?|[a-z]:/Users(?:/[^/\s*]+(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?)?/?|[a-z]:/Windows(?:/System32)?/?|\$\{?env:(?:USERPROFILE|HOMEDRIVE|HOMEPATH|SystemDrive|SystemRoot|windir|APPDATA|LOCALAPPDATA|ProgramData)\}?(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?/?)(?:\*(?:\.\*)?)?)\s+(?:\S+\s+)*?-r[a-z]*(?::\$true)?(?:\s|$)|(?:^|\s)(?:Remove-Item|ri)\s+(?:\S+\s+)*?-r[a-z]*(?::\$true)?(?:\s+\S+)*?\s+(?:-(?:Path|LiteralPath):)?((?:~|\$HOME|\$\{HOME\})(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?/?(?:\*(?:\.\*)?)?)(?:\s|$)|(?:^|\s)(?:Remove-Item|ri)\s+(?:\S+\s+)*?(?:-(?:Path|LiteralPath):)?((?:~|\$HOME|\$\{HOME\})(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?/?(?:\*(?:\.\*)?)?)\s+(?:\S+\s+)*?-r[a-z]*(?::\$true)?(?:\s|$)'
  description: "This deletes a whole Windows drive, your user profile or the Windows directory, with every file and folder in it."
  id: fs:windows_remove_item_recursive
  severity: Critical
- from: fs
  # PowerShell: everything a listing finds, piped into a delete
  # (`Get-ChildItem C:\ -Recurse | Remove-Item -Force`, `ls ~ | rm -Recurse`).
  test: '(?i)(?:^|\s)(?:Get-ChildItem|gci|dir|ls)[ \t]+(?:[^\s|;&]+[ \t]+)*?(?:-(?:Path|LiteralPath):)?((?:[a-z]:/?|[a-z]:/Users(?:/[^/\s*]+(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?)?/?|[a-z]:/Windows(?:/System32)?/?|\$\{?env:(?:USERPROFILE|HOMEDRIVE|HOMEPATH|SystemDrive|SystemRoot|windir|APPDATA|LOCALAPPDATA|ProgramData)\}?(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?/?)(?:\*(?:\.\*)?)?|(?:~|\$HOME|\$\{HOME\})(?:/(?:Documents|Desktop|Downloads|Pictures|Videos|Music|AppData|OneDrive[^/\s]*|\.ssh))?/?(?:\*(?:\.\*)?)?)(?:[ \t]+[^\s|;&]+)*?[ \t]*\|[ \t]*(?:Remove-Item|ri|rm|rmdir|rd|del|erase)(?:[ \t]+-\S+(?:[ \t]+(?:0|[1-9]|SilentlyContinue|Ignore|Continue|Stop|\$true|\$false))?)*[ \t]*(?:$|[|;\n])'
  description: "This lists a whole Windows drive or your user profile and pipes every item into a delete."
  id: fs:windows_pipeline_delete
  severity: Critical
- from: fs
  # Windows `format D:` (switches before or after the drive) and the
  # PowerShell storage cmdlets that erase a volume or a disk.
  test: '(?i)(?:^|\s)format(?:\.com)?\s+(?:/\S+\s+)*[a-z]:(?:\s|$)|^(?:Format-Volume|Clear-Disk|Initialize-Disk|Remove-Partition|Reset-PhysicalDisk)(?:\s|$)|(?:^|\s)diskpart(?:\.exe)?\s+(?:\S+\s+)*?/s(?:\s|$)'
  description: "This formats a Windows drive or wipes a disk, erasing all data on it."
  id: fs:windows_format_drive
  severity: Critical
- from: fs
  # A direct child of the home directory (`~/projects`, `~/data`) or a
  # directory outside the current one (`../webapp`).
  test: '^rm\s+(?:\S+\s+)*?(?:-[a-zA-Z]*[rR][a-zA-Z]*|--recursive)\s+(?:\S+\s+)*?((?:~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)/[^/\s*]+|(?:\.\./)+[^/\s.][^\s]*|(?:\.\./)+\.[^/\s.][^\s]*)(?:\s|$)'
  description: "This recursively deletes a top-level folder of your home directory (for example `~/projects` or `~/data`), or a directory outside the current one reached with `../`."
  id: fs:recursively_delete_outside
  severity: High
  filters:
    - type: PathExists
      value: 1
    # Package-manager caches only (`rm -rf /root/.cache` in a Dockerfile).
    - type: NotMatches
      value: '^rm\s+(?:-\S+\s+)*(?:(?:(?:>|2>|&>)\s*/dev/null|2>&\s*1|1>&\s*2)\s+)*(?:(?:/var/lib/apt/lists|/var/cache/(?:apt|apk|yum|dnf|debconf|pacman/pkg)|/root/\.cache|~/\.cache|\$HOME/\.cache|/root/\.npm/_cacache|/usr/share/doc|/usr/share/man)(?:/\S*)?(?:\s+|$))+(?:\s*(?:(?:>|2>|&>)\s*/dev/null|2>&\s*1|1>&\s*2))*\s*$'
    # zsh's completion cache (`rm -rf ~/.zcompdump*`): compinit rebuilds it.
    - type: NotMatches
      value: '^rm\s+(?:-[a-zA-Z]+\s+)*(?:(?:~|\$HOME|\$\{HOME\}|\$ZDOTDIR|\$\{ZDOTDIR(?::-[^}\s]*)?\}|/home/[^/\s]+|/Users/[^/\s]+|/root)/\.zcompdump[^\s/]*(?:\s+|$))+$'
  alternative: "trash <path>"
  alternative_info: "Moves to trash instead of permanent deletion."
- from: fs
  # A whole well-known folder of a home directory by name (`rm -rf
  # ~/Documents`, `rm -rf /Users/alice/Library`, `rm -rf ~/.ssh`): matched
  # whether or not the folder exists where shellfirm runs (another user, a
  # different HOME). A file or folder inside one (`~/Documents/old.txt`,
  # `~/Library/Caches/app`) is not matched.
  test: '^rm\s+(?:\S+\s+)*?(?:-[a-zA-Z]*[rR][a-zA-Z]*|--recursive)\s+(?:\S+\s+)*?((?:~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)/(?i:Documents|Desktop|Downloads|Pictures|Photos|Music|Movies|Videos|Public|Projects|work|workspaces?|src|sources?|code|dev|Developer|repos|repositories|git|github|Library|Applications|Dropbox|OneDrive|Google\x01Drive|\.config|\.local|\.local/share|Library/Keychains|Library/Mail|Library/Messages|Library/Application\x01Support|Library/Mobile\x01Documents|\.mozilla|\.thunderbird|\.ssh|\.gnupg|\.aws|\.kube|\.azure|\.docker|\.password-store|\.oci|\.terraform\.d|\.config/gcloud|\.config/gh|\.ethereum|\.electrum|\.bitcoin))(?:\s|$)'
  description: "This recursively deletes a whole folder of a home directory (Documents, Desktop, Projects, Library, .config, .ssh, cloud credentials, ...): everything in it is lost."
  id: fs:delete_home_folder
  severity: High
  alternative: "trash <path>"
  alternative_info: "Moves to trash instead of permanent deletion."
- from: fs
  # Moving a whole home folder into a scratch directory that is cleared on
  # its own (`mv ~/Projects /tmp/x`, `mv -t /tmp ~/Projects`, `mv
  # --target=/tmp ~/Projects` (any prefix of --target-directory), `rsync
  # --remove-source-files ~/Documents /tmp/x`). Credential folders are
  # fs:move_credentials.
  test: '^mv\s+(?:\S+\s+)*?((?:~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)/(?i:Documents|Desktop|Downloads|Pictures|Photos|Music|Movies|Videos|Public|Projects|work|workspaces?|src|sources?|code|dev|Developer|repos|repositories|git|github|Library|Applications|Dropbox|OneDrive|Google\x01Drive|\.config|\.local|\.local/share|Library/Keychains|Library/Mail|Library/Messages|Library/Application\x01Support|Library/Mobile\x01Documents|\.mozilla|\.thunderbird))\s+(?:\S+\s+)*?(?:/tmp|/var/tmp|/private/tmp|/private/var/tmp|/dev/shm|\$TMPDIR|\$\{TMPDIR\})(?:/\S*)?\s*$|^mv\s+(?:\S+\s+)*?(?:-[a-zA-Z]*t\s*|--t[a-z-]*(?:\s+|=))(?:/tmp|/var/tmp|/private/tmp|/private/var/tmp|/dev/shm|\$TMPDIR|\$\{TMPDIR\})(?:/\S*)?\s+(?:\S+\s+)*?((?:~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)/(?i:Documents|Desktop|Downloads|Pictures|Photos|Music|Movies|Videos|Public|Projects|work|workspaces?|src|sources?|code|dev|Developer|repos|repositories|git|github|Library|Applications|Dropbox|OneDrive|Google\x01Drive|\.config|\.local|\.local/share|Library/Keychains|Library/Mail|Library/Messages|Library/Application\x01Support|Library/Mobile\x01Documents|\.mozilla|\.thunderbird))/?(?:\s|$)|^rsync\s+(?:\S+\s+)*?--remove-source-files\s+(?:\S+\s+)*?((?:~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)/(?i:Documents|Desktop|Downloads|Pictures|Photos|Music|Movies|Videos|Public|Projects|work|workspaces?|src|sources?|code|dev|Developer|repos|repositories|git|github|Library|Applications|Dropbox|OneDrive|Google\x01Drive|\.config|\.local|\.local/share|Library/Keychains|Library/Mail|Library/Messages|Library/Application\x01Support|Library/Mobile\x01Documents|\.mozilla|\.thunderbird))/?\s+(?:\S+\s+)*?(?:/tmp|/var/tmp|/private/tmp|/private/var/tmp|/dev/shm|\$TMPDIR|\$\{TMPDIR\})(?:/\S*)?\s*$'
  description: "This moves a whole folder of your home directory (Documents, Projects, Library, ...) into a temporary folder that the system clears on its own."
  id: fs:move_home_folder
  severity: High
  alternative: "trash <path>"
  alternative_info: "Moves to trash, where the folder can be restored."
- from: fs
  # Emitted by shellfirm when `rm -r` deletes the root of a git repository
  # (a directory with a readable .git), by any spelling (`../webapp`,
  # `~/projects/webapp`, `/srv/app`).
  test: '^shellfirm-delete-repository\s'
  description: "This recursively deletes a whole git repository, including any work that was never pushed."
  id: fs:delete_repository
  severity: High
  alternative: "trash <path>"
  alternative_info: "Moves to trash instead of permanent deletion."
- from: fs
  # Emitted by shellfirm when `rm -r` deletes the `.git` directory of a
  # repository by another spelling than `.git` (`projects/webapp/.git` from
  # the home directory, `../.git` from a sub-directory).
  test: '^shellfirm-delete-git-dir\s'
  description: "This deletes the .git directory of a repository: its whole history, branches, stashes and every commit that was never pushed."
  id: fs:delete_git_dir
  severity: Critical
  alternative: "trash <path>"
  alternative_info: "Moves to trash instead of permanent deletion."
- from: fs
  # Emitted by shellfirm when `mv DIR /tmp/…` moves a folder of a git
  # repository that holds uncommitted or untracked files (or that could not
  # be checked in time) to a scratch folder that is cleared on its own.
  test: '^shellfirm-move-work-away\s'
  description: "This moves a folder with work git does not have to a scratch folder that is cleared on its own."
  id: fs:move_work_away
  severity: High
  alternative: "git stash push --include-untracked -- <path>"
  alternative_info: "Keeps the uncommitted and untracked files in a stash you can bring back with `git stash pop`."
- from: fs
  # Emitted by shellfirm for every `context.sensitive_paths` entry the
  # command deletes or overwrites (see `shellfirm config context paths`).
  test: '^shellfirm-sensitive-path\s'
  description: "This deletes or overwrites a path you marked as sensitive (context.sensitive_paths)."
  id: fs:sensitive_path
  severity: High
- from: fs
  test: (?:^|\s)mv\s+(?:-\S+\s+)*([^-\s]\S*)(?:\s+\S+)*\s+/dev/null(?:\s|$)
  description: "Moving files to /dev/null destroys them."
  id: fs:move_to_dev_null
  severity: Critical
  filters:
    - type: PathExists
      value: 1
- from: fs
  test: (?:^|\s)(?:[0-9]*|&)>\|?\s+(\S+)
  description: "This redirection overwrites the existing file and erases its contents."
  id: fs:flush_file_content
  severity: Medium
  filters:
    - type: PathExists
      value: 1
    - type: NotMatches
      value: '(?:^|\s)(?:[0-9]*|&)>\|?\s+/(?:dev|proc|sys)/'
    # A fresh `mktemp` file, and truncating log files on purpose (`: >
    # app.log`, `for f in *.log; do : > "$f"; done`) are routine.
    - type: NotMatches
      value: '(?:^|\s)(?:[0-9]*|&)>\|?\s+/tmp/shellfirm-mktemp\.\S*(?:\s|$)'
    - type: NotMatches
      value: '^(?::|true)\s+>\|?\s+\S*\.log(?:\.\d+)?(?:\s|$)'
- from: fs
  test: '(?:^|\s)chmod\s+(?:\S+\s+)*(?:-[a-zA-Z]*R[a-zA-Z]*|--recursive)\s+(?:\S+\s+)*(/\*?|~(?:/\*)?|\$HOME(?:/\*)?|\.+|\*|/(?:home|etc|usr|var|bin|sbin|lib|lib64|boot|opt|root|srv|Users|Library|System)(?:/\*)?|/(?:home|Users)/[^/\s]+/?\*?)(?:\s|$)'
  description: "Recursively changing permissions on the root, home or current directory can break the system and SSH keys."
  id: fs:recursively_chmod
  severity: Critical
  filters:
    # Adding your own permissions, adding read access or taking write access
    # away from others (`chmod -R u+rwX .`, `a+r`, `go-w`) on the project or
    # the home directory repairs access; it does not break anything
    # (fs-strict:change_permissions still reports it).
    - type: NotMatches
      value: '^chmod(?:\s+-\S+)*\s+(?:u+\+[rwxXst]+|[ugoa]*\+[rX]+|[go]+-[rwxXst]+)(?:,(?:u+\+[rwxXst]+|[ugoa]*\+[rX]+|[go]+-[rwxXst]+))*(?:\s+-\S+)*(?:\s+(?:\.{1,2}|\*|~|\$HOME|/(?:home|Users)/[^/\s]+)(?:/\S*)?)+$'
    # `chmod -R 755 .` on the project: everything readable, nothing writable
    # by others (fs-strict:change_permissions still reports it, Low).
    - type: NotMatches
      value: '^chmod(?:\s+-\S+)*\s+0?755(?:\s+-\S+)*\s+\.(?:/)?$'
# Opening the SSH / GnuPG keys to other users, or locking them with 000:
# recursively any group / other permission, on a single file any group /
# other write. `chmod -R 700 ~/.ssh`, `go-rwx` and `chmod 644 ~/.ssh/id.pub`
# are the right fix / normal and stay quiet.
- from: fs
  test: '(?:^|\s)chmod\s+(?:(?:-\S+\s+)*(?:-[a-zA-Z]*R[a-zA-Z]*|--recursive)\s+(?:-\S+\s+)*(?:0?[0-7][0-7][1-7]|0?[0-7][1-7][0-7]|0?000|(?:[ugoa]*[goa][ugoa]*)?[+=][rwxXst]+)|(?:-\S+\s+)*(?:0?[0-7][0-7][2367]|0?[0-7][2367][0-7]|0?000|(?:[ugoa]*[goa][ugoa]*)?[+=][rwxXst]*w[rwxXst]*))\s+(?:\S+\s+)*?(?:~|\$HOME|/(?:home|Users)/[^/\s]+)/\.(?:ssh|gnupg)(?:/\S*)?(?:\s|$)'
  description: "This changes the permissions of your SSH or GnuPG keys so that other users can read them (ssh then refuses the keys), or so that nobody can."
  id: fs:chmod_credentials
  severity: High
  alternative: "chmod -R go-rwx ~/.ssh"
  alternative_info: "Keys must be readable only by you: `chmod 700 ~/.ssh` and `chmod 600` for the files in it."

# find -delete: without a name/time/size filter it deletes everything under
# the starting point. find evaluates its expression left to right, so a
# filter only narrows the delete when it comes *before* `-delete`
# (`find . -delete -name '*.log'` deletes everything). A filter on a critical root (`find / -name x -delete`)
# is still critical; a filtered delete elsewhere is Medium.
- from: fs
  test: (?:^|\s)find\s.*\s-delete(\s|$)
  description: "find -delete without a name, time or size filter deletes everything under the path: here the filesystem root, your home directory, a system directory, the repository or the current directory."
  id: fs:delete_find_files
  severity: Critical
  filters:
    - type: NotMatches
      value: '\s-(?:i?name|i?path|i?wholename|i?regex|newer\S*|[ac]newer|[amc](?:time|min)|size|user|group|[ug]id|perm|empty|links|inum|samefile|i?lname|used)\s(?:.*\s)?-delete(?:\s|$)'
    # Starting point: `.`, `..`, `~`, `/`, a system or home directory, `.git`,
    # a glob or a variable. A named subdirectory is fs:delete_find_files_dir.
    - type: Matches
      value: '(?:^|\s)find\s+(?:-[HLP]\s+)*(?:[^-\s(!]\S*\s+)*?(?:/\*?|~(?:/[^/\s]+)?|\$HOME(?:/[^/\s]+)?|\.\.?(?:/\S*)?|\*|\.git(?:/\S*)?|\$\{?\w+\}?\S*|\{\}|/(?:home|Users)/[^/\s]+(?:/[^/\s]+)?|/(?:home|etc|usr|var|bin|sbin|lib|lib64|boot|opt|root|srv|sys|proc|dev|System|Users|Library|Applications)(?:/[^/\s]+)?)(?:\s|$)'
  alternative: "find <path> -name '<pattern>' -print"
  alternative_info: "Preview what would be deleted with -print first, then add -delete when you're sure."
- from: fs
  # `find build -mindepth 1 -delete`, `find dist -type f -delete`: emptying a
  # named directory, like `rm -rf build/*`.
  test: (?:^|\s)find\s.*\s-delete(\s|$)
  description: "find -delete without a name, time or size filter deletes every file under the given directory."
  id: fs:delete_find_files_dir
  severity: Medium
  filters:
    - type: NotMatches
      value: '\s-(?:i?name|i?path|i?wholename|i?regex|newer\S*|[ac]newer|[amc](?:time|min)|size|user|group|[ug]id|perm|empty|links|inum|samefile|i?lname|used)\s(?:.*\s)?-delete(?:\s|$)'
    - type: NotMatches
      value: '(?:^|\s)find\s+(?:-[HLP]\s+)*(?:[^-\s(!]\S*\s+)*?(?:/\*?|~(?:/[^/\s]+)?|\$HOME(?:/[^/\s]+)?|\.\.?(?:/\S*)?|\*|\.git(?:/\S*)?|\$\{?\w+\}?\S*|\{\}|/(?:home|Users)/[^/\s]+(?:/[^/\s]+)?|/(?:home|etc|usr|var|bin|sbin|lib|lib64|boot|opt|root|srv|sys|proc|dev|System|Users|Library|Applications)(?:/[^/\s]+)?)(?:\s|$)'
  alternative: "find <path> -name '<pattern>' -print"
  alternative_info: "Preview what would be deleted with -print first, then add -delete when you're sure."
- from: fs
  test: '^find\s+(?:-[HLP]\s+)*(?:[^-\s(!]\S*\s+)*?(?:/|~|\$HOME|\.\.(?:/\S*)?|\$\{?\w+\}?\S*|\{\}|/(?:home|etc|usr|var|bin|sbin|lib|lib64|boot|opt|root|srv|sys|proc|dev|System|Users|Library|Applications))\s(?:.*\s)?-delete(?:\s|$)'
  description: "find -delete under the filesystem root, your home directory, a system directory, a parent directory (..) or an unresolved variable path deletes every matching file there."
  id: fs:delete_find_files_critical_root
  severity: Critical
  filters:
    # The unfiltered form is covered by fs:delete_find_files.
    - type: Matches
      value: '\s-(?:i?name|i?path|i?wholename|i?regex|newer\S*|[ac]newer|[amc](?:time|min)|size|user|group|[ug]id|perm|empty|links|inum|samefile|i?lname|used)\s(?:.*\s)?-delete(?:\s|$)'
  alternative: "find <path> -name '<pattern>' -print"
  alternative_info: "Preview what would be deleted with -print first, then add -delete when you're sure."
- from: fs
  test: (?:^|\s)find\s.*\s-delete(\s|$)
  description: "find -delete permanently deletes every file matching the filter."
  id: fs:delete_find_files_filtered
  severity: Medium
  filters:
    - type: Matches
      value: '\s-(?:i?name|i?path|i?wholename|i?regex|newer\S*|[ac]newer|[amc](?:time|min)|size|user|group|[ug]id|perm|empty|links|inum|samefile|i?lname|used)\s(?:.*\s)?-delete(?:\s|$)'
    # Applies under sudo / doas too (checks.rs PRIVILEGED_MARKER).
    - type: NotMatches
      value: '^(?:shellfirm-privileged\s+)?find\s+(?:-[HLP]\s+)*(?:[^-\s(!]\S*\s+)*?(?:/|~|\$HOME|\.\.(?:/\S*)?|\$\{?\w+\}?\S*|\{\}|/(?:home|etc|usr|var|bin|sbin|lib|lib64|boot|opt|root|srv|sys|proc|dev|System|Users|Library|Applications))(?:\s|$)'
    # Clearing caches under the current directory or one folder in it
    # (`find src -name __pycache__ -exec rm -rf {} +`, `find . -name '*.pyc'
    # -delete`, `.DS_Store`): one exact name test, no `-o` / `!` / `(` and
    # nothing else deleted.
    - type: NotMatches
      value: '^find\s+(?:\.|[^-\s.~/$(!*?\[{][^\s/*?\[{$]*)\s+(?:-depth\s+)?(?:-type\s+[df]\s+)?-i?name\s+(?:node_modules|__pycache__|\.mypy_cache|\.ruff_cache|\.pytest_cache|\.turbo|\.parcel-cache|\.next|\.nuxt|\.svelte-kit|\.angular|\.gradle|\.tox|\.nox|\.dart_tool|\.nyc_output|\.venv|\*\.py[co]|\.DS_Store)\s+(?:-type\s+[df]\s+)?(?:-prune\s+)?(?:-depth\s+)?-delete(?:\s*(?:(?:>|2>|&>)\s*/dev/null|2>&\s*1|1>&\s*2))*\s*$'
  alternative: "find <path> -name '<pattern>' -print"
  alternative_info: "Preview what would be deleted with -print first, then add -delete when you're sure."
- from: fs
  # Any block device: local (`sda`, `nvme0n1`), cloud (`xvdf`, `vdb`), RAID,
  # device-mapper / LVM (`mapper/vg-data`, `vg/lv`) and stable names
  # (`disk/by-id/…`).
  test: '(?:^|\s)dd\s+(?:\S+\s+)*?of=(/dev/(?:[hsv]d[a-z]+[0-9]*|r?disk[0-9]+(?:s[0-9]+)*|xvd[a-z]+[0-9]*|mmcblk[0-9]+(?:p[0-9]+)?|nvme[0-9]+n[0-9]+(?:p[0-9]+)?|md[0-9]+(?:p[0-9]+)?|md/[^\s/]+|dm-[0-9]+|mapper/[^\s/]+|disk/by-[a-z-]+/[^\s/]+|loop[0-9]+(?:p[0-9]+)?|rbd[0-9]+|zd[0-9]+|[a-zA-Z][\w.-]*/[\w.+-]+))(?:\s|$)'
  description: "Writing directly to a block device can overwrite the whole disk."
  id: fs:dd_block_device
  severity: Critical
  filters:
    # Pseudo files under /dev that are not disks.
    - type: NotMatches
      value: 'of=/dev/(?:fd|pts|shm|tcp|udp|std(?:in|out|err)|mqueue|hugepages|net)/'
- from: fs
  # Any options before the device (`-F`, `-L data`, `-t xfs`) and any block
  # device name (see fs:dd_block_device).
  test: '(?:^|\s)(?:mkfs(?:\.\w+)?|mke2fs|mkswap)\s+(?:\S+\s+)*?(/dev/(?:[hsv]d[a-z]+[0-9]*|r?disk[0-9]+(?:s[0-9]+)*|xvd[a-z]+[0-9]*|mmcblk[0-9]+(?:p[0-9]+)?|nvme[0-9]+n[0-9]+(?:p[0-9]+)?|md[0-9]+(?:p[0-9]+)?|md/[^\s/]+|dm-[0-9]+|mapper/[^\s/]+|disk/by-[a-z-]+/[^\s/]+|loop[0-9]+(?:p[0-9]+)?|rbd[0-9]+|zd[0-9]+|[a-zA-Z][\w.-]*/[\w.+-]+))(?:\s|$)'
  description: "Formatting a device erases all data on it."
  id: fs:mkfs_format
  severity: Critical
  filters:
    # `mkfs.ext4 -n` / `mke2fs -n` / `mkfs.xfs -N`: a dry run that writes nothing.
    - type: NotMatches
      value: '(?:^|\s)(?:mkfs\.ext[2-4]|mke2fs|mkfs\.xfs)\s+(?:\S+\s+)*?-[nN](?:\s|$)'
- from: fs
  test: '(?:^|\s)parted\s+(?:\S+\s+)*?(/dev/(?:[hsv]d[a-z]+[0-9]*|r?disk[0-9]+(?:s[0-9]+)*|xvd[a-z]+[0-9]*|mmcblk[0-9]+(?:p[0-9]+)?|nvme[0-9]+n[0-9]+(?:p[0-9]+)?|md[0-9]+(?:p[0-9]+)?|md/[^\s/]+|dm-[0-9]+|mapper/[^\s/]+|disk/by-[a-z-]+/[^\s/]+|loop[0-9]+(?:p[0-9]+)?|rbd[0-9]+|zd[0-9]+|[a-zA-Z][\w.-]*/[\w.+-]+))(?:\s|$)'
  description: "Modifying disk partitions can erase all data on the disk."
  id: fs:parted_disk_modify
  severity: Critical
  filters:
    # parted runs every command word in order: only `print`, `free` and
    # `unit UNIT` read the table without writing. No command word at all is
    # interactive (commands come from stdin). Its output may only be piped
    # on into commands that show it (`| grep gpt`, `| head`, `tee FILE`).
    - type: NotMatches
      value: '(?:^|\s)parted(?:\s+(?:-s|--script|-m|--machine|-l|--list))*\s+/dev/\S+(?:\s+(?:print|free|unit\s+(?:s|B|kB|MB|MiB|GB|GiB|TB|TiB|%|cyl|chs|compact)))+(?:\s+2>&\s*1)?(?:\s+\|\s+(?:(?:head|tail|grep|egrep|fgrep|less|wc|sort)(?:\s+[^\s|;&<>()$`]+)*|tee(?:\s+-a)?\s+[^-\s|;&<>()$`][^\s|;&<>()$`]*))*(?:\s*$|\s+(?:&&|\|\||;)\s)'
- from: fs
  test: '(?:^|\s)fdisk\s+(?:\S+\s+)*?(/dev/(?:[hsv]d[a-z]+[0-9]*|r?disk[0-9]+(?:s[0-9]+)*|xvd[a-z]+[0-9]*|mmcblk[0-9]+(?:p[0-9]+)?|nvme[0-9]+n[0-9]+(?:p[0-9]+)?|md[0-9]+(?:p[0-9]+)?|md/[^\s/]+|dm-[0-9]+|mapper/[^\s/]+|disk/by-[a-z-]+/[^\s/]+|loop[0-9]+(?:p[0-9]+)?|rbd[0-9]+|zd[0-9]+|[a-zA-Z][\w.-]*/[\w.+-]+))(?:\s|$)'
  description: "Modifying disk partitions can erase all data on the disk."
  id: fs:fdisk_disk_modify
  severity: Critical
- from: fs
  test: '(?:^|\s)sfdisk\s+(?:\S+\s+)*?(/dev/(?:[hsv]d[a-z]+[0-9]*|r?disk[0-9]+(?:s[0-9]+)*|xvd[a-z]+[0-9]*|mmcblk[0-9]+(?:p[0-9]+)?|nvme[0-9]+n[0-9]+(?:p[0-9]+)?|md[0-9]+(?:p[0-9]+)?|md/[^\s/]+|dm-[0-9]+|mapper/[^\s/]+|disk/by-[a-z-]+/[^\s/]+|loop[0-9]+(?:p[0-9]+)?|rbd[0-9]+|zd[0-9]+|[a-zA-Z][\w.-]*/[\w.+-]+))(?:\s|$)'
  description: "Modifying disk partitions can erase all data on the disk."
  id: fs:sfdisk_disk_modify
  severity: Critical
- from: fs
  test: '(?:^|\s)dd\s+.*(?:conv=notrunc|seek=\d+|skip=\d+).*of=(/dev/(?:[hsv]d[a-z]+[0-9]*|r?disk[0-9]+(?:s[0-9]+)*|xvd[a-z]+[0-9]*|mmcblk[0-9]+(?:p[0-9]+)?|nvme[0-9]+n[0-9]+(?:p[0-9]+)?|md[0-9]+(?:p[0-9]+)?|md/[^\s/]+|dm-[0-9]+|mapper/[^\s/]+|disk/by-[a-z-]+/[^\s/]+|loop[0-9]+(?:p[0-9]+)?|rbd[0-9]+|zd[0-9]+|[a-zA-Z][\w.-]*/[\w.+-]+))(?:\s|$)'
  description: "Writing to a specific disk sector can corrupt data."
  id: fs:dd_advanced_disk_write
  severity: Critical
- from: fs
  test: '(?:^|\s)gdisk\s+(?:\S+\s+)*?(/dev/(?:[hsv]d[a-z]+[0-9]*|r?disk[0-9]+(?:s[0-9]+)*|xvd[a-z]+[0-9]*|mmcblk[0-9]+(?:p[0-9]+)?|nvme[0-9]+n[0-9]+(?:p[0-9]+)?|md[0-9]+(?:p[0-9]+)?|md/[^\s/]+|dm-[0-9]+|mapper/[^\s/]+|disk/by-[a-z-]+/[^\s/]+|loop[0-9]+(?:p[0-9]+)?|rbd[0-9]+|zd[0-9]+|[a-zA-Z][\w.-]*/[\w.+-]+))(?:\s|$)'
  description: "Modifying GPT disk partitions can erase all data on the disk."
  id: fs:gdisk_disk_modify
  severity: Critical
- from: fs
  # `sgdisk --zap-all` / `-Z` destroys the GPT and MBR, `-z` / `--zap` the
  # GPT, `-o` / `--clear` writes a new empty partition table.
  test: '(?:^|\s)sgdisk\s+(?:\S+\s+)*?(?:--zap-all|--zap|--clear|-[a-zA-Z]*[Zzo][a-zA-Z]*)(?:\s|$)'
  description: "This destroys the partition table of the disk (with --zap-all also the MBR); every partition on it becomes unreachable."
  id: fs:sgdisk_zap
  severity: Critical
  alternative: "sgdisk --print <device>"
  alternative_info: "Print the partition table first and back it up with `sgdisk --backup=<file> <device>`."
- from: fs
  test: '(?:^|\s)blkdiscard\s+(?:\S+\s+)*?(/dev/(?:[hsv]d[a-z]+[0-9]*|r?disk[0-9]+(?:s[0-9]+)*|xvd[a-z]+[0-9]*|mmcblk[0-9]+(?:p[0-9]+)?|nvme[0-9]+n[0-9]+(?:p[0-9]+)?|md[0-9]+(?:p[0-9]+)?|md/[^\s/]+|dm-[0-9]+|mapper/[^\s/]+|disk/by-[a-z-]+/[^\s/]+|loop[0-9]+(?:p[0-9]+)?|rbd[0-9]+|zd[0-9]+|[a-zA-Z][\w.-]*/[\w.+-]+))(?:\s|$)'
  description: "This discards every block of the device (TRIM): the data on it is gone at once and cannot be recovered."
  id: fs:blkdiscard
  severity: Critical
  alternative: "lsblk -f <device>"
  alternative_info: "Check which filesystem and mount point the device has first."
- from: fs
  test: '(?:^|\s)mdadm\s+(?:\S+\s+)*?--zero-superblock(?:\s|$)'
  description: "This erases the RAID superblock of the device: the array it belongs to can no longer be assembled from it."
  id: fs:mdadm_zero_superblock
  severity: Critical
  alternative: "mdadm --examine <device>"
  alternative_info: "Check which array the device belongs to first."
- from: fs
  test: '(?:^|\s)partprobe\s+.*(/dev/([hs]d[a-z]|mmcblk[0-9]|nvme[0-9]+n[0-9]+))'
  description: "Re-reading the partition table can affect mounted partitions."
  id: fs:partprobe_disk_update
  severity: High
- from: fs
  test: '(?:^|\s)blockdev\s+.*(/dev/([hs]d[a-z]|mmcblk[0-9]|nvme[0-9]+n[0-9]+))'
  description: "Changing block device parameters can affect disk operations."
  id: fs:blockdev_disk_modify
  severity: High
- from: fs
  test: '(?:^|\s)mount\s+.*(/dev/([hs]d[a-z]|mmcblk[0-9]|nvme[0-9]+n[0-9]+))'
  description: "Mounting a raw block device can affect system stability."
  id: fs:mount_operations
  severity: High
- from: fs
  test: '(?:^|\s)(?:sudo\s+)?(?:lvremove|pvremove|vgremove|lvreduce)\s+(?:-[a-zA-Z]+\s+)?(?:--[a-zA-Z-]+\s+)?(?:/dev/(?:[hs]d[a-z]|mmcblk[0-9])|/dev/mapper/[^\s]+|[^\s]+)'
  description: "Removing a logical volume, physical volume or volume group, or shrinking a logical volume, deletes its data."
  id: fs:lvm_operations
  severity: Critical
  filters:
    # Test mode (`-t` / `--test`) changes no metadata. Every option must be
    # one that takes no value (`--config` / `--select` can turn it off).
    - type: NotMatches
      value: '^(?:lvremove|vgremove|pvremove)(?:\s+(?:--verbose|--force|--yes|--quiet|-[vfyq]+|[^-\s]\S*))*\s+(?:--test|-[vfyq]*t[tvfyq]*)(?:\s+(?:--test|--verbose|--force|--yes|--quiet|-[tvfyq]+|[^-\s]\S*))*$'
- from: fs
  test: '(?:^|\s)(?:dump|restore)\s+.*(/dev/([hs]d[a-z]|mmcblk[0-9]|nvme[0-9]+n[0-9]+))'
  description: "Dumping or restoring a raw filesystem device can affect system stability."
  id: fs:filesystem_backup
  severity: High
- from: fs
  test: '(?:^|\s)cryptsetup\s+.*(/dev/([hs]d[a-z]|mmcblk[0-9]|nvme[0-9]+n[0-9]+))'
  description: "Encrypting or reformatting a device with cryptsetup can make its data inaccessible."
  id: fs:encryption_operations
  severity: Critical
  filters:
    # `luksDump` / `isLuks` only read the LUKS header (no key-dump or key
    # options).
    - type: NotMatches
      value: '^cryptsetup(?:\s+(?:-v|--verbose|--debug))*\s+(?:isLuks(?:\s+(?:-v|--verbose|--debug))*\s+[^-\s]\S*(?:\s+(?:-v|--verbose|--debug))*|luksDump(?:\s+(?:-v|--verbose|--debug|--dump-json-metadata))*\s+[^-\s]\S*(?:\s+(?:-v|--verbose|--debug|--dump-json-metadata))*)$'
- from: fs
  test: (?:^|\s)chmod\s
  description: "This changes file permissions."
  id: fs-strict:change_permissions
  severity: Low
  filters:
    # Making one file executable or giving it the usual mode (`chmod +x
    # deploy.sh`, `chmod u+x run`, `chmod 644 notes.txt`) is routine: no
    # prompt for a single file outside the system directories, your home
    # itself and dot directories (`~/.ssh`).
    # Applies under sudo / doas too (checks.rs PRIVILEGED_MARKER).
    - type: NotMatches
      value: '^(?:shellfirm-privileged\s+)?chmod\s+(?:[ugoa]*\+[rx]+|u[+=][rwx]+|0?(?:644|755|600|700|640|750))\s+(?:(?:\./)?[^\s/~$.*\-][^\s*]*|(?:~|\$HOME|/tmp)/[^.\s*][^\s*]*)(?:\s*(?:(?:>|2>|&>)\s*/dev/null|2>&\s*1|1>&\s*2))*\s*$'
    # Taking write access away from group / others (`chmod go-w f`, the
    # oh-my-zsh `compaudit | xargs chmod g-w,o-w` fix) only tightens
    # permissions. A recursive change on / or a system directory is still
    # fs:recursively_chmod.
    - type: NotMatches
      value: '(?:^|\s)chmod(?:\s+-\S+)*\s+[go]+-w+(?:,[go]+-w+)*(?:\s|$)'
    # Making one project dotfile owner-only (`chmod 600 .env`) only takes
    # access away. A folder or symbolic link (`.git`) is spelled with a
    # trailing `/` (checks.rs), so it is not skipped.
    - type: NotMatches
      value: '^chmod\s+(?:0?600|0?400|go-rwx|og-rwx)\s+\.[^\s/*?\[{$.~][^\s/*?\[{$]*(?:\s*(?:(?:>|2>|&>)\s*/dev/null|2>&\s*1|1>&\s*2))*\s*$'
- from: fs
  test: ^rm\s+(.+)$
  description: "This permanently deletes the given files."
  id: fs-strict:any_deletion
  severity: Low
  filters:
    - type: PathExists
      value: 1
    # Recursive deletes are covered by fs-strict:recursive_deletion.
    - type: NotMatches
      value: '\s(?:-[a-zA-Z]*[rR][a-zA-Z]*|--recursive)(?:\s|$)'
    # Lockfiles and tool caches the next install or build recreates
    # (`rm yarn.lock`, `.eslintcache`, `*.tsbuildinfo`): relative, without
    # `..`, globs or variables.
    - type: NotMatches
      value: '^rm\s+(?:-[a-zA-Z]+\s+)*(?:(?:(?:>|2>|&>)\s*/dev/null|2>&\s*1|1>&\s*2)\s+)*(?:(?:(?:[^\s/*?\[{$.~]|\.[^\s/*?\[{$.])[^\s/*?\[{$]*/)*(?:package-lock\.json|yarn\.lock|pnpm-lock\.yaml|bun\.lockb?|\.eslintcache|(?:(?:[^\s/*?\[{$.~]|\.[^\s/*?\[{$.])[^\s/*?\[{$]*)?\.tsbuildinfo)(?:\s+|$))+(?:\s*(?:(?:>|2>|&>)\s*/dev/null|2>&\s*1|1>&\s*2))*\s*$'
    # zsh's completion cache (`rm -f ~/.zcompdump*`): compinit rebuilds it.
    # Applies under sudo / doas too (checks.rs PRIVILEGED_MARKER).
    - type: NotMatches
      value: '^(?:shellfirm-privileged\s+)?rm\s+(?:-[a-zA-Z]+\s+)*(?:(?:~|\$HOME|\$\{HOME\}|\$ZDOTDIR|\$\{ZDOTDIR(?::-[^}\s]*)?\}|/home/[^/\s]+|/Users/[^/\s]+|/root)/\.zcompdump[^\s/]*(?:\s+|$))+$'
- from: fs
  test: '^rm\s+((?:\S+\s+)*?(?:-[a-zA-Z]*[rR][a-zA-Z]*|--recursive)(?:\s.*)?)$'
  description: "This recursively deletes the given files or directories, with everything inside them."
  id: fs-strict:recursive_deletion
  severity: Medium
  filters:
    - type: PathExists
      value: 1
    # Package-manager caches (the Dockerfile `rm -rf /var/lib/apt/lists/*`
    # idiom), a `.cache` directory and top-level named entries of /tmp: only
    # regenerable / throwaway files, when every operand is one of them.
    # Applies under sudo / doas too (checks.rs PRIVILEGED_MARKER).
    - type: NotMatches
      value: '^(?:shellfirm-privileged\s+)?rm\s+(?:-\S+\s+)*(?:(?:(?:>|2>|&>)\s*/dev/null|2>&\s*1|1>&\s*2)\s+)*(?:(?:(?:(?:\./)?\.cache|/var/lib/apt/lists|/var/cache/(?:apt|apk|yum|dnf|debconf|pacman/pkg)|/root/\.cache|~/\.cache|\$HOME/\.cache|/root/\.npm/_cacache|/usr/share/doc|/usr/share/man)(?:/\S*)?|/tmp/[^\s/*?]+|/var/tmp/[^\s/*?]+)(?:\s+|$))+(?:\s*(?:(?:>|2>|&>)\s*/dev/null|2>&\s*1|1>&\s*2))*\s*$'
    # zsh's completion cache (`rm -rf ~/.zcompdump*`): compinit rebuilds it.
    # Applies under sudo / doas too (checks.rs PRIVILEGED_MARKER).
    - type: NotMatches
      value: '^(?:shellfirm-privileged\s+)?rm\s+(?:-[a-zA-Z]+\s+)*(?:(?:~|\$HOME|\$\{HOME\}|\$ZDOTDIR|\$\{ZDOTDIR(?::-[^}\s]*)?\}|/home/[^/\s]+|/Users/[^/\s]+|/root)/\.zcompdump[^\s/]*(?:\s+|$))+$'
    # A project's dependency / tool caches (`rm -rf node_modules`,
    # `__pycache__`, `.mypy_cache`, `node_modules/.vite`, with
    # `package-lock.json` / `yarn.lock` / `pnpm-lock.yaml` / `bun.lock(b)`
    # beside them): a reinstall or the next run rebuilds them. Every operand
    # is one of them, relative, without `..`, globs, variables or a trailing
    # `/`, and never starting with `/`, `~` or `$` (a home or absolute path
    # such as `~/projects/node_modules` is a real top-level delete, so it
    # asks). Build output folders (`.next`, `.venv`, `.tox`, `.gradle`, ...)
    # count only as relative paths (`~/.gradle` holds credentials). A symbolic
    # link on the way is spelled with a trailing `/` (checks.rs), so it is not
    # skipped.
    - type: NotMatches
      value: '^rm\s+(?:-\S+\s+)*(?:(?:(?:>|2>|&>)\s*/dev/null|2>&\s*1|1>&\s*2)\s+)*(?:(?:(?:(?:[^\s/*?\[{$.~]|\.[^\s/*?\[{$.])[^\s/*?\[{$]*/)*)(?:package-lock\.json|yarn\.lock|pnpm-lock\.yaml|bun\.lockb?)\s+)*(?:(?:(?:[^\s/*?\[{$.~]|\.[^\s/*?\[{$.])[^\s/*?\[{$]*/)*(?:node_modules|__pycache__|\.mypy_cache|\.ruff_cache|\.pytest_cache|\.turbo|\.parcel-cache|\.next|\.nuxt|\.svelte-kit|\.angular|\.gradle|\.tox|\.nox|\.dart_tool|\.nyc_output|\.venv|build|dist|target|out|coverage|venv|\.output)|node_modules(?:/(?:[^\s/*?\[{$.~]|\.[^\s/*?\[{$.])[^\s/*?\[{$]*)+)(?:\s+(?:(?:(?:(?:[^\s/*?\[{$.~]|\.[^\s/*?\[{$.])[^\s/*?\[{$]*/)*(?:node_modules|__pycache__|\.mypy_cache|\.ruff_cache|\.pytest_cache|\.turbo|\.parcel-cache|\.next|\.nuxt|\.svelte-kit|\.angular|\.gradle|\.tox|\.nox|\.dart_tool|\.nyc_output|\.venv|build|dist|target|out|coverage|venv|\.output)|node_modules(?:/(?:[^\s/*?\[{$.~]|\.[^\s/*?\[{$.])[^\s/*?\[{$]*)+)|(?:(?:(?:[^\s/*?\[{$.~]|\.[^\s/*?\[{$.])[^\s/*?\[{$]*/)*)(?:package-lock\.json|yarn\.lock|pnpm-lock\.yaml|bun\.lockb?)))*(?:\s*(?:(?:>|2>|&>)\s*/dev/null|2>&\s*1|1>&\s*2))*\s*$'
  alternative: "trash <path>"
  alternative_info: "Moves to trash instead of permanent deletion."
- from: fs
  test: (?:^|\s)rmdir\s+(.*)\s*
  description: "This deletes the given directory."
  id: fs-strict:folder_deletion
  severity: Medium
  filters:
    - type: PathExists
      value: 1
- from: fs
  test: (?:^|\s)rsync\s+.*--delete(\s|$)
  description: "Syncs with deletion — removes files at destination not present in source."
  id: fs:rsync_delete
  severity: High
  alternative: "rsync --dry-run --delete ..."
  alternative_info: "Preview what would be deleted with --dry-run first."
  filters:
    # A real `-n` / `--dry-run` option word before any `--` (not a `-n`
    # inside a path, an option value or the quoted `-e` command), and no
    # later dry-run word. The word before it must not be an option that takes
    # a value (`--exclude -avn`, `-T -n`), and `n` must not follow a short
    # option that takes the rest of the word as its value (`-en`).
    - type: NotMatches
      value: '^rsync\s+(?:(?:(?:[^-\s]\S*|-[^-\s]\S*|--\S+)\s+)*?(?:[^-\s]\S*|-[^-\sBefTM@]|-[^-\s]\S*[^\sBefTM@]|--[^\s=]+=\S*|--(?:[^abcdefgilmoprstuwz\s=][^\s=]*|a(?:[^d\s=][^\s=]*|d(?:[^d\s=][^\s=]*|d(?:[^r\s=][^\s=]*|r(?:[^e\s=][^\s=]*|e(?:[^s\s=][^\s=]*|s(?:[^s\s=][^\s=]*|s(?:[^\s=][^\s=]*))?)?)?)?)?)?|b(?:[^alw\s=][^\s=]*|a(?:[^c\s=][^\s=]*|c(?:[^k\s=][^\s=]*|k(?:[^u\s=][^\s=]*|u(?:[^p\s=][^\s=]*|p(?:[^\-\s=][^\s=]*|-(?:[^d\s=][^\s=]*|d(?:[^i\s=][^\s=]*|i(?:[^r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=][^\s=]*|o(?:[^u\s=][^\s=]*|u(?:[^t\s=][^\s=]*|t(?:[^\s=][^\s=]*))?)?)?)?)?)?|u(?:[^s\s=][^\s=]*|s(?:[^e\s=][^\s=]*|e(?:[^r\s=][^\s=]*|r(?:[^m\s=][^\s=]*|m(?:[^a\s=][^\s=]*|a(?:[^p\s=][^\s=]*|p(?:[^\s=][^\s=]*))?)?)?)?)?)?|w(?:[^r\s=][^\s=]*|r(?:[^i\s=][^\s=]*|i(?:[^t\s=][^\s=]*|t(?:[^e\s=][^\s=]*|e(?:[^\-\s=][^\s=]*|-(?:[^b\s=][^\s=]*|b(?:[^a\s=][^\s=]*|a(?:[^t\s=][^\s=]*|t(?:[^c\s=][^\s=]*|c(?:[^h\s=][^\s=]*|h(?:[^\s=][^\s=]*))?)?)?)?)?)?)?)?)?)?|z(?:[^c\s=][^\s=]*|c(?:[^\s=][^\s=]*))?))\s+)?(?:-[a-dg-zAC-LN-SU-Z0-9]*n[a-zA-Z0-9]*|--dry-run)(?:\s+(?:[^\-\s]\S*|-(?:[^\-d\s]\S*|-(?:[^dn\s]\S*|d(?:[^r\s]\S*|r(?:[^y\s]\S*))?|n(?:[^o\s]\S*|o(?:[^\-\s]\S*|-(?:[^d\s]\S*|d(?:[^r\s]\S*|r(?:[^y\s]\S*))?)?)?)?)?|d(?:[^r\s]\S*|r(?:[^y\s]\S*)?)?)?))*\s*$'
- from: fs
  # Same targets as fs:recursively_chmod, plus a home directory by its absolute
  # path and its `.ssh` / `.gnupg` (`chown -R dev ./src` and
  # `chown -R dev /home/dev/project` are fine).
  test: '(?:^|\s)chown\s+(?:\S+\s+)*(?:-[a-zA-Z]*R[a-zA-Z]*|--recursive)\s+(?:\S+\s+)*(/\*?|~(?:/\*)?|\$HOME(?:/\*)?|(?:~|\$HOME)/\.(?i:ssh|gnupg)\S*|\.+|\*|/(?:home|etc|usr|var|bin|sbin|lib|lib64|boot|opt|root|srv|Users|Library|System)(?:/\*)?|/(?:home|Users)/[^/\s]+(?:/\*?|/\.(?:ssh|gnupg)\S*)?)(?:\s|$)'
  description: "Recursively changing the owner of the root, home or current directory, a system directory or your SSH keys can break the system and lock you out."
  id: fs:recursively_chown
  severity: Critical
  filters:
    # Taking ownership of your own project or home directory (`sudo chown -R
    # $USER .` after a `sudo npm install`, `chown -R dev:dev ~/projects/webapp`,
    # `sudo chown -R $(whoami) ~`): your own user, written as
    # `$USER`, `$LOGNAME`, `$(whoami)` or `$(id -u)`. Your home's dot
    # directories (`~/.ssh`) and other users' homes stay critical.
    - type: NotMatches
      value: '^chown(?:\s+-\S+)*\s+(?:\$\{?(?:USER|LOGNAME)\}?|\$\(whoami\)|\$\(id\s+-u?n?\))(?:[:.]\S*)?(?:\s+-\S+)*(?:\s+(?:\.|\*|(?:~|\$HOME|\$\{HOME\})/?|(?:~|\$HOME|\$\{HOME\}|/(?:home|Users)/[^/\s]+)/[^.\s]\S*))+$'
- from: fs
  test: (?:^|\s)shred\s+
  description: "Shred overwrites file data at the block level, making recovery impossible unlike rm."
  id: fs:shred
  severity: High
  alternative: "rm <file>"
  alternative_info: "Regular rm only unlinks the file. Use shred only when you truly need irrecoverable deletion."
- from: fs
  test: (?:^|\s)truncate\s+(?:\S+\s+)*?-s\s*0\s+(?:\S+\s+)*?([^-\s]\S*)
  description: "This truncates the file to zero bytes and erases its contents."
  id: fs:truncate_zero
  severity: High
  filters:
    # A file that does not exist is only created (empty).
    - type: PathExists
      value: 1
    # Emptying log files, like `: > app.log`.
    - type: NotMatches
      value: '^truncate\s+(?:-\S+\s+)*-s\s*0\s+(?:\S+\.log(?:\.\d+)?(?:\s+|$))+$'
- from: fs
  # `{}` is the parser's placeholder for arguments that come from another
  # command (xargs input, a filtered find -exec). A bare variable target is
  # unverifiable: when it is empty or unset the command deletes `/` or `.`.
  test: '^rm\s+(?:\S+\s+)*?(?:-[a-zA-Z]*[rR][a-zA-Z]*|--recursive)\s+(?:\S+\s+)*?(\{\}|/?\$\{?\w+\}?(?:/\*?)?)(?:\s|$)'
  description: "The deletion target comes from a variable, from another command's output (xargs, find -exec) or from a link created earlier on the line, so it cannot be verified. An empty variable, unexpected input or a link to an important directory deletes far more than intended."
  id: fs:recursive_delete_unknown_target
  severity: High
  filters:
    # $HOME is a known critical target (fs:recursively_delete).
    - type: NotMatches
      value: '\s\$(?:HOME|\{HOME\})(?:/\*?)?(?:\s|$)'
  alternative: "echo <targets>"
  alternative_info: "Print what the command would delete and review the list before deleting it."
- from: fs
  test: '^rm\s+(?:\S+\s+)*?(\{\})(?:\s|$)'
  description: "This deletes files whose names come from another command's output (xargs, find -exec), so the targets cannot be verified."
  id: fs:delete_unknown_target
  severity: Medium
  filters:
    # Recursive deletes are covered by fs:recursive_delete_unknown_target.
    - type: NotMatches
      value: '\s(?:-[a-zA-Z]*[rR][a-zA-Z]*|--recursive)(?:\s|$)'
- from: fs
  # Everything in a home folder or a folder one level down (`rm -rf
  # ~/Documents/*`, `~/projects/webapp/*`). A deeper folder
  # (`~/projects/webapp/dist/*`, a build output) is left to
  # fs-strict:recursive_deletion; emptying a cache or the trash
  # (`~/.cache/*`, `~/.Trash/*`, `~/Library/Caches/*`, `~/.npm/_logs/*`) is
  # not matched.
  test: '^rm\s+(?:\S+\s+)*?(?:-[a-zA-Z]*[rR][a-zA-Z]*|--recursive)\s+(?:\S+\s+)*?((?:~|\$HOME)/[^/\s]+(?:/[^/\s]+)?/\*)(?:\s|$)'
  description: "This deletes everything inside a directory in your home folder."
  id: fs:recursive_delete_home_glob
  severity: High
  filters:
    - type: NotMatches
      value: '^rm\s+(?:-\S+\s+)*(?:(?:~|\$HOME)/(?:\.cache|\.Trash|Library/Caches|\.npm/_logs|\.npm/_cacache)/\*(?:\s+|$))+$'
  alternative: "trash <path>"
  alternative_info: "Moves to trash instead of permanent deletion."
- from: fs
  test: '^rm\s+(?:\S+\s+)*?(?:-[a-zA-Z]*[rR][a-zA-Z]*|--recursive)\s+(?:\S+\s+)*?(/var/lib/(?:postgresql|pgsql|mysql|mariadb|docker|containerd|mongodb|mongo|redis|etcd|elasticsearch|opensearch|cassandra|kubelet|rabbitmq|influxdb|clickhouse|couchdb|neo4j)(?:/\S*)?)(?:\s|$)'
  description: "This deletes a database or container-engine data directory: every database, image or volume stored there is lost."
  id: fs:recursive_delete_data_dir
  severity: High
- from: fs
  test: '^rm\s+(?:\S+\s+)*?(?:-[a-zA-Z]*[rR][a-zA-Z]*|--recursive)\s+(?:\S+\s+)*?(/[^/\s*$]+)(?:\s|$)'
  description: "This recursively deletes a top-level directory of the filesystem."
  id: fs:recursive_delete_top_level_dir
  severity: High
  filters:
    # System directories are covered by fs:recursively_delete (Critical).
    - type: NotMatches
      value: '\s/(?:home|etc|usr|var|bin|sbin|lib|lib64|boot|opt|root|srv|sys|proc|dev|System|Users|Library|Applications)(?:/\*)?(?:\s|$)'
- from: fs
  # A system file (any write), or a shell startup file replaced (`>`,
  # `tee` without `-a`); appending to a startup file is
  # fs:append_shell_startup_file.
  test: '(?:(?:^|\s)\d*(?:>>?|>\||&>>?)\s+|(?:^|\s)tee\s+(?:\S+\s+)*?)(/etc/\S+|/boot/\S+|/usr/\S+|/s?bin/\S+|/lib(?:64)?/\S+|/System/\S+)(?:\s|$)|(?:(?:^|\s)\d*(?:>|>\||&>)\s+|(?:^|\s)tee\s+(?:(?:-[b-zA-Z]\S*|--[b-z]\S*|[^-\s]\S*)\s+)*?)((?:~|\$HOME)/\.(?i:bashrc|bash_profile|bash_login|profile|zshrc|zprofile|zshenv|zlogin|cshrc|tcshrc|config/fish/config\.fish))(?:\s|$)'
  description: "This writes to a system file or replaces a shell startup file. Overwriting it can break logins, networking or the whole system."
  id: fs:overwrite_system_file
  severity: High
- from: fs
  # `echo 'export PATH=…' >> ~/.bashrc`: a routine setup step, but it also
  # runs on every new shell.
  test: '(?:(?:^|\s)\d*(?:>>|&>>)\s+|(?:^|\s)tee\s+(?:\S+\s+)*?(?:-[a-zA-Z]*a[a-zA-Z]*|--append)\s+(?:\S+\s+)*?)((?:~|\$HOME)/\.(?i:bashrc|bash_profile|bash_login|profile|zshrc|zprofile|zshenv|zlogin|cshrc|tcshrc|config/fish/config\.fish))(?:\s|$)'
  description: "This appends to a shell startup file: the added lines run in every new shell."
  id: fs:append_shell_startup_file
  severity: Low
- from: fs
  # A hard link (`ln ~/.bashrc x`, `ln -f ~/.ssh/id_rsa k`, `ln -- …`,
  # `ln --target-directory=. …`, `link …`, `cp -l …`, `cp -al ~/.ssh k`) to
  # a credential file or folder, a shell startup file or a git hook. The link is the
  # same file under another name, so writing or reading it gets past every
  # rule that looks at the path. Symbolic links (`ln -s`) are not matched:
  # they are resolved to what they point to.
  test: '^(?:(?:ln|link)\s+(?:(?:-[a-rt-zA-Z]+|--|--(?:[a-rt-z][\w-]*|s(?:[a-xz][\w-]*)?)(?:=\S+)?|[^-\s]\S*)\s+)*?|cp\s+(?:\S+\s+)*?(?:-[a-zA-Z]*l[a-zA-Z]*|--link)\s+(?:\S+\s+)*?)(?:(?:~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)/\.(?i:(?:ssh|aws|gnupg|kube|docker)/?|ssh/(?:id_[\w-]+|authorized_keys2?)|aws/(?:credentials|config)|gnupg/\S+|kube/config|docker/config\.json|config/gcloud/\S+|netrc|git-credentials|npmrc|pypirc|bashrc|bash_profile|bash_login|profile|zshrc|zprofile|zshenv|zlogin|zlogout|cshrc|tcshrc|config/fish/config\.fish)|\S*\.git/hooks/[\w.-]+)(?:\s|$)'
  description: "This makes a hard link to a credential file, shell startup file or git hook: the same file under a new name, so changes or reads through it are not seen as touching the original."
  id: fs:hard_link_protected_file
  severity: Medium
  alternative: "ln -s <path> <link>"
  alternative_info: "A symbolic link stays visibly tied to the original file, so the path checks still apply to it."
- from: fs
  # Emitted by shellfirm for an alias written to a shell startup file that
  # makes a command run a different, destructive one (`alias ls=rm`).
  test: '^shellfirm-startup-alias\s'
  description: "This adds an alias to a shell startup file that makes a command run a different, destructive one (for example `ls` running `rm`) in every new shell."
  id: fs:startup_alias_replaces_command
  severity: High
- from: fs
  test: '^(?:rm|unlink)\s+(?:\S+\s+)*?(/etc/\S+|/boot/\S+|/(?:usr/)?s?bin/\S+|/usr/lib\S*/\S+|/lib(?:64)?/\S+)(?:\s|$)'
  description: "This deletes a system file. The system may not boot or let you log in afterwards."
  id: fs:delete_system_file
  severity: High
- from: fs
  # Moving a system file or directory away (`mv /etc/passwd /tmp/`) breaks the
  # system like deleting it. Moving something *into* /usr/local/bin is fine.
  test: '^mv\s+(?:\S+\s+)*?(/etc(?:/\S*)?|/boot(?:/\S*)?|/(?:usr/)?s?bin(?:/\S*)?|/usr|/usr/lib\S*|/lib(?:64)?(?:/\S*)?|~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)\s+(?:\S+\s+)*?\S+\s*$'
  description: "This moves a system file or directory (or your home directory) away. Programs and logins that need it break until it is moved back."
  id: fs:move_system_file
  severity: High
- from: fs
  # Moving the credential directories away (`mv ~/.ssh /tmp/`, `mv -t /tmp
  # ~/.ssh`, `mv --target=/tmp ~/.ssh`) or into an archive that deletes them
  # (`zip -rm x.zip ~/.ssh`, `tar --remove-files -cf x.tar ~/.ssh`, also
  # abbreviated `--remove` and as `gtar`).
  test: '^(?:mv\s+(?:\S+\s+)*?((?:~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)/\.(?i:ssh|gnupg|aws|kube|azure|docker|password-store|config/gcloud)(?:/\S*)?)\s+(?:\S+\s+)*?\S+\s*$|mv\s+(?:\S+\s+)*?(?:-[a-zA-Z]*t\s*|--t[a-z-]*(?:\s+|=))\S+\s+(?:\S+\s+)*?((?:~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)/\.(?i:ssh|gnupg|aws|kube|azure|docker|password-store|config/gcloud)(?:/\S*)?)(?:\s|$)|(?:zip\s+(?:\S+\s+)*?(?:-[a-zA-Z]*m[a-zA-Z]*|--move)|g?tar\s+(?:\S+\s+)*?--rem(?:o(?:v(?:e(?:-(?:f(?:i(?:l(?:es?)?)?)?)?)?)?)?)?)\s+(?:\S+\s+)*?((?:~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)/\.(?i:ssh|gnupg|aws|kube|azure|docker|password-store|config/gcloud)(?:/\S*)?)(?:\s|$))'
  description: "This moves SSH keys, GPG keys or cloud / cluster credentials out of your home directory (or into an archive that deletes them): logins and CLIs that use them stop working, and the copy may be readable elsewhere."
  id: fs:move_credentials
  severity: High
- from: fs
  # Moving away the folder that holds an AI agent's settings (`mv ~/.claude
  # /tmp/old`, `mv ~/.codex …`), or removing the repository's Copilot hook
  # folder (`git rm -r .github/hooks`): the shellfirm hook in it stops
  # running. Moving a file or folder inside it (`mv ~/.claude/projects/x
  # /tmp`) and `git rm --cached` are not matched; shellfirm's own folder is
  # covered by the self-protection rules.
  test: '^(?:mv\s+(?:\S+\s+)*?(?:~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)/\.(?:claude|codex)/?\s+(?:\S+\s+)*?\S+\s*$|mv\s+(?:\S+\s+)*?(?:-[a-zA-Z]*t\s*|--t[a-z-]*(?:\s+|=))\S+\s+(?:\S+\s+)*?(?:~|\$HOME|/home/[^/\s]+|/Users/[^/\s]+|/root)/\.(?:claude|codex)/?(?:\s|$)|(?:git\s+rm|rm)\s+(?:\S+\s+)*?(?:\./)?\.github/hooks/?(?:\s|$))'
  description: "This moves or removes the folder that holds an AI agent's settings or a repository's agent hooks, so the shellfirm hook in it stops running."
  id: fs:move_agent_settings_dir
  severity: High
  filters:
    - type: NotMatches
      value: '(?:^|\s)--cached(?:\s|$)'
  alternative: "move only the file you need"
  alternative_info: "Move or delete the single file inside the folder (for example one project's history), not the whole settings folder."
- from: fs
  # `zip -m` / `zip -rm`, `tar --remove-files` (also abbreviated, `--remove`,
  # and as `gtar`) and `7z a -sdel` delete the files they add.
  test: '(?:^|\s)(?:(?:zip\s+(?:\S+\s+)*?(?:-[a-zA-Z]*m[a-zA-Z]*|--move)|g?tar\s+(?:\S+\s+)*?--rem(?:o(?:v(?:e(?:-(?:f(?:i(?:l(?:es?)?)?)?)?)?)?)?)?)\s|(?:7z|7za|7zz|7zr)\s+[au]\s+(?:\S+\s+)*?-sdel(?:\s|$))'
  description: "zip -m, tar --remove-files and 7z -sdel delete the original files after adding them to the archive."
  id: fs:zip_move
  severity: Medium
- from: fs
  test: '^(?:cp(?:\s+-\S+)*|ln(?:\s+-\S+)*\s+-[a-zA-Z]*f[a-zA-Z]*(?:\s+-\S+)*)\s+/dev/(?:null|zero)\s+(\S+)(?:\s|$)'
  description: "This replaces the file with /dev/null and erases its contents."
  id: fs:clobber_with_dev_null
  severity: High
  filters:
    - type: PathExists
      value: 1
- from: fs
  test: '(?:^|\s)chmod\s+(?:-\S+\s+)*\S+\s+(/|~|\$HOME|/(?:etc|usr|bin|sbin|boot|home|root|var|lib|lib64|System|Users|Library))(?:\s|$)'
  description: "Changing permissions of the root, home or a system directory can lock you out and break SSH and system services."
  id: fs:chmod_critical_location
  severity: High
  filters:
    # The recursive form is covered by fs:recursively_chmod.
    - type: NotMatches
      value: '\s(?:-[a-zA-Z]*R[a-zA-Z]*|--recursive)(?:\s|$)'
- from: fs
  test: '^watch\s+(?:\S+\s+)*?(?:sudo\s+)?rm\s'
  description: "watch runs this deletion again and again, every interval."
  id: fs:watch_repeated_delete
  severity: Medium
- from: fs
  # Only the code argument (`-c` / `-e` / `--eval` / `-r` / `deno eval`; a
  # program read from stdin is matched as `-c`), not file names or test
  # filters (`python3 -m pytest tests/test_rmtree.py -k rm_rf`).
  test: '^(?:python[0-9.]*|perl|ruby|node|nodejs|bun|deno|php|tsx|ts-node)\s+(?:\S+\s+)*?(?:-[a-zA-Z]*[ceE]|-[pr]|--eval|--print|eval)(?:\s+|=)\S*(?:rmtree|rm_rf|rm_r\b|remove_tree|rmSync|rmdirSync|unlinkSync|os\.remove|os\.unlink|os\.rmdir|os\.removedirs|\bunlink\s*\(|FileUtils\.rm|File\.delete)'
  description: "This inline script deletes files or directories."
  id: fs:interpreter_inline_delete
  severity: Medium
- from: fs
  test: (?:^|\s)wipefs\s+(?:\S+\s+)*?(?:-[a-zA-Z]*a[a-zA-Z]*|--all|-o|--offset)(?:\s|$|=)
  description: "This erases the filesystem / partition-table signatures of a device, making its data unreachable."
  id: fs:wipefs
  severity: Critical
- from: fs
  test: (?:^|\s)umount\s+(?:\S+\s+)*?(?:-l|--lazy|-[a-zA-Z]*l[a-zA-Z]*)(?:\s|$)
  description: "A lazy unmount detaches the filesystem while programs still use it; writes in flight can be lost."
  id: fs:umount_lazy
  severity: Medium
- from: fs
  test: '(?:^|\s)(?:zfs\s+(?:\S+\s+)*?destroy|zpool\s+(?:\S+\s+)*?destroy)\s'
  description: "This destroys a ZFS dataset / pool and all of its data and snapshots."
  id: fs:zfs_destroy
  severity: High
  filters:
    # `zfs destroy -n` only prints what would be destroyed: option bundles
    # of `nvpdfrR` before the one dataset, one of them with `n`. Its output
    # may only be piped on into commands that show it (`| tail -1`, `| grep
    # reclaim`, `tee FILE`); `| sh` keeps it flagged.
    - type: NotMatches
      value: '(?:^|\s)zfs\s+destroy(?:\s+-[vpdfrR]+)*\s+-[vpdfrR]*n[nvpdfrR]*(?:\s+-[nvpdfrR]+)*\s+[^-\s]\S*(?:\s+2>&\s*1)?(?:\s+\|\s+(?:(?:head|tail|grep|egrep|fgrep|less|wc|sort)(?:\s+[^\s|;&<>()$`]+)*|tee(?:\s+-a)?\s+[^-\s|;&<>()$`][^\s|;&<>()$`]*))*(?:\s*$|\s+(?:&&|\|\||;)\s)'
- from: fs
  # A recursive owner / mode change in a data or service directory
  # (`/var/lib/postgresql`, `/srv/www`, `/opt/app`) breaks the service that
  # owns it.
  test: '(?:^|\s)(?:chown|chgrp|chmod)\s+(?:\S+\s+)*?(?:-[a-zA-Z]*R[a-zA-Z]*|--recursive)\s+(?:\S+\s+)*?(/(?:var/lib|var/www|var/log|srv|opt|etc|usr)/[^\s]+)(?:\s|$)'
  description: "This recursively changes the owner or permissions of a system data / service directory: the service that owns it can stop working."
  id: fs:recursive_owner_change_system_dir
  severity: High
- from: fs
  # `cp -r /tmp/evil/. ~/`: the contents of a directory copied over the home
  # directory replace its dotfiles (shell startup files, `.ssh`).
  test: '(?:^|\s)cp\s+(?:\S+\s+)*?-[a-zA-Z]*[rRa][a-zA-Z]*\s+(?:\S+\s+)*?\S+/(?:\.|\*|\.\*)\s+(?:~|\$HOME|/(?:home|Users)/[^/\s]+|/root)/?(?:\s|$)'
  description: "This copies the contents of a directory over your home directory, replacing your dotfiles (shell startup files, SSH configuration)."
  id: fs:copy_over_home
  severity: Medium
- from: fs
  # `-r` destroys every snapshot (and with -R every clone) newer than the
  # target.
  test: '(?:^|\s)zfs\s+(?:\S+\s+)*?rollback\s+(?:\S+\s+)*?-[a-zA-Z]*[rR][a-zA-Z]*\s'
  description: "This rolls the ZFS dataset back and destroys every later snapshot (and with -R their clones)."
  id: fs:zfs_rollback_recursive
  severity: High
  alternative: "zfs list -t snapshot -o name,creation <dataset>"
  alternative_info: "List the snapshots that would be destroyed first."