Skip to content

Commit 0da2ccd

Browse files
noreplyclaude
andcommitted
fork release: guards in a tested script, write token only where it publishes
- A version that already has a tag or a published release is refused before the draft is created and again right before it is published, so "Re-run failed jobs" cannot replace a live release's files. - Leftover drafts are removed under pipefail and each removal is logged; a failed release listing fails the step instead of reading as "no drafts". - Only released upstream code: refuses any commit between the base tag and the build that is on upstream's default branch, a base tag that is not the newest stable tag in the history (a stray tag on an old commit), and a base above upstream's latest release. - Marked latest only when above every published fork release. - The CLI archives (the `t3` executable bundles the server again) must name this repository, not only apps/server/dist. - Actions pinned by commit; fail_on_unmatched_files; named signing secrets instead of secrets: inherit; persist-credentials: false. Files are collected in a read-only assemble job; the publish job holds the write token, runs no package code, and only its three release steps see the token. - The workflow's `published` output used jobs.publish.result, which workflow_call outputs cannot read; it now comes from the publish step. Tests: .github/scripts/fork-release.test.sh (local git repos, fake gh; also run in preflight) and fork-release-workflow.test.py (workflow shape). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 48526ae commit 0da2ccd

4 files changed

Lines changed: 742 additions & 71 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"""Checks the shape of .github/workflows/fork-release.yml that its guards rely on.
2+
3+
python3 .github/scripts/fork-release-workflow.test.py [path/to/fork-release.yml]
4+
5+
Needs PyYAML. Each check prints ok/FAIL; any FAIL exits 1.
6+
"""
7+
8+
import pathlib
9+
import re
10+
import sys
11+
12+
import yaml
13+
14+
root = pathlib.Path(__file__).resolve().parents[2]
15+
path = pathlib.Path(sys.argv[1]) if len(sys.argv) > 1 else root / ".github/workflows/fork-release.yml"
16+
text = path.read_text()
17+
wf = yaml.safe_load(text)
18+
desktop = yaml.safe_load((root / ".github/workflows/release-desktop.yml").read_text())
19+
jobs = wf["jobs"]
20+
failures = []
21+
22+
23+
def check(name, ok, detail=""):
24+
print(("ok " if ok else "FAIL ") + name + ("" if ok or not detail else f"\n | {detail}"))
25+
if not ok:
26+
failures.append(name)
27+
28+
29+
def steps(job):
30+
return job.get("steps", [])
31+
32+
33+
def all_steps():
34+
for job_id, job in jobs.items():
35+
for step in steps(job):
36+
yield job_id, step
37+
38+
39+
# Actions run by commit, so a moved tag cannot change what runs (the release action holds a write
40+
# token). Local actions and workflows are part of this repository.
41+
unpinned = []
42+
for line in text.splitlines():
43+
m = re.match(r"\s*(?:-\s*)?uses:\s*(\S+)(.*)$", line)
44+
if not m or m.group(1).startswith("./"):
45+
continue
46+
if not re.fullmatch(r"[\w.-]+/[\w./-]+@[0-9a-f]{40}", m.group(1)) or not re.match(r"\s*#\s*v\d", m.group(2)):
47+
unpinned.append(m.group(1))
48+
check("external actions are pinned by commit SHA with a version comment", not unpinned, ", ".join(unpinned))
49+
50+
# Named secrets only: the signing secrets release-desktop.yml declares, nothing inherited.
51+
declared = set(desktop[True]["workflow_call"]["secrets"])
52+
inherit = [j for j, job in jobs.items() if job.get("secrets") == "inherit"]
53+
check("no job passes secrets: inherit", not inherit, ", ".join(inherit))
54+
wrong = []
55+
for job_id, job in jobs.items():
56+
if job.get("uses") == "./.github/workflows/release-desktop.yml":
57+
passed = job.get("secrets")
58+
if not isinstance(passed, dict) or set(passed) != declared or any(
59+
v != "${{ secrets.%s }}" % k for k, v in passed.items()
60+
):
61+
wrong.append(job_id)
62+
check("release-desktop.yml gets exactly the secrets it declares", not wrong, ", ".join(wrong))
63+
check("FORK_PUSH_TOKEN appears nowhere", "FORK_PUSH_TOKEN" not in text)
64+
65+
bad = [j for j, s in all_steps() if str(s.get("uses", "")).startswith("actions/checkout@")
66+
and (s.get("with") or {}).get("persist-credentials") is not False]
67+
check("every checkout sets persist-credentials: false", not bad, ", ".join(bad))
68+
69+
release_steps = [(j, s) for j, s in all_steps() if str(s.get("uses", "")).startswith("softprops/action-gh-release@")]
70+
check("one release-action step", len(release_steps) == 1)
71+
check("the release action fails on unmatched files",
72+
all((s.get("with") or {}).get("fail_on_unmatched_files") is True for _, s in release_steps))
73+
74+
# The write token: only the publish job has it, and only its three release-writing steps use it.
75+
write_jobs = [j for j, job in jobs.items() if (job.get("permissions") or {}).get("contents") == "write"]
76+
check("only the publish job has contents: write", write_jobs == ["publish"], ", ".join(write_jobs))
77+
package_code = re.compile(r"(^|[\s;&|(])(vp|node|npm|npx|pnpm|bun)\s")
78+
for job_id in write_jobs:
79+
job_steps = steps(jobs[job_id])
80+
runs_packages = [s.get("name") for s in job_steps
81+
if str(s.get("uses", "")).startswith("./") or package_code.search(str(s.get("run", "")))]
82+
check(f"{job_id}: runs no package code", not runs_packages, ", ".join(map(str, runs_packages)))
83+
token_steps = [s for s in job_steps if "github.token" in yaml.safe_dump(s) or "secrets." in yaml.safe_dump(s)
84+
or str(s.get("uses", "")).startswith("softprops/")]
85+
allowed = [s for s in token_steps if str(s.get("uses", "")).startswith("softprops/")
86+
or re.fullmatch(r"bash \.github/scripts/fork-release\.sh (remove-drafts|publish) .*", str(s.get("run", "")).strip())]
87+
check(f"{job_id}: the token reaches only the remove-drafts, create and publish steps",
88+
len(token_steps) == len(allowed) == 3, ", ".join(str(s.get("name")) for s in token_steps))
89+
90+
def index(pred):
91+
return next((i for i, s in enumerate(job_steps) if pred(s)), -1)
92+
93+
remove = index(lambda s: "fork-release.sh remove-drafts" in str(s.get("run", "")))
94+
create = index(lambda s: str(s.get("uses", "")).startswith("softprops/"))
95+
publish = index(lambda s: "fork-release.sh publish" in str(s.get("run", "")))
96+
check(f"{job_id}: refuse/remove drafts, then create the draft, then publish it",
97+
-1 < remove < create < publish)
98+
if create >= 0 and publish >= 0:
99+
check(f"{job_id}: publishes the draft it created, by id",
100+
job_steps[create].get("id") == "draft"
101+
and "steps.draft.outputs.id" in yaml.safe_dump(job_steps[publish]))
102+
103+
# Shell steps fail on any error in a pipeline.
104+
lax = []
105+
for job_id, s in all_steps():
106+
run = s.get("run")
107+
if run is None:
108+
continue
109+
lines = [l for l in run.strip().splitlines() if l.strip() and not l.strip().startswith("#")]
110+
if s.get("shell") != "bash" or (len(lines) > 1 and lines[0].strip() != "set -euo pipefail"):
111+
lax.append(f"{job_id}/{s.get('name')}")
112+
check("run steps use bash and multi-line ones start with set -euo pipefail", not lax, "; ".join(lax))
113+
114+
runs = "\n".join(str(s.get("run", "")) for _, s in all_steps())
115+
check("preflight resolves the version through the guard script",
116+
"fork-release.sh resolve" in "\n".join(str(s.get("run", "")) for s in steps(jobs["preflight"])))
117+
check("preflight runs the guard tests", "fork-release.test.sh" in runs)
118+
check("the CLI archives are checked for the release repository", "fork-release.sh check-archives release-assets" in runs)
119+
120+
if failures:
121+
print(f"{len(failures)} failed")
122+
sys.exit(1)
123+
print("all passed")

