|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Fail if any GitHub Actions job skips the mandatory Wix gateway proxy action. |
| 3 | +
|
| 4 | +There is no per-job opt-out marker by design. A job that genuinely cannot run the |
| 5 | +proxy changes this script in the same PR, so the exception gets reviewed in the |
| 6 | +open — which is exactly how PUBLISH_WORKFLOWS below came to exist. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import pathlib |
| 12 | +import sys |
| 13 | + |
| 14 | +import yaml |
| 15 | + |
| 16 | +REPO_ROOT = pathlib.Path(__file__).resolve().parents[2] |
| 17 | +PROXY_ACTION = "./.github/actions/wix-gateway-proxy" |
| 18 | +# The action copies .github/certs/wix-embargo.pem via a path relative to itself, |
| 19 | +# so a sparse checkout has to materialize both directories. |
| 20 | +REQUIRED_PATHS = (".github/actions/wix-gateway-proxy", ".github/certs") |
| 21 | + |
| 22 | +# Publish workflows, exempt from the gateway by secplatform's interim policy for |
| 23 | +# OSS repos: the gateway cannot carry `npm publish`, so these rely on the |
| 24 | +# committed package-lock.json plus .npmrc's min-release-age instead. |
| 25 | +# |
| 26 | +# Adding a file here drops its embargo protection. That is a security decision, |
| 27 | +# not a formality — do not do it lightly. |
| 28 | +PUBLISH_WORKFLOWS = frozenset( |
| 29 | + { |
| 30 | + ".github/workflows/manual-publish.yml", |
| 31 | + ".github/workflows/preview-publish.yml", |
| 32 | + } |
| 33 | +) |
| 34 | + |
| 35 | +FIX_HINT = """Every job must run the Wix gateway proxy immediately after a checkout that |
| 36 | +puts it on disk, or that job's npm installs bypass the Wix embargo gateway. |
| 37 | +
|
| 38 | +Job that already checks out this repo first: |
| 39 | +
|
| 40 | + - uses: actions/checkout@v7 |
| 41 | +
|
| 42 | + - name: Wix gateway proxy (mandatory) |
| 43 | + uses: ./.github/actions/wix-gateway-proxy |
| 44 | +
|
| 45 | +Job with no leading same-repo checkout -- prepend a bootstrap one. The action |
| 46 | +installs the CA under /usr/local/share, so a later full checkout does not undo it: |
| 47 | +
|
| 48 | + - name: Checkout for wix gateway proxy |
| 49 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 |
| 50 | + with: |
| 51 | + sparse-checkout: .github |
| 52 | +
|
| 53 | + - name: Wix gateway proxy (mandatory) |
| 54 | + uses: ./.github/actions/wix-gateway-proxy |
| 55 | +
|
| 56 | +Non-cone sparse checkout -- list both paths explicitly: |
| 57 | +
|
| 58 | + with: |
| 59 | + sparse-checkout: | |
| 60 | + <your existing paths> |
| 61 | + .github/actions/wix-gateway-proxy |
| 62 | + .github/certs |
| 63 | + sparse-checkout-cone-mode: false |
| 64 | +""" |
| 65 | + |
| 66 | + |
| 67 | +def _uses(step: dict) -> str: |
| 68 | + return str(step.get("uses", "")).strip().rstrip("/") |
| 69 | + |
| 70 | + |
| 71 | +def _covers(pattern: str, path: str) -> bool: |
| 72 | + return path == pattern or path.startswith(pattern + "/") |
| 73 | + |
| 74 | + |
| 75 | +def _overlaps(pattern: str, path: str) -> bool: |
| 76 | + # An exclusion breaks the action whether it removes the whole directory or a |
| 77 | + # single file inside it, so overlap in either direction disqualifies. |
| 78 | + return _covers(pattern, path) or _covers(path, pattern) |
| 79 | + |
| 80 | + |
| 81 | +def _sparse_covers_action(patterns: str) -> bool: |
| 82 | + listed = [p.strip().rstrip("/") for p in patterns.splitlines() if p.strip()] |
| 83 | + included = [p for p in listed if not p.startswith("!")] |
| 84 | + excluded = [p[1:] for p in listed if p.startswith("!")] |
| 85 | + return all( |
| 86 | + any(_covers(p, required) for p in included) |
| 87 | + and not any(_overlaps(p, required) for p in excluded) |
| 88 | + for required in REQUIRED_PATHS |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +def _checkout_problem(step: dict) -> str | None: |
| 93 | + if not _uses(step).startswith("actions/checkout"): |
| 94 | + return f'is preceded by "{_uses(step) or "a run step"}" instead of a checkout' |
| 95 | + with_ = step.get("with") or {} |
| 96 | + if with_.get("repository"): |
| 97 | + return f'is preceded by a checkout of {with_["repository"]}' |
| 98 | + if with_.get("path"): |
| 99 | + return f'is preceded by a checkout into {with_["path"]}/, not the workspace root' |
| 100 | + patterns = with_.get("sparse-checkout") |
| 101 | + if patterns and not _sparse_covers_action(str(patterns)): |
| 102 | + return "is preceded by a sparse checkout that omits " + " or ".join(REQUIRED_PATHS) |
| 103 | + return None |
| 104 | + |
| 105 | + |
| 106 | +def job_problem(job: dict, workflows: frozenset[str]) -> str | None: |
| 107 | + """Describe why this job fails to run the proxy, or None if it runs it correctly.""" |
| 108 | + if "uses" in job: |
| 109 | + target = str(job["uses"]).strip() |
| 110 | + if target.removeprefix("./") in workflows: |
| 111 | + return None |
| 112 | + return f"calls {target}, whose jobs this check cannot verify" |
| 113 | + |
| 114 | + steps = job.get("steps") or [] |
| 115 | + index = next((i for i, s in enumerate(steps) if _uses(s) == PROXY_ACTION), None) |
| 116 | + if index is None: |
| 117 | + return "does not run the Wix gateway proxy" |
| 118 | + if index == 0: |
| 119 | + return "runs the Wix gateway proxy first, but a local action needs a checkout before it" |
| 120 | + if index > 1: |
| 121 | + return f"runs the Wix gateway proxy at step {index + 1}; it must be step 2" |
| 122 | + step = steps[index] |
| 123 | + # Presence, not truthiness: `if: false` parses to False and would skip the step. |
| 124 | + if "if" in step: |
| 125 | + return "guards the Wix gateway proxy behind an if:, but it is mandatory" |
| 126 | + if step.get("continue-on-error"): |
| 127 | + return "lets the Wix gateway proxy fail via continue-on-error, but it is mandatory" |
| 128 | + with_ = step.get("with") or {} |
| 129 | + if "proxy-ip" in with_ and not str(with_["proxy-ip"]).strip(): |
| 130 | + return "passes an empty proxy-ip, leaving registry.npmjs.org resolving publicly" |
| 131 | + return _checkout_problem(steps[0]) |
| 132 | + |
| 133 | + |
| 134 | +def _job_lines(text: str) -> dict[str, int]: |
| 135 | + document = yaml.compose(text) |
| 136 | + if document is None: |
| 137 | + return {} |
| 138 | + for key, value in document.value: |
| 139 | + if key.value == "jobs": |
| 140 | + return {job.value: job.start_mark.line + 1 for job, _ in value.value} |
| 141 | + return {} |
| 142 | + |
| 143 | + |
| 144 | +def main(repo_root: pathlib.Path = REPO_ROOT) -> int: |
| 145 | + workflows_dir = repo_root / ".github" / "workflows" |
| 146 | + paths = sorted(p for p in workflows_dir.iterdir() if p.suffix in (".yml", ".yaml")) |
| 147 | + workflows = frozenset(p.relative_to(repo_root).as_posix() for p in paths) |
| 148 | + problems = [] |
| 149 | + jobs = calls = 0 |
| 150 | + exempt_paths = set() |
| 151 | + |
| 152 | + for path in paths: |
| 153 | + rel = path.relative_to(repo_root).as_posix() |
| 154 | + if rel in PUBLISH_WORKFLOWS: |
| 155 | + exempt_paths.add(rel) |
| 156 | + continue |
| 157 | + text = path.read_text(encoding="utf-8") |
| 158 | + lines = _job_lines(text) |
| 159 | + for job_id, job in ((yaml.safe_load(text) or {}).get("jobs") or {}).items(): |
| 160 | + job = job or {} |
| 161 | + jobs += 1 |
| 162 | + calls += "uses" in job |
| 163 | + problem = job_problem(job, workflows) |
| 164 | + if problem: |
| 165 | + problems.append((path.relative_to(repo_root), lines[job_id], job_id, problem)) |
| 166 | + |
| 167 | + for path, line, job_id, problem in problems: |
| 168 | + print(f'::error file={path},line={line}::Job "{job_id}" {problem}.') |
| 169 | + |
| 170 | + if problems: |
| 171 | + print(f"\n{len(problems)} job(s) skip the Wix gateway proxy.\n") |
| 172 | + print(FIX_HINT) |
| 173 | + return 1 |
| 174 | + |
| 175 | + print( |
| 176 | + f"Wix gateway proxy: verified {jobs - calls} of {jobs} jobs across " |
| 177 | + f"{len(paths) - len(exempt_paths)} workflows ({calls} reusable-workflow calls " |
| 178 | + f"delegate to the workflow they call)." |
| 179 | + ) |
| 180 | + if exempt_paths: |
| 181 | + print("Publish workflows exempt by policy: " + ", ".join(sorted(exempt_paths))) |
| 182 | + return 0 |
| 183 | + |
| 184 | + |
| 185 | +if __name__ == "__main__": |
| 186 | + sys.exit(main()) |
0 commit comments