Files
ZitPlane/tests/ci/transitive_override_contract.py
T

96 lines
4.1 KiB
Python

"""P05-SEC-02: read-only PNPM targeted override and locked-resolutions contract.
Invoked in Gitea CI before frozen install. Deliberately no application execution
and no allowance for global/broad major-version overrides.
"""
from __future__ import annotations
from pathlib import Path
import re
import sys
ROOT = Path(__file__).resolve().parents[2]
WORKSPACE = ROOT / "auth/ui/v2"
GLOB_VERSION = "14.0.6"
MINIMATCH_VERSION = "11.0.9"
EXPECTED = {
"rimraf@5.0.10>glob": GLOB_VERSION,
"archiver-utils@5.0.2>glob": GLOB_VERSION,
"@eslint/config-array@0.23.5>minimatch": MINIMATCH_VERSION,
"@typescript-eslint/typescript-estree@8.67.0>minimatch": MINIMATCH_VERSION,
"eslint@10.8.1>minimatch": MINIMATCH_VERSION,
"nodemon@3.1.14>minimatch": MINIMATCH_VERSION,
"nx@22.7.12>minimatch": MINIMATCH_VERSION,
}
def overridden(data: str) -> dict[str, str]:
found: dict[str, str] = {}
for line in data.splitlines():
match = re.match(r"^ ['\"]?([^'\"#][^:]*>[^:'\"]+)['\"]?: ['\"]?([^'\" #]+)", line)
if match:
found[match.group(1)] = match.group(2).rstrip("'\":")
return found
def main() -> None:
work = (WORKSPACE / "pnpm-workspace.yaml").read_text(encoding="utf-8")
lock = (WORKSPACE / "pnpm-lock.yaml").read_text(encoding="utf-8")
if "lockfileVersion: '9.0'" not in lock:
raise AssertionError("Unexpected pnpm lock format")
if not lock.startswith("lockfileVersion:"):
raise AssertionError("Unexpected lock header")
before, after = lock.split("\nimporters:\n", 1)
expected = EXPECTED
found_workspace = overridden(work)
found_lock = overridden(before)
if found_workspace != expected:
raise AssertionError(f"Unexpected workspace parent-scoped overrides: {found_workspace!r}")
if found_lock != expected:
raise AssertionError(f"Lock and source override selectors diverged: {found_lock!r}")
if " - apps/login\n" not in work:
raise AssertionError("Official Login importer removed")
for part in (" apps/login:", " packages/zitadel-client:", " packages/zitadel-proto:"):
if part not in after:
raise AssertionError(f"Required importer missing: {part}")
# The package + snapshot registry sections (not arbitrary free text) must
# have no glob >=9 <=14.0.1 or minimatch >=7 <11.0.1.
for line in lock.splitlines():
m = re.match(r"^ ['\"]?(glob|minimatch)@([0-9]+)\.([0-9]+)\.([0-9]+)(?:['\"]?[:(]|:)", line)
if not m:
continue
name = m.group(1)
version = tuple(int(m.group(i)) for i in range(2, 5))
if name == "glob" and (9, 0, 0) <= version <= (14, 0, 1):
raise AssertionError(f"Vulnerable glob lock entry: {line.strip()}")
if name == "minimatch" and (7, 0, 0) <= version < (11, 0, 1):
raise AssertionError(f"Vulnerable minimatch lock entry: {line.strip()}")
if " glob@14.0.6:" not in lock or " minimatch@11.0.9:" not in lock:
raise AssertionError("Patched glob/minimatch snapshot not present")
snapshots = lock.split("\nsnapshots:\n", 1)[1]
refs: dict[str, list[tuple[str, str]]] = {}
for block in re.split(r"(?m)^ (?=\S)", snapshots):
header = block.splitlines()[0].rstrip(":").strip().strip("'")
for ln in block.splitlines():
m = re.match(r"^ (glob|minimatch): ([0-9]+\.[0-9]+\.[0-9]+)", ln)
if m:
refs.setdefault(header, []).append((m.group(1), m.group(2)))
for selector, version in expected.items():
parent, name = selector.rsplit(">", 1)
matches = [refs[key] for key in refs if key == parent or key.startswith(parent + "(")]
if len(matches) != 1 or (name, version) not in matches[0]:
raise AssertionError(f"Parent {parent} does not resolve exact {name}@{version}")
print("ZITPLANE_EXACT_SEVEN_PARENT_OVERRIDES_PASS")
print("ZITPLANE_GLOB_MINIMATCH_PATCHED_LOCK_CONTRACT_PASS")
if __name__ == "__main__":
try:
main()
except (AssertionError, ValueError, FileNotFoundError, IndexError) as exc:
print(f"ZITPLANE_TRANSITIVE_OVERRIDE_CONTRACT_FAIL: {exc}", file=sys.stderr)
raise SystemExit(1)