‎.github/scripts/fork-release.sh‎

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/usr/bin/env bash
2+
# The guards of fork-release.yml, one subcommand each, so they can be tested without a run
3+
# (fork-release.test.sh). Every refusal exits non-zero with a ::error:: line; nothing falls back.
4+
#
5+
# resolve Preflight: picks base tag, version and tag for HEAD (key=value lines).
6+
# remove-drafts TAG Refuses a published TAG, then deletes drafts with TAG left by earlier runs.
7+
# publish ID TAG VERSION Refuses a published TAG, then publishes draft ID, marked latest only
8+
# when VERSION is above every published fork release.
9+
# check-archives DIR Every CLI archive in DIR must name REPOSITORY as its release repository.
10+
#
11+
# Environment: REPOSITORY (owner/name of this fork; GITHUB_REPOSITORY in a run), UPSTREAM
12+
# (owner/name, resolve looks it up), UPSTREAM_URL (git URL of upstream, default GitHub), ORIGIN
13+
# (git remote or URL of this fork, default origin). Output goes to $GITHUB_OUTPUT, else stdout.
14+
set -euo pipefail
15+
16+
REPOSITORY="${REPOSITORY:-${GITHUB_REPOSITORY:?REPOSITORY or GITHUB_REPOSITORY}}"
17+
ORIGIN="${ORIGIN:-origin}"
18+
OUTPUT="${GITHUB_OUTPUT:-/dev/stdout}"
19+
20+
die() {
21+
printf '::error::%s\n' "$*" >&2
22+
exit 1
23+
}
24+
25+
stable_version_re='^v[0-9]+\.[0-9]+\.[0-9]+$'
26+
fork_version_re='^v?[0-9]+\.[0-9]+\.[0-9]+-fork\.[0-9]+$'
27+
28+
# version_gt A B: A sorts above B. Takes X.Y.Z or X.Y.Z-fork.N (leading v allowed). A fork
29+
# release of X.Y.Z sorts above X.Y.Z itself here, which is what this fork's ordering needs.
30+
version_gt() {
31+
local a="${1#v}" b="${2#v}" i
32+
local -a x y
33+
IFS=. read -r -a x <<< "${a/-fork./.}"
34+
IFS=. read -r -a y <<< "${b/-fork./.}"
35+
for i in 0 1 2 3; do
36+
if (( 10#${x[i]:-0} != 10#${y[i]:-0} )); then
37+
(( 10#${x[i]:-0} > 10#${y[i]:-0} ))
38+
return
39+
fi
40+
done
41+
return 1
42+
}
43+
44+
# Prints the releases of this fork through a jq filter; a failed listing stops everything.
45+
releases() {
46+
gh api "repos/$REPOSITORY/releases" --paginate --jq "$1" \
47+
|| die "Could not list the releases of $REPOSITORY; nothing was changed."
48+
}
49+
50+
# A published (non-draft) release or a tag with this name means the version is taken: "Re-run failed
51+
# jobs" reuses the preflight's version, and publishing again would replace a live release's files.
52+
refuse_published() {
53+
local tag="$1" published
54+
if git ls-remote --exit-code --tags "$ORIGIN" "refs/tags/$tag" > /dev/null; then
55+
die "Tag $tag already exists, so $tag is published or being published. Start a new run (Re-run all jobs) for the next number; nothing was changed."
56+
else
57+
# 2 means no such ref; anything else is a failed lookup, which must not read as "free".
58+
[[ $? == 2 ]] || die "Could not list the tags of $ORIGIN."
59+
fi
60+
published="$(releases ".[] | select((.draft | not) and .tag_name == \"$tag\") | .id")"
61+
if [[ -n "$published" ]]; then
62+
die "A published release ($published) already has tag $tag. Start a new run (Re-run all jobs); nothing was changed."
63+
fi
64+
}
65+
66+
cmd_resolve() {
67+
local upstream="${UPSTREAM:-}"
68+
if [[ -z "$upstream" ]]; then
69+
upstream="$(gh api "repos/$REPOSITORY" --jq '.parent.full_name // empty')"
70+
[[ -n "$upstream" ]] || die "$REPOSITORY is not a fork, so there is no upstream release to build on."
71+
fi
72+
local url="${UPSTREAM_URL:-https://github.com/$upstream.git}"
73+
local head
74+
head="$(git rev-parse HEAD)"
75+
76+
# Read-only fetch of upstream's tags and default branch into refs this run alone uses.
77+
git fetch --quiet --no-tags "$url" "+refs/tags/v*:refs/upstream-tags/v*" "+HEAD:refs/upstream-head"
78+
79+
# Lists are read into variables first: a failure inside <(...) would go unnoticed.
80+
local refs tag
81+
local -a contained=()
82+
refs="$(git for-each-ref --format='%(refname:strip=2)' refs/upstream-tags)"
83+
for tag in $(grep -E "$stable_version_re" <<< "$refs" | sort -rV || true); do
84+
if git merge-base --is-ancestor "refs/upstream-tags/$tag^{commit}" HEAD; then
85+
contained+=("$tag")
86+
fi
87+
done
88+
(( ${#contained[@]} > 0 )) || die "$head contains no stable $upstream release tag."
89+
local base_tag="${contained[0]}"
90+
local base
91+
base="$(git rev-parse "refs/upstream-tags/$base_tag^{commit}")"
92+
93+
# The base must be the newest stable tag in the series' history, not just the highest name: a
94+
# stray tag on an old commit (say v1.0.0) would otherwise outrank every later real release.
95+
for tag in "${contained[@]:1}"; do
96+
git merge-base --is-ancestor "refs/upstream-tags/$tag^{commit}" "$base" \
97+
|| die "$base_tag sorts highest but is not the newest stable $upstream tag in this history ($tag is not part of it). Delete or fix the stray tag; nothing was built."
98+
done
99+
100+
# Upstream's own ordering: never above the release upstream marks latest.
101+
local upstream_latest
102+
upstream_latest="$(gh api "repos/$upstream/releases/latest" --jq '.tag_name')"
103+
[[ "$upstream_latest" =~ $stable_version_re ]] \
104+
|| die "$upstream's latest release is '$upstream_latest', not a stable vX.Y.Z tag."
105+
if version_gt "$base_tag" "$upstream_latest"; then
106+
die "$base_tag sorts above $upstream's latest release $upstream_latest. Only released upstream versions are built."
107+
fi
108+
109+
# Releases only: every commit after the base tag must be the fork's own. Upstream commits there
110+
# are unreleased upstream code (for example a branch that still equals upstream's default branch).
111+
local total own
112+
total="$(git rev-list --count "$base..HEAD")"
113+
own="$(git rev-list --count "$base..HEAD" --not refs/upstream-head)"
114+
if [[ "$total" != "$own" ]]; then
115+
die "$head holds $((total - own)) unreleased $upstream commits after $base_tag. Build a branch of $base_tag plus the fork's own commits."
116+
fi
117+
118+
local version="${base_tag#v}" last
119+
last="$(git ls-remote --tags --refs "$ORIGIN" "v$version-fork.*" \
120+
| sed -n "s|.*refs/tags/v$version-fork\.\([0-9]\+\)$|\1|p" | sort -n | tail -n1)"
121+
version="$version-fork.$(( ${last:-0} + 1 ))"
122+
{
123+
echo "ref=$head"
124+
echo "upstream=$upstream"
125+
echo "base_tag=$base_tag"
126+
echo "version=$version"
127+
echo "tag=v$version"
128+
} >> "$OUTPUT"
129+
echo "Building $version from $(git rev-parse --short HEAD) on $upstream $base_tag." \
130+
| tee -a "${GITHUB_STEP_SUMMARY:-/dev/null}" >&2
131+
}
132+
133+
cmd_remove_drafts() {
134+
local tag="${1:?tag}" id
135+
refuse_published "$tag"
136+
local ids
137+
ids="$(releases ".[] | select(.draft and .tag_name == \"$tag\") | .id")"
138+
for id in $ids; do
139+
gh api -X DELETE "repos/$REPOSITORY/releases/$id" > /dev/null
140+
echo "Removed leftover draft $id for $tag."
141+
done
142+
[[ -n "$ids" ]] || echo "No leftover draft for $tag."
143+
}
144+
145+
cmd_publish() {
146+
local id="${1:?release id}" tag="${2:?tag}" version="${3:?version}"
147+
[[ "$version" =~ $fork_version_re ]] || die "'$version' is not a fork version."
148+
refuse_published "$tag"
149+
# Latest only when above every published fork release: building an older ref must not move
150+
# /releases/latest backwards, where the desktop updater would then find nothing newer.
151+
local latest=true other published
152+
published="$(releases '.[] | select((.draft or .prerelease) | not) | .tag_name')"
153+
for other in $published; do
154+
[[ "$other" =~ $fork_version_re ]] || continue
155+
if ! version_gt "$version" "$other"; then
156+
latest=false
157+
echo "$other is published and not below $version, so $tag is not marked latest."
158+
break
159+
fi
160+
done
161+
gh api -X PATCH "repos/$REPOSITORY/releases/$id" -F draft=false -f make_latest="$latest" > /dev/null
162+
echo "Published $tag (latest: $latest)."
163+
{
164+
echo "published=true"
165+
echo "latest=$latest"
166+
} >> "$OUTPUT"
167+
}
168+
169+
cmd_check_archives() {
170+
local dir="${1:?directory}" archive work found=0
171+
shopt -s nullglob
172+
for archive in "$dir"/t3-*.tar.gz "$dir"/t3-*.zip; do
173+
found=1
174+
work="$(mktemp -d)"
175+
case "$archive" in
176+
*.tar.gz) tar -xzf "$archive" -C "$work" ;;
177+
*.zip) python3 -I -m zipfile -e "$archive" "$work" ;;
178+
esac
179+
# The `t3` executable bundles the server again (release-desktop.yml build-exe), so this checks
180+
# the code that ships rather than the separate JS bundle.
181+
grep -rqaF "\"$REPOSITORY\"" "$work" \
182+
|| die "$(basename "$archive") does not name $REPOSITORY as its release repository, so it would update from elsewhere."
183+
rm -rf "$work"
184+
echo "$(basename "$archive") names $REPOSITORY."
185+
done
186+
(( found )) || die "No CLI archive in $dir."
187+
}
188+
189+
command="${1:-}"
190+
shift || true
191+
case "$command" in
192+
resolve) cmd_resolve "$@" ;;
193+
remove-drafts) cmd_remove_drafts "$@" ;;
194+
publish) cmd_publish "$@" ;;
195+
check-archives) cmd_check_archives "$@" ;;
196+
version-gt) version_gt "$@" ;;
197+
*) die "usage: $0 resolve | remove-drafts TAG | publish ID TAG VERSION | check-archives DIR" ;;
198+
esac

0 commit comments

Comments
 (0)