Skip to content

Commit 17416ca

Browse files
committed
Restrict manual sources and preserve manual identity
Signed-off-by: Ravneet Arora <[email protected]>
1 parent 013a5bf commit 17416ca

1 file changed

Lines changed: 60 additions & 13 deletions

File tree

‎.github/workflows/docker-manual.yml‎

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ on:
77
workflow_dispatch:
88
inputs:
99
source_ref:
10-
description: Branch, tag, full commit SHA, or refs/pull/NUMBER/head to build
10+
description: block/buzz branch, tag, or full SHA reachable from a repository branch
1111
required: true
1212
type: string
1313

1414
permissions: {}
1515

16-
concurrency:
17-
group: manual-relay-image-${{ github.run_id }}
18-
cancel-in-progress: false
19-
2016
env:
2117
IMAGE_NAME: ${{ vars.GHCR_IMAGE != '' && vars.GHCR_IMAGE || 'ghcr.io/block/buzz' }}
2218

@@ -31,17 +27,69 @@ jobs:
3127
sha: ${{ steps.source.outputs.sha }}
3228
tag: ${{ steps.source.outputs.tag }}
3329
steps:
34-
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
35-
with:
36-
ref: ${{ inputs.source_ref }}
37-
persist-credentials: false
38-
- name: Pin the selected commit for all builds
30+
- name: Require workflow from main in block/buzz
31+
env:
32+
DISPATCH_REF: ${{ github.ref }}
33+
REPOSITORY: ${{ github.repository }}
34+
shell: bash
35+
run: |
36+
set -euo pipefail
37+
[[ "$REPOSITORY" == block/buzz && "$DISPATCH_REF" == refs/heads/main ]] || {
38+
echo '::error::Run the workflow from main in block/buzz; use source_ref to select code.'
39+
exit 1
40+
}
41+
- name: Resolve a source reachable from a repository branch
3942
id: source
43+
env:
44+
SOURCE_REF: ${{ inputs.source_ref }}
4045
shell: bash
4146
run: |
4247
set -euo pipefail
43-
sha=$(git rev-parse 'HEAD^{commit}')
48+
if [[ -z "$SOURCE_REF" || ${#SOURCE_REF} -gt 255 || ! "$SOURCE_REF" =~ ^[A-Za-z0-9._/@-]+$ ]]; then
49+
echo '::error::Supply a repository branch, tag, or full commit SHA.'
50+
exit 1
51+
fi
52+
case "$SOURCE_REF" in
53+
-*|*..*|*.lock|refs/pull/*|pull/*|*/pull/*)
54+
echo '::error::PR refs and malformed refs are not allowed.'
55+
exit 1
56+
;;
57+
esac
58+
workdir=$(mktemp -d)
59+
trap 'rm -rf "$workdir"' EXIT
60+
git -C "$workdir" init --quiet
61+
git -C "$workdir" remote add origin https://github.com/block/buzz.git
62+
# Fetch only canonical branch histories, never refs/pull or arbitrary SHAs.
63+
# Full history is needed to prove ancestry; blobs are fetched only by build jobs.
64+
git -C "$workdir" fetch --filter=blob:none --no-tags origin \
65+
'+refs/heads/*:refs/remotes/origin/*'
66+
if [[ "$SOURCE_REF" =~ ^[0-9a-fA-F]{40}$ ]]; then
67+
sha=$(git -C "$workdir" rev-parse --verify "${SOURCE_REF}^{commit}")
68+
elif [[ "$SOURCE_REF" == refs/heads/* ]]; then
69+
sha=$(git -C "$workdir" rev-parse --verify "refs/remotes/origin/${SOURCE_REF#refs/heads/}^{commit}")
70+
else
71+
tag_ref="$SOURCE_REF"
72+
if [[ "$SOURCE_REF" != refs/* ]] && \
73+
sha=$(git -C "$workdir" rev-parse --verify "refs/remotes/origin/${SOURCE_REF}^{commit}" 2>/dev/null); then
74+
:
75+
else
76+
if [[ "$SOURCE_REF" != refs/* ]]; then
77+
tag_ref="refs/tags/$SOURCE_REF"
78+
fi
79+
[[ "$tag_ref" == refs/tags/* ]] || {
80+
echo '::error::Only repository branches and tags are allowed.'
81+
exit 1
82+
}
83+
git -C "$workdir" fetch --no-tags origin "$tag_ref:refs/tags/manual-source"
84+
sha=$(git -C "$workdir" rev-parse --verify 'refs/tags/manual-source^{commit}')
85+
fi
86+
fi
4487
[[ "$sha" =~ ^[0-9a-f]{40}$ ]]
88+
branches=$(git -C "$workdir" for-each-ref --format='%(refname)' --contains="$sha" refs/remotes/origin/)
89+
[[ -n "$branches" ]] || {
90+
echo '::error::Selected commit must be reachable from a block/buzz branch; PR-only commits are not allowed.'
91+
exit 1
92+
}
4593
echo "sha=$sha" >> "$GITHUB_OUTPUT"
4694
echo "tag=manual-${sha:0:12}-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}" >> "$GITHUB_OUTPUT"
4795
echo "Selected source: $sha. CI qualification is skipped."
@@ -180,15 +228,14 @@ jobs:
180228
jq -n --arg image "$pinned" --arg tag "$image" --arg digest "$merged_digest" \
181229
--arg sha "$SOURCE_SHA" --arg source "$source_url" --argjson platforms "$runtime_digests" \
182230
'{image:$image, tag:$tag, digest:$digest, source_sha:$sha, source_url:$source,
183-
platforms:$platforms, deployment_tag:("sha-" + $sha[0:7]),
231+
platforms:$platforms,
184232
ci_qualification:"skipped", status:"registry-verified"}' \
185233
> "$RUNNER_TEMP/manual-image.json"
186234
{
187235
echo '## Manual relay image published to GHCR'
188236
echo
189237
echo "Source commit: $SOURCE_SHA"
190238
echo "Registry tag: $image"
191-
echo "Deployment metadata tag: $(jq -r .deployment_tag "$RUNNER_TEMP/manual-image.json")"
192239
echo "Digest: $(jq -r .digest "$RUNNER_TEMP/manual-image.json")"
193240
echo 'Platforms: linux/amd64, linux/arm64'
194241
echo 'CI qualification was skipped. No deployment was started.'

0 commit comments

Comments
 (0)