Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ jobs:
env:
GH_TOKEN: ${{ github.token }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
# Optional SSH private key; when present the pin commit is
# SSH-signed so the default-branch commit-signature ruleset
# accepts it. Without it the pin PR needs a manual signed
# re-author before it can merge.
BOT_SIGNING_KEY: ${{ secrets.BOT_SIGNING_KEY }}
run: |
# Runs after the release exists: the pin PR body references the
# published artifact, and merging before publication would
Expand Down
33 changes: 32 additions & 1 deletion scripts/release/open-spm-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ source "${BASH_SOURCE[0]%/*}/semver.sh"
#
# Usage: open-spm-pr.sh <TAG> <VERSION> <XCSUM> <DEFAULT_BRANCH>
# Requires: GH_TOKEN, GITHUB_REPOSITORY (provided by Actions).
# Optional: BOT_SIGNING_KEY (SSH private key) — when set, the pin commit
# is SSH-signed so the default-branch commit-signature ruleset accepts
# it; the key material is removed right after the commit.
set -euo pipefail

TAG="${1:?TAG required}"
Expand Down Expand Up @@ -211,7 +214,35 @@ elif [ -n "${origin_pin_tag}" ]; then
exit 1
fi
fi
git commit -m "chore: pin the SPM checksum to ${TAG}"
# The release must be published (not a draft) before pinning: SPM
# resolves the pinned checksum against the release's published assets,
# and the pin-order gate refuses draft releases later with a less
# actionable error. Fail closed when the release state cannot be read.
release_is_draft="$(gh release view "${TAG}" --repo "${GITHUB_REPOSITORY}" \
--json isDraft --jq '.isDraft' 2>/dev/null)" \
|| { echo "::error::cannot verify the release state for $(esc "${TAG}")"; exit 1; }
Comment on lines +217 to +223

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · medium
The draft-release gate runs just before the commit, after git checkout -B and git add have already mutated the local checkout. The remote is untouched (no push/PR create happens before it), so the fail-closed intent holds, but the check reads only from the GitHub API and depends on no local state — hoisting it above the tag-ancestor/HEAD checks (near the other input validations at the top of the script) would make it a pure precondition, fail earlier, and make the assert_no_mutations guarantee in the test harness structurally obvious rather than incidental.

Suggestion:

Suggested change
# The release must be published (not a draft) before pinning: SPM
# resolves the pinned checksum against the release's published assets,
# and the pin-order gate refuses draft releases later with a less
# actionable error. Fail closed when the release state cannot be read.
release_is_draft="$(gh release view "${TAG}" --repo "${GITHUB_REPOSITORY}" \
--json isDraft --jq '.isDraft' 2>/dev/null)" \
|| { echo "::error::cannot verify the release state for $(esc "${TAG}")"; exit 1; }
# Hoist near the top of the script, before `git checkout -B`:
release_is_draft="$(gh release view "${TAG}" --repo "${GITHUB_REPOSITORY}" \
--json isDraft --jq '.isDraft' 2>/dev/null)" \
|| { echo "::error::cannot verify the release state for $(esc "${TAG}")"; exit 1; }

if [ "${release_is_draft}" != "false" ]; then
echo "::error::release $(esc "${TAG}") is a draft; publish it (gh release edit $(esc "${TAG}") --draft=false) before opening the pin pr"
exit 1
fi

# Sign the pin commit when a bot signing key is configured (the
# default-branch ruleset requires verified commits; workflow-authored
# commits are unsigned without it). The key is written with owner-only
# permissions, used for exactly this commit, and removed right after.
if [ -n "${BOT_SIGNING_KEY:-}" ]; then
key_file="$(mktemp)"
printf '%s\n' "${BOT_SIGNING_KEY}" > "${key_file}"
chmod 600 "${key_file}"
git config gpg.format ssh
git config user.signingkey "${key_file}"
git commit -S -m "chore: pin the SPM checksum to ${TAG}"
rm -f "${key_file}"
git config --unset gpg.format || true
git config --unset user.signingkey || true
Comment on lines +234 to +242

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security · high
With set -euo pipefail, if git commit -S fails (missing/invalid SSH key, signing failure), the script exits immediately without executing rm -f "${key_file}" or the git config --unset calls. The BOT_SIGNING_KEY private key material is then left on disk (world-readable-nameable mktemp path) and user.signingkey keeps pointing at it for any later steps in the same job. Move cleanup into an EXIT trap so it runs on every path, and drop the persisted repo config in favor of one-shot -c options so interrupted runs can't leave a dangling signingkey in the checkout.

Suggestion:

Suggested change
key_file="$(mktemp)"
printf '%s\n' "${BOT_SIGNING_KEY}" > "${key_file}"
chmod 600 "${key_file}"
git config gpg.format ssh
git config user.signingkey "${key_file}"
git commit -S -m "chore: pin the SPM checksum to ${TAG}"
rm -f "${key_file}"
git config --unset gpg.format || true
git config --unset user.signingkey || true
key_file="$(mktemp)"
printf '%s\n' "${BOT_SIGNING_KEY}" > "${key_file}"
chmod 600 "${key_file}"
trap 'rm -f "${key_file}"' EXIT
git -c gpg.format=ssh -c user.signingkey="${key_file}" \
commit -S -m "chore: pin the SPM checksum to ${TAG}"
rm -f "${key_file}"
trap - EXIT

else
git commit -m "chore: pin the SPM checksum to ${TAG}"
fi

# Preflight canary: fail fast when the PR-state lookups are broken
# before anything is mutated. The value itself is discarded (hence the
Expand Down
79 changes: 79 additions & 0 deletions scripts/release/test-open-spm-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ EOF
cat > "${STUBS}/gh" << 'EOF'
#!/usr/bin/env bash
echo "gh $*" >> "${LOG}"
if [ "$1" = "release" ] && [ "$2" = "view" ]; then
draft="$(cat "${CASE_DIR}/release_is_draft" 2>/dev/null || echo false)"
[ "${draft}" = "fail" ] && exit 1
printf '%s' "${draft}"
exit 0
fi
if [ "$1" = "api" ]; then
# Two-stage fetch: metadata (asset list) then the sums body. Each
# stage has its own fail knob value so both closed paths are covered.
Expand Down Expand Up @@ -196,6 +202,7 @@ set_fixtures() {
printf 'tagcommitsha' > "${TMP}/head_commit"
printf 'tagcommitsha' > "${TMP}/tag_commit"
printf 'false' > "${TMP}/is_shallow"
printf 'false' > "${TMP}/release_is_draft"
# Realistic shape: the file header comment also carries a
# wallet-bindings-vX.Y.Z literal ahead of the pin line.
# shellcheck disable=SC2016 # \n are printf escapes, no expansion
Expand All @@ -222,10 +229,12 @@ run_case() {
local name="$1" expected="$2" repo="${3:-mintlayer/rust-sdk}"
local branch="${4:-main}" xcsum="${5:-${XCSUM}}" actual=0
local version="${6:-1.2.3}" tag="${7:-wallet-bindings-v1.2.3}"
local bot_key="${8:-}"
: > "${LOG}"
( cd "${STUBS}/repo" && \
PATH="${STUBS}:${PATH}" \
GITHUB_REPOSITORY="${repo}" \
BOT_SIGNING_KEY="${bot_key}" \
CASE_DIR="${TMP}" \
bash "${UNDER_TEST}" "${tag}" "${version}" "${xcsum}" "${branch}" \
> "${OUT}" 2>&1 ) || actual=$?
Expand Down Expand Up @@ -1477,5 +1486,75 @@ else
bad "tag regex identical across all ten copies" "release.yml=${wf}, script=${sc}, guard=${gd}, ci=${cy}, gate=${gate}, pin-order=${cpo}"
fi

# 78. Draft release -> refuse before any mutation.
set_fixtures 0 1 "${FULLY_PINNED}" pr.none
printf 'true' > "${TMP}/release_is_draft"
: > "${LOG}"
( cd "${STUBS}/repo" && \
PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \
bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \
> "${OUT}" 2>&1 )
rc=$?
LOG_CONTENTS="$(cat "${LOG}")"
if [ "${rc}" = "1" ] && grep -q "is a draft" <<< "$(cat "${OUT}")"; then
ok "draft release refuses the pin pr"
else
bad "draft release refuses the pin pr" "exit ${rc}: $(cat "${OUT}")"
fi
assert_no_mutations

# 79. Unreadable release state -> fail closed, no mutations.
set_fixtures 0 1 "${FULLY_PINNED}" pr.none
printf 'fail' > "${TMP}/release_is_draft"
: > "${LOG}"
( cd "${STUBS}/repo" && \
PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \
bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \
> "${OUT}" 2>&1 )
rc=$?
LOG_CONTENTS="$(cat "${LOG}")"
if [ "${rc}" = "1" ] && grep -q "cannot verify the release state" <<< "$(cat "${OUT}")"; then
ok "unreadable release state fails closed"
else
bad "unreadable release state fails closed" "exit ${rc}: $(cat "${OUT}")"
fi
assert_no_mutations

# 80. BOT_SIGNING_KEY set -> the pin commit is signed (-S).
set_fixtures 0 1 "${FULLY_PINNED}" pr.none
printf 'false' > "${TMP}/release_is_draft"
: > "${LOG}"
( cd "${STUBS}/repo" && \
PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \
BOT_SIGNING_KEY="test-key-material" \
bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \
> "${OUT}" 2>&1 )
rc=$?
LOG_CONTENTS="$(cat "${LOG}")"
if [ "${rc}" = "0" ] \
&& grep -q "git commit -S -m chore: pin the SPM checksum to wallet-bindings-v1.2.3" <<< "${LOG_CONTENTS}" \
&& grep -q "git config user.signingkey" <<< "${LOG_CONTENTS}"; then
ok "signing key produces a signed commit invocation"
else
bad "signing key produces a signed commit invocation" "exit ${rc}: $(cat "${OUT}")"
fi

# 81. No BOT_SIGNING_KEY -> plain unsigned commit (unchanged behavior).
set_fixtures 0 1 "${FULLY_PINNED}" pr.none
: > "${LOG}"
( cd "${STUBS}/repo" && \
PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \
bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \
> "${OUT}" 2>&1 )
rc=$?
LOG_CONTENTS="$(cat "${LOG}")"
if [ "${rc}" = "0" ] \
&& grep -q "git commit -m chore: pin the SPM checksum to wallet-bindings-v1.2.3" <<< "${LOG_CONTENTS}" \
&& ! grep -q "git commit -S" <<< "${LOG_CONTENTS}"; then
ok "unsigned fallback unchanged"
else
bad "unsigned fallback unchanged" "exit ${rc}: $(cat "${OUT}")"
fi

echo "passed: ${pass}, failed: ${fail}"
[ "${fail}" = "0" ]
Loading