@tyhld/conductor 0.12.156 → 0.12.157

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tyhld/conductor",
3
- "version": "0.12.156",
3
+ "version": "0.12.157",
4
4
  "description": "采配くん管制の見守りアプリ(conductor-agent)。各PCで常駐し、中央(devlog-tracker)へ定期的に生存報告(heartbeat)を送る常駐CLI。",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1073,6 +1073,24 @@ def glued_harmless_redirect(target: str):
1073
1073
  return head, target[cut:].lstrip(_SUBST_CLOSERS)
1074
1074
 
1075
1075
 
1076
+ def harmless_device_with_tail(target: str) -> bool:
1077
+ """無害な捨て先の直後に、閉じでもパスの字でもない字が付いた形か(`/dev/null#` など)。
1078
+
1079
+ ★引用符を見ない拾い方(redirect_targets)で、引用符の中の文字が書き込み先に見えた形だけを
1080
+ 見分ける。直後が閉じ(`)` `"` …)の形は貼り付き(discipline・試験 a1〜a8)なので含めない。
1081
+ 直後がパスの字(英数字・`/` `.` `_` `-`)の形(`/dev/nullx`・`/dev/fd/..`)も含めない。
1082
+ ★/etc・/proc など無害な捨て先でない頭は含めない(引用符の中でも今までどおり止める)。
1083
+ """
1084
+ for head in _HARMLESS_DEVICES:
1085
+ if not target.startswith(head) or len(target) == len(head):
1086
+ continue
1087
+ ch = target[len(head)]
1088
+ if ch in _SUBST_CLOSERS or ch.isalnum() or ch in '/._-':
1089
+ continue
1090
+ return True
1091
+ return False
1092
+
1093
+
1076
1094
  def _unglue_redirects(text: str):
1077
1095
  """貼り付いた捨て先を「本当の捨て先」と「残りの語」に分け直した文と、その件数を返す。
1078
1096
 
@@ -1293,6 +1311,9 @@ def _wrapper_danger(command: str, depth: int = 0, cwd=None):
1293
1311
  or _admin_merge_danger(v, toks))
1294
1312
  if hit:
1295
1313
  return f'{hit} ({where})'
1314
+ hit = _real_redirect_danger(inner, cwd)
1315
+ if hit:
1316
+ return f'{hit} ({where})'
1296
1317
  # ★抜け道も包みの中で測り直す(`bash -c "git push origin main"` を素通りさせない)。
1297
1318
  hit = _bypass_scan(inner, cwd)
1298
1319
  if hit:
@@ -1386,7 +1407,8 @@ def _var_expansion_danger(command: str, cwd=None):
1386
1407
  or _admin_merge_danger(v, toks))
1387
1408
  if hit:
1388
1409
  return hit
1389
- hit = (_guard_write_scan(expanded, cwd) or _env_write_scan(expanded, cwd)
1410
+ hit = (_real_redirect_danger(expanded, cwd)
1411
+ or _guard_write_scan(expanded, cwd) or _env_write_scan(expanded, cwd)
1390
1412
  or _find_feed_danger(expanded, cwd) or _bypass_scan(expanded, cwd)
1391
1413
  or _wrapper_danger(expanded, 0, cwd))
1392
1414
  if hit:
@@ -3695,9 +3717,23 @@ def _secret_danger(segment: str, verb: str, tokens, cwd=None):
3695
3717
  syr = system_write_reason(a)
3696
3718
  if syr:
3697
3719
  return f'write to {syr}'
3720
+ # dd は書き込み先を `of=` で受ける(便 93e3c6d8・`dd of=<装置>` が素通りしていた)。
3721
+ # ★読み元(if=)はシステム領域でも止めない(読むだけ)。
3722
+ if verb == 'dd':
3723
+ for raw in args:
3724
+ if raw.startswith('of='):
3725
+ r = secret_reason(raw[3:], cwd) or system_write_reason(raw[3:])
3726
+ if r:
3727
+ return f'write to {r}'
3698
3728
 
3699
3729
  # リダイレクト先(> / >>)も「書き込む作用」。
3700
3730
  for t in redirect_targets(segment):
3731
+ # ★無害な捨て先の後ろに `#` などが付いただけの形(`/dev/null#`)は、ここでは測らない。
3732
+ # ここは引用符を見ないので、引用符の中の文字(sed の置き換え文字列など)まで書き込み先に
3733
+ # 見えてしまう(便 baeebe4e・93e3c6d8)。本物の書き込み先としての同じ形は、引用符を見る
3734
+ # _real_redirect_danger が今までどおり厳しく止める=危ない物は通らない。
3735
+ if harmless_device_with_tail(t):
3736
+ continue
3701
3737
  sr = secret_reason(t, cwd)
3702
3738
  if sr:
3703
3739
  return f'redirect into {sr}'
@@ -4311,6 +4347,28 @@ def _subst_payloads(text: str):
4311
4347
  return out
4312
4348
 
4313
4349
 
4350
+ def _real_redirect_danger(command: str, cwd=None, depth: int = 0):
4351
+ """引用符の外の本物の書き込み先(`>` `>>` `>|` `&>` `<>`)を、秘密・システム領域で厳しく測る。
4352
+
4353
+ ★`$(…)`・バッククォート・プロセス置換の中身も、1つのコマンドとして測り直す。
4354
+ ★止める側を足すだけ(今までの redirect_targets の判定は1つも外さない)。引用符を見ない側が
4355
+ 測らなくなった形(`/dev/null#` など・harmless_device_with_tail)も、本物の書き込み先ならここで止まる。
4356
+ """
4357
+ for seg in split_commands(command, bodies=False):
4358
+ s = _normalize_segment(seg)
4359
+ for t in _real_redirect_targets(s):
4360
+ r = secret_reason(t, cwd) or system_write_reason(t)
4361
+ if r:
4362
+ return f'redirect into {r}'
4363
+ if depth < _UNWRAP_MAX_DEPTH:
4364
+ head = s.split('\n', 1)[0] if '<<' in s else s
4365
+ for inner in _subst_payloads(head):
4366
+ hit = _real_redirect_danger(inner, cwd, depth + 1)
4367
+ if hit:
4368
+ return hit
4369
+ return None
4370
+
4371
+
4314
4372
  def _heredoc_body(head: str, command: str):
4315
4373
  """このコマンドの頭に書かれたヒアドキュメントの本文(無ければ None)。"""
4316
4374
  m = _HEREDOC_START_RE.search(head)
@@ -5820,6 +5878,10 @@ def _bash_danger(command: str, cwd=None):
5820
5878
  or _admin_merge_danger(v, toks))
5821
5879
  if hit:
5822
5880
  return hit
5881
+ # ★引用符の外の本物の書き込み先(便 93e3c6d8・`>|` もここで拾う)。
5882
+ hit = _real_redirect_danger(command, cwd)
5883
+ if hit:
5884
+ return hit
5823
5885
  hit = _find_feed_danger(command, cwd)
5824
5886
  if hit:
5825
5887
  return hit
@@ -0,0 +1,151 @@
1
+ #!/usr/bin/env python3
2
+ """引用符の中の捨て先を「/dev への書き込み」と取り違えない検査(便 baeebe4e → 93e3c6d8)。
3
+
4
+ 実行: python3 scripts/hooks/test_quoted_devnull.py
5
+ 1件でも期待と違えば exit 1。
6
+
7
+ 【なぜ要るか(実地 2026-10-30・ノートPC FshimaNP・写し kenzokun-demo)】
8
+ sed の置き換え文字列(=引用符の中)に `2>/dev/null#` と書いただけの命令が
9
+ `danger: redirect into system path (/dev)` で止まった。書き込み先を引用符を見ずに拾う
10
+ redirect_targets が、`/dev/null#` を書き込み先として取り出し、無害な捨て先の一覧に当たらなかったため。
11
+
12
+ 【ここで固定すること】
13
+ (a) 止めない側 … 前便で止まった命令の全文・引用符の中の捨て先・実際の出力先としての捨て先
14
+ (b) 止める側 … /dev の装置への書き込み(> >| &> >> tee dd of=)・引用符の中の /etc への書き込み・
15
+ 本物の書き込み先としての `/dev/null#…`(引用符の外・包みの中)
16
+ (c) 見分けの部品そのもの(harmless_device_with_tail)
17
+
18
+ ★この台本は装置名・捨て先を素の字で置かない(組み立てて使う)。職人の命令の字面に出さないため。
19
+ """
20
+ import os
21
+ import sys
22
+
23
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
24
+
25
+ import policy # noqa: E402
26
+
27
+ failures = []
28
+
29
+
30
+ def check(label, got, want):
31
+ ok = got == want
32
+ if not ok:
33
+ failures.append(f'{label}: got={got!r} want={want!r}')
34
+ print(f'{"OK " if ok else "NG "} {label:58} {str(got)[:46]}')
35
+
36
+
37
+ CWD = '/tmp/claude-1000'
38
+ DEV = '/dev/'
39
+ NULL = DEV + 'null'
40
+ STDOUT = DEV + 'stdout'
41
+ STDERR = DEV + 'stderr'
42
+ SDA = DEV + 'sd' + 'a'
43
+ NVME = DEV + 'nvme' + '0n1'
44
+ MEM = DEV + 'mem'
45
+ KMEM = DEV + 'k' + 'mem'
46
+ ETC = '/et' + 'c/'
47
+
48
+
49
+ def decide(cmd):
50
+ return policy.decide_bash(cmd, CWD)
51
+
52
+
53
+ def kind_of(reason):
54
+ return reason.split(':', 1)[0]
55
+
56
+
57
+ # ── 前便で止まった命令の全文(捨て先だけを組み立て形にした・ほかは1文字も変えていない)──
58
+ S_LINE = ('S=/tmp/claude-1000/-home-fshimanp-projects-kenzokun-demo/'
59
+ 'e8e147e2-5578-40be-a907-441951ea8959/scratchpad')
60
+ SED1 = ("sed -i 's#--no-sandbox --disable-gpu#--no-sandbox --disable-gpu "
61
+ "--user-data-dir=$TMPDIR/chrome-prof#g' $S/shoot.sh")
62
+ SED2 = "sed -i 's#^NEXT_PUBLIC_SUPABASE_URL=#setsid env NEXT_PUBLIC_SUPABASE_URL=#' $S/shoot.sh"
63
+ SED3 = ('sed -i "s#^kill \\$PID.*#kill -- -\\$PID 2>' + NULL
64
+ + '; sleep 1; kill -9 -- -\\$PID 2>' + NULL + '#" $S/shoot.sh')
65
+ LAST = "grep -n -E 'setsid|kill|user-data' $S/shoot.sh; PREFIX=before $S/shoot.sh"
66
+ REAL = '\n'.join([S_LINE, SED1, SED2, SED3, LAST])
67
+
68
+ # ═════════════════════════════════════════════════════════════════════════════
69
+ # (a) 止めない側
70
+ # ═════════════════════════════════════════════════════════════════════════════
71
+ print('── (a) 止めない側(allow) ' + '─' * 40)
72
+ ALLOW = [
73
+ ('a01 前便で止まった命令の全文', REAL),
74
+ ('a02 その3行目だけ', SED3),
75
+ ('a03 3行目を S の代入つきで', S_LINE + '\n' + SED3),
76
+ ('a04 単引用符の中の捨て先', "sed -i 's#a#b 2>" + NULL + "#' f.sh"),
77
+ ('a05 二重引用符の中の捨て先(後ろが空白)', 'echo "x 2>' + NULL + ' y"'),
78
+ ('a06 二重引用符の中の捨て先(後ろが #)', 'echo "a 2>' + NULL + '#b"'),
79
+ ('a07 標準出力の名前(引用符の中・後ろが #)', "sed -i 's#a#b >" + STDOUT + "#' f.sh"),
80
+ ('a08 実際の出力先 >捨て先', 'ls >' + NULL),
81
+ ('a09 実際の出力先 2>捨て先', 'ls 2>' + NULL),
82
+ ('a10 実際の出力先 &>捨て先', 'ls &>' + NULL),
83
+ ('a11 実際の出力先 >捨て先 2>&1', 'ls >' + NULL + ' 2>&1'),
84
+ ('a12 実際の出力先 標準出力', 'echo x >' + STDOUT),
85
+ ('a13 実際の出力先 標準エラー', 'echo x >' + STDERR),
86
+ ('a14 プロセスの組を止めて文句を捨てる', 'kill -- -123 2>' + NULL),
87
+ ('a15 包みの中の引用符の中の捨て先',
88
+ 'bash -c "sed -i \'s#a#b 2>' + NULL + '#\' f.sh"'),
89
+ ('a16 dd で捨て先へ書く', 'dd if=' + DEV + 'zero of=' + NULL + ' count=1'),
90
+ ('a17 dd でシステム領域から読むだけ', 'dd if=' + ETC + 'hostname of=/tmp/claude-1000/h'),
91
+ ]
92
+ for label, cmd in ALLOW:
93
+ r = decide(cmd)
94
+ check(label, (r['decision'], r['reason'][:60]), ('allow', 'no gate, no danger'))
95
+
96
+ # ═════════════════════════════════════════════════════════════════════════════
97
+ # (b) 止める側(danger のまま)
98
+ # ═════════════════════════════════════════════════════════════════════════════
99
+ print('── (b) 止める側(danger) ' + '─' * 41)
100
+ DANGER = [
101
+ ('b01 装置へ >', 'echo x > ' + SDA),
102
+ ('b02 装置へ >|', 'echo x >| ' + SDA),
103
+ ('b03 装置へ &>', 'echo x &> ' + SDA),
104
+ ('b04 装置へ >>', 'echo x >> ' + NVME),
105
+ ('b05 装置へ tee', 'echo x | tee ' + NVME),
106
+ ('b06 装置へ dd of=', 'dd if=' + DEV + 'zero of=' + SDA),
107
+ ('b07 メモリへ dd of=', 'dd if=x of=' + MEM),
108
+ ('b08 カーネルのメモリへ >', 'cat x > ' + KMEM),
109
+ ('b09 カーネルのメモリへ >|', 'cat x >| ' + KMEM),
110
+ ('b10 /etc へ >|', 'echo x >| ' + ETC + 'hosts'),
111
+ ('b11 引用符の中の /etc への書き込み', 'echo "see >' + ETC + 'hosts"'),
112
+ ('b12 sed の置き換え文字列の中の /etc', "sed -i 's#a#b >" + ETC + "x#' f.sh"),
113
+ ('b13 | bash へ流す /etc への書き込み', 'echo "cat x > ' + ETC + 'hosts" | bash'),
114
+ ('b14 本物の書き込み先としての 捨て先#…', 'echo x 2>' + NULL + '#x'),
115
+ ('b15 本物の書き込み先としての 捨て先#', 'echo x >' + NULL + '#'),
116
+ ('b16 包みの中の本物の 捨て先#', 'bash -c "echo x >' + NULL + '#"'),
117
+ ('b17 コマンド置換の中の本物の 捨て先#', 'V=$(a 2>' + NULL + '#q); echo x'),
118
+ ('b18 コマンド置換の中の装置への書き込み', 'V=$(a 2>' + SDA + '); echo x'),
119
+ ('b19 似て非なる捨て先(後ろが英数字)', 'echo x > ' + NULL + 'x'),
120
+ ('b20 捨て先の下を指す形', 'echo x > ' + NULL + '/../sda'),
121
+ ]
122
+ for label, cmd in DANGER:
123
+ r = decide(cmd)
124
+ check(label, (r['decision'], kind_of(r['reason'])), ('deny', 'danger'))
125
+
126
+ # ═════════════════════════════════════════════════════════════════════════════
127
+ # (c) 見分けの部品
128
+ # ═════════════════════════════════════════════════════════════════════════════
129
+ print('── (c) 捨て先の後ろに字が付いた形の見分け ' + '─' * 24)
130
+ PARTS = [
131
+ ('c1 捨て先#', NULL + '#', True),
132
+ ('c2 捨て先#…', NULL + '#kill', True),
133
+ ('c3 標準エラー#', STDERR + '#', True),
134
+ ('c4 素の捨て先', NULL, False),
135
+ ('c5 貼り付き(括弧の閉じ)は含めない', NULL + ')/x', False),
136
+ ('c6 貼り付き(引用符の閉じ)は含めない', NULL + '"#', False),
137
+ ('c7 後ろが英数字は含めない', NULL + 'x', False),
138
+ ('c8 後ろが / は含めない', NULL + '/x', False),
139
+ ('c9 /etc は含めない', ETC + 'hosts#', False),
140
+ ('c10 装置は含めない', SDA + '#', False),
141
+ ]
142
+ for label, target, want in PARTS:
143
+ check(label, policy.harmless_device_with_tail(target), want)
144
+
145
+ print()
146
+ if failures:
147
+ print(f'NG {len(failures)} 件')
148
+ for f in failures:
149
+ print(f' - {f}')
150
+ sys.exit(1)
151
+ print('すべて期待どおり')