Repository navigation
Expand file tree
/
Copy pathpatch_policy.py
More file actions
463 lines (410 loc) · 22.4 KB
/
Copy pathpatch_policy.py
File metadata and controls
463 lines (410 loc) · 22.4 KB
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
#!/usr/bin/env python3
r"""CI-privileged patch-path policy for the groom auto-builder (BE-4404).
The groom `build` job hands a prompt-injectable, credential-free builder agent a
finding and lets it edit a checkout; a later job opens a **review-gated** PR from
that patch. Review gates the *merge*, not CI *execution* — a same-repo branch
push runs the caller's CI (with repository secrets + a writable token) BEFORE any
human reads the diff. So a patch that touches a path the caller's pre-review CI
*executes* is a code-execution primitive, not a doc change. This module is the
deny-list that downgrades such a patch from an auto-PR to a filed issue (a human
authors the privileged change instead). It also covers owner-gated
**dataset-of-record** paths (BE-9609): graded eval cases whose merge publishes
immutable versions and whose changes are reserved for the dataset owner — not a
CI-execution risk, but the same downgrade-to-issue treatment.
`denied_paths(paths)` returns the subset of changed paths in either class;
`denied_entries(entries)` additionally denies, by MODE, what path shape cannot
see — a symlink-typed change in a `suites` tree, the indirection that would
point a dataset importer's glob at an undenied tree. Both take an optional
`extra_patterns` tuple of compiled regexes so a caller can EXTEND (never narrow)
the deny-list without editing the reusable workflow (BE-4405, `extra_denied_paths`).
`main()` reads raw-diff records from stdin (the `git diff --cached --no-renames
--raw -z` producer in groom.yml's `Capture patch` step — `--raw`, not
`--name-only`, precisely so the mode bits reach the policy), compiles the
caller's extra patterns from the `EXTRA_DENIED_PATHS` env var, and prints the
denied paths. Happy path exits 0 (a no-match is empty output, not a nonzero
exit) — the caller tests non-emptiness, not the exit code. The ONE nonzero exit
is fail-closed: an `EXTRA_DENIED_PATHS` pattern that will not compile prints an
`::error::` and exits 2, so the `Capture patch` pipeline (`set -euo pipefail`,
no `|| true`) aborts and NO PR opens for any finding until the typo is fixed —
because silently dropping an uncompilable deny pattern would widen the ALLOW
side, the one direction a security control must never fail toward (see main()).
Two load-bearing invariants (see also the trimmed comment in groom.yml):
1. **Conservative DEFAULT, not proof of completeness.** The list covers the paths
a *typical* repo's CI executes (workflow/action defs, package manifests,
lockfiles, build/test config across ecosystems). It is target-repo-specific:
a repo whose CI runs something else privileged (a checked-in `scripts/`
entrypoint) must add it here. Erring WIDE is the safe direction — a false
positive only downgrades a legit refactor from a PR to an issue, and the
finding still lands in the ledger + a `groom` issue; nothing is dropped. Never
under-block.
2. **NUL-delimited comparison is mandatory.** git C-quotes and double-quote-wraps
any path containing a quote, backslash or control byte in its DEFAULT output,
so `.github/workflows/ev"il.yml` is emitted as `".github/workflows/ev\"il.yml"`
whose leading quote slips past a `^\.github/` anchor. `-z` emits raw bytes with
no quoting; this module reads those bytes. A path may itself contain a newline
(`-z` does not escape it), so each path is tested WHOLE **and** split on
newlines with EVERY line tested — the split mirrors the old
`tr '\0' '\n' | grep` line semantics, and the whole-path test catches a match
that straddles the newline (which the split alone would miss for any
`$`-anchored branch). Both directions only ADD denials — over-block, never
under-block (the safe direction).
The producer must also pass `--no-renames` (see groom.yml): with git's default
rename detection a rename collapses into ONE record naming only the
destination-side pairing, so moving a denied path to an undenied one hides the
source from the policy. `--no-renames` emits both sides as delete+add. (A
rename record also carries TWO path fields, which `parse_raw_z` would reject
as misaligned — failing closed, not open.)
Run/test: python3 -m unittest discover -s .github/groom/tests -p 'test_*.py' -v
"""
import os
import re
import sys
from collections.abc import Iterable
# --- Patterns ---------------------------------------------------------------
# Ported verbatim from the inline `grep -E` the `Capture patch` step used before
# BE-4404 (root-anchored `.github/` prefixes + the basename set), then extended.
# Path prefixes matched ROOT-anchored only (verbatim port — `^\.github/...`).
_ROOT_PREFIXES = (
r"\.github/workflows/",
r"\.github/actions/",
)
# Directory segments privileged wherever they appear in the path
# (`.husky/pre-commit`, `frontend/.husky/...`, `android/gradle/wrapper/...`).
_SEGMENT_PREFIXES = (
r"\.husky/",
r"gradle/wrapper/",
)
# Exact basenames, privileged at ANY depth (`package.json`, `sub/package.json`).
_BASENAMES = (
# --- ported verbatim (pre-BE-4404) ---
"package.json",
"Makefile",
"GNUmakefile",
"makefile", # GNU Make searches GNUmakefile, makefile, Makefile — and PREFERS
# lowercase `makefile` over `Makefile`, so it must be denied too.
"conftest.py",
"noxfile.py",
"tox.ini",
"pytest.ini",
"setup.py",
"setup.cfg",
"pyproject.toml",
"Dockerfile",
".pre-commit-config.yaml",
# --- added by BE-4404: lockfiles ---
# `npm ci` / `yarn install` / `pip install -r` re-resolve dependency tarballs
# from these on every CI run, so a rewritten `resolved` URL + `integrity` pair
# executes an attacker tarball's postinstall in credentialed CI before review
# (the BE-4012 headline gap — the pilot caller runs `npm ci` on every push).
"package-lock.json",
"npm-shrinkwrap.json",
"yarn.lock",
"pnpm-lock.yaml",
"bun.lock",
"bun.lockb",
"poetry.lock",
"uv.lock",
"Pipfile.lock",
"Cargo.lock",
"Gemfile.lock",
# --- added by BE-4404: other build/test config that runs in pre-review CI ---
"action.yml",
"action.yaml",
".gitmodules",
"Pipfile",
"Cargo.toml",
"build.rs",
"Package.swift",
"settings.gradle", # also covered by the *.gradle glob; explicit for clarity
"Gemfile",
"Rakefile",
"rakefile", # `rake` accepts the lowercase form too.
"CMakeLists.txt",
"WORKSPACE",
"WORKSPACE.bazel",
"BUILD",
"BUILD.bazel",
"Jenkinsfile",
"Taskfile.yml",
"Taskfile.yaml",
"taskfile.yml", # go-task also loads the lowercase spellings.
"taskfile.yaml",
"justfile",
"Justfile",
# Gradle wrapper scripts: Gradle CI invokes `./gradlew` (or `gradlew.bat` on
# Windows) directly, so an edited wrapper runs arbitrary code. These are at the
# repo root with no extension, so the `*.gradle` glob and `gradle/wrapper/`
# segment (which cover the jar/properties) do NOT reach them — deny by name.
"gradlew",
"gradlew.bat",
# Go: `go build`/`go test` in CI re-fetch modules from these; a `replace`
# directive in go.mod can redirect a dependency to attacker-controlled code.
"go.mod",
"go.sum",
"go.work",
"go.work.sum",
"Package.resolved", # SwiftPM pins resolved from this on `swift build`.
".pnpmfile.cjs", # pnpm executes this hook during install.
)
# Path suffixes anchored on a full segment boundary (basenames won't do — these
# carry a directory component). `.cargo/config[.toml]` can set a build `runner` or
# linker that executes during `cargo build`/`cargo test` in pre-review CI.
_PATH_SUFFIXES = (
r"\.cargo/config\.toml",
r"\.cargo/config",
)
# Dataset-of-record paths (BE-9609). NOT CI-privileged — these are graded eval
# cases whose merge publishes IMMUTABLE, owner-owned versions downstream, a change
# class a caller reserves for the dataset owner. A builder patch here is downgraded
# to an issue so a human authors it. Deliberately WIDE per invariant 1 (an
# over-block only downgrades a PR to an issue; an under-block is the hole):
# * segment-anchored (`(?:^|/)`), so a nested `suites/` tree is covered;
# * `(?:[^/]+/)*` — ZERO or more segments between `suites/` and `cases/`, so
# grouped or versioned layouts (`suites/eval/agent/cases/`,
# `suites/agent/v2/cases/`) are not a bypass, and a flat `suites/cases/`
# layout is denied too — every description of this class (README, groom.yml)
# says `suites/**/cases/`, and `**` spans zero segments;
# * `(?s:.*)` tail — any depth under `cases/`, an empty stem (`cases/.yaml`),
# and a raw newline in the name (see `_is_denied`: a glob-driven importer
# still picks such a file up, so the gate must too);
# * second alternative: a change whose FINAL segment is `cases` under a suite.
# git tracks no directories, so that path shape is a file or a SYMLINK — one
# of the indirections that would let a builder point `suites/<x>/cases` at an
# undenied directory. Path shape alone cannot catch a symlink at any OTHER
# component of the importer's glob (`suites` itself, a suite dir, a
# non-YAML name inside `cases/`) — those are caught by MODE, not path:
# see `denied_entries`, which denies any symlink-typed change carrying a
# `suites` segment.
_DATASET_OF_RECORD_REGEXES = (
r"suites/(?:[^/]+/)*cases/(?s:.*)\.ya?ml",
r"suites/(?:[^/]+/)*cases",
)
# Basename globs (`*` = any run of non-`/` chars), privileged at ANY depth.
_BASENAME_GLOBS = (
"requirements*.txt", # requirements.txt, requirements-dev.txt, ...
"*.pbxproj",
"*.gradle",
"*.gradle.kts",
"*.gemspec",
"*.cmake",
"*.bzl",
# Multi-Dockerfile conventions CI builds via `docker build -f`: the
# `Dockerfile.<tag>` suffix form (Dockerfile.prod) and the `<tag>.Dockerfile`
# extension form (prod.Dockerfile). `Dockerfile*` also over-catches
# `Dockerfile.md`-style docs — a safe over-block (downgrades to an issue).
"Dockerfile*",
"*.Dockerfile",
)
def _glob_to_regex(glob: str) -> str:
"""Translate a basename glob to a regex fragment (`*` never crosses `/`)."""
return re.escape(glob).replace(r"\*", r"[^/]*")
# IGNORECASE: macOS and Windows CI runners use case-INSENSITIVE filesystems, so a
# builder can commit `PACKAGE.JSON` / `MAKEFILE` / `DOCKERFILE` which a
# case-sensitive Linux checker would miss but which resolves to the real,
# CI-executed file once checked out on the runner. The policy explicitly targets
# Swift/Xcode (macOS) callers, so match case-insensitively. This also over-blocks
# some lowercase near-collisions (e.g. a file literally named `build`) — the safe
# direction per the module contract (over-block only downgrades a PR to an issue).
_PATTERN = re.compile(
"|".join(
[rf"^{p}" for p in _ROOT_PREFIXES]
+ [rf"(?:^|/){p}" for p in _SEGMENT_PREFIXES]
+ [rf"(?:^|/){re.escape(b)}$" for b in _BASENAMES]
+ [rf"(?:^|/){_glob_to_regex(g)}$" for g in _BASENAME_GLOBS]
+ [rf"(?:^|/){s}$" for s in _PATH_SUFFIXES]
+ [rf"(?:^|/){r}$" for r in _DATASET_OF_RECORD_REGEXES]
),
re.IGNORECASE,
)
def _is_denied(path: str, extra_patterns: Iterable[re.Pattern] = ()) -> bool:
"""True if `path` — whole, or any of its newline-split lines — is denied.
`extra_patterns` are the caller's compiled `EXTRA_DENIED_PATHS` regexes
(BE-4405). They are tested with the SAME semantics as the built-in `_PATTERN`
— `.search()` against the whole path and each newline-split line, so an
additive pattern behaves identically to a built-in one — and, being an
additive OR, they can only widen the deny-list, never narrow it.
"""
# Split on newlines and test every line: git -z can emit a path containing a
# raw newline, and the old grep matched line-by-line. Testing every line
# over-blocks such a path, never under-blocks (the safe direction).
#
# ALSO test the unsplit path. Splitting alone under-blocks any `$`-anchored
# branch whose match would STRADDLE the newline: `suites/s/cases/a\nb.yaml`
# splits into `suites/s/cases/a` + `b.yaml`, and neither line matches — yet a
# `suites/*/cases/*.yaml` glob importer happily reads that file. The
# dataset-of-record tail uses `(?s:.*)` so it matches across the newline here.
# Strictly widening: this only ever adds denials, per invariant 1.
candidates = (path, *path.split("\n"))
if any(_PATTERN.search(s) for s in candidates):
return True
return any(pat.search(s) for pat in extra_patterns for s in candidates)
def denied_paths(paths: Iterable[str], extra_patterns: Iterable[re.Pattern] = ()) -> list[str]:
"""Return the subset of `paths` a human must author, not the builder.
Two distinct classes, deliberately returned UNDIFFERENTIATED because the sole
consumer (groom.yml's `Capture patch` gate) only tests non-emptiness: paths
the caller's pre-review CI EXECUTES, and owner-gated dataset-of-record paths.
Both get the same downgrade-to-issue treatment. A future caller that needs to
ask "would this path execute in pre-review CI?" must return the class too —
do not infer it from membership in this list.
`extra_patterns` (BE-4405) are the caller's compiled `EXTRA_DENIED_PATHS`
regexes, folded into the deny test as additive-only denials — a caller may
widen the list for a repo-specific privileged path, never narrow it.
"""
# A bare `str` is iterable; without this guard a caller that passes one path as
# a string would iterate its characters and silently under-block. Fail loud.
if isinstance(paths, (str, bytes)):
raise TypeError("denied_paths expects an iterable of paths, not a single str/bytes")
return [p for p in paths if _is_denied(p, extra_patterns)]
# `git diff --raw` meta record: `:<oldmode> <newmode> <oldsha> <newsha> <status>`.
# Modes are 6 octal digits (`100644`, `120000`, `000000`); shas may be
# abbreviated (with trailing dots), and with `--no-renames` the status is a
# single letter with no score — only the two modes are consumed, so match just
# the record's leading shape.
_RAW_META = re.compile(r"\A:([0-7]{6}) ([0-7]{6}) ")
_SYMLINK_MODE = "120000"
def _has_suites_segment(path: str) -> bool:
"""True if any `/`-separated segment of `path` (or of any of its
newline-split lines — mirroring `_is_denied`'s over-blocking split) is
`suites`, case-insensitively (case-insensitive runners; see `_PATTERN`)."""
return any(
seg.lower() == "suites"
for line in (path, *path.split("\n"))
for seg in line.split("/")
)
def parse_raw_z(data: bytes) -> list[tuple[str, str, str]]:
"""Parse `git diff --raw --no-renames -z` output to (old_mode, new_mode, path).
In `-z` mode each entry is `:<oldmode> <newmode> <oldsha> <newsha> <status>`
NUL `<path>` NUL — meta and path strictly alternate (with `--no-renames`
there are no two-path R/C entries). `-z` emits path bytes verbatim with NO
C-quoting; they are decoded with `surrogateescape` (which never raises) to
preserve exotic/non-UTF-8 paths for matching. A field where a meta
record must sit but doesn't parse raises ValueError: under the caller's
`set -euo pipefail` that aborts the gate, which fails CLOSED (no PR opens) —
never silently skip an entry, which would fail open.
"""
fields = data.split(b"\x00")
# git terminates the final path with NUL, leaving one trailing empty field.
if fields and not fields[-1]:
fields.pop()
if len(fields) % 2:
raise ValueError("truncated --raw -z input: unpaired meta/path fields")
entries = []
for meta_bytes, path_bytes in zip(fields[::2], fields[1::2]):
meta = _RAW_META.match(meta_bytes.decode("utf-8", "surrogateescape"))
if not meta:
raise ValueError(f"malformed --raw -z meta record: {meta_bytes[:80]!r}")
entries.append(
(meta.group(1), meta.group(2), path_bytes.decode("utf-8", "surrogateescape"))
)
return entries
def denied_entries(
entries: Iterable[tuple[str, str, str]],
extra_patterns: Iterable[re.Pattern] = (),
) -> list[str]:
"""Return the denied paths among (old_mode, new_mode, path) raw-diff entries.
Everything `denied_paths` denies by PATH (including the caller's additive
`extra_patterns`, BE-4405), plus the mode-visible class path shape cannot
express: a SYMLINK-typed change anywhere in a `suites` tree.
A symlink at any component the dataset importer's `suites/**/cases/*.y*ml`
glob traverses (`suites` itself, a suite dir, a link inside `cases/` with no
YAML tail) redirects resolution to an undenied tree holding builder-authored
cases — the path regexes only see the shapes ending in `cases`/`.ya?ml`.
Either mode being `120000` denies: a change FROM a symlink is a resolution
change too, and over-blocking is the safe direction (invariant 1).
RESIDUAL (recorded, not shut): this sees only the BUILDER'S diff. If the
repo already carries a human-authored symlink pointing a suites component at
an outside tree, a builder file added under that TARGET tree is importable
without any denied path or mode appearing in the diff. That is invariant 1's
territory: the deny-list is a conservative default, and a caller whose
dataset surface extends beyond literal `suites/` paths must extend it.
"""
return [
path
for old_mode, new_mode, path in entries
if _is_denied(path, extra_patterns)
or (_SYMLINK_MODE in (old_mode, new_mode) and _has_suites_segment(path))
]
# Caller-supplied additive deny patterns (BE-4405). groom.yml's `Capture patch`
# step threads `inputs.extra_denied_paths` through this env var (never inline
# `${{ }}` into `run:`, matching the file's SINK/CADENCE/etc. convention).
_EXTRA_DENIED_PATHS_ENV = "EXTRA_DENIED_PATHS"
class InvalidExtraPattern(ValueError):
"""An `EXTRA_DENIED_PATHS` line that `re.compile` rejects.
Carries the offending `.pattern` so `main()` can name it in the `::error::`.
A dedicated type (not a bare `re.error`/`ValueError`) so `main()`'s handler
cannot accidentally swallow `parse_raw_z`'s own `ValueError` for malformed
diff input, whose fail-closed contract is separate.
"""
def __init__(self, pattern: str, error: "re.error"):
self.pattern = pattern
super().__init__(f"{pattern!r} did not compile: {error}")
def compile_extra_patterns(raw: str) -> list[re.Pattern]:
r"""Compile a caller's newline-separated `extra_denied_paths` value (BE-4405).
Semantics mirror the built-in `_PATTERN`, so an additive pattern behaves
exactly like a built-in one:
* split on newlines; a blank or whitespace-only line is a no-op (skipped),
so an unset/empty input compiles to `[]` and leaves behavior byte-identical;
* each surviving line is `.strip()`-ed before compiling — a stray leading/
trailing space from a YAML block scalar would otherwise anchor the regex
on whitespace a real path never has and silently match NOTHING (widening
the ALLOW side); stripping errs toward matching, the safe direction;
* compiled with `re.IGNORECASE` for the same reason `_PATTERN` is — macOS/
Windows CI runners resolve `SCRIPTS/CI/x` to the real file, so the checker
must too. A caller wanting case-sensitivity can scope it inline (`(?-i:…)`).
A line that will not compile raises `InvalidExtraPattern` rather than being
dropped: `main()` turns that into a fail-closed `::error::` + nonzero exit. A
typo in a security deny-list must never silently widen the allow side.
"""
patterns: list[re.Pattern] = []
for line in raw.split("\n"):
stripped = line.strip()
if not stripped:
continue
try:
patterns.append(re.compile(stripped, re.IGNORECASE))
except re.error as exc:
raise InvalidExtraPattern(stripped, exc) from exc
return patterns
def main() -> int:
# Compile the caller's additive patterns FIRST, and independently of the diff:
# a broken deny-list must fail closed even for a patch that touches nothing
# else denied. On a compile error, print a loud ::error:: and exit NONZERO
# (fail closed) rather than emitting a synthetic "denied path" that would bail
# this patch to an issue. Two reasons abort beats bail-to-issue here:
# * the pipe in groom.yml (`TOUCHED_CI=$(… | patch_policy.py)`, `set -euo
# pipefail`, NO `|| true`) turns a nonzero exit into a step abort → no PR
# opens for ANY finding this run. That is the module's existing fail-closed
# idiom (see parse_raw_z), maximally conservative for a broken deny-list;
# * a bail-to-issue would record the finding's signature in the dedup ledger
# (build_pr's file_issue writes signature_marker), so a config TYPO would
# permanently dedup real findings AND file them under a misleading
# "modifies a CI-privileged path" reason. Aborting drops nothing to the
# ledger: every finding is simply re-proposed next run, once the operator
# fixes the pattern. Loud, self-healing, no state pollution.
# The ::error:: goes to STDERR on purpose: this process's stdout is captured
# by groom.yml's `$(…)` command substitution, so a workflow command written
# there would be swallowed into $TOUCHED_CI instead of rendering; stderr is
# not captured and the runner scans it for annotations.
try:
extra_patterns = compile_extra_patterns(os.environ.get(_EXTRA_DENIED_PATHS_ENV, ""))
except InvalidExtraPattern as exc:
print(
f"::error::extra_denied_paths contains a pattern that will not compile: {exc}. "
"Fix it in the caller's extra_denied_paths input; until then the groom builder "
"opens NO PRs this run (fail-closed — a broken deny-list must never silently "
"widen the allow side).",
file=sys.stderr,
)
return 2
# Write raw bytes, not text: the parsers decode with `surrogateescape`, so a
# denied path carrying non-UTF-8 bytes holds lone surrogates that the default
# strict `sys.stdout` (text) would reject with UnicodeEncodeError — crashing on
# the very path we must report. Round-trip through the same codec so a non-UTF-8
# CI-privileged path is still emitted (and thus still blocked), never dropped.
out = sys.stdout.buffer
for path in denied_entries(parse_raw_z(sys.stdin.buffer.read()), extra_patterns):
out.write(path.encode("utf-8", "surrogateescape") + b"\n")
out.flush()
return 0
if __name__ == "__main__":
sys.exit(main())