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
|
#!/usr/bin/env python3
"""Write scan-meta.json for a run: the record of what was scanned.
Mints the scan's id and captures, from git itself: the revision, the scan
root's path within the repository, the credential-free https form of its
remote and, for a whole-repository scan, the tree's top-level directories,
printed as a JSON array on a `top_level_dirs:` line and recorded in the meta
file with any root-level symbolic links left out of them.
Usage:
write_scan_meta.py <run_dir> <scan_root> --mode scan|changes|commit
--effort low|medium|high|max [--scope a,b] [--base <ref>]
[--merge-base <sha>] [--commit <sha>]
Exits 0 on success, 1 on a refusal naming what is wrong, 2 on a usage error;
the file is written only on success.
Python 3.9-compatible, stdlib only.
"""
from __future__ import annotations
import argparse
import os
import re
import stat
import subprocess
import sys
import uuid
from pathlib import Path
from typing import Literal, NamedTuple, TypedDict
from urllib.parse import quote, unquote, urlsplit
# 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, plugin, strictjson
GIT_ENV = dict(os.environ, GIT_TERMINAL_PROMPT="0")
class Revision(TypedDict, total=False):
"""What was scanned. `versioned` is always present; the rest when in git."""
versioned: bool
commit: str | None
parent: str | None
branch: str | None
dirty: bool | None
sparse: Literal[True]
not_checked_out_dirs: list[str]
base: str | None
merge_base: str | None
class Args(argparse.Namespace):
"""The parsed command line."""
run_dir: str = ""
scan_root: str = ""
mode: str = ""
effort: str = ""
scope: str = ""
base: str | None = None
merge_base: str | None = None
commit: str | None = None
class MetaError(Exception):
"""A refusal: the command line was well-formed but the run cannot be recorded."""
def git(cwd: str, *args: str) -> str | None:
"""One read-only git call, prompts suppressed. None on any failure."""
try:
out = subprocess.run(
["git", "-C", cwd, *args],
env=GIT_ENV,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
timeout=30,
check=False,
)
except (OSError, subprocess.SubprocessError):
return None
if out.returncode != 0:
return None
return out.stdout.decode("utf-8", "surrogateescape").rstrip("\r\n")
class Extent(NamedTuple):
"""The scan target's top-level directories, its root-level symbolic links, and
the tracked top-level directories its working tree does not hold."""
dirs: list[str]
symlinks: list[str]
absent: list[str]
def tree_extent(scan_root: str) -> Extent | None:
"""The scan target's top-level directories, computed from the tree itself.
Inside a git work tree the tracked files decide; where nothing is tracked
the immediate subdirectories do. Entries are classified without following
symbolic links, so nothing outside the checkout is read: a root-level
symbolic link is never one of the directories, and is named in `symlinks`
so the report can say it was not followed. `.git` and `CLAUDE-SECURITY-*`
report directories are excluded. None when the tree could not be listed.
"""
names: set[str] = set()
symlinks: set[str] = set()
listing = git(scan_root, "ls-files", "-z")
if listing:
for path in listing.split("\0"):
top, sep, _rest = path.partition("/")
if sep and top:
names.add(top)
elif path:
try:
mode = os.lstat(os.path.join(scan_root, path)).st_mode
except OSError:
continue
if stat.S_ISLNK(mode):
symlinks.add(path)
elif stat.S_ISDIR(mode):
names.add(path)
else:
try:
with os.scandir(scan_root) as entries:
for entry in entries:
if entry.is_symlink():
symlinks.add(entry.name)
elif entry.is_dir(follow_symlinks=False):
names.add(entry.name)
except OSError:
return None
names.discard(".git")
kept = sorted(n for n in names if not n.startswith(plugin.REPORT_DIR_PREFIX))
on_disk = {n for n in kept if os.path.lexists(os.path.join(scan_root, n))}
dirs = [n for n in kept if n in on_disk]
return Extent(dirs, sorted(symlinks), [n for n in kept if n not in on_disk])
def sparse_checkout(scan_root: str, extent: Extent | None) -> list[str] | None:
"""The tracked top-level directories a sparse checkout left out; None when it is not one."""
if git(scan_root, "config", "--bool", "core.sparseCheckout") != "true":
return None
return extent.absent if extent else []
REMOTE_SCHEMES = frozenset({"http", "https", "ssh", "git", "git+ssh"})
def sanitize_remote(url: str | None) -> str | None:
"""`url` as a credential-free https URL naming the same repository, or None.
Userinfo, query and fragment are stripped; the scheme becomes https and the
host lowercase; a port survives only from an http or https URL; scp-like
`user@host:path` is read as ssh; a trailing `/` or `.git` is dropped, so
the ssh and https spellings of one repository come out equal. A URL that
does not name a hosted repository is None.
"""
text = (url or "").strip()
if not text or "[" in text or "]" in text or strictjson.has_lone_surrogate(text):
return None
if "://" in text:
try:
parts = urlsplit(text)
port = parts.port if parts.scheme.lower() in {"http", "https"} else None
except ValueError:
return None
if parts.scheme.lower() not in REMOTE_SCHEMES:
return None
host = (parts.hostname or "").lower()
location = host if port is None else f"{host}:{port}"
path = parts.path
else:
# Userinfo splits off first: an optional user@ group backtracks and leaks the secret.
rest = text.rpartition("@")[2]
matched = re.match(r"([^@:/\\]{2,}):(.*)", rest)
if not matched:
return None
location = matched[1].lower()
path = matched[2]
if not re.fullmatch(r"[a-z0-9.-]+(?::\d+)?", location):
return None
path = quote(unquote(path.strip("/"))).removesuffix(".git").rstrip("/")
if not path:
return None
return f"https://{location}/{path}"
def worktree_dirty(scan_root: str) -> bool | None:
"""True/False/None (unknown) for the working tree, ignoring report dirs."""
status = git(scan_root, "status", "--porcelain", "--untracked-files=all")
if status is None:
return None
for line in status.splitlines():
if len(line) < len("XY P"):
continue
path = line[3:].split(" -> ")[-1]
if any(part.startswith(plugin.REPORT_DIR_PREFIX) for part in path.split("/")[:-1]):
continue
return True
return False
def capture_revision(scan_root: str, opts: Args) -> Revision:
versioned = git(scan_root, "rev-parse", "--is-inside-work-tree") == "true"
if opts.mode == "commit":
if not versioned:
msg = f"--mode commit needs a git repository; {scan_root!r} is not one"
raise MetaError(msg)
commit_arg = opts.commit or ""
sha = git(scan_root, "rev-parse", "--verify", "--quiet", commit_arg + "^{commit}")
if not sha:
msg = f"--commit {commit_arg!r} does not resolve to a commit"
raise MetaError(msg)
return {
"versioned": True,
"commit": sha,
"parent": git(scan_root, "rev-parse", "--verify", "--quiet", sha + "^") or None,
"branch": git(scan_root, "rev-parse", "--abbrev-ref", "HEAD"),
"dirty": False,
}
if not versioned:
return {"versioned": False}
revision: Revision = {
"versioned": True,
"commit": git(scan_root, "rev-parse", "HEAD"),
"branch": git(scan_root, "rev-parse", "--abbrev-ref", "HEAD"),
"dirty": worktree_dirty(scan_root),
}
if opts.mode == "changes":
revision["base"] = opts.base
revision["merge_base"] = opts.merge_base
return revision
def scoped(entry: str, scan_root: str) -> str:
"""A scope entry relative to the scan root; an absolute one that is not inside it is refused."""
if not absolute.spelled(entry):
return entry
literal = os.path.abspath(entry)
parent, name = os.path.split(literal)
try:
resolutions = [
literal,
os.path.join(os.path.realpath(parent), name),
os.path.realpath(entry),
]
except OSError:
resolutions = [literal]
for resolved in resolutions:
try:
relative = os.path.relpath(resolved, scan_root).replace("\\", "/")
except ValueError:
continue
if relative != ".." and not relative.startswith("../"):
return relative
msg = f"--scope entry {entry!r} is not inside the scan root {scan_root!r}"
raise MetaError(msg)
def parse_options(argv: list[str]) -> Args:
"""The parsed command line; anything wrong with it is argparse's exit 2."""
ap = argparse.ArgumentParser(prog="write_scan_meta")
ap.add_argument("run_dir")
ap.add_argument("scan_root")
ap.add_argument("--mode", required=True, choices=plugin.MODES)
ap.add_argument("--effort", required=True, choices=["low", "medium", "high", "max"])
ap.add_argument("--scope")
ap.add_argument("--base")
ap.add_argument("--merge-base", dest="merge_base")
ap.add_argument("--commit")
opts = ap.parse_args(argv, namespace=Args())
if opts.mode == "commit" and not opts.commit:
ap.error("--mode commit requires --commit <sha>")
if not os.path.isdir(opts.run_dir):
ap.error(f"run directory does not exist: {opts.run_dir}")
return opts
def main(argv: list[str]) -> int:
opts = parse_options(argv)
run_dir = os.path.realpath(os.path.abspath(opts.run_dir))
scan_root = os.path.realpath(os.path.abspath(opts.scan_root))
revision = capture_revision(scan_root, opts)
extent = tree_extent(scan_root)
absent = sparse_checkout(scan_root, extent) if revision.get("versioned") else None
if absent is not None:
revision["sparse"] = True
revision["not_checked_out_dirs"] = absent
scan_prefix = (
git(scan_root, "rev-parse", "--show-prefix") if revision.get("versioned") else None
)
remote = (
sanitize_remote(git(scan_root, "remote", "get-url", "origin"))
if revision.get("versioned")
else None
)
scope = [scoped(entry.strip(), scan_root) for entry in opts.scope.split(",") if entry.strip()]
if scope and all(s in {".", "./"} for s in scope):
scope = []
whole_repo = opts.mode == "scan" and not scope
if whole_repo and extent is None:
sys.stderr.write(f"write_scan_meta: could not list {scan_root}; top_level_dirs unknown\n")
top_level, symlinks = (extent.dirs, extent.symlinks) if whole_repo and extent else (None, None)
if symlinks:
sys.stderr.write(
"write_scan_meta: root-level symbolic links not followed, "
f"left out of top_level_dirs: {', '.join(symlinks)}\n"
)
meta: dict[str, object] = {
"scan_id": str(uuid.uuid4()),
"scan_root": scan_root,
"scan_prefix": scan_prefix,
"remote": remote,
"run_dir": run_dir,
"flow": "scan" if opts.mode == "scan" else "changes",
"agent": f"{plugin.NAME}:{plugin.NAME}",
"mode": opts.mode,
"scope": scope,
"effort": opts.effort,
"model": None,
"revision": revision,
"revision_source": "self-reported",
"top_level_dirs": top_level,
"unfollowed_symlinks": symlinks,
}
path = os.path.join(run_dir, "scan-meta.json")
with open(path, "w", encoding="utf-8", newline="\n") as out:
out.write(strictjson.text(meta, indent=2) + "\n")
sys.stdout.write(f"scan-meta.json written: {path}\n")
sys.stdout.write(f"revision: {revision.get('commit') or 'UNVERSIONED'}\n")
if absent is not None:
listed = strictjson.text(absent)
sys.stdout.write(f"sparse checkout: top-level directories not checked out: {listed}\n")
sys.stdout.write(f"top_level_dirs: {strictjson.text(top_level)}\n")
return 0
if __name__ == "__main__":
console.tolerate_undecodable_names()
try:
sys.exit(main(sys.argv[1:]))
except MetaError as error:
sys.stderr.write(f"write_scan_meta: {error}\n")
sys.exit(1)
except OSError as error:
sys.stderr.write(f"write_scan_meta: could not write the run's output: {error}\n")
sys.exit(1)
|