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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
|
#!/usr/bin/env python3
"""Prepare a patch run, and render the suggested-fix products from it.
`--prepare` lays out a patch run for a report: it checks the repository root
and the base revision, makes the run's working directory and the report's
`patches/` directory, writes one `findings/F<n>.json` per selected finding
(the report's record, its `file` made repository-relative), and prints the two
directories and the selected ids.
The render form reads the run's `patches.json` and raw `F<n>.diff` files, and
writes into the report's `patches/` directory:
* `F<n>.patch` -- the raw diff behind an explanatory comment header;
* `F<n>.md` -- a short note per finding, whether or not a patch was written;
* `PATCHES.md` and `patches.jsonl` -- the index, prose and machine form;
* the report directory's `.gitignore` (the single line `*`) if it lacks one.
Each written patch is checked read-only against the repository with
`git apply --check`, and the whole patch run directory -- scratch workspaces,
prepared findings, raw diffs and the record -- is removed once the products are
written, along with the run directory above it when nothing else remains there.
Usage:
patch_artifacts.py --prepare <report_dir> <selection> --repo-root <dir> --base <sha>
patch_artifacts.py <patch_dir> <patches_dir> <scan_root> --base <sha>
patch_artifacts.py --remove-scratch <workspace>
Exits 0 on success (declined findings included), 1 on a refusal naming what is
wrong, 2 on a usage error.
Python 3.9-compatible, stdlib only.
"""
from __future__ import annotations
import argparse
import functools
import os
import re
import shlex
import shutil
import stat
import subprocess
import sys
from collections import Counter
from contextlib import suppress
from datetime import datetime, timezone
from enum import Enum
from pathlib import Path
from typing import TYPE_CHECKING, NamedTuple, TypedDict
# The lib/ package lives next to this script. Python normally adds a script's own
# directory to the import path, but not under -P or PYTHONSAFEPATH, so we add it here.
sys.path.insert(0, str(Path(__file__).resolve().parent))
from lib import absolute, console, git, plugin, strictjson
from lib.finding import Severity, repository_file, scan_prefix_shaped
from lib.link import is_link
from lib.strictjson import JsonMap, is_int, is_list, is_map, is_str
if TYPE_CHECKING:
from collections.abc import Callable
from types import TracebackType
from typing import Literal
FINDING_ID_PATTERN = "F[0-9]{1,9}"
FINDING_ID_RE = re.compile(rf"^{FINDING_ID_PATTERN}\Z")
def finding_number(finding_id: str) -> int:
"""The number in a finding id, for sorting ids as a reader expects (F2 before F10)."""
return int(finding_id[1:])
REGULAR_FILE_MODE = "100644"
PATCHES_DIR_NAME = "patches"
FINDINGS_DIR_NAME = "findings"
SCRATCH_NAME_RE = re.compile(rf"^scratch-{FINDING_ID_PATTERN}\Z")
PATCH_DIR_PREFIX = "patch-"
PATCH_DIR_RE = re.compile(rf"^{re.escape(PATCH_DIR_PREFIX)}[0-9][0-9-]*\Z")
DIFF_HEADER = "diff --git "
CLAIM_KEYS = ("targeted", "no_new_vulnerability", "behaviour_unchanged")
CLAIM_LABELS = {
"targeted": "the change is highly targeted to this finding",
"no_new_vulnerability": "the change introduces no new security vulnerability",
"behaviour_unchanged": (
"beyond closing the finding, the change does not alter the code's "
"behaviour or the inputs it accepts"
),
}
CLAIM_STATES = ("CONFIDENT", "NOT_CONFIDENT", "UNSURE")
STATUSES = ("patch_written", "declined", "skipped_stale")
NOT_CONFIRMED = (
"The review found this finding is not exploitable as the report describes at this "
"revision, so no patch was written for it."
)
UNTESTED_NOTICE = "no project test of the patched code was run"
class Claim(TypedDict):
"""One of the verifier's three confidence claims."""
state: str
evidence: str
class PathKind(str, Enum):
"""The kind the fresh reviewer gave an attack path it reported."""
OPENED = "opened"
STILL_REACHABLE = "still_reachable"
SET_ASIDE = "set_aside"
@property
def blocks_patch(self) -> bool:
return self is not PathKind.SET_ASIDE
PATH_KIND_LABELS = {
PathKind.OPENED: "opened by the rejected change",
PathKind.STILL_REACHABLE: "the finding's exploit, still reachable by another route",
PathKind.SET_ASIDE: "set aside as outside this finding; not addressed here",
}
class AttackPath(TypedDict):
"""An attack path the fresh reviewer reported, with the kind it gave it."""
kind: PathKind
text: str
class DiffStat(TypedDict):
"""Per-file added/deleted line counts, "-" for a binary file as git prints it."""
path: str
added: int | Literal["-"]
deleted: int | Literal["-"]
class Unit(TypedDict):
"""A validated unit record, ready to be written out."""
id: str
title: str
status: str
summary: str
claims: dict[str, Claim]
untested: bool
tests_run: str
reviewed_paths: list[str]
attack_paths: list[AttackPath]
attempts: int | None
confirmed: bool | None
decline_reason: str
recommendation: str
class RenderArgs(argparse.Namespace):
"""The parsed render command line."""
patch_dir: str = ""
patches_dir: str = ""
scan_root: str = ""
base: str = ""
class PrepareArgs(argparse.Namespace):
"""The parsed `--prepare` command line."""
report_dir: str = ""
selection: str = ""
repo_root: str = ""
base: str = ""
class RemoveArgs(argparse.Namespace):
"""The parsed `--remove-scratch` command line."""
workspace: str = ""
class ReportFinding(NamedTuple):
"""A report finding as `--prepare` reads it: its severity, and its record to hand on."""
severity: Severity
record: JsonMap
class PatchError(Exception):
"""A refusal; the message names what the caller must correct."""
def field(value: object, what: str) -> str:
"""A record field as text; None reads as empty."""
if value is None:
return ""
if not is_str(value):
msg = f"{what} must be a string"
raise PatchError(msg)
if strictjson.has_lone_surrogate(value):
msg = f"{what} contains an unpaired surrogate; it is not valid text"
raise PatchError(msg)
return value
def line_field(value: object, what: str) -> str:
"""A one-line record field; line breaks folded to spaces."""
return field(value, what).replace("\r", " ").replace("\n", " ")
def field_list(value: object, what: str) -> list[str]:
"""A list-of-strings record field."""
if value is None:
return []
if not is_list(value):
msg = f"{what} must be a list of strings"
raise PatchError(msg)
return [field(item, f"{what}[{index}]") for index, item in enumerate(value)]
def build_claims(raw: object, unit_id: str, status: str) -> dict[str, Claim]:
"""Validate the three named claims. A written patch needs all three CONFIDENT."""
claims_map: JsonMap = {}
if raw is not None:
if not is_map(raw):
msg = f"{unit_id}: claims must be an object keyed by claim name"
raise PatchError(msg)
claims_map = raw
out: dict[str, Claim] = {}
for key in CLAIM_KEYS:
claim = claims_map.get(key)
if not is_map(claim):
if status == "patch_written":
msg = f"{unit_id}: status is patch_written but claim {key!r} is missing"
raise PatchError(msg)
continue
state = field(claim.get("state"), f"{unit_id} claim {key}.state").upper()
if state not in CLAIM_STATES:
msg = (
f"{unit_id}: claim {key!r} has state {state!r}; want one of "
f"{', '.join(CLAIM_STATES)}"
)
raise PatchError(msg)
evidence = line_field(claim.get("evidence"), f"{unit_id} claim {key}.evidence")
out[key] = Claim(state=state, evidence=evidence)
if status == "patch_written":
not_confident = [k for k in CLAIM_KEYS if out[k]["state"] != "CONFIDENT"]
if not_confident:
msg = (
f"{unit_id}: status is patch_written but {', '.join(not_confident)} "
"is not CONFIDENT -- a patch is written only when all three claims "
"are; record the unit as declined instead."
)
raise PatchError(msg)
return out
def build_attack_path(item: object, what: str) -> AttackPath:
"""One of the fresh reviewer's attack paths: a known kind and a non-empty text."""
if not is_map(item):
msg = f"{what} is not an object with a kind and a text"
raise PatchError(msg)
try:
kind = PathKind(field(item.get("kind"), f"{what}.kind"))
except ValueError as error:
kinds = ", ".join(k.value for k in PathKind)
msg = f"{what} kind {item.get('kind')!r} is not one of {kinds}"
raise PatchError(msg) from error
text = line_field(item.get("text"), f"{what}.text").strip()
if not text:
msg = f"{what} has no text saying what the path is"
raise PatchError(msg)
return AttackPath(kind=kind, text=text)
def build_attack_paths(raw: object, unit_id: str, status: str) -> list[AttackPath]:
"""Validate the fresh reviewer's attack paths; a written patch lists them, none blocking it."""
written = status == "patch_written"
if raw is None:
if written:
msg = (
f'{unit_id}: status is patch_written but "attack_paths" is missing -- it must '
"list what the fresh reviewer reported, or be empty when it reported none."
)
raise PatchError(msg)
return []
if not is_list(raw):
msg = f"{unit_id}: attack_paths must be a list"
raise PatchError(msg)
paths = [build_attack_path(item, f"{unit_id} attack_paths[{i}]") for i, item in enumerate(raw)]
blocking = next((path for path in paths if path["kind"].blocks_patch), None)
if written and blocking is not None:
msg = (
f"{unit_id}: status is patch_written but the fresh reviewer reported a path it did not "
f"set aside ({blocking['kind'].value}: {blocking['text']}) -- that blocks the "
"patch: record the unit as declined, or, if a later round's reviewer reported no "
"path that counts, leave this earlier round's path out of the record."
)
raise PatchError(msg)
return paths
def build_attempts(raw: object, unit_id: str, status: str) -> int | None:
"""The number of fix attempts made; None for a stale unit, whatever its record says."""
if status == "skipped_stale":
return None
if raw is None:
msg = (
f'{unit_id}: "attempts" is missing -- it must say how many fix attempts were made '
"(1, or 2 for a unit that went through the revision round; 0 only when its workspace "
"could not be opened)."
)
raise PatchError(msg)
if not is_int(raw) or raw < 0:
msg = f'{unit_id}: "attempts" must be a whole number of fix attempts made'
raise PatchError(msg)
if status == "patch_written" and raw == 0:
msg = f"{unit_id}: a written patch took at least one attempt; attempts cannot be 0"
raise PatchError(msg)
return raw
def build_confirmed(raw: object, unit_id: str, status: str) -> bool | None:
"""The verifier's word on whether the finding is real; None if unstated or for a stale unit."""
if raw is None or status == "skipped_stale":
return None
if not isinstance(raw, bool):
msg = f'{unit_id}: "confirmed" must be true or false'
raise PatchError(msg)
if not raw and status != "declined":
msg = f'{unit_id}: "confirmed" is false, so the unit must be declined: no patch for it'
raise PatchError(msg)
return raw
def build_unit(item: object, index: int) -> Unit:
"""Validate one unit from patches.json into the shape the writers use."""
if not is_map(item):
msg = f"patches.json unit {index} is not an object"
raise PatchError(msg)
unit_id = field(item.get("id"), f"unit {index} id")
if not FINDING_ID_RE.match(unit_id):
msg = f"unit {index} id {unit_id!r} is not a finding id (want F<number>, at most 9 digits)"
raise PatchError(msg)
status = field(item.get("status"), f"{unit_id} status")
if status not in STATUSES:
msg = f"{unit_id}: status {status!r} is not one of {', '.join(STATUSES)}"
raise PatchError(msg)
claims = build_claims(item.get("claims"), unit_id, status)
decline_reason = field(item.get("decline_reason"), f"{unit_id} decline_reason")
if status != "patch_written" and not decline_reason:
msg = f"{unit_id}: status {status} needs a decline_reason saying why no patch was written"
raise PatchError(msg)
attack_paths = build_attack_paths(item.get("attack_paths"), unit_id, status)
confirmed = build_confirmed(item.get("confirmed"), unit_id, status)
reachable = next((p for p in attack_paths if p["kind"] is PathKind.STILL_REACHABLE), None)
if confirmed is False and reachable is not None:
msg = (
f'{unit_id}: "confirmed": false cannot sit beside a still_reachable path '
f"({reachable['text']}) -- if that path reaches the function or line the finding "
'names, the panel disagreed: drop "confirmed" and decline on that path; if it '
"reaches only other code, the refutation stands: record that path's kind as set_aside"
)
raise PatchError(msg)
untested = item.get("untested")
if untested is None and status == "patch_written":
msg = (
f'{unit_id}: status is patch_written but "untested" is missing -- it must '
"say (true/false) whether the project's own tests exercise the patched "
"code, because the patch header warns the reader when they do not."
)
raise PatchError(msg)
if untested is not None and not isinstance(untested, bool):
msg = f'{unit_id}: "untested" must be true or false'
raise PatchError(msg)
return Unit(
id=unit_id,
title=line_field(item.get("title"), f"{unit_id} title") or unit_id,
status=status,
summary=line_field(item.get("summary"), f"{unit_id} summary"),
claims=claims,
untested=untested is True,
tests_run=line_field(item.get("tests_run"), f"{unit_id} tests_run"),
reviewed_paths=field_list(item.get("reviewed_paths"), f"{unit_id} reviewed_paths"),
attack_paths=attack_paths,
attempts=build_attempts(item.get("attempts"), unit_id, status),
confirmed=confirmed,
decline_reason=decline_reason,
recommendation=field(item.get("recommendation"), f"{unit_id} recommendation"),
)
def load_units(patch_dir: Path) -> list[Unit]:
"""Read and validate patches.json (an object with a `units` array) against the prepared units.
Every unit id must have the `findings/<id>.json` that `--prepare` laid out.
"""
try:
raw = strictjson.load(patch_dir / "patches.json")
except FileNotFoundError as error:
msg = "patches.json is missing from the patch directory. Write it before running this."
raise PatchError(msg) from error
except ValueError as error:
msg = f"patches.json is not valid JSON: {error}"
raise PatchError(msg) from error
units_raw = raw.get("units") if is_map(raw) else raw
if not is_list(units_raw):
msg = 'patches.json must be an object with a "units" array'
raise PatchError(msg)
units = [build_unit(item, i) for i, item in enumerate(units_raw)]
counted = Counter(unit["id"] for unit in units)
repeated = sorted(
(unit_id for unit_id, count in counted.items() if count > 1), key=finding_number
)
if repeated:
msg = f"patches.json uses these unit ids more than once: {', '.join(repeated)}"
raise PatchError(msg)
findings_dir = patch_dir / FINDINGS_DIR_NAME
if not os.path.isdir(findings_dir):
msg = f"the patch dir has no {FINDINGS_DIR_NAME}/ folder; run --prepare first"
raise PatchError(msg)
prepared = {path.stem for path in findings_dir.glob("*.json") if FINDING_ID_RE.match(path.stem)}
unprepared = [unit["id"] for unit in units if unit["id"] not in prepared]
if unprepared:
msg = (
f"patches.json names {', '.join(unprepared)}, which --prepare did not lay out; "
f"this run's units are {', '.join(sorted(prepared, key=finding_number)) or 'none'} "
"(the ids --prepare printed)"
)
raise PatchError(msg)
return units
def read_diff(patch_dir: Path, unit_id: str, required: bool) -> bytes | None:
"""The raw diff git wrote for this unit; None only if absent and optional.
A required one (a written patch) must exist and hold at least one
`diff --git` section, since the patch and its diffstat are built from it.
"""
path = patch_dir / f"{unit_id}.diff"
if not os.path.isfile(path):
if required:
msg = (
f"{unit_id}: status is patch_written but {unit_id}.diff is missing from the "
"patch directory. Write the staged diff with git diff --output before "
"running this script."
)
raise PatchError(msg)
return None
data = path.read_bytes()
if required and DIFF_HEADER.encode("ascii") not in data:
msg = f"{unit_id}.diff contains no '{DIFF_HEADER.strip()}' header; it is not a git diff"
raise PatchError(msg)
return data
def display_name(name: str | None) -> str | None:
"""A `--- `/`+++ ` line's file name for display: a/ or b/ dropped, None for /dev/null."""
if name is None:
return None
name = name.rstrip("\r")
if not name.startswith('"'):
name = name.split("\t", 1)[0]
if name == "/dev/null":
return None
if name.startswith(('"a/', '"b/')):
return '"' + name[3:]
return name[2:] if name[:2] in {"a/", "b/"} else name
def section_stat(lines: list[str]) -> DiffStat:
"""One `diff --git` section's file name and added/deleted line counts."""
names: dict[str, str] = {}
modes: dict[str, str] = {}
added = deleted = 0
binary = False
in_hunk = False
for line in lines[1:]:
if in_hunk:
if line.startswith("+"):
added += 1
elif line.startswith("-"):
deleted += 1
elif line.startswith(("GIT binary patch", "Binary files ")):
binary = True
elif line.startswith("@@ "):
in_hunk = True
else:
for key in ("--- ", "+++ "):
if line.startswith(key):
names[key.strip()] = line[4:]
for key in ("old mode", "new mode", "new file mode", "rename from", "rename to"):
if line.startswith(key + " "):
modes[key] = line[len(key) + 1 :].strip()
if modes.get("rename from") and modes.get("rename to"):
path = f"{modes['rename from']} => {modes['rename to']}"
else:
header = lines[0][len(DIFF_HEADER) :].rstrip("\r")
cut = header.rfind(" b/")
fallback = header[cut + 3 :] if cut >= 0 else header
path = display_name(names.get("+++")) or display_name(names.get("---")) or fallback
old_mode, new_mode = modes.get("old mode"), modes.get("new mode")
if old_mode and new_mode and old_mode != new_mode:
path += f" (mode {old_mode} -> {new_mode})"
elif modes.get("new file mode") not in {None, REGULAR_FILE_MODE}:
path += f" (new file, mode {modes['new file mode']})"
return DiffStat(path=path, added="-" if binary else added, deleted="-" if binary else deleted)
def numstat(diff: bytes) -> list[DiffStat]:
"""Per-file added/deleted line counts, parsed from the diff itself."""
stats: list[DiffStat] = []
section: list[str] = []
for line in diff.decode("utf-8", "replace").splitlines():
if line.startswith(DIFF_HEADER):
if section:
stats.append(section_stat(section))
section = [line]
elif section:
section.append(line)
if section:
stats.append(section_stat(section))
return stats
def git_toplevel(scan_root: str) -> str | None:
"""The repository root containing scan_root, or None when git can't say."""
return git.run(scan_root, "rev-parse", "--show-toplevel") or None
def apply_check(top: str | None, patch_path: Path) -> str:
"""`git apply --check` against the user's tree: 'clean', 'conflicts: ...', or 'not_run'."""
if top is None:
return "not_run"
try:
out = subprocess.run(
["git", "-C", top, "apply", "--check", os.path.abspath(patch_path)],
env=git.ENV,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
timeout=60,
check=False,
)
except (OSError, subprocess.SubprocessError):
return "not_run"
if out.returncode == 0:
return "clean"
first = out.stderr.decode("utf-8", "replace").strip().splitlines()
return "conflicts" + (f": {first[0]}" if first else "")
def line_counts(entry: DiffStat) -> str:
"""A file's size in the diffstat: "(+a -d)", or "(binary)" where git counts no lines."""
if entry["added"] == "-":
return "(binary)"
return f"(+{entry['added']} -{entry['deleted']})"
def diffstat_lines(stats: list[DiffStat] | None) -> list[str]:
"""Diffstat as markdown bullets, or a one-line note when there is no diff to size."""
if stats is None:
return ["- _(no attempt diff was saved)_"]
if not stats:
return ["- _(no file changes recorded)_"]
return [f"- `{s['path']}` {line_counts(s)}" for s in stats]
def attack_path_lines(paths: list[AttackPath]) -> list[str]:
"""The fresh reviewer's attack paths as a note section; no lines when it reported none."""
if not paths:
return []
bullets = [f"- **{PATH_KIND_LABELS[path['kind']]}** -- {path['text']}" for path in paths]
return ["## What the fresh reviewer reported", "", *bullets, ""]
def header_comment(unit: Unit, base: str, report_ref: str) -> str:
"""The comment block prepended above the first `diff --git`; git apply ignores it."""
short = base[: plugin.SHORT_ID_CHARS]
lines = [
f"# Claude Security -- suggested patch for {unit['id']}: {unit['title']}",
f"# Applies to revision {short} (the revision these patches were built against).",
"#",
"# Verified by a panel of agents: an independent verifier reviewed this",
"# change against the finding, and a second, fresh reviewer challenged the",
"# diff for an attack path it opens, or a route it leaves open to the",
"# finding's exploit. The patch was written only because the panel stated",
"# all three of these with confidence:",
]
for key in CLAIM_KEYS:
claim = unit["claims"][key]
lines.append(f"# - {CLAIM_LABELS[key]}: {claim['evidence'] or claim['state']}")
if unit["untested"]:
lines += [
"#",
f"# NOTE: {UNTESTED_NOTICE}. The claim that",
"# behaviour is unchanged rests on review of the change and its callers,",
"# not on a test run -- weigh it accordingly before applying.",
]
if unit["summary"]:
lines += ["#", f"# {unit['summary']}"]
if unit["tests_run"]:
lines += [f"# Tests run: {unit['tests_run']}"]
lines += [
"#",
(f"# Apply, from the repository root: git apply {report_ref}/patches/{unit['id']}.patch"),
"#",
"",
]
return "\n".join(lines)
def note_written(unit: Unit, stats: list[DiffStat] | None, check: str, report_ref: str) -> str:
"""The F<n>.md note for a finding that earned a patch."""
lines = [
f"# {unit['id']}: {unit['title']}",
"",
f"**Status:** patch written -> `{unit['id']}.patch`",
"",
(
"**Verified by a panel of agents.** An independent verifier reviewed the "
"change against the finding and stated the three claims below with "
"confidence, and a second, fresh reviewer challenged the diff for an "
"attack path it opens, or a route it leaves open to the finding's exploit. "
"The patch was written only because the panel could vouch for it; nothing "
"here was applied for you."
),
"",
]
if unit["summary"]:
lines += [unit["summary"], ""]
lines += ["## Confidence", ""]
for key in CLAIM_KEYS:
claim = unit["claims"][key]
lines.append(f"- **{CLAIM_LABELS[key]}** -- {claim['state']}: {claim['evidence']}")
if unit["untested"]:
lines += [
"",
(
f"**{UNTESTED_NOTICE[:1].upper()}{UNTESTED_NOTICE[1:]}.** The behaviour claim "
"rests on review of the change and its callers, not on a test run."
),
]
lines += ["", f"**Tests run:** {unit['tests_run'] or 'none recorded'}", ""]
lines += ["## Change", ""]
lines += diffstat_lines(stats)
lines += ["", *attack_path_lines(unit["attack_paths"]), "## Applying it", ""]
if check == "clean":
lines.append("Applies cleanly to the working tree (checked with `git apply --check`).")
elif check == "not_run":
lines.append("The clean-apply check could not run here (git unavailable); try it yourself.")
else:
detail = check.split(": ", 1)[-1]
lines.append(
f"`git apply --check` reported a conflict ({detail}). The patch was built against the "
"recorded revision, so this usually means the working tree has uncommitted or newer "
"changes in these files -- apply it to a checkout of that revision, or merge by "
"hand."
)
lines += [
"",
"```",
f"git apply {report_ref}/patches/{unit['id']}.patch",
"```",
"",
"Or ask Claude Security to apply it, or to open a pull request for it.",
"",
]
return "\n".join(lines)
def decline_wording(unit: Unit) -> tuple[str, str] | None:
"""A declined unit's outcome as (note sentence, index parenthesis); None for a stale unit."""
count = unit["attempts"]
if count is None:
return None
if unit["confirmed"] is False:
return NOT_CONFIRMED, "finding not confirmed at this revision"
if count == 0:
sentence = "No fix for this finding could be attempted, so no patch was written."
return sentence, "no attempt possible"
made = "1 attempt" if count == 1 else f"{count} attempts"
sentence = (
f"After {made}, no fix for this finding was produced that passed the panel's review, "
"so no patch was written."
)
return sentence, f"no fix passed review in {made}"
def note_declined(unit: Unit, stats: list[DiffStat] | None) -> str:
"""The F<n>.md note for a finding with no patch."""
lines = [
f"# {unit['id']}: {unit['title']}",
"",
"**Status:** no patch produced",
"",
*([wording[0], ""] if (wording := decline_wording(unit)) else []),
unit["decline_reason"],
"",
]
blocking = [(k, c) for k, c in unit["claims"].items() if c["state"] != "CONFIDENT"]
if blocking:
lines += ["## What could not be claimed with confidence", ""]
for key, claim in blocking:
lines.append(f"- **{CLAIM_LABELS[key]}** -- {claim['state']}: {claim['evidence']}")
lines.append("")
lines += attack_path_lines(unit["attack_paths"])
if stats is not None:
lines += ["## What the rejected attempt changed", ""]
lines += diffstat_lines(stats)
lines.append("")
if unit["recommendation"]:
lines += ["## The report's original recommendation", "", unit["recommendation"], ""]
return "\n".join(lines)
def index_markdown(units: list[Unit], base: str, report_dir_name: str, report_ref: str) -> str:
"""PATCHES.md: the one-page index of every unit's outcome."""
patched = [u for u in units if u["status"] == "patch_written"]
declined = [u for u in units if u["status"] != "patch_written"]
short = base[: plugin.SHORT_ID_CHARS]
lines = [
"# Suggested patches",
"",
(
f"Targeted patches for findings in `{report_dir_name}`, each written against "
f"revision `{short}` and verified by a panel of agents before it was "
"written. Nothing here is applied, committed, or opened as a pull request "
"until you choose to do so."
),
"",
]
if patched:
lines += ["## Patches written", ""]
for unit in patched:
caveat = f" _({UNTESTED_NOTICE})_" if unit["untested"] else ""
lines.append(f"- **{unit['id']}** -- {unit['title']}: `{unit['id']}.patch`{caveat}")
lines.append("")
if declined:
lines += ["## No patch produced", ""]
for unit in declined:
wording = decline_wording(unit)
suffix = f" ({wording[1]})" if wording else ""
lines.append(f"- **{unit['id']}** -- {unit['title']}{suffix}: {unit['decline_reason']}")
lines.append("")
aside = [
(unit["id"], path)
for unit in units
for path in unit["attack_paths"]
if path["kind"] is PathKind.SET_ASIDE
]
if aside:
lines += ["## Set aside by the reviewers", ""]
lines += [f"- while reviewing **{unit_id}** -- {path['text']}" for unit_id, path in aside]
lines += [
"",
(
"Each of these was seen while reviewing the finding named beside it and was "
"set aside as outside that finding, so nothing written for that finding "
"addresses it. Unless another finding here covers it, review it yourself, or "
"ask Claude Security to scan or patch it."
),
"",
]
lines += [
"## Applying a patch",
"",
"From the repository root:",
"",
"```",
f"git apply {report_ref}/patches/F<n>.patch",
"```",
"",
(
"Each `F<n>.md` beside the patch explains the change and what was verified. "
"The job that wrote these applied, committed, pushed, and opened nothing; "
"if you want one applied, or turned into a pull request, ask Claude "
"Security and it handles that as a separate request."
),
"",
]
return "\n".join(lines)
def jsonl(
units: list[Unit],
base: str,
stats_by_id: dict[str, list[DiffStat] | None],
checks: dict[str, str],
) -> str:
"""patches.jsonl: one record per unit, machine-readable for tooling."""
return "".join(
strictjson.text({
"id": unit["id"],
"status": unit["status"],
"base": base,
"patch": f"{unit['id']}.patch" if unit["status"] == "patch_written" else None,
"note": f"{unit['id']}.md",
"claims": unit["claims"],
"untested": unit["untested"],
"tests_run": unit["tests_run"] or None,
"reviewed_paths": unit["reviewed_paths"],
"attack_paths": unit["attack_paths"],
"attempts": unit["attempts"],
"confirmed": unit["confirmed"],
"diffstat": stats_by_id.get(unit["id"]),
"apply_check": checks.get(unit["id"]),
"decline_reason": unit["decline_reason"] or None,
})
+ "\n"
for unit in units
)
def clear_stale_products(patches_dir: Path, produced: set[str]) -> list[str]:
"""Remove F<n>.patch / F<n>.md files an earlier run left that this run did not write.
Only the script's own product names (F<n>.patch, F<n>.md) are removed;
every other file in the folder is left alone.
"""
removed: list[str] = []
for path in sorted(patches_dir.iterdir()):
if path.suffix not in {".patch", ".md"} or not FINDING_ID_RE.match(path.stem):
continue
if path.name in produced or os.path.isdir(path):
continue
path.unlink()
removed.append(path.name)
return removed
def ensure_gitignore(report_dir: Path) -> str:
"""Fence the report directory with a `*` .gitignore if it has none.
Returns "written" when the fence was just added, "present" when an
existing .gitignore already ignores everything, and "open" when one exists
but has no bare `*` line; an existing file is never rewritten.
"""
path = report_dir / ".gitignore"
if os.path.lexists(path):
try:
existing = path.read_text(encoding="utf-8", errors="replace")
except OSError:
return "open"
return "present" if "*" in (line.strip() for line in existing.splitlines()) else "open"
path.write_bytes(b"*\n")
return "written"
def report_path_from_root(report_dir: Path, top: str | None, fallback: str) -> str:
"""The report directory as a path from the repository root, for the apply command.
Falls back to the bare folder name when git cannot name a root or the
folder sits outside it.
"""
if top is None:
return fallback
return absolute.relative(os.path.realpath(report_dir), os.path.realpath(top)) or fallback
def resolve_report_dir(patches_dir: Path) -> Path:
"""The report directory holding `patches_dir`, validated by name."""
# abspath folds ".." without following symlinks, so the checks below see this path's own names.
patches_abs = Path(os.path.abspath(patches_dir))
report_dir = patches_abs.parent
if patches_abs.name != PATCHES_DIR_NAME:
msg = (
f"patches dir must be a directory named {PATCHES_DIR_NAME!r} inside the "
f"report directory; got {patches_abs}"
)
raise PatchError(msg)
if not plugin.REPORT_DIR_RE.match(report_dir.name):
msg = (
"patches dir must live inside a CLAUDE-SECURITY-<timestamp> report "
f"directory; its parent is {report_dir.name!r}. Refusing rather than "
"fence the wrong directory with a .gitignore."
)
raise PatchError(msg)
return report_dir
def stamp_scan_prefix(report_dir: Path) -> str:
"""The `scan_prefix` recorded in the report's one revision stamp."""
stamps = [path for path in report_dir.iterdir() if plugin.is_revision_stamp(path)]
if len(stamps) != 1:
msg = (
f"the report directory must hold exactly one {plugin.REVISION_PREFIX}*.json "
f"stamp; {report_dir.name} holds {len(stamps)}"
)
raise PatchError(msg)
try:
stamp = strictjson.load(stamps[0])
except ValueError as error:
msg = f"{stamps[0].name} is not valid JSON: {error}"
raise PatchError(msg) from error
prefix = stamp.get("scan_prefix") if is_map(stamp) else None
if not is_str(prefix) or not scan_prefix_shaped(prefix):
msg = f"{stamps[0].name} scan_prefix {prefix!r} is not a path prefix"
raise PatchError(msg)
return prefix
def report_findings(report_dir: Path, scan_prefix: str) -> dict[str, ReportFinding]:
"""The report's findings by id, in report order, each `file` made repository-relative."""
name = plugin.JSONL_NAME
try:
lines = (report_dir / name).read_text(encoding="utf-8").splitlines()
except FileNotFoundError as error:
msg = f"{name} is missing from the report directory"
raise PatchError(msg) from error
except ValueError as error:
msg = f"{name} is not UTF-8 text: {error}"
raise PatchError(msg) from error
findings: dict[str, ReportFinding] = {}
for number, line in enumerate(lines, 1):
if not line.strip():
continue
try:
record = strictjson.loads(line)
except ValueError as error:
msg = f"{name} line {number} is not valid JSON: {error}"
raise PatchError(msg) from error
if not is_map(record):
msg = f"{name} line {number} is not a finding object"
raise PatchError(msg)
finding_id = field(record.get("id"), f"{name} line {number} id")
if not FINDING_ID_RE.match(finding_id):
msg = f"{name} line {number}: id {finding_id!r} is not a finding id"
raise PatchError(msg)
if finding_id in findings:
msg = f"{finding_id} appears more than once in {name}"
raise PatchError(msg)
severity = field(record.get("severity"), f"{finding_id} severity")
if severity not in Severity.__members__:
msg = f"{name} line {number}: {finding_id} severity {severity!r} is not a severity"
raise PatchError(msg)
declared = field(record.get("file"), f"{finding_id} file")
file = repository_file(scan_prefix, file=declared)
off_shape = not declared or declared.startswith("/") or "\\" in declared
if off_shape or file.split("/")[0] in {".", ".."}:
msg = f"{name} line {number}: {finding_id} file {declared!r} is not a repository path"
raise PatchError(msg)
findings[finding_id] = ReportFinding(Severity[severity], {**record, "file": file})
return findings
def select_units(findings: dict[str, ReportFinding], selection: str) -> list[str]:
"""The ids `selection` names -- `all`, `high`, or ids like `F1,F3` -- in report order."""
if selection == "all":
chosen = set(findings)
elif selection == "high":
chosen = {i for i, finding in findings.items() if finding.severity >= Severity.HIGH}
else:
chosen = {part.strip() for part in selection.split(",")}
malformed = sorted(i for i in chosen if not FINDING_ID_RE.match(i))
if malformed:
msg = (
f"selection {selection!r}: {', '.join(map(repr, malformed))} is not a finding id "
"(F<number>, at most 9 digits); a selection is all, high, or ids like F1,F3"
)
raise PatchError(msg)
unknown = sorted(chosen - findings.keys(), key=finding_number)
if unknown:
msg = f"{', '.join(unknown)} is not a finding in the report's {plugin.JSONL_NAME}"
raise PatchError(msg)
ids = [i for i in findings if i in chosen]
if not ids:
msg = f"nothing to patch: no finding in the report matches the selection {selection!r}"
raise PatchError(msg)
return ids
def prepare(report_dir: Path, *, selection: str, repo_root: str, base: str) -> int:
"""Lay out a patch run for the selected findings; print its directories and unit ids."""
report_dir = Path(os.path.abspath(report_dir))
if not plugin.REPORT_DIR_RE.match(report_dir.name):
msg = f"{report_dir.name!r} is not a CLAUDE-SECURITY-<timestamp> report directory"
raise PatchError(msg)
top = git_toplevel(repo_root)
if top is None:
msg = f"--repo-root {repo_root!r}: git names no repository there"
raise PatchError(msg)
if not Path(repo_root).samefile(top):
msg = f"--repo-root {repo_root!r} is not the top level of its repository; git names {top}"
raise PatchError(msg)
if git.run(repo_root, "rev-parse", "--verify", "--quiet", f"{base}^{{commit}}") is None:
msg = f"--base {base}: git cannot resolve it to a commit in {repo_root}"
raise PatchError(msg)
findings = report_findings(report_dir, stamp_scan_prefix(report_dir))
ids = select_units(findings, selection)
started = datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S")
patch_dir = report_dir / plugin.RUN_DIR_NAME / f"{PATCH_DIR_PREFIX}{started}"
try:
patch_dir.mkdir(parents=True)
except FileExistsError as error:
msg = f"{patch_dir} already exists (a run prepared this same second); run this again"
raise PatchError(msg) from error
findings_dir = patch_dir / FINDINGS_DIR_NAME
findings_dir.mkdir()
patches_dir = report_dir / PATCHES_DIR_NAME
patches_dir.mkdir(exist_ok=True)
for unit_id in ids:
text = strictjson.text(findings[unit_id].record, indent=2) + "\n"
(findings_dir / f"{unit_id}.json").write_bytes(text.encode())
print(f"patch_dir: {patch_dir}")
print(f"patches_dir: {patches_dir}")
print(f"units: {strictjson.text(ids)}")
return 0
def render(patch_dir: Path, patches_dir: Path, scan_root: str, base: str) -> int:
units = load_units(patch_dir)
report_dir = resolve_report_dir(patches_dir)
top = git_toplevel(scan_root)
report_ref = shlex.quote(report_path_from_root(report_dir, top, report_dir.name))
stats_by_id: dict[str, list[DiffStat] | None] = {}
checks: dict[str, str] = {}
produced: set[str] = set()
for unit in units:
written = unit["status"] == "patch_written"
diff = read_diff(patch_dir, unit["id"], required=written)
stats = numstat(diff) if diff is not None else None
stats_by_id[unit["id"]] = stats
if written and diff is not None:
patch_path = patches_dir / f"{unit['id']}.patch"
header = header_comment(unit, base, report_ref)
patch_path.write_bytes(header.encode("utf-8", "surrogateescape") + diff)
check = apply_check(top, patch_path)
checks[unit["id"]] = check
note = note_written(unit, stats, check, report_ref)
produced.add(f"{unit['id']}.patch")
print(f"{unit['id']}: patch written -> {patch_path} (apply check: {check})")
else:
note = note_declined(unit, stats)
print(f"{unit['id']}: no patch ({unit['status']}) -> {unit['id']}.md")
(patches_dir / f"{unit['id']}.md").write_bytes(note.encode("utf-8", "surrogateescape"))
produced.add(f"{unit['id']}.md")
index = index_markdown(units, base, report_dir.name, report_ref)
(patches_dir / "PATCHES.md").write_bytes(index.encode("utf-8", "surrogateescape"))
(patches_dir / "patches.jsonl").write_bytes(jsonl(units, base, stats_by_id, checks).encode())
for name in clear_stale_products(patches_dir, produced):
print(f"removed stale {name} (not produced by this run)")
swept, warnings = remove_workspaces_in(patch_dir)
for name in swept:
print(f"removed workspace {name}")
removed, more_warnings = remove_patch_run(patch_dir)
for path in removed:
print(f"removed {path}")
for warning in warnings + more_warnings:
print(f"WARNING: {warning}")
fence = ensure_gitignore(report_dir)
if fence == "written":
print(f"fenced {report_dir} with .gitignore")
elif fence == "open":
print(
f"WARNING: {report_dir}/.gitignore exists but does not ignore everything "
"('*'); the report and these patches are NOT fenced off from git add. "
"Left untouched -- edit it yourself if you want them ignored."
)
patched = sum(1 for u in units if u["status"] == "patch_written")
print(
f"wrote PATCHES.md and patches.jsonl into {patches_dir} "
f"({patched} patched, {len(units) - patched} declined)"
)
return 0
def misplaced_patch_run_reason(patch_dir: Path) -> str | None:
"""Why `patch_dir` is not where a patch run lives, or None when it is:
`<report>/.claude-security-run/patch-<ts>`, none of the three a symbolic link."""
run_dir = patch_dir.parent
report_dir = run_dir.parent
if not PATCH_DIR_RE.match(patch_dir.name):
return f"'{patch_dir.name}' is not named patch-<timestamp>"
if run_dir.name != plugin.RUN_DIR_NAME:
return f"'{patch_dir.name}' is not inside {plugin.RUN_DIR_NAME}/"
if not plugin.REPORT_DIR_RE.match(report_dir.name):
return f"'{report_dir.name}' is not a CLAUDE-SECURITY-<timestamp> report directory"
for directory in (patch_dir, run_dir, report_dir):
if is_link(directory):
return f"'{directory}' is a symbolic link"
return None
def refuse_reason(path: Path) -> str | None:
"""Why `path` may NOT be deleted as a scratch workspace, or None when it may.
Only `<report>/.claude-security-run/patch-<ts>/scratch-F<n>` holding its
own `.git` may be deleted; every other shape is refused.
"""
leaf = Path(os.path.abspath(path))
if is_link(leaf):
return "it is a symbolic link"
if not os.path.isdir(leaf):
return "it is not a directory"
if not SCRATCH_NAME_RE.match(leaf.name):
return "its name is not scratch-F<n>"
if reason := misplaced_patch_run_reason(leaf.parent):
return reason
dot_git = leaf / ".git"
if is_link(dot_git) or not os.path.isdir(dot_git):
return "it holds no .git directory of its own"
return None
def retry_removal(
root: str,
func: Callable[..., object],
path: str,
exc_info: tuple[type[BaseException], BaseException, TracebackType],
) -> None:
"""Retry a removal rmtree could not do, after making the entry deletable.
On Windows that is the entry's read-only bit (never a symlink's); on POSIX the owner write bit
of its parent, granted only to a directory (never a symlink) inside `root`, the tree being
removed. An entry already gone counts as deleted.
"""
if isinstance(exc_info[1], FileNotFoundError):
return
if func not in {os.unlink, os.rmdir}:
raise exc_info[1]
with suppress(FileNotFoundError):
if os.name == "nt":
if not is_link(path):
Path(path).chmod(stat.S_IWRITE)
else:
parent = Path(os.path.abspath(path)).parent
if os.path.commonpath([root, parent]) == root and not is_link(parent):
mode = parent.stat().st_mode
if not mode & stat.S_IWUSR:
parent.chmod(mode | stat.S_IWUSR)
func(path)
def remove_workspace(path: Path) -> None:
"""Delete one scratch workspace, refusing anything off the fenced layout."""
reason = refuse_reason(path)
if reason is not None:
msg = f"refusing to remove '{path}': {reason}"
raise PatchError(msg)
root = os.path.abspath(path)
try:
shutil.rmtree(root, onerror=functools.partial(retry_removal, root))
except OSError as error:
detail = console.removal_failure_detail(error)
msg = f"could not remove '{path}': {detail}"
raise PatchError(msg) from error
def remove_workspaces_in(patch_dir: Path) -> tuple[list[str], list[str]]:
"""Remove every scratch workspace in a patch run directory.
Returns (removed names, warnings). Never raises: a workspace that cannot
be removed is reported as a warning.
"""
removed: list[str] = []
warnings: list[str] = []
try:
paths = sorted(patch_dir.iterdir())
except OSError as error:
return removed, [f"could not list '{patch_dir}': {error}"]
for path in paths:
if not path.name.startswith("scratch-"):
continue
try:
remove_workspace(path)
except PatchError as error:
warnings.append(str(error))
else:
removed.append(path.name)
return removed, warnings
def remove_patch_run(patch_dir: Path) -> tuple[list[Path], list[str]]:
"""Remove a finished patch run directory, and its run directory if now empty.
Returns (removed paths, warnings). Never raises; only the recipe's own
`<report>/.claude-security-run/patch-<ts>` layout is deleted.
"""
target = Path(os.path.abspath(patch_dir))
run_dir = target.parent
if reason := misplaced_patch_run_reason(target):
return [], [f"left '{patch_dir}' in place: {reason}"]
root = str(target)
try:
shutil.rmtree(root, onerror=functools.partial(retry_removal, root))
except OSError as error:
detail = console.removal_failure_detail(error)
return [], [f"could not remove '{patch_dir}': {detail}"]
try:
run_dir.rmdir()
except OSError:
return [target], []
return [target, run_dir], []
def remove_args(argv: list[str]) -> RemoveArgs:
"""The `--remove-scratch` command line, parsed; a usage error exits 2."""
parser = argparse.ArgumentParser(
prog="patch_artifacts.py --remove-scratch",
description="Delete one fenced scratch workspace.",
allow_abbrev=False,
)
parser.add_argument("workspace", help="the scratch workspace a patch run made")
return parser.parse_args(argv, namespace=RemoveArgs())
def prepare_args(argv: list[str]) -> PrepareArgs:
"""The `--prepare` command line, parsed; a usage error exits 2."""
parser = argparse.ArgumentParser(
prog="patch_artifacts.py --prepare",
description="Lay out a patch run for a report's selected findings.",
allow_abbrev=False,
)
parser.add_argument("report_dir", help="the CLAUDE-SECURITY-<timestamp> report directory")
parser.add_argument("selection", help="all, high, or finding ids such as F1,F3")
parser.add_argument("--repo-root", required=True, help="the repository's top-level directory")
parser.add_argument("--base", required=True, help="the revision every patch is built against")
args = parser.parse_args(argv, namespace=PrepareArgs())
if not os.path.isdir(args.report_dir):
parser.error(f"report dir is not a directory: {args.report_dir}")
if not plugin.SHA_RE.match(args.base):
parser.error(f"--base {args.base!r} is not a hex revision id")
return args
def render_args(argv: list[str]) -> RenderArgs:
"""The render command line, parsed; a usage error exits 2."""
parser = argparse.ArgumentParser(
prog="patch_artifacts.py",
description="Render suggested-fix patch files and notes from a patch run directory.",
epilog=(
"Also: --prepare <report_dir> <selection> --repo-root <dir> --base <sha> lays out "
"a patch run; --remove-scratch <workspace> deletes one fenced scratch workspace."
),
allow_abbrev=False,
)
parser.add_argument("patch_dir", help="the patch run dir holding patches.json and F<n>.diff")
parser.add_argument("patches_dir", help="the report's patches/ directory to write into")
parser.add_argument("scan_root", help="the user's repository root (for git apply --check)")
parser.add_argument("--base", required=True, help="the revision every patch applies to")
args = parser.parse_args(argv, namespace=RenderArgs())
for label, path in (("patch dir", args.patch_dir), ("patches dir", args.patches_dir)):
if not os.path.isdir(path):
parser.error(f"{label} is not a directory: {path}")
if not plugin.SHA_RE.match(args.base):
parser.error(f"--base {args.base!r} is not a hex revision id")
return args
def dispatch(argv: list[str]) -> int:
"""Run the form the first argument names. Raises PatchError; a usage error exits 2."""
if argv and argv[0] == "--remove-scratch":
workspace = remove_args(argv[1:]).workspace
remove_workspace(Path(workspace))
print(f"removed workspace {workspace!r}")
return 0
if argv and argv[0] == "--prepare":
wanted = prepare_args(argv[1:])
return prepare(
Path(wanted.report_dir),
selection=wanted.selection,
repo_root=wanted.repo_root,
base=wanted.base,
)
args = render_args(argv)
return render(Path(args.patch_dir), Path(args.patches_dir), args.scan_root, args.base)
def main(argv: list[str]) -> int:
try:
return dispatch(argv)
except PatchError as error:
sys.stderr.write(f"patch_artifacts.py: {error}\n")
return 1
except OSError as error:
sys.stderr.write(
f"patch_artifacts.py: could not read or write the report's files: {error}\n"
)
return 1
if __name__ == "__main__":
console.tolerate_undecodable_names()
sys.exit(main(sys.argv[1:]))
|