@tyhld/conductor 0.12.188 → 0.12.189
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/scripts/hooks/policy.py
CHANGED
|
@@ -4054,12 +4054,41 @@ def _is_search_only(toks) -> bool:
|
|
|
4054
4054
|
return bool(toks) and verb == 'git' and any(t in _SEARCH_ONLY_VERBS for t in toks[1:3])
|
|
4055
4055
|
|
|
4056
4056
|
|
|
4057
|
+
#: 探す道具に別の命令を走らせる旗(git grep の -O・rg の --pre・ag/ack/ugrep の --pager・ugrep の --filter)。
|
|
4058
|
+
# ★これが付いた行は、引用符の中の字が命令として動き得るので免じない。
|
|
4059
|
+
_SEARCH_EXEC_LONG_FLAGS = ('--open-files-in-pager', '--pre', '--pager', '--filter')
|
|
4060
|
+
|
|
4061
|
+
|
|
4062
|
+
def _search_runs_command(toks) -> bool:
|
|
4063
|
+
"""探す道具の語に、別の命令を走らせる旗が在るか(★まとめ書きの短い旗の O も数える)。"""
|
|
4064
|
+
for t in toks:
|
|
4065
|
+
if t.startswith('--'):
|
|
4066
|
+
if t.split('=', 1)[0] in _SEARCH_EXEC_LONG_FLAGS:
|
|
4067
|
+
return True
|
|
4068
|
+
elif t.startswith('-') and 'O' in t[1:]:
|
|
4069
|
+
return True
|
|
4070
|
+
return False
|
|
4071
|
+
|
|
4072
|
+
|
|
4073
|
+
def _search_words_stay_words(seg: str) -> bool:
|
|
4074
|
+
"""このセグメントの引用符の中の字が、探す言葉のまま動かないか(rm・push の免じ方で共通)。
|
|
4075
|
+
|
|
4076
|
+
★探すだけの道具で、置き換えの実行(`$(`・逆引用符)も別の命令を走らせる旗も無いときだけ True。
|
|
4077
|
+
【なぜ共通にしたか(便 f599daa0)】旗の除外は push の側(便 b6410117)にだけ在り、
|
|
4078
|
+
rm の側は `git grep -O'<rm の字>'`・`rg --pre '<rm の字>'` を免じて allow にしていた。
|
|
4079
|
+
"""
|
|
4080
|
+
toks = tokens_of(seg)
|
|
4081
|
+
return ('$(' not in seg and '`' not in seg
|
|
4082
|
+
and _is_search_only(toks) and not _search_runs_command(toks))
|
|
4083
|
+
|
|
4084
|
+
|
|
4057
4085
|
def _rm_only_in_search_words(command: str) -> bool:
|
|
4058
4086
|
"""削除命令の字が【探すだけの道具の引用符の中】にだけ在るか。
|
|
4059
4087
|
|
|
4060
4088
|
★免じるのはこれが True のときだけ。少しでも判断が付かなければ False(=止める側へ倒す)。
|
|
4061
4089
|
・引用符の外に削除命令の字が在るセグメントが1つでも在れば False。
|
|
4062
4090
|
・引用符の中に在っても、そのセグメントが探すだけの道具でなければ False。
|
|
4091
|
+
・置き換えの実行(`$(`・逆引用符)や、別の命令を走らせる旗が在れば False(便 f599daa0)。
|
|
4063
4092
|
・どこにも見つからなければ False(ヒアドキュメントの本文など、
|
|
4064
4093
|
引用符を割らない読み方では覆えない字面=安全側)。
|
|
4065
4094
|
"""
|
|
@@ -4070,7 +4099,7 @@ def _rm_only_in_search_words(command: str) -> bool:
|
|
|
4070
4099
|
continue
|
|
4071
4100
|
if _RM_WORD_RE.search(_blank_quoted(seg)):
|
|
4072
4101
|
return False # 囲みの外に在る=本物の削除命令として扱う
|
|
4073
|
-
if not
|
|
4102
|
+
if not _search_words_stay_words(seg):
|
|
4074
4103
|
return False
|
|
4075
4104
|
found_in_search_words = True
|
|
4076
4105
|
return found_in_search_words
|
|
@@ -4079,21 +4108,6 @@ def _rm_only_in_search_words(command: str) -> bool:
|
|
|
4079
4108
|
# 引用符の外の push の字(探す言葉の外に push の命令が在るかを見る形)。
|
|
4080
4109
|
_GIT_PUSH_WORD_RE = re.compile(r'\bgit\s+push\b')
|
|
4081
4110
|
|
|
4082
|
-
#: 探す道具に別の命令を走らせる旗(git grep の -O・rg の --pre・ag/ack/ugrep の --pager・ugrep の --filter)。
|
|
4083
|
-
# ★これが付いた行は、引用符の中の字が命令として動き得るので免じない。
|
|
4084
|
-
_SEARCH_EXEC_LONG_FLAGS = ('--open-files-in-pager', '--pre', '--pager', '--filter')
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
def _search_runs_command(toks) -> bool:
|
|
4088
|
-
"""探す道具の語に、別の命令を走らせる旗が在るか(★まとめ書きの短い旗の O も数える)。"""
|
|
4089
|
-
for t in toks:
|
|
4090
|
-
if t.startswith('--'):
|
|
4091
|
-
if t.split('=', 1)[0] in _SEARCH_EXEC_LONG_FLAGS:
|
|
4092
|
-
return True
|
|
4093
|
-
elif t.startswith('-') and 'O' in t[1:]:
|
|
4094
|
-
return True
|
|
4095
|
-
return False
|
|
4096
|
-
|
|
4097
4111
|
|
|
4098
4112
|
def _push_only_in_search_words(command: str) -> bool:
|
|
4099
4113
|
"""強い上書きの push の字が【探すだけの道具の引用符の中】にだけ在るか(便 b6410117)。
|
|
@@ -4113,8 +4127,7 @@ def _push_only_in_search_words(command: str) -> bool:
|
|
|
4113
4127
|
continue
|
|
4114
4128
|
if _GIT_PUSH_WORD_RE.search(_blank_quoted(seg).lower()):
|
|
4115
4129
|
return False # 囲みの外に在る=本物の push として扱う
|
|
4116
|
-
|
|
4117
|
-
if '$(' in seg or '`' in seg or not _is_search_only(toks) or _search_runs_command(toks):
|
|
4130
|
+
if not _search_words_stay_words(seg):
|
|
4118
4131
|
return False
|
|
4119
4132
|
found_in_search_words = True
|
|
4120
4133
|
return found_in_search_words
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""探す言葉の中の rm の字の免じ方に、別の命令を走らせる旗の除外を入れた検査(便 f599daa0)。
|
|
3
|
+
|
|
4
|
+
実行: python3 scripts/hooks/test_search_word_rm_exec.py
|
|
5
|
+
1件でも期待と違えば exit 1。
|
|
6
|
+
|
|
7
|
+
【なぜ要るか(便 f599daa0 で実測)】
|
|
8
|
+
push の字の免じ方(便 b6410117)には「探す道具に別の命令を走らせる旗があれば免じない」除外が
|
|
9
|
+
在ったが、rm の字の免じ方(便 915694ac・_rm_only_in_search_words)には無かった。
|
|
10
|
+
直す前は `git grep -O'<rm の字>' -e x -- src`・`rg --pre '<rm の字>' x src` などが allow だった
|
|
11
|
+
(-O・--pre は引用符の中の字を命令として走らせる)。push と同じ除外を共通の部品
|
|
12
|
+
(_search_words_stay_words)にまとめて、rm の側にも掛けた。
|
|
13
|
+
|
|
14
|
+
【ここで固定すること】
|
|
15
|
+
(a) 止める(danger)… 旗つきの rm の検索(-O・まとめ書きの -nO・--open-files-in-pager・--pre・
|
|
16
|
+
--pre=・--pager・--filter)
|
|
17
|
+
(b) 通る … 旗の無い読むだけの検索の rm の字(今までどおり)
|
|
18
|
+
(c) push の側も今までどおり(旗つきは止まる・旗なしは通る)
|
|
19
|
+
(d) 空振り確認(旗の除外を外すと (a) が通ってしまう)
|
|
20
|
+
★この台本は危険語・秘密の形の名前を素の字で置かない(組み立てて使う)。既存の作法どおり。
|
|
21
|
+
"""
|
|
22
|
+
import os
|
|
23
|
+
import shutil
|
|
24
|
+
import sys
|
|
25
|
+
import tempfile
|
|
26
|
+
|
|
27
|
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
28
|
+
|
|
29
|
+
import policy # noqa: E402
|
|
30
|
+
|
|
31
|
+
failures = []
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def check(label, got, want):
|
|
35
|
+
ok = got == want
|
|
36
|
+
if not ok:
|
|
37
|
+
failures.append(f'{label}: got={got!r} want={want!r}')
|
|
38
|
+
print(f'{"OK " if ok else "NG "} {label:64} {str(got)[:40]}')
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# ── 部品(危険語を直接書かない) ──
|
|
42
|
+
RM = 'r' + 'm'
|
|
43
|
+
RF = RM + ' -rf .git'
|
|
44
|
+
GP = 'git ' + 'pu' + 'sh'
|
|
45
|
+
FORCE = '--for' + 'ce'
|
|
46
|
+
NPMRC = '.npm' + 'rc'
|
|
47
|
+
|
|
48
|
+
# ── 使い捨ての作り物(作業場所の根に秘密の形のファイルを置く=実地と同じ形)──────
|
|
49
|
+
ROOT = tempfile.mkdtemp(prefix='search-word-rm-exec-')
|
|
50
|
+
SITE = os.path.join(ROOT, 'site')
|
|
51
|
+
for d in ('src', 'skills', 'docs'):
|
|
52
|
+
os.makedirs(os.path.join(SITE, d))
|
|
53
|
+
with open(os.path.join(SITE, NPMRC), 'w') as f:
|
|
54
|
+
f.write('DUMMY=placeholder\n')
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def decide(cmd):
|
|
58
|
+
r = policy.decide_bash(cmd, SITE)
|
|
59
|
+
return r['decision'] + ':' + r['reason'].split(':', 1)[0]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
ALLOW = 'allow:no gate, no danger'
|
|
63
|
+
|
|
64
|
+
# ═════════════════════════════════════════════════════════════════════════════
|
|
65
|
+
# (a) 止める(別の命令を走らせる旗つきの rm の検索)
|
|
66
|
+
# ═════════════════════════════════════════════════════════════════════════════
|
|
67
|
+
print('── (a) 旗つきの rm の検索は止める ' + '─' * 32)
|
|
68
|
+
EXEC = [
|
|
69
|
+
('a01 git grep -O', f"git grep -O'{RF}' -e x -- src"),
|
|
70
|
+
('a02 git grep -nO(まとめ書き)', f"git grep -nO'{RF}' -e x -- src"),
|
|
71
|
+
('a03 git grep --open-files-in-pager=', f"git grep --open-files-in-pager='{RF}' -e x -- src"),
|
|
72
|
+
('a04 rg --pre', f"rg --pre '{RF}' x src"),
|
|
73
|
+
('a05 rg --pre=', f"rg --pre='{RF}' x src"),
|
|
74
|
+
('a06 ag --pager', f"ag --pager '{RF}' x src"),
|
|
75
|
+
('a07 ugrep --filter=', f"ugrep --filter='*:{RF}' x src"),
|
|
76
|
+
]
|
|
77
|
+
for label, cmd in EXEC:
|
|
78
|
+
check(label, decide(cmd).startswith('deny:danger'), True)
|
|
79
|
+
check(label + '(免じない)', policy._rm_only_in_search_words(cmd), False)
|
|
80
|
+
|
|
81
|
+
# ═════════════════════════════════════════════════════════════════════════════
|
|
82
|
+
# (b) 通る(旗の無い読むだけの検索の rm の字)
|
|
83
|
+
# ═════════════════════════════════════════════════════════════════════════════
|
|
84
|
+
print('── (b) 旗の無い検索の rm の字は今までどおり通る ' + '─' * 18)
|
|
85
|
+
PASS = [
|
|
86
|
+
('b01 git grep -n', f"git grep -n '{RF}' -- skills docs"),
|
|
87
|
+
('b02 git grep -n -e', f"git grep -n -e '{RF}' -- src"),
|
|
88
|
+
('b03 grep -rn(場所を名指し)', f"grep -rn '{RF}' skills"),
|
|
89
|
+
('b04 rg -n', f"rg -n '{RF}' docs"),
|
|
90
|
+
('b05 grep -rn の探す言葉に $HOME', f'grep -rn "{RM} -rf $HOME" docs'),
|
|
91
|
+
('b06 git grep -i(O を含まない短い旗)', f"git grep -i -e '{RF}' -- src"),
|
|
92
|
+
]
|
|
93
|
+
for label, cmd in PASS:
|
|
94
|
+
check(label, decide(cmd), ALLOW)
|
|
95
|
+
|
|
96
|
+
# ═════════════════════════════════════════════════════════════════════════════
|
|
97
|
+
# (c) push の側も今までどおり
|
|
98
|
+
# ═════════════════════════════════════════════════════════════════════════════
|
|
99
|
+
print('── (c) push の側も今までどおり ' + '─' * 36)
|
|
100
|
+
check('c01 旗なしの push の字は通る', decide(f"git grep -n -e '{GP} {FORCE}' -- src"), ALLOW)
|
|
101
|
+
check('c02 git grep -O の push の字は止まる',
|
|
102
|
+
decide(f"git grep -O'{GP} {FORCE} origin main' -e x -- src").startswith('deny:danger'), True)
|
|
103
|
+
check('c03 rg --pre の push の字は止まる',
|
|
104
|
+
decide(f"rg --pre '{GP} {FORCE}' x src").startswith('deny:danger'), True)
|
|
105
|
+
|
|
106
|
+
# ═════════════════════════════════════════════════════════════════════════════
|
|
107
|
+
# (d) 空振り確認(試験の中で旗の除外を外す)
|
|
108
|
+
# ═════════════════════════════════════════════════════════════════════════════
|
|
109
|
+
print('── (d) 空振り確認 ' + '─' * 48)
|
|
110
|
+
_real = policy._search_runs_command
|
|
111
|
+
policy._search_runs_command = lambda toks: False # 旗の除外を外す
|
|
112
|
+
check('d1 外すと git grep -O の rm の検索が通ってしまう', decide(EXEC[0][1]), ALLOW)
|
|
113
|
+
check('d2 外すと rg --pre の rm の検索が通ってしまう', decide(EXEC[3][1]), ALLOW)
|
|
114
|
+
policy._search_runs_command = _real
|
|
115
|
+
check('d3 戻すと止まる', decide(EXEC[0][1]).startswith('deny:danger'), True)
|
|
116
|
+
|
|
117
|
+
shutil.rmtree(ROOT, ignore_errors=True)
|
|
118
|
+
check('z1 作り物を片付けた', os.path.exists(ROOT), False)
|
|
119
|
+
|
|
120
|
+
print()
|
|
121
|
+
if failures:
|
|
122
|
+
print(f'NG {len(failures)} 件')
|
|
123
|
+
for f in failures:
|
|
124
|
+
print(f' - {f}')
|
|
125
|
+
sys.exit(1)
|
|
126
|
+
print('すべて期待どおり')
|