Repository navigation
Expand file tree
/
Copy pathcommand_guard.py
More file actions
1408 lines (1273 loc) · 52.7 KB
/
Copy pathcommand_guard.py
File metadata and controls
1408 lines (1273 loc) · 52.7 KB
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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tokenizer-based classifier behind the last-resort Bash deny hook (engine.py, PreToolUse).
``classify_command(text)`` is a PURE function (stdlib only, never mutates anything, never runs
anything) that answers one question: does this Bash command contain one of the hard-coded deny
shapes? It replaces the old regex-over-text matcher, which (a) was fooled by quoting and global
git options, (b) denied commands that merely MENTIONED a denied shape, and (c) went super-linear
on repeated tokens (24 KB of ``dd `` took 2.7 s inside the cockpit's event loop).
How it reads a command
----------------------
* A small shell lexer (quotes, backslashes, ``$(...)``, backticks, ``<(...)``, heredocs,
comments, env-assignment prefixes, the separators ``; & | && || |& ;;`` and newlines, reserved
words such as ``if/then/do/{``) turns the text into a list of simple commands. Command
substitutions are lexed IN PLACE and their commands join the same list, so
``echo "$(git reset --hard)"`` is judged like ``git reset --hard``.
* DATA IS NOT A COMMAND. A rule only fires on a command's own argv. The arguments of ``echo`` /
``printf`` / ``grep``, the ``-m`` / ``-F`` text of ``git commit``, comments and heredoc bodies
that feed a non-shell command (``cat > f <<'EOF'``) are never inspected.
* Content handed to an INTERPRETER is parsed as a program again: ``bash|sh|zsh|... -c``,
``eval``, ``ssh host "<cmd>"``, ``su -c``, ``sudo -s``, ``env -S``, a heredoc / here-string /
piped ``echo`` that feeds a shell, and the command behind the wrappers ``sudo doas env xargs
nohup timeout nice time command exec setsid stdbuf ...`` and ``find -exec``.
* Rules are functions of argv (``git`` global options before the subcommand are skipped, short
options are read as clusters, long options as git's unambiguous prefixes), not substrings.
Limits (fail closed for the NARROW list, never for everything)
---------------------------------------------------------------
Input above ``MAX_CHARS``, nesting deeper than ``MAX_DEPTH``, more lexing work than the shared
budget allows, or text the lexer cannot parse (unterminated quote) falls back to a coarse
scan: the text is cut at ``; & | newline ( ) `` and whitespace, quote characters are stripped
and the same argv rules run on a bounded window after every trigger word (``git``, ``rm``,
``chmod``, ``docker``, ``dd``, ``mkfs``, ...). Nothing else is denied, and no step of either
path backtracks, so the cost is linear in the input.
Not covered (honest list): code written to a file and then run (``echo '...' > x.sh; sh x.sh``),
foreign interpreters (``python -c``, ``perl -e``), git aliases, variable-built command names.
This is a circuit breaker for the usual accidents, not a sandbox.
"""
from __future__ import annotations
import functools
import re
from typing import NamedTuple, Optional
MAX_CHARS = 262_144 # above this the lexer is not even started
MAX_DEPTH = 24 # nesting of $( ), backticks and re-parsed payloads (bash -c "...")
WORK_FACTOR = 8 # chars lexed over ALL nested programs <= WORK_FACTOR * len + 20_000
_CRUDE_WINDOW = 64 # tokens looked at after a trigger word in the fallback scan
_CRUDE_TRIGGERS = 4000 # trigger words that get the full window; later ones get the short one
_CRUDE_TAIL_WINDOW = 12
class Finding(NamedTuple):
rule: str # stable id, written to the audit log
reason: str # human-readable, shown to the agent
# Stable rule ids -> reason. Reasons for the pre-existing rules keep their historical wording.
RULES = {
"fork-bomb": "fork bomb: exhausts host processes/memory",
"rm-root-home": "rm -rf targeting the filesystem root or $HOME wipes the host",
"chmod-root": "chmod -R on the filesystem root can lock out the whole host",
"git-force-push-protected":
"git push --force into master/main rewrites shared history irreversibly",
"git-hard-reset": "git reset --hard discards uncommitted work irreversibly",
"ssh-dir-mutation": "writing to or deleting ~/.ssh can lock out or leak host SSH access",
"ssh-dir-chmod-open":
"opening ~/.ssh permissions to group/other exposes the host's SSH keys",
"docker-system-prune":
"docker system prune deletes every unused image/volume/network on the host",
"mkfs": "mkfs formats a filesystem and destroys existing data",
"dd-block-device": "dd writing to a raw block device can overwrite the host disk",
"git-skip-hooks":
"skipping git hooks (--no-verify, or -n on commit/am) bypasses the pre-commit secret "
"scan, the last barrier before a credential lands in the public history "
"(a scan false positive goes into .secretscanignore)",
"git-hookspath":
"overriding core.hooksPath turns off the repository's git hooks, including the "
"pre-commit secret scan (a scan false positive goes into .secretscanignore)",
"skip-secret-scan":
"SKIP_SECRET_SCAN switches off the pre-commit secret scan, the last barrier before a "
"credential lands in the public history (a scan false positive goes into "
".secretscanignore)",
}
def _f(rule: str) -> Finding:
return Finding(rule, RULES[rule])
class _Unparseable(Exception):
"""The lexer cannot (or may not) finish: the caller falls back to the coarse scan."""
class _Budget:
__slots__ = ("left",)
def __init__(self, n: int) -> None:
self.left = n
def spend(self, n: int) -> None:
self.left -= n
if self.left < 0:
raise _Unparseable("work budget exceeded")
# ───────────────────────────────────────── lexer ───────────────────────────────────────────────
_RUNWS = re.compile(r"""([^ \t\r\f\v\n'"\\$`;&|<>()]+)([ \t\r\f\v]*)""")
_WS = re.compile(r"[ \t\r\f\v]+")
_DQRUN = re.compile(r'[^"\\$`]+')
_PARAMRUN = re.compile(r"""[^}{\\'"$`]+""")
_ANSI_END = re.compile(r"(?:\\.|[^'\\])*'", re.S)
_BT_END = re.compile(r"(?:\\.|[^`\\])*`", re.S)
_ARITH_RUN = re.compile(r"""[^()$`'";\n\\]+""")
_BT_ESC = re.compile(r"\\([$`\\])")
_EXPANSION = re.compile(r"\\[\s\S]|\$\(|`")
_ANSI_ESC = re.compile(r"\\(x[0-9a-fA-F]{1,2}|u[0-9a-fA-F]{1,4}|[0-7]{1,3}|[\s\S])")
_ANSI_SIMPLE = {"n": "\n", "t": "\t", "r": "\r", "a": "\a", "b": "\b", "e": "\x1b", "E": "\x1b",
"f": "\f", "v": "\v"}
# Reserved words dropped when they open a command: the next word is the command.
_DROP = frozenset({"if", "then", "elif", "else", "fi", "while", "until", "do", "done", "{", "}",
"!", "esac", "coproc"})
_WRITE_REDIRS = frozenset({">", ">>", ">|", ">&", "&>", "&>>", "<>"})
class _Cmd:
"""One simple command: its words (quotes removed, variables NOT expanded), its redirects
``[op, word, heredoc_body, delimiter_was_quoted]`` and the previous stage of its pipeline."""
__slots__ = ("words", "redirs", "pipe_from")
def __init__(self, pipe_from: "Optional[_Cmd]" = None) -> None:
self.words: "list[str]" = []
self.redirs: "list[list]" = []
self.pipe_from = pipe_from
@functools.lru_cache(maxsize=64)
def _heredoc_end(delim: str, strip_tabs: bool) -> "re.Pattern":
return re.compile(r"^" + (r"\t*" if strip_tabs else "") + re.escape(delim) + r"\r?$", re.M)
def _decode_ansi(raw: str) -> str:
def one(m: "re.Match") -> str:
g = m.group(1)
c = g[0]
if c == "x" and len(g) > 1 or c == "u" and len(g) > 1:
return chr(int(g[1:], 16))
if c in "01234567":
return chr(int(g, 8) & 0xFF)
return _ANSI_SIMPLE.get(g, g)
return _ANSI_ESC.sub(one, raw)
class _Lexer:
def __init__(self, s: str, out: "list[_Cmd]", bud: _Budget, depth: int,
spans: "Optional[list]" = None, heredocs: "Optional[list]" = None) -> None:
self.s = s
self.n = len(s)
self.out = out
self.bud = bud
self.depth = depth
self.spans = spans if spans is not None else [] # quoted/comment/heredoc regions
self.heredocs = heredocs if heredocs is not None else [] # waiting for their body
# -- nested constructs ---------------------------------------------------------------------
def sub(self, pos: int, dollar: bool = True) -> int:
"""Lex a `$(`, `<(` or `>(` region in place; returns the index after its `)`.
`$((` is arithmetic when it closes with `))` (its `<<` is a shift, not a heredoc)."""
if self.depth >= MAX_DEPTH:
raise _Unparseable("nesting too deep")
if dollar and self.s.startswith("(", pos):
end = self.arith(pos + 1, False)
if end >= 0:
return end
child = _Lexer(self.s, self.out, self.bud, self.depth + 1, self.spans, self.heredocs)
return child.run(pos, True)
def arith(self, pos: int, semis: bool) -> int:
"""`$(( ... ))` / `(( ... ))`: `pos` is just after the opening pair. Returns the index
after the closing `))`, or -1 when the text is not arithmetic (then it is parsed as
nested subshells). Two phases keep the work linear: a flat scan decides whether the
text IS arithmetic (no recursion, so a failed guess costs one pass); only then are the
commands inside `$(...)` / backticks lexed, with `<` and `>` as plain characters."""
end = self._arith_end(pos, semis)
if end < 0:
return -1
content = self.s[pos:end - 2]
if self.depth >= MAX_DEPTH:
raise _Unparseable("nesting too deep")
self.bud.spend(len(content) + 1)
_Lexer(content, self.out, self.bud, self.depth + 1).run(0, False, True)
return end
def _arith_end(self, pos: int, semis: bool) -> int:
s, n = self.s, self.n
i, depth = pos, 2
result = -1
while i < n:
m = _ARITH_RUN.match(s, i)
if m:
i = m.end()
if i >= n:
break
c = s[i]
if c == "(":
depth += 1
i += 1
elif c == ")":
depth -= 1
if depth == 1: # first of the closing pair
if s.startswith(")", i + 1):
result = i + 2
break
i += 1
elif c == "$":
i += 1
elif c == "`":
j = s.find("`", i + 1)
if j < 0:
break
i = j + 1
elif c == "'" or c == '"':
j = s.find(c, i + 1)
if j < 0:
break
i = j + 1
elif c == "\\":
i += 2
elif c == ";" and semis:
i += 1
else: # newline, or `;` in `$(( ))`
break
self.bud.spend(min(i, n) - pos + 1)
return result
def backtick(self, pos: int) -> int:
m = _BT_END.match(self.s, pos + 1)
if m is None:
raise _Unparseable("unterminated backtick")
code = _BT_ESC.sub(r"\1", self.s[pos + 1:m.end() - 1])
self._program(code)
return m.end()
def _program(self, code: str) -> None:
if self.depth >= MAX_DEPTH:
raise _Unparseable("nesting too deep")
self.bud.spend(len(code) + 1)
_Lexer(code, self.out, self.bud, self.depth + 1).run(0, False)
def param(self, pos: int) -> "tuple[str, int]":
"""`${...}` (literal text kept, e.g. `${HOME}`); commands inside it are collected."""
s, n = self.s, self.n
i, depth = pos + 2, 1
while i < n:
m = _PARAMRUN.match(s, i)
if m:
i = m.end()
if i >= n:
break
c = s[i]
if c == "}":
depth -= 1
i += 1
if depth == 0:
return s[pos:i], i
elif c == "{":
depth += 1
i += 1
elif c == "\\":
i += 2
elif c == "'":
j = s.find("'", i + 1)
if j < 0:
raise _Unparseable("unterminated quote")
i = j + 1
elif c == '"':
_t, i = self.dq(i + 1)
elif c == "$" and s.startswith("$(", i):
i = self.sub(i + 2)
elif c == "`":
i = self.backtick(i)
else:
i += 1
raise _Unparseable("unterminated ${")
def dq(self, pos: int) -> "tuple[str, int]":
"""Body of a double-quoted string starting at `pos` (just after the opening quote)."""
s, n = self.s, self.n
start = pos - 1
buf: "list[str]" = []
while True:
m = _DQRUN.match(s, pos)
if m:
buf.append(m.group())
pos = m.end()
if pos >= n:
raise _Unparseable("unterminated double quote")
c = s[pos]
if c == '"':
pos += 1
break
if c == "\\":
nx = s[pos + 1:pos + 2]
if nx and nx in '$`"\\':
buf.append(nx)
pos += 2
elif nx == "\n":
pos += 2
else:
buf.append("\\")
pos += 1
elif c == "$":
nx = s[pos + 1:pos + 2]
if nx == "(":
pos = self.sub(pos + 2)
buf.append("$()")
elif nx == "{":
text, pos = self.param(pos)
buf.append(text)
else:
buf.append("$")
pos += 1
else: # backtick
pos = self.backtick(pos)
buf.append("`")
self.spans.append((start, pos))
return "".join(buf), pos
def ansi(self, pos: int) -> "tuple[str, int]":
m = _ANSI_END.match(self.s, pos)
if m is None:
raise _Unparseable("unterminated $'")
self.spans.append((pos - 2, m.end()))
return _decode_ansi(self.s[pos:m.end() - 1]), m.end()
def consume_heredocs(self, pos: int) -> int:
"""Read the bodies of the heredocs opened on the line that just ended."""
s, n = self.s, self.n
for hd in self.heredocs:
m = _heredoc_end(hd[1] or "", hd[0] == "<<-").search(s, pos)
if m is None:
body, end = s[pos:], n
else:
body, end = s[pos:m.start()], m.end()
if end < n and s[end] == "\n":
end += 1
hd[2] = body
self.spans.append((pos, end))
if not hd[3] and ("$(" in body or "`" in body):
# an unquoted delimiter: the body is expanded, so its substitutions RUN
self.bud.spend(len(body) + 1)
_Lexer(body, self.out, self.bud, self.depth + 1).expansions_only()
pos = end
self.heredocs.clear()
return pos
def expansions_only(self) -> None:
if self.depth >= MAX_DEPTH:
raise _Unparseable("nesting too deep")
s, pos = self.s, 0
while True:
m = _EXPANSION.search(s, pos)
if m is None:
return
t = m.group()
if t == "$(":
pos = self.sub(m.end())
elif t == "`":
pos = self.backtick(m.start())
else:
pos = m.end()
# -- the main loop -------------------------------------------------------------------------
def run(self, pos: int, until_paren: bool, arith: bool = False) -> int:
s, n, out = self.s, self.n, self.out
spans, heredocs = self.spans, self.heredocs
parts: "list[str]" = []
inword = False
quoted = False
cmd = _Cmd()
pending: "Optional[list]" = None # a redirect operator waiting for its target word
cases: "list[str]" = [] # case state: subject / pattern / body
paren = 0
fn_name_next = False
def flush_word() -> None:
nonlocal inword, quoted, pending, fn_name_next
if not inword:
return
w = parts[0] if len(parts) == 1 else "".join(parts)
parts.clear()
q = quoted
inword = quoted = False
if pending is not None:
pending[1] = w
if pending[0] == "<<" or pending[0] == "<<-":
pending[3] = q
heredocs.append(pending)
pending = None
return
words = cmd.words
if not words and not q:
if fn_name_next:
fn_name_next = False
return
if w in _DROP:
if w == "esac" and cases and cases[-1] != "subject":
cases.pop()
return
if w == "function":
fn_name_next = True
return
if w == "case":
cases.append("subject") # `for x in a b` / `case x in` headers: their
# first word (`for`, `case`) is no known command
elif cases and cases[-1] == "subject" and w == "in" and not q:
cases[-1] = "pattern"
words.append(w)
def end_cmd(pipe: bool = False) -> None:
nonlocal cmd, pending
flush_word()
pending = None
nxt = None
if cmd.words or cmd.redirs:
out.append(cmd)
if pipe:
nxt = cmd
cmd = _Cmd(nxt)
while pos < n:
m = _RUNWS.match(s, pos)
if m is not None:
chunk = m.group(1)
if chunk[0] == "#" and not inword:
e = s.find("\n", pos)
if e < 0:
e = n
spans.append((pos, e))
pos = e
continue
parts.append(chunk)
inword = True
pos = m.end()
if m.end(1) != pos: # whitespace follows: the word is complete
flush_word()
continue
c = s[pos]
if c in " \t\r\f\v":
flush_word()
pos = _WS.match(s, pos).end()
elif c == "\n":
end_cmd()
pos += 1
if heredocs:
pos = self.consume_heredocs(pos)
elif c == "'":
j = s.find("'", pos + 1)
if j < 0:
raise _Unparseable("unterminated single quote")
parts.append(s[pos + 1:j])
inword = quoted = True
spans.append((pos, j + 1))
pos = j + 1
elif c == '"':
text, pos = self.dq(pos + 1)
parts.append(text)
inword = quoted = True
elif c == "\\":
nx = s[pos + 1:pos + 2]
if nx == "\n":
pos += 2 # line continuation
elif nx == "":
parts.append("\\")
inword = True
pos += 1
else:
parts.append(nx)
inword = quoted = True
pos += 2
elif c == "$":
nx = s[pos + 1:pos + 2]
if nx == "(":
pos = self.sub(pos + 2)
parts.append("$()")
elif nx == "{":
text, pos = self.param(pos)
parts.append(text)
elif nx == "'":
text, pos = self.ansi(pos + 2)
parts.append(text)
quoted = True
elif nx == '"':
text, pos = self.dq(pos + 2)
parts.append(text)
quoted = True
else:
parts.append("$")
pos += 1
inword = True
elif c == "`":
pos = self.backtick(pos)
parts.append("`")
inword = True
elif c == ";":
if s.startswith(";;&", pos):
pos += 3
if cases and cases[-1] == "body":
cases[-1] = "pattern"
elif s.startswith(";;", pos) or s.startswith(";&", pos):
pos += 2
if cases and cases[-1] == "body":
cases[-1] = "pattern"
else:
pos += 1
end_cmd()
elif c == "|":
if s.startswith("||", pos):
pos += 2
end_cmd()
elif s.startswith("|&", pos):
pos += 2
end_cmd(True)
else:
pos += 1
if cases and cases[-1] == "pattern": # `a|b)` — alternatives, not a pipe
flush_word()
cmd = _Cmd()
pending = None
else:
end_cmd(True)
elif c == "&":
if s.startswith("&&", pos):
pos += 2
end_cmd()
elif s.startswith("&>", pos):
flush_word()
op = "&>>" if s.startswith("&>>", pos) else "&>"
pending = [op, None, None, False]
cmd.redirs.append(pending)
pos += len(op)
else:
pos += 1
end_cmd()
elif (c == "<" or c == ">") and arith: # arithmetic: shifts and comparisons
parts.append(c)
inword = True
pos += 1
elif c == "<" or c == ">":
nx = s[pos + 1:pos + 2]
if nx == "(": # process substitution
pos = self.sub(pos + 2, False)
parts.append("<()")
inword = True
continue
if inword and not quoted and len(parts) == 1 and parts[0].isdigit():
parts.clear() # `2>file`: the digit is an fd
inword = False
else:
flush_word()
if c == "<":
if s.startswith("<<<", pos):
op = "<<<"
elif s.startswith("<<-", pos):
op = "<<-"
elif nx == "<":
op = "<<"
elif nx == "&":
op = "<&"
elif nx == ">":
op = "<>"
else:
op = "<"
elif nx == ">":
op = ">>"
elif nx == "&":
op = ">&"
elif nx == "|":
op = ">|"
else:
op = ">"
pos += len(op)
pending = [op, None, None, False]
cmd.redirs.append(pending)
elif c == "(":
flush_word()
if s.startswith("((", pos) and not cmd.words and not cmd.redirs:
end = self.arith(pos + 2, True) # `(( i < 5 ))`, `(( x = 1 << 3 ))`
if end >= 0:
pos = end
continue
if not (cases and cases[-1] == "pattern"):
end_cmd()
paren += 1
pos += 1
else: # ")"
pos += 1
if cases and cases[-1] == "pattern":
flush_word()
cmd = _Cmd()
pending = None
cases[-1] = "body"
elif paren > 0:
end_cmd()
paren -= 1
else:
end_cmd()
if until_paren:
return pos
end_cmd()
return n
# ─────────────────────────────────────── fork bomb ─────────────────────────────────────────────
_FN_OPEN = re.compile(r"\(\s*\)\s*\{")
_FN_NAME = re.compile(r"([^\s;&|(){}<>'\"`]{1,40})\s*$")
def _forkbomb(text: str, spans: "list[tuple[int, int]]") -> "Optional[Finding]":
"""``name(){ name|name& };name`` in the CODE part of the text (quotes, comments and heredoc
bodies are blanked first, so a quoted mention is data)."""
if "{" not in text or "&" not in text or "|" not in text or "(" not in text:
return None
if spans:
spans = sorted(spans)
pieces, last = [], 0
for a, b in spans:
if a >= last:
pieces.append(text[last:a])
pieces.append(" ")
last = b
elif b > last:
last = b
pieces.append(text[last:])
text = "".join(pieces)
checked = 0
for m in _FN_OPEN.finditer(text):
checked += 1
if checked > 64:
break
nm = _FN_NAME.search(text, max(0, m.start() - 48), m.start())
if nm is None:
continue
esc = re.escape(nm.group(1))
window = text[m.end():m.end() + 400]
if re.search(esc + r"\s*\|\s*" + esc + r"\s*&[^}]{0,200}\}\s*;\s*" + esc, window):
return _f("fork-bomb")
return None
# ─────────────────────────────────────── rule helpers ──────────────────────────────────────────
_ASSIGN = re.compile(r"[A-Za-z_][A-Za-z0-9_]*\+?=")
_ROOTISH = re.compile(r"(?:~|\$HOME|\$\{HOME\}|/)/*\**")
_SSH_PATH = re.compile(r"(?:~|\$\{?HOME\}?)/\.ssh(?![A-Za-z0-9_])")
_ROOT_MODE = re.compile(r"777|000|a[+=]rwx")
_OPEN_OCTAL = re.compile(r"[0-7]{2,3}[1-7]")
_OPEN_SYMBOLIC = re.compile(r"[ugoa]*[+=][^\s]*[rwx]")
_DD_OF = re.compile(r"of=/dev/(?!null\b|zero\b)")
_MKFS = re.compile(r"mkfs(?:\.\w+)?")
_DOCKER_VALUE = frozenset({"-H", "--host", "-c", "--context", "-l", "--log-level", "--config",
"--tlscacert", "--tlscert", "--tlskey"})
_SSH_VERBS = frozenset({"rm", "rmdir", "mv", "shred", "truncate", "chown", "tee"})
_SHELLS = frozenset({"sh", "bash", "dash", "zsh", "ksh", "ash", "mksh", "csh", "tcsh", "fish",
"rbash"})
def _cmd_name(w: str) -> str:
return w.rpartition("/")[2]
def _check_assignment(word: str) -> "Optional[Finding]":
"""`NAME=value` as a prefix assignment, an `export`/`env` operand, ..."""
eq = word.find("=")
name = word[:eq].rstrip("+")
val = word[eq + 1:]
if name == "SKIP_SECRET_SCAN":
if val != "":
return _f("skip-secret-scan")
elif name.startswith("GIT_CONFIG_"):
low = val.lower()
if (name.startswith("GIT_CONFIG_KEY_") and low.strip() == "core.hookspath") or \
(name == "GIT_CONFIG_PARAMETERS" and "core.hookspath" in low):
return _f("git-hookspath")
return None
# -- git ---------------------------------------------------------------------------------------
_GIT_GLOBAL_VALUE = frozenset({"-C", "--git-dir", "--work-tree", "--namespace", "--super-prefix",
"--attr-source"})
# Subcommands that take --no-verify (git 2.47 `git <sub> -h`; cherry-pick does not today, but
# a hook-skipping flag there could only ever be an attempt, so it is denied too).
_NOVERIFY_SUBS = frozenset({"commit", "push", "merge", "am", "cherry-pick", "rebase"})
# `-n` IS --no-verify only for these two: `git commit -n` and `git am -n` ("bypass pre-applypatch
# and applypatch-msg hooks", see `git am -h`). Everywhere else it is something harmless:
# push -n = --dry-run, merge/rebase -n = --no-stat, cherry-pick -n = --no-commit.
_N_IS_NOVERIFY = frozenset({"commit", "am"})
# per subcommand: (long options taking a separate value, short options taking a value,
# short options whose value is optional and must be stuck to the flag)
_GIT_OPTS = {
"commit": (frozenset({"--message", "--file", "--reuse-message", "--reedit-message",
"--author", "--date", "--template", "--fixup", "--squash",
"--cleanup", "--trailer", "--pathspec-from-file"}),
frozenset("mFCct"), frozenset("uS")),
"push": (frozenset({"--push-option", "--receive-pack", "--exec", "--repo"}),
frozenset("o"), frozenset()),
"merge": (frozenset({"--message", "--file", "--strategy", "--strategy-option",
"--into-name", "--cleanup"}),
frozenset("mFsX"), frozenset("S")),
"am": (frozenset({"--whitespace", "--directory", "--exclude", "--include", "--patch-format",
"--resolvemsg", "--empty", "--quoted-cr"}),
frozenset("Cp"), frozenset("S")),
"cherry-pick": (frozenset({"--mainline", "--strategy", "--strategy-option"}),
frozenset("mX"), frozenset("S")),
"rebase": (frozenset({"--onto", "--strategy", "--strategy-option", "--exec", "--whitespace"}),
frozenset("sXxC"), frozenset("S")),
}
_CFG_READ = frozenset({"--get", "--get-all", "--get-regexp", "--get-urlmatch", "--unset",
"--unset-all", "--list", "-l", "--remove-section", "--rename-section",
"--edit", "-e", "--get-color", "--get-colorbool"})
_CFG_VALUE = frozenset({"-f", "--file", "--blob", "--type", "--default", "--comment"})
def _is_hookspath_key(v: str) -> bool:
return v.split("=", 1)[0].strip().lower() == "core.hookspath"
def _is_noverify_long(t: str) -> bool:
# git accepts any unambiguous prefix of a long option. Prefixes shorter than `--no-veri`
# collide with --no-verbose and git rejects them, so denying from `--no-v` up costs nothing.
return len(t) >= 6 and "--no-verify".startswith(t)
def _git_noverify(sub: str, rest: "list[str]") -> "Optional[Finding]":
long_val, short_val, short_opt = _GIT_OPTS[sub]
n_flag = sub in _N_IS_NOVERIFY
skip = False
for t in rest:
if skip:
skip = False
continue
if t == "--":
break
if len(t) < 2 or t[0] != "-":
continue
if t[1] == "-":
if _is_noverify_long(t):
return _f("git-skip-hooks")
if t in long_val:
skip = True
continue
body = t[1:]
last = len(body) - 1
for k, ch in enumerate(body):
if ch == "n" and n_flag:
return _f("git-skip-hooks")
if ch in short_val: # the rest of the cluster (or the next word) is its value
skip = k == last
break
if ch in short_opt: # optional value, stuck to the flag
break
return None
def _git_push_force(rest: "list[str]") -> "Optional[Finding]":
force = lease = remote_by_flag = False
pos: "list[str]" = []
opts = True
i, n = 0, len(rest)
while i < n:
t = rest[i]
i += 1
if opts and t == "--":
opts = False
continue
if opts and len(t) > 1 and t[0] == "-":
if t[1] == "-":
name, eq, _v = t.partition("=")
if name == "--force":
force = True
elif name == "--force-with-lease":
lease = True
elif name == "--repo":
remote_by_flag = True
if not eq:
i += 1
elif name in ("--push-option", "--receive-pack", "--exec") and not eq:
i += 1
else:
last = len(t) - 1
for k in range(1, len(t)):
ch = t[k]
if ch == "f":
force = True
elif ch == "o": # `-o <option>`: value, not more flags
if k == last:
i += 1
break
continue
pos.append(t)
refspecs = pos if remote_by_flag else pos[1:]
if not refspecs:
# an unqualified forced push targets the current branch, which in this workflow
# (one branch, master) is the protected one
return _f("git-force-push-protected") if (force or lease) else None
for r in refspecs:
plus = r[0] == "+" and len(r) > 1
if not (force or lease or plus):
continue
body = r[1:] if r[0] == "+" else r
dst = body.partition(":")[2] if ":" in body else body
if dst.startswith("refs/heads/"):
dst = dst[11:]
if dst == "master" or dst == "main":
return _f("git-force-push-protected")
return None
def _git_config(rest: "list[str]") -> "Optional[Finding]":
pos: "list[str]" = []
read = False
i, n = 0, len(rest)
while i < n:
t = rest[i]
i += 1
if t == "--":
pos.extend(rest[i:])
break
if len(t) > 1 and t[0] == "-":
if t in _CFG_READ:
read = True
elif t in _CFG_VALUE:
i += 1
continue
pos.append(t)
if read:
return None
if pos and pos[0] == "set": # `git config set <key> <value>` (git >= 2.46)
pos = pos[1:]
elif pos and pos[0] in ("get", "unset", "list", "edit", "rename-section", "remove-section"):
return None
if len(pos) >= 2 and _is_hookspath_key(pos[0]):
return _f("git-hookspath")
return None
def _h_git(args: "list[str]") -> "Optional[Finding]":
i, n = 0, len(args)
hookspath = False
while i < n: # global options before the subcommand
t = args[i]
if t == "-c" or t == "--config-env":
if i + 1 < n and _is_hookspath_key(args[i + 1]):
hookspath = True
i += 2
elif t.startswith("--config-env="):
if _is_hookspath_key(t[13:]):
hookspath = True
i += 1
elif t in _GIT_GLOBAL_VALUE:
i += 2
elif len(t) > 1 and t[0] == "-":
i += 1
else:
break
if hookspath:
return _f("git-hookspath")
if i >= n:
return None
sub, rest = args[i], args[i + 1:]
if sub == "config":
return _git_config(rest)
if sub == "reset":
for t in rest:
if t == "--":
break
if len(t) >= 4 and "--hard".startswith(t): # --ha / --har / --hard
return _f("git-hard-reset")
return None
if sub in _NOVERIFY_SUBS:
f = _git_noverify(sub, rest)
if f is None and sub == "push":
f = _git_push_force(rest)
return f
return None
# -- everything else ---------------------------------------------------------------------------
def _h_rm(args: "list[str]") -> "Optional[Finding]":
rec = force = False
targets: "list[str]" = []
opts = True
for a in args:
if opts and a == "--":
opts = False
elif opts and len(a) > 1 and a[0] == "-":
if a[1] == "-":
if len(a) >= 3 and "--recursive".startswith(a):
rec = True
elif len(a) >= 3 and "--force".startswith(a):
force = True
else:
if "r" in a or "R" in a:
rec = True
if "f" in a:
force = True
else:
targets.append(a)
if rec and force:
for t in targets:
if _ROOTISH.fullmatch(t):
return _f("rm-root-home")
return None
def _h_chmod(args: "list[str]") -> "Optional[Finding]":
rec = False
pos: "list[str]" = []
opts = True
for a in args:
if opts and a == "--":
opts = False
elif opts and len(a) > 1 and a[0] == "-":
if a[1] == "-":
if len(a) >= 3 and "--recursive".startswith(a):
rec = True
elif "R" in a:
rec = True
else:
pos.append(a)
if rec and any(_ROOT_MODE.search(p) for p in pos) and any(_ROOTISH.fullmatch(p) for p in pos):
return _f("chmod-root")
if any(_SSH_PATH.search(p) for p in pos):
for p in pos:
if "/" in p or _SSH_PATH.search(p):
continue
if _OPEN_OCTAL.fullmatch(p) or _OPEN_SYMBOLIC.search(p):
return _f("ssh-dir-chmod-open")
return None
def _h_ssh_verb(args: "list[str]") -> "Optional[Finding]":
for a in args:
if _SSH_PATH.search(a):
return _f("ssh-dir-mutation")
return None
def _h_sed(args: "list[str]") -> "Optional[Finding]":
for a in args:
if a.startswith("--in-place") or (len(a) > 1 and a[0] == "-" and a[1] != "-" and "i" in a):
return _h_ssh_verb(args)
return None
def _h_docker(args: "list[str]") -> "Optional[Finding]":
pos: "list[str]" = []
i, n = 0, len(args)
while i < n and len(pos) < 2:
a = args[i]
if len(a) > 1 and a[0] == "-":
i += 2 if a in _DOCKER_VALUE else 1
else:
pos.append(a)
i += 1
return _f("docker-system-prune") if pos == ["system", "prune"] else None
def _h_dd(args: "list[str]") -> "Optional[Finding]":
for a in args:
if _DD_OF.match(a):
return _f("dd-block-device")
return None
def _h_declare(args: "list[str]") -> "Optional[Finding]":
for a in args:
if _ASSIGN.match(a):
f = _check_assignment(a)
if f:
return f
return None
# -- wrappers: `<wrapper> [options] cmd args` -> the command behind them ------------------------
def _w_generic(value_opts: "frozenset[str]" = frozenset(), npos: int = 0):
def fn(words, i, ctx):
j, n = i + 1, len(words)
while j < n:
t = words[j]
if t == "--":
j += 1
break
if len(t) > 1 and t[0] == "-":
j += 2 if t in value_opts else 1
else:
break
j += npos
return j if j < n else None