@tyhld/conductor 0.12.163 → 0.12.165
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/scripts/hooks/policy.py
CHANGED
|
@@ -848,6 +848,12 @@ def _opaque_shell_danger(tokens):
|
|
|
848
848
|
|
|
849
849
|
def verb_of(tokens):
|
|
850
850
|
"""セグメントの実行動詞(ラッパ・変数代入を飛ばした先頭語)。無ければ ''。"""
|
|
851
|
+
i = _verb_index(tokens)
|
|
852
|
+
return _exe_base(tokens[i]) if i >= 0 else ''
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
def _verb_index(tokens):
|
|
856
|
+
"""verb_of と同じ読み方で、実行動詞の位置を返す。無ければ -1(便 9bf7e26e)。"""
|
|
851
857
|
i = 0
|
|
852
858
|
while i < len(tokens):
|
|
853
859
|
t = tokens[i]
|
|
@@ -870,8 +876,8 @@ def verb_of(tokens):
|
|
|
870
876
|
and not tokens[i].startswith('-'):
|
|
871
877
|
i += 1
|
|
872
878
|
continue
|
|
873
|
-
return
|
|
874
|
-
return
|
|
879
|
+
return i
|
|
880
|
+
return -1
|
|
875
881
|
|
|
876
882
|
|
|
877
883
|
# 「書き込む」作用を持つ動詞(対象パスに書く/消す/複製する)。
|
|
@@ -1273,11 +1279,178 @@ def _inner_payloads(command: str):
|
|
|
1273
1279
|
return out
|
|
1274
1280
|
|
|
1275
1281
|
|
|
1282
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
1283
|
+
# 文字を命令として動かす道具(便 9bf7e26e・えふさん決定 2026-11-02)
|
|
1284
|
+
#
|
|
1285
|
+
# 【塞いだ穴(便 7932bd6a の重大発見1・CC1 が 0.12.164 で実測)】
|
|
1286
|
+
# `ssh h "echo a; cat <秘密>"`・`eval "…"`・`git rebase --exec "…" main`・
|
|
1287
|
+
# `git -c alias.x='!…' x`・`echo "…" | bash` が allow だった。機械的な区切りの切れ端が
|
|
1288
|
+
# `<秘密>"` と引用符つきになり、秘密の名前に見えなかった(bash -c の包みは中身を測っていた)。
|
|
1289
|
+
# 【どう塞ぐか】渡す文字を取り出し、bash -c の包みと同じ判定(_wrapper_danger)にかける。
|
|
1290
|
+
# ★_inner_payloads には混ぜない=関門・作法・本番 DB の判定は1つも変えない(危険の判定だけ)。
|
|
1291
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
1292
|
+
|
|
1293
|
+
#: ssh の旗のうち、値を取る物(`-p 22`・`-p22`)。最初の旗でない語が行き先で、その後ろが命令。
|
|
1294
|
+
_SSH_VALUE_OPTS = frozenset('BbcDEeFIiJLlmOoPpQRSWw')
|
|
1295
|
+
#: git の設定のうち、値が命令として動く物(`!` で始まる別名・`*.helper` は別に見る)。
|
|
1296
|
+
_GIT_COMMAND_KEYS = frozenset({
|
|
1297
|
+
'core.sshcommand', 'core.pager', 'core.editor', 'core.fsmonitor', 'sequence.editor',
|
|
1298
|
+
'diff.external',
|
|
1299
|
+
})
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
def _flag_values(args, longs=(), shorts=''):
|
|
1303
|
+
"""旗の値を返す(`--file x`・`--file=x`・`-F x`・`-Fx`・まとめた `-aF x`)。
|
|
1304
|
+
|
|
1305
|
+
★長い旗は git の省略形(`--fil`)も拾う。読み違いは秘密の値のときだけ止める側に倒れる。
|
|
1306
|
+
"""
|
|
1307
|
+
out = []
|
|
1308
|
+
for i, t in enumerate(args):
|
|
1309
|
+
if t == '--':
|
|
1310
|
+
break
|
|
1311
|
+
nxt = args[i + 1] if i + 1 < len(args) else ''
|
|
1312
|
+
if t.startswith('--'):
|
|
1313
|
+
name, eq, val = t.partition('=')
|
|
1314
|
+
if len(name) > 3 and any(L.startswith(name) for L in longs):
|
|
1315
|
+
out.append(val if eq else nxt)
|
|
1316
|
+
continue
|
|
1317
|
+
if t.startswith('-') and len(t) > 1:
|
|
1318
|
+
for k, ch in enumerate(t[1:]):
|
|
1319
|
+
if ch in shorts:
|
|
1320
|
+
out.append(t[k + 2:] or nxt)
|
|
1321
|
+
break
|
|
1322
|
+
return out
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
def _ssh_remote_command(args) -> str:
|
|
1326
|
+
"""ssh の後ろの語から、送り先で動く命令(行き先の後ろの語を空白でつないだ物)を返す。"""
|
|
1327
|
+
i = 0
|
|
1328
|
+
while i < len(args):
|
|
1329
|
+
a = args[i]
|
|
1330
|
+
if a == '--':
|
|
1331
|
+
i += 1
|
|
1332
|
+
break
|
|
1333
|
+
if not a.startswith('-') or len(a) == 1:
|
|
1334
|
+
break
|
|
1335
|
+
cluster = a[1:]
|
|
1336
|
+
for k, ch in enumerate(cluster):
|
|
1337
|
+
if ch in _SSH_VALUE_OPTS:
|
|
1338
|
+
if k == len(cluster) - 1:
|
|
1339
|
+
i += 1
|
|
1340
|
+
break
|
|
1341
|
+
i += 1
|
|
1342
|
+
return ' '.join(args[i + 1:])
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
def _git_config_command(kv: str) -> str:
|
|
1346
|
+
"""`-c 名前=値` のうち、値が命令として動く物ならその命令を返す(でなければ '')。"""
|
|
1347
|
+
key, eq, val = kv.partition('=')
|
|
1348
|
+
key = key.lower()
|
|
1349
|
+
if not eq:
|
|
1350
|
+
return ''
|
|
1351
|
+
if (key.startswith('alias.') or key.endswith('.helper')) and val.startswith('!'):
|
|
1352
|
+
return val[1:]
|
|
1353
|
+
return val if key in _GIT_COMMAND_KEYS else ''
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
def _git_exec_payloads(args):
|
|
1357
|
+
"""git の `-c 別名=!命令` と `rebase --exec 命令` の命令を返す。"""
|
|
1358
|
+
out = []
|
|
1359
|
+
i = 0
|
|
1360
|
+
while i < len(args) and args[i].startswith('-') and args[i] != '--':
|
|
1361
|
+
f = args[i]
|
|
1362
|
+
if f == '-c' and i + 1 < len(args):
|
|
1363
|
+
body = _git_config_command(args[i + 1])
|
|
1364
|
+
if body.strip():
|
|
1365
|
+
out.append(('shell', 'git -c', body))
|
|
1366
|
+
i += 2 if ('=' not in f and f in _GIT_GLOBAL_VALUE_FLAGS) else 1
|
|
1367
|
+
sub, rest = _git_subcommand(args)
|
|
1368
|
+
if sub == 'rebase':
|
|
1369
|
+
for body in _flag_values(rest, ('--exec',), 'x'):
|
|
1370
|
+
if body.strip():
|
|
1371
|
+
out.append(('shell', 'git rebase --exec', body))
|
|
1372
|
+
return out
|
|
1373
|
+
|
|
1374
|
+
|
|
1375
|
+
def _unescape_echo(text: str) -> str:
|
|
1376
|
+
"""echo -e・printf が改行・タブに替える字を替える(`printf 'cat x\\n' | sh` の x を測るため)。"""
|
|
1377
|
+
return text.replace('\\n', '\n').replace('\\t', '\t')
|
|
1378
|
+
|
|
1379
|
+
|
|
1380
|
+
def _stdin_shell(toks) -> str:
|
|
1381
|
+
"""殻が標準入力を命令として読む形(`bash`・`sh -s`・`bash -`)なら殻の名前を返す。"""
|
|
1382
|
+
vi = _verb_index(toks)
|
|
1383
|
+
if vi < 0:
|
|
1384
|
+
return ''
|
|
1385
|
+
verb = _exe_base(toks[vi])
|
|
1386
|
+
rest = toks[vi + 1:]
|
|
1387
|
+
if verb not in _SHELL_RUNNERS or any(_is_code_flag(a) for a in rest):
|
|
1388
|
+
return ''
|
|
1389
|
+
if '-s' not in rest and any(not a.startswith('-') for a in rest):
|
|
1390
|
+
return '' # 台本のファイルを読む形(標準入力は命令ではない)
|
|
1391
|
+
return verb
|
|
1392
|
+
|
|
1393
|
+
|
|
1394
|
+
def _piped_shell_payloads(command: str):
|
|
1395
|
+
"""`echo "…" | bash`・`printf '…' | sh` の、殻へ流れ込む文字を返す。"""
|
|
1396
|
+
text = command or ''
|
|
1397
|
+
cmds = _quoted_words(text)
|
|
1398
|
+
if not cmds:
|
|
1399
|
+
return []
|
|
1400
|
+
toks = [[x for s, e, _r in words for x in (tokens_of(text[s:e]) or [''])] for words, _p in cmds]
|
|
1401
|
+
out = []
|
|
1402
|
+
for k in range(len(cmds) - 1):
|
|
1403
|
+
if not cmds[k][1]:
|
|
1404
|
+
continue
|
|
1405
|
+
shell = _stdin_shell(toks[k + 1])
|
|
1406
|
+
if not shell:
|
|
1407
|
+
continue
|
|
1408
|
+
up = toks[k]
|
|
1409
|
+
vi = _verb_index(up)
|
|
1410
|
+
if vi < 0 or _exe_base(up[vi]) not in _PLAIN_ECHO_VERBS:
|
|
1411
|
+
continue
|
|
1412
|
+
words = up[vi + 1:]
|
|
1413
|
+
if _exe_base(up[vi]) == 'echo':
|
|
1414
|
+
while words and re.fullmatch(r'-[neE]+', words[0]):
|
|
1415
|
+
words = words[1:]
|
|
1416
|
+
body = _unescape_echo(' '.join(words))
|
|
1417
|
+
if body.strip():
|
|
1418
|
+
out.append(('shell', f'| {shell}', body))
|
|
1419
|
+
return out
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
def _exec_string_payloads(command: str):
|
|
1423
|
+
"""文字を命令として動かす道具の、その文字を (種別, 呼び出した道具, 中身) で返す。"""
|
|
1424
|
+
out = []
|
|
1425
|
+
for seg in split_commands(command, bodies=False):
|
|
1426
|
+
toks = tokens_of(seg)
|
|
1427
|
+
vi = _verb_index(toks)
|
|
1428
|
+
if vi < 0:
|
|
1429
|
+
continue
|
|
1430
|
+
verb = _exe_base(toks[vi])
|
|
1431
|
+
args = toks[vi + 1:]
|
|
1432
|
+
if verb == 'eval':
|
|
1433
|
+
body = ' '.join(args)
|
|
1434
|
+
elif verb == 'ssh':
|
|
1435
|
+
body = _ssh_remote_command(args)
|
|
1436
|
+
else:
|
|
1437
|
+
body = ''
|
|
1438
|
+
if body.strip():
|
|
1439
|
+
out.append(('shell', verb, body))
|
|
1440
|
+
if verb == 'git':
|
|
1441
|
+
out += _git_exec_payloads(args)
|
|
1442
|
+
if verb in _SHELL_RUNNERS:
|
|
1443
|
+
for k, a in enumerate(args[:-1]):
|
|
1444
|
+
if a == '<<<':
|
|
1445
|
+
out.append(('shell', f'{verb} <<<', args[k + 1]))
|
|
1446
|
+
return out + _piped_shell_payloads(command)
|
|
1447
|
+
|
|
1448
|
+
|
|
1276
1449
|
def _wrapper_danger(command: str, depth: int = 0, cwd=None):
|
|
1277
1450
|
"""ラッパの中身に同じ判定を当てる。危険なら理由を返す。"""
|
|
1278
1451
|
if depth >= _UNWRAP_MAX_DEPTH:
|
|
1279
1452
|
return None
|
|
1280
|
-
for kind, runner, inner in _inner_payloads(command):
|
|
1453
|
+
for kind, runner, inner in _inner_payloads(command) + _exec_string_payloads(command):
|
|
1281
1454
|
if not inner or not inner.strip():
|
|
1282
1455
|
continue
|
|
1283
1456
|
where = f'inside {runner}' if runner else 'inside wrapper'
|
|
@@ -3755,6 +3928,142 @@ def _rm_danger_scan(command: str, work_dir: str = None):
|
|
|
3755
3928
|
return None
|
|
3756
3929
|
|
|
3757
3930
|
|
|
3931
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
3932
|
+
# git・gh のファイルを読む旗・添付の値(便 9bf7e26e・えふさん決定 2026-11-02)
|
|
3933
|
+
#
|
|
3934
|
+
# 【塞いだ穴(便 7932bd6a の重大発見1・CC1 が 0.12.164 で実測)】
|
|
3935
|
+
# `git commit -F <秘密>`・`gh pr create --body-file <秘密>`・`gh release create v1 <秘密>` が
|
|
3936
|
+
# allow だった。git・gh は「読む動詞」に無いので、ファイルの中身を記録・GitHub へ送る形が
|
|
3937
|
+
# 1度も測られていなかった。
|
|
3938
|
+
# 【どう塞ぐか】実物の旗(`git <命令> -h`・`gh help reference` で確かめた一覧)の値と添付の値だけを
|
|
3939
|
+
# 秘密の判定(secret_reason)にかける。★説明文(-m・--body)や題名は測らない=説明文に秘密の
|
|
3940
|
+
# 名前を書いただけでは止めない(便 7932bd6a の「ただの字は通す」を変えない)。
|
|
3941
|
+
# ★秘密でない値(/tmp/claude-1000 の下の本文のファイルなど)は今までどおり通す。
|
|
3942
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
3943
|
+
|
|
3944
|
+
#: git の下位の命令 → (ファイルを読む長い旗, 短い旗の字)。
|
|
3945
|
+
_GIT_FILE_FLAGS = {
|
|
3946
|
+
'commit': (('--file', '--template'), 'Ft'),
|
|
3947
|
+
'tag': (('--file',), 'F'),
|
|
3948
|
+
'notes': (('--file',), 'F'),
|
|
3949
|
+
'merge': (('--file',), 'F'),
|
|
3950
|
+
}
|
|
3951
|
+
|
|
3952
|
+
#: gh の命令 → 読み方。
|
|
3953
|
+
# files … ファイルを読む旗 (長い旗, 短い旗の字)
|
|
3954
|
+
# fields … `key=@ファイル` の形でファイルを読む旗 (長い旗, 短い旗の字)
|
|
3955
|
+
# assets … 旗でない語を添付のファイルとして読むときの「頭から飛ばす語の数」(読まないなら None)
|
|
3956
|
+
# values … 添付を数えるときに値ごと飛ばす旗
|
|
3957
|
+
_GH_BODY_FILE = {'files': (('--body-file',), 'F')}
|
|
3958
|
+
_GH_FILE_SPECS = {
|
|
3959
|
+
('pr', 'create'): {'files': (('--body-file', '--template'), 'FT')},
|
|
3960
|
+
('pr', 'edit'): _GH_BODY_FILE,
|
|
3961
|
+
('pr', 'comment'): _GH_BODY_FILE,
|
|
3962
|
+
('pr', 'review'): _GH_BODY_FILE,
|
|
3963
|
+
('pr', 'merge'): _GH_BODY_FILE,
|
|
3964
|
+
('issue', 'create'): _GH_BODY_FILE,
|
|
3965
|
+
('issue', 'edit'): _GH_BODY_FILE,
|
|
3966
|
+
('issue', 'comment'): _GH_BODY_FILE,
|
|
3967
|
+
('release', 'create'): {
|
|
3968
|
+
'files': (('--notes-file',), 'F'), 'assets': 1,
|
|
3969
|
+
'values': {'--discussion-category', '-n', '--notes', '-F', '--notes-file',
|
|
3970
|
+
'--notes-start-tag', '--target', '-t', '--title', '-R', '--repo'}},
|
|
3971
|
+
('release', 'edit'): {'files': (('--notes-file',), 'F')},
|
|
3972
|
+
('release', 'upload'): {'assets': 1, 'values': {'-R', '--repo'}},
|
|
3973
|
+
('gist', 'create'): {'assets': 0, 'values': {'-d', '--desc', '-f', '--filename'}},
|
|
3974
|
+
('gist', 'edit'): {'files': (('--add',), 'a')},
|
|
3975
|
+
('secret', 'set'): {'files': (('--env-file',), 'f')},
|
|
3976
|
+
('variable', 'set'): {'files': (('--env-file',), 'f')},
|
|
3977
|
+
('ssh-key', 'add'): {'assets': 0, 'values': {'-t', '--title', '--type'}},
|
|
3978
|
+
('gpg-key', 'add'): {'assets': 0, 'values': {'-t', '--title'}},
|
|
3979
|
+
('repo', 'deploy-key'): {'assets': 1, 'values': {'-t', '--title', '-R', '--repo'}},
|
|
3980
|
+
('codespace', 'cp'): {'assets': 0,
|
|
3981
|
+
'values': {'-c', '--codespace', '-R', '--repo', '--repo-owner'}},
|
|
3982
|
+
('api',): {'files': (('--input',), ''), 'fields': (('--field',), 'F')},
|
|
3983
|
+
('workflow', 'run'): {'fields': (('--field',), 'F')},
|
|
3984
|
+
}
|
|
3985
|
+
|
|
3986
|
+
|
|
3987
|
+
def _gh_file_values(args):
|
|
3988
|
+
"""gh の後ろの語から、(命令の名前, 読むファイルの一覧) を返す。当てはまらなければ ('', [])."""
|
|
3989
|
+
path = []
|
|
3990
|
+
for a in args[:3]:
|
|
3991
|
+
if a.startswith('-'):
|
|
3992
|
+
break
|
|
3993
|
+
path.append(a)
|
|
3994
|
+
for n in (2, 1):
|
|
3995
|
+
spec = _GH_FILE_SPECS.get(tuple(path[:n]))
|
|
3996
|
+
if spec:
|
|
3997
|
+
break
|
|
3998
|
+
else:
|
|
3999
|
+
return '', []
|
|
4000
|
+
rest = args[n:]
|
|
4001
|
+
vals = _flag_values(rest, *spec.get('files', ((), '')))
|
|
4002
|
+
for v in _flag_values(rest, *spec.get('fields', ((), ''))):
|
|
4003
|
+
if '=@' in v:
|
|
4004
|
+
vals.append(v.split('=@', 1)[1])
|
|
4005
|
+
skip = spec.get('assets')
|
|
4006
|
+
if skip is not None:
|
|
4007
|
+
values, assets, i = spec.get('values', set()), [], 0
|
|
4008
|
+
while i < len(rest):
|
|
4009
|
+
a = rest[i]
|
|
4010
|
+
if a == '--':
|
|
4011
|
+
assets += rest[i + 1:]
|
|
4012
|
+
break
|
|
4013
|
+
if a.startswith('-') and len(a) > 1:
|
|
4014
|
+
i += 2 if a in values else 1
|
|
4015
|
+
continue
|
|
4016
|
+
assets.append(a)
|
|
4017
|
+
i += 1
|
|
4018
|
+
for a in assets[skip:]:
|
|
4019
|
+
vals.append(a)
|
|
4020
|
+
if '#' in a:
|
|
4021
|
+
vals.append(a.split('#', 1)[0]) # `ファイル#表示名` の形
|
|
4022
|
+
return 'gh ' + ' '.join(path[:n]), vals
|
|
4023
|
+
|
|
4024
|
+
|
|
4025
|
+
def _vcs_file_danger(tokens, cwd=None):
|
|
4026
|
+
"""git・gh がファイルを読む旗・添付の値が秘密なら理由を返す(便 9bf7e26e)。
|
|
4027
|
+
|
|
4028
|
+
★値が `-`(標準入力)のときは、同じ命令の `< <秘密>` を見る(`git commit -F - < <秘密>`)。
|
|
4029
|
+
"""
|
|
4030
|
+
vi = _verb_index(tokens or [])
|
|
4031
|
+
if vi < 0:
|
|
4032
|
+
return None
|
|
4033
|
+
verb = _exe_base(tokens[vi])
|
|
4034
|
+
args = tokens[vi + 1:]
|
|
4035
|
+
where = cwd
|
|
4036
|
+
if verb == 'git':
|
|
4037
|
+
sub, rest = _git_subcommand(args)
|
|
4038
|
+
spec = _GIT_FILE_FLAGS.get(sub)
|
|
4039
|
+
if not spec:
|
|
4040
|
+
return None
|
|
4041
|
+
label, vals = f'git {sub}', _flag_values(rest, *spec)
|
|
4042
|
+
got = _git_cwd(args, cwd)
|
|
4043
|
+
where = cwd if got is _UNKNOWN_DIR else got
|
|
4044
|
+
elif verb == 'gh':
|
|
4045
|
+
label, vals = _gh_file_values(args)
|
|
4046
|
+
if not label:
|
|
4047
|
+
return None
|
|
4048
|
+
else:
|
|
4049
|
+
return None
|
|
4050
|
+
if '-' in vals:
|
|
4051
|
+
# 標準入力から読む形(`-F -`・`--body-file -`・gist create の `-`)。
|
|
4052
|
+
for k, t in enumerate(args):
|
|
4053
|
+
if t in ('<', '0<') and k + 1 < len(args):
|
|
4054
|
+
vals.append(args[k + 1])
|
|
4055
|
+
elif re.fullmatch(r'0?<[^<(&].*', t):
|
|
4056
|
+
vals.append(t.lstrip('0')[1:])
|
|
4057
|
+
for v in vals:
|
|
4058
|
+
v = (v or '').strip()
|
|
4059
|
+
if not v or v == '-':
|
|
4060
|
+
continue
|
|
4061
|
+
sr = secret_reason(v, where)
|
|
4062
|
+
if sr:
|
|
4063
|
+
return f'{sr} sent by {label}'
|
|
4064
|
+
return None
|
|
4065
|
+
|
|
4066
|
+
|
|
3758
4067
|
def _secret_danger(segment: str, verb: str, tokens, cwd=None):
|
|
3759
4068
|
"""このセグメントが秘密に触れる/システム領域へ書くなら理由を返す。
|
|
3760
4069
|
|
|
@@ -3763,6 +4072,10 @@ def _secret_danger(segment: str, verb: str, tokens, cwd=None):
|
|
|
3763
4072
|
# 秘密を含む環境変数の露出(echo $DATABASE_URL、curl -d "$TOKEN" など)。
|
|
3764
4073
|
if _SECRET_ENV_RE.search(segment):
|
|
3765
4074
|
return 'secret environment variable exposure'
|
|
4075
|
+
# git・gh のファイルを読む旗・添付の値(便 9bf7e26e)。
|
|
4076
|
+
hit = _vcs_file_danger(tokens, cwd)
|
|
4077
|
+
if hit:
|
|
4078
|
+
return hit
|
|
3766
4079
|
|
|
3767
4080
|
args = tokens[1:] if tokens else []
|
|
3768
4081
|
# ★探す文字(grep の1つ目の引数・-e / --regexp の値・引用符が壊れて割れた続き)は
|
|
@@ -5923,11 +6236,463 @@ def _discipline_scan(command: str, cwd=None):
|
|
|
5923
6236
|
return None
|
|
5924
6237
|
|
|
5925
6238
|
|
|
6239
|
+
# ═════════════════════════════════════════════════════════════════════════════
|
|
6240
|
+
# 引用符の中のただの字と、引用符の中の展開(便 7932bd6a・えふさん決定 2026-11-01/11-02)
|
|
6241
|
+
#
|
|
6242
|
+
# 【なぜ要るか(便 dbf1e335)】危険の判定は機械的な区切り(split_segments)で割るので、
|
|
6243
|
+
# `git commit -m "fix: 例 ; cat <秘密>"` の説明文の途中が1つの命令として測られ、
|
|
6244
|
+
# 実行されない字のせいで便がまるごと止まった。逆に、引用符の中の `$(…)`・`` `…` `` は
|
|
6245
|
+
# 本当に実行されるのに1つも測っていなかった(`git commit -m "$(cat <秘密>)"` が allow)。
|
|
6246
|
+
# 【どう直すか】
|
|
6247
|
+
# ① ただの字:説明文の値(git commit/tag/notes/stash/merge の -m、gh pr/issue/release の
|
|
6248
|
+
# 題名・本文)と、行に縦棒の無い echo/printf の引数のうち、引用符の中の展開を含まない字にある
|
|
6249
|
+
# 区切りの字(; & | 改行)を空白に替えた写しを作る。今の判定が当たっても、その写しで何も
|
|
6250
|
+
# 当たらなければ通す。
|
|
6251
|
+
# ★伏せた写しは「引用符の中のただの字」だけが元と違う=通るようになるのはその字のせいで
|
|
6252
|
+
# 止まっていた物だけ(ほかの判定・包みの中・ヒアドキュメントの本文・引用符の外は1字も変えない)。
|
|
6253
|
+
# ★-F/--body-file のようにファイルを読む旗の値、パスに使える引数は伏せない(持ち出しの穴にしない)。
|
|
6254
|
+
# ② 展開:シングルクォートの外の `$(…)`・`` `…` ``(入れ子も・区切りが引用符でないヒアドキュメントの
|
|
6255
|
+
# 本文の中も)の中身を、1つの命令として同じ危険の判定にかける(止める側を足すだけ)。
|
|
6256
|
+
# ═════════════════════════════════════════════════════════════════════════════
|
|
6257
|
+
|
|
6258
|
+
#: 説明文を -m で受け取る git の下位の命令。
|
|
6259
|
+
_GIT_MESSAGE_SUBS = frozenset({'commit', 'tag', 'notes', 'stash', 'merge'})
|
|
6260
|
+
#: その説明文の旗(値は次の語・`--message=…`・`-m…` の形)。
|
|
6261
|
+
_GIT_MESSAGE_FLAGS = ('-m', '--message')
|
|
6262
|
+
#: 題名・本文を文字で受け取る gh の命令。
|
|
6263
|
+
_GH_TEXT_SUBS = frozenset({'pr', 'issue', 'release'})
|
|
6264
|
+
#: その題名・本文の旗(★--body-file など、ファイルを読む旗は入れない)。
|
|
6265
|
+
_GH_TEXT_FLAGS = ('-t', '--title', '-b', '--body', '-n', '--notes', '--subject')
|
|
6266
|
+
#: 中身を外へ出すだけで、命令として読まない道具(★流れ込む先がある形は伏せない)。
|
|
6267
|
+
_PLAIN_ECHO_VERBS = frozenset({'echo', 'printf'})
|
|
6268
|
+
#: 展開の入れ子を測る上限(これより深い物は測れないので止める)。
|
|
6269
|
+
_SUBST_MAX_DEPTH = 4
|
|
6270
|
+
#: 伏せる字=命令の区切りに見える字だけ(★説明文の中身を見る決まり=[skip ci] 類は字が残るので
|
|
6271
|
+
# 今までどおり当たる)。★`<` `>` は伏せない=引用符の中の書き込み先に見える字は今までどおり止める
|
|
6272
|
+
# (便 93e3c6d8 の決まり・test_quoted_devnull.py b11)。
|
|
6273
|
+
_MASK_CHARS = frozenset(';&|\n')
|
|
6274
|
+
#: 展開の中身で当たったときに理由へ添える印。
|
|
6275
|
+
_SUBST_WHERE = '(inside command substitution)'
|
|
6276
|
+
|
|
6277
|
+
|
|
6278
|
+
# ★下の _skip_* は、閉じていない形では「末尾+1」を返す(=崩れた字面の印。伏せる側は伏せない)。
|
|
6279
|
+
|
|
6280
|
+
def _skip_squote(text: str, i: int) -> int:
|
|
6281
|
+
"""text[i] が ' のとき、閉じた ' の次の位置を返す。"""
|
|
6282
|
+
j = text.find("'", i + 1)
|
|
6283
|
+
return len(text) + 1 if j < 0 else j + 1
|
|
6284
|
+
|
|
6285
|
+
|
|
6286
|
+
def _skip_backtick(text: str, i: int) -> int:
|
|
6287
|
+
"""text[i] が ` のとき、閉じた ` の次の位置を返す(\\ の後ろの1文字は飛ばす)。"""
|
|
6288
|
+
j, n = i + 1, len(text)
|
|
6289
|
+
while j < n:
|
|
6290
|
+
if text[j] == '\\':
|
|
6291
|
+
j += 2
|
|
6292
|
+
continue
|
|
6293
|
+
if text[j] == '`':
|
|
6294
|
+
return j + 1
|
|
6295
|
+
j += 1
|
|
6296
|
+
return n + 1
|
|
6297
|
+
|
|
6298
|
+
|
|
6299
|
+
def _skip_dquote(text: str, i: int) -> int:
|
|
6300
|
+
"""text[i] が " のとき、閉じた " の次の位置を返す(中の展開は丸ごと飛ばす)。"""
|
|
6301
|
+
j, n = i + 1, len(text)
|
|
6302
|
+
while j < n:
|
|
6303
|
+
c = text[j]
|
|
6304
|
+
if c == '\\':
|
|
6305
|
+
j += 2
|
|
6306
|
+
continue
|
|
6307
|
+
if c == '"':
|
|
6308
|
+
return j + 1
|
|
6309
|
+
if text.startswith('$(', j):
|
|
6310
|
+
j = _skip_subst(text, j)
|
|
6311
|
+
continue
|
|
6312
|
+
if c == '`':
|
|
6313
|
+
j = _skip_backtick(text, j)
|
|
6314
|
+
continue
|
|
6315
|
+
j += 1
|
|
6316
|
+
return n + 1
|
|
6317
|
+
|
|
6318
|
+
|
|
6319
|
+
def _skip_heredoc_bodies(text: str, i: int, pending) -> int:
|
|
6320
|
+
"""text[i] が行の頭のとき、待っているヒアドキュメントの本文を読み飛ばした位置を返す。"""
|
|
6321
|
+
n = len(text)
|
|
6322
|
+
for delim, strip_tabs, _quoted in pending:
|
|
6323
|
+
while i < n:
|
|
6324
|
+
j = text.find('\n', i)
|
|
6325
|
+
line = text[i:n if j < 0 else j]
|
|
6326
|
+
i = n if j < 0 else j + 1
|
|
6327
|
+
if (line.lstrip('\t') if strip_tabs else line).strip() == delim:
|
|
6328
|
+
break
|
|
6329
|
+
return i
|
|
6330
|
+
|
|
6331
|
+
|
|
6332
|
+
def _skip_subst(text: str, i: int) -> int:
|
|
6333
|
+
"""text[i:] が `$(` のとき、対になる `)` の次の位置を返す(引用符・入れ子・本文を見る)。"""
|
|
6334
|
+
j, n, depth, pending = i + 2, len(text), 1, []
|
|
6335
|
+
while j < n:
|
|
6336
|
+
c = text[j]
|
|
6337
|
+
if c == '\\':
|
|
6338
|
+
j += 2
|
|
6339
|
+
continue
|
|
6340
|
+
if c == "'":
|
|
6341
|
+
j = _skip_squote(text, j)
|
|
6342
|
+
continue
|
|
6343
|
+
if c == '"':
|
|
6344
|
+
j = _skip_dquote(text, j)
|
|
6345
|
+
continue
|
|
6346
|
+
if c == '`':
|
|
6347
|
+
j = _skip_backtick(text, j)
|
|
6348
|
+
continue
|
|
6349
|
+
if text.startswith('$(', j):
|
|
6350
|
+
j = _skip_subst(text, j)
|
|
6351
|
+
continue
|
|
6352
|
+
if c == '#' and (j == 0 or text[j - 1] in ' \t\n;&|('):
|
|
6353
|
+
k = text.find('\n', j)
|
|
6354
|
+
j = n if k < 0 else k
|
|
6355
|
+
continue
|
|
6356
|
+
if text.startswith('<<', j) and not text.startswith('<<<', j):
|
|
6357
|
+
m = _HEREDOC_START_RE.match(text, j)
|
|
6358
|
+
if m:
|
|
6359
|
+
pending.append((m.group(3), m.group(1) == '-', bool(m.group(2))))
|
|
6360
|
+
j = m.end()
|
|
6361
|
+
continue
|
|
6362
|
+
if c == '\n' and pending:
|
|
6363
|
+
j = _skip_heredoc_bodies(text, j + 1, pending)
|
|
6364
|
+
pending = []
|
|
6365
|
+
continue
|
|
6366
|
+
if c == '(':
|
|
6367
|
+
depth += 1
|
|
6368
|
+
elif c == ')':
|
|
6369
|
+
depth -= 1
|
|
6370
|
+
if depth == 0:
|
|
6371
|
+
return j + 1
|
|
6372
|
+
j += 1
|
|
6373
|
+
return n + 1
|
|
6374
|
+
|
|
6375
|
+
|
|
6376
|
+
def _subst_spans_in(text: str, start: int, end: int, out: list, in_dq: bool = False):
|
|
6377
|
+
"""text[start:end] の中の展開(`$(…)`・`` `…` ``)の中身を out へ足す(シングルクォートの中は除く)。"""
|
|
6378
|
+
j = start
|
|
6379
|
+
while j < end:
|
|
6380
|
+
c = text[j]
|
|
6381
|
+
if c == '\\':
|
|
6382
|
+
j += 2
|
|
6383
|
+
continue
|
|
6384
|
+
if c == "'" and not in_dq:
|
|
6385
|
+
j = _skip_squote(text, j)
|
|
6386
|
+
continue
|
|
6387
|
+
if c == '"' and not in_dq:
|
|
6388
|
+
k = _skip_dquote(text, j)
|
|
6389
|
+
_subst_spans_in(text, j + 1, max(j + 1, k - 1), out, in_dq=True)
|
|
6390
|
+
j = k
|
|
6391
|
+
continue
|
|
6392
|
+
if text.startswith('$((', j):
|
|
6393
|
+
j += 3
|
|
6394
|
+
continue
|
|
6395
|
+
if text.startswith('$(', j):
|
|
6396
|
+
k = _skip_subst(text, j)
|
|
6397
|
+
out.append(text[j + 2:max(j + 2, k - 1)])
|
|
6398
|
+
j = k
|
|
6399
|
+
continue
|
|
6400
|
+
if c == '`':
|
|
6401
|
+
k = _skip_backtick(text, j)
|
|
6402
|
+
out.append(text[j + 1:max(j + 1, k - 1)])
|
|
6403
|
+
j = k
|
|
6404
|
+
continue
|
|
6405
|
+
j += 1
|
|
6406
|
+
|
|
6407
|
+
|
|
6408
|
+
def _subst_commands(command: str):
|
|
6409
|
+
"""命令の中の展開の中身を、外側の1段だけ返す(★入れ子は中身をもう一度測るときに拾う)。
|
|
6410
|
+
|
|
6411
|
+
★シングルクォートの中・注釈・区切りが引用符のヒアドキュメントの本文(ただの字)は拾わない。
|
|
6412
|
+
区切りが引用符でないヒアドキュメントの本文は、シェルが展開するので拾う。
|
|
6413
|
+
"""
|
|
6414
|
+
text = command or ''
|
|
6415
|
+
out, pending = [], []
|
|
6416
|
+
i, n = 0, len(text)
|
|
6417
|
+
while i < n:
|
|
6418
|
+
c = text[i]
|
|
6419
|
+
if c == '\\':
|
|
6420
|
+
i += 2
|
|
6421
|
+
continue
|
|
6422
|
+
if c == "'":
|
|
6423
|
+
i = _skip_squote(text, i)
|
|
6424
|
+
continue
|
|
6425
|
+
if c == '"':
|
|
6426
|
+
k = _skip_dquote(text, i)
|
|
6427
|
+
_subst_spans_in(text, i + 1, max(i + 1, k - 1), out, in_dq=True)
|
|
6428
|
+
i = k
|
|
6429
|
+
continue
|
|
6430
|
+
if c == '#' and (i == 0 or text[i - 1] in ' \t\n;&|('):
|
|
6431
|
+
k = text.find('\n', i)
|
|
6432
|
+
i = n if k < 0 else k
|
|
6433
|
+
continue
|
|
6434
|
+
if text.startswith('<<', i) and not text.startswith('<<<', i):
|
|
6435
|
+
m = _HEREDOC_START_RE.match(text, i)
|
|
6436
|
+
if m:
|
|
6437
|
+
pending.append((m.group(3), m.group(1) == '-', bool(m.group(2))))
|
|
6438
|
+
i = m.end()
|
|
6439
|
+
continue
|
|
6440
|
+
if c == '\n' and pending:
|
|
6441
|
+
body_start = i + 1
|
|
6442
|
+
i = _skip_heredoc_bodies(text, body_start, pending)
|
|
6443
|
+
if any(not quoted for _d, _s, quoted in pending):
|
|
6444
|
+
_subst_spans_in(text, body_start, i, out, in_dq=True)
|
|
6445
|
+
pending = []
|
|
6446
|
+
continue
|
|
6447
|
+
if text.startswith('$((', i):
|
|
6448
|
+
i += 3
|
|
6449
|
+
continue
|
|
6450
|
+
# ★プロセス置換 `<(…)`・`>(…)` も同じく中身を1つの命令として測る(便 9bf7e26e)。
|
|
6451
|
+
# 引用符の中ではただの字なので、ここ(引用符の外)だけで拾う。
|
|
6452
|
+
if text.startswith('$(', i) or text.startswith('<(', i) or text.startswith('>(', i):
|
|
6453
|
+
k = _skip_subst(text, i)
|
|
6454
|
+
out.append(text[i + 2:max(i + 2, k - 1)])
|
|
6455
|
+
i = k
|
|
6456
|
+
continue
|
|
6457
|
+
if c == '`':
|
|
6458
|
+
k = _skip_backtick(text, i)
|
|
6459
|
+
out.append(text[i + 1:max(i + 1, k - 1)])
|
|
6460
|
+
i = k
|
|
6461
|
+
continue
|
|
6462
|
+
i += 1
|
|
6463
|
+
return [s for s in out if s.strip()]
|
|
6464
|
+
|
|
6465
|
+
|
|
6466
|
+
def _quoted_words(command: str):
|
|
6467
|
+
"""引用符の外の本物の区切りで割り、命令ごとに語の並びを返す。
|
|
6468
|
+
|
|
6469
|
+
返す形: [(語の一覧, 後ろが縦棒で流れ込むか)]。語は (始まり, 終わり, 伏せてよい字の範囲の一覧)。
|
|
6470
|
+
伏せてよい字=引用符の中のただの字(シングルクォートの中・ダブルクォートの中の展開の外)と、
|
|
6471
|
+
ダブルクォートの中の展開そのもの(★中身は _subst_commands で別に測る)。
|
|
6472
|
+
★ヒアドキュメントの本文は語にも範囲にも入れない(本文は今までどおり測る)。
|
|
6473
|
+
★引用符・展開が閉じていない字面は読み切れないので None を返す(伏せる側は伏せない)。
|
|
6474
|
+
"""
|
|
6475
|
+
text = command or ''
|
|
6476
|
+
cmds, words, pending = [], [], []
|
|
6477
|
+
cur = None # いまの語 [始まり, 終わり, 範囲]
|
|
6478
|
+
i, n = 0, len(text)
|
|
6479
|
+
|
|
6480
|
+
def end_word():
|
|
6481
|
+
nonlocal cur
|
|
6482
|
+
if cur is not None:
|
|
6483
|
+
cur[1] = i
|
|
6484
|
+
words.append(tuple(cur))
|
|
6485
|
+
cur = None
|
|
6486
|
+
|
|
6487
|
+
def end_cmd(piped):
|
|
6488
|
+
nonlocal words
|
|
6489
|
+
end_word()
|
|
6490
|
+
if words:
|
|
6491
|
+
cmds.append((words, piped))
|
|
6492
|
+
words = []
|
|
6493
|
+
|
|
6494
|
+
def word():
|
|
6495
|
+
nonlocal cur
|
|
6496
|
+
if cur is None:
|
|
6497
|
+
cur = [i, i, []]
|
|
6498
|
+
return cur
|
|
6499
|
+
|
|
6500
|
+
while i < n:
|
|
6501
|
+
c = text[i]
|
|
6502
|
+
if c in ' \t':
|
|
6503
|
+
end_word()
|
|
6504
|
+
i += 1
|
|
6505
|
+
continue
|
|
6506
|
+
if c == '\\':
|
|
6507
|
+
word()
|
|
6508
|
+
i += 2
|
|
6509
|
+
continue
|
|
6510
|
+
if c == "'":
|
|
6511
|
+
w = word()
|
|
6512
|
+
k = _skip_squote(text, i)
|
|
6513
|
+
if k > n:
|
|
6514
|
+
return None
|
|
6515
|
+
w[2].append((i + 1, max(i + 1, k - 1)))
|
|
6516
|
+
i = k
|
|
6517
|
+
continue
|
|
6518
|
+
if c == '"':
|
|
6519
|
+
w = word()
|
|
6520
|
+
k = _skip_dquote(text, i)
|
|
6521
|
+
if k > n:
|
|
6522
|
+
return None
|
|
6523
|
+
j, stop, lit = i + 1, max(i + 1, k - 1), i + 1
|
|
6524
|
+
while j < stop:
|
|
6525
|
+
d = text[j]
|
|
6526
|
+
if d == '\\':
|
|
6527
|
+
j += 2
|
|
6528
|
+
continue
|
|
6529
|
+
if d == '$' and j + 1 < stop and (text[j + 1] == '{' or text[j + 1] == '_'
|
|
6530
|
+
or text[j + 1].isalnum()):
|
|
6531
|
+
# ★$NAME・${NAME} は展開(伏せない=今までどおり測る)。
|
|
6532
|
+
w[2].append((lit, j))
|
|
6533
|
+
if text[j + 1] == '{':
|
|
6534
|
+
e = text.find('}', j)
|
|
6535
|
+
j = stop if e < 0 or e >= stop else e + 1
|
|
6536
|
+
else:
|
|
6537
|
+
j += 1
|
|
6538
|
+
while j < stop and (text[j] == '_' or text[j].isalnum()):
|
|
6539
|
+
j += 1
|
|
6540
|
+
lit = j
|
|
6541
|
+
continue
|
|
6542
|
+
j += 1
|
|
6543
|
+
w[2].append((lit, stop))
|
|
6544
|
+
i = k
|
|
6545
|
+
continue
|
|
6546
|
+
if text.startswith('$(', i) or c == '`':
|
|
6547
|
+
word()
|
|
6548
|
+
i = _skip_subst(text, i) if c == '$' else _skip_backtick(text, i)
|
|
6549
|
+
if i > n:
|
|
6550
|
+
return None
|
|
6551
|
+
continue
|
|
6552
|
+
if c == '#' and cur is None:
|
|
6553
|
+
k = text.find('\n', i)
|
|
6554
|
+
i = n if k < 0 else k
|
|
6555
|
+
continue
|
|
6556
|
+
if text.startswith('<<', i) and not text.startswith('<<<', i):
|
|
6557
|
+
m = _HEREDOC_START_RE.match(text, i)
|
|
6558
|
+
if m:
|
|
6559
|
+
end_word()
|
|
6560
|
+
pending.append((m.group(3), m.group(1) == '-', bool(m.group(2))))
|
|
6561
|
+
i = m.end()
|
|
6562
|
+
continue
|
|
6563
|
+
if c == '\n':
|
|
6564
|
+
end_cmd(False)
|
|
6565
|
+
i = _skip_heredoc_bodies(text, i + 1, pending) if pending else i + 1
|
|
6566
|
+
pending = []
|
|
6567
|
+
continue
|
|
6568
|
+
if c == '&' and ((i > 0 and text[i - 1] == '>') or text.startswith('&>', i)):
|
|
6569
|
+
word()
|
|
6570
|
+
i += 1
|
|
6571
|
+
continue
|
|
6572
|
+
if c == '|' and i > 0 and text[i - 1] == '>':
|
|
6573
|
+
word()
|
|
6574
|
+
i += 1
|
|
6575
|
+
continue
|
|
6576
|
+
if text.startswith('&&', i) or text.startswith('||', i):
|
|
6577
|
+
end_cmd(False)
|
|
6578
|
+
i += 2
|
|
6579
|
+
continue
|
|
6580
|
+
if c in ';&|':
|
|
6581
|
+
end_cmd(c == '|')
|
|
6582
|
+
i += 2 if text.startswith('|&', i) else 1
|
|
6583
|
+
continue
|
|
6584
|
+
word()
|
|
6585
|
+
i += 1
|
|
6586
|
+
end_cmd(False)
|
|
6587
|
+
return cmds
|
|
6588
|
+
|
|
6589
|
+
|
|
6590
|
+
#: 書き込み先・読み込み元を受ける語(`>`・`2>>`・`&>`・`<`・`>f` の形)。この語と、演算子だけの語の次の語は伏せない。
|
|
6591
|
+
_REDIRECT_WORD_RE = re.compile(r'^(?:\d*|&)[<>]')
|
|
6592
|
+
_REDIRECT_ONLY_RE = re.compile(r'^(?:\d*|&)[<>]+[&|]?$')
|
|
6593
|
+
|
|
6594
|
+
|
|
6595
|
+
def _maskable_words(texts, echo_ok: bool):
|
|
6596
|
+
"""1つの命令の語(字面)のうち、引用符の中を伏せてよい語の番号を返す。
|
|
6597
|
+
|
|
6598
|
+
★echo_ok=False(行のどこかに縦棒がある・展開の中身を測っている)のときは echo/printf を伏せない
|
|
6599
|
+
=出した字が別の命令に読まれる形(`echo '…' | sh`・`eval "$(echo '…')"`)を通さない。
|
|
6600
|
+
★書き込み先・読み込み元の語は伏せない(`echo x > "<秘密>"` を今までどおり止める)。
|
|
6601
|
+
"""
|
|
6602
|
+
toks = [(tokens_of(t) or [''])[0] for t in texts]
|
|
6603
|
+
redirect = set()
|
|
6604
|
+
for p, raw in enumerate(texts):
|
|
6605
|
+
if _REDIRECT_WORD_RE.match(raw):
|
|
6606
|
+
redirect.add(p)
|
|
6607
|
+
if _REDIRECT_ONLY_RE.match(raw):
|
|
6608
|
+
redirect.add(p + 1)
|
|
6609
|
+
k = 0
|
|
6610
|
+
while k < len(toks) and (_ASSIGN_RE.match(toks[k]) or toks[k] in _SHELL_KEYWORDS):
|
|
6611
|
+
k += 1
|
|
6612
|
+
if k >= len(toks):
|
|
6613
|
+
return set()
|
|
6614
|
+
verb = toks[k].split('/')[-1]
|
|
6615
|
+
if verb in _PLAIN_ECHO_VERBS:
|
|
6616
|
+
return set(range(k + 1, len(toks))) - redirect if echo_ok else set()
|
|
6617
|
+
if verb == 'git':
|
|
6618
|
+
j = k + 1
|
|
6619
|
+
while j < len(toks) and toks[j] in ('-C', '--no-pager'):
|
|
6620
|
+
j += 2 if toks[j] == '-C' else 1
|
|
6621
|
+
if j >= len(toks) or toks[j] not in _GIT_MESSAGE_SUBS:
|
|
6622
|
+
return set()
|
|
6623
|
+
flags = _GIT_MESSAGE_FLAGS
|
|
6624
|
+
elif verb == 'gh':
|
|
6625
|
+
j = k + 1
|
|
6626
|
+
if j >= len(toks) or toks[j] not in _GH_TEXT_SUBS:
|
|
6627
|
+
return set()
|
|
6628
|
+
flags = _GH_TEXT_FLAGS
|
|
6629
|
+
else:
|
|
6630
|
+
return set()
|
|
6631
|
+
out = set()
|
|
6632
|
+
for p in range(j + 1, len(toks)):
|
|
6633
|
+
t, prev = toks[p], toks[p - 1]
|
|
6634
|
+
if prev in flags or any(t.startswith(f + '=') for f in flags if f.startswith('--')) \
|
|
6635
|
+
or (t.startswith('-m') and not t.startswith('--') and len(t) > 2 and flags is _GIT_MESSAGE_FLAGS):
|
|
6636
|
+
out.add(p)
|
|
6637
|
+
return out - redirect
|
|
6638
|
+
|
|
6639
|
+
|
|
6640
|
+
def _mask_quoted_text(command: str, echo_ok: bool = True) -> str:
|
|
6641
|
+
"""説明文・echo の文字の、引用符の中のただの字を伏せた写しを返す(伏せる所が無ければ元のまま)。"""
|
|
6642
|
+
text = command or ''
|
|
6643
|
+
cmds = _quoted_words(text)
|
|
6644
|
+
if cmds is None:
|
|
6645
|
+
return text # 引用符・展開が閉じていない=読み切れないので伏せない
|
|
6646
|
+
echo_ok = echo_ok and not any(piped for _w, piped in cmds)
|
|
6647
|
+
spans = []
|
|
6648
|
+
for words, _piped in cmds:
|
|
6649
|
+
texts = [text[s:e] for s, e, _r in words]
|
|
6650
|
+
for p in _maskable_words(texts, echo_ok):
|
|
6651
|
+
spans.extend(words[p][2])
|
|
6652
|
+
if not spans:
|
|
6653
|
+
return text
|
|
6654
|
+
chars = list(text)
|
|
6655
|
+
for s, e in spans:
|
|
6656
|
+
for x in range(s, e):
|
|
6657
|
+
if chars[x] in _MASK_CHARS:
|
|
6658
|
+
chars[x] = ' '
|
|
6659
|
+
return ''.join(chars)
|
|
6660
|
+
|
|
6661
|
+
|
|
6662
|
+
def _subst_danger(command: str, cwd=None, outside_grep_tree=True, depth: int = 0):
|
|
6663
|
+
"""展開(`$(…)`・`` `…` ``)の中身を1つの命令として測り、危険なら理由を返す。"""
|
|
6664
|
+
inner = _subst_commands(command)
|
|
6665
|
+
expanded = _expand_local_vars(command)
|
|
6666
|
+
if expanded != command:
|
|
6667
|
+
inner += [s for s in _subst_commands(expanded) if s not in inner]
|
|
6668
|
+
for payload in inner:
|
|
6669
|
+
if depth + 1 >= _SUBST_MAX_DEPTH:
|
|
6670
|
+
return 'command substitution nested too deep to verify'
|
|
6671
|
+
hit = _bash_danger(payload, cwd, outside_grep_tree, _depth=depth + 1)
|
|
6672
|
+
if hit:
|
|
6673
|
+
return hit if hit.endswith(_SUBST_WHERE) else f'{hit} {_SUBST_WHERE}'
|
|
6674
|
+
return None
|
|
6675
|
+
|
|
6676
|
+
|
|
5926
6677
|
# ═════════════════════════════════════════════════════════════════════════════
|
|
5927
6678
|
# 入口: Bash
|
|
5928
6679
|
# ═════════════════════════════════════════════════════════════════════════════
|
|
5929
6680
|
|
|
5930
|
-
def _bash_danger(command: str, cwd=None, outside_grep_tree=True):
|
|
6681
|
+
def _bash_danger(command: str, cwd=None, outside_grep_tree=True, _depth: int = 0):
|
|
6682
|
+
"""「危険」の段を当て、最初に当たった理由を返す(当たらなければ None)。
|
|
6683
|
+
|
|
6684
|
+
★今の判定(_bash_danger_plain)が当たっても、引用符の中のただの字を伏せた写しで何も
|
|
6685
|
+
当たらなければ通す(便 7932bd6a)。そのあと展開の中身を同じ判定で測る(止める側を足すだけ)。
|
|
6686
|
+
"""
|
|
6687
|
+
hit = _bash_danger_plain(command, cwd, outside_grep_tree)
|
|
6688
|
+
if hit:
|
|
6689
|
+
masked = _mask_quoted_text(command, echo_ok=_depth == 0)
|
|
6690
|
+
if masked == command or _bash_danger_plain(masked, cwd, outside_grep_tree) is not None:
|
|
6691
|
+
return hit
|
|
6692
|
+
return _subst_danger(command, cwd, outside_grep_tree, _depth)
|
|
6693
|
+
|
|
6694
|
+
|
|
6695
|
+
def _bash_danger_plain(command: str, cwd=None, outside_grep_tree=True):
|
|
5931
6696
|
"""「危険」の段を順番どおり当て、最初に当たった理由を返す(当たらなければ None)。
|
|
5932
6697
|
|
|
5933
6698
|
★decide_bash はこの関数の答えをそのまま使う=危険の判定の正本を2か所に置かない。
|