From 0f6ea10ef2a682ee3075acdd1460e0244da37624 Mon Sep 17 00:00:00 2001 From: Erick Bourgeois Date: Sat, 21 Nov 2026 16:44:44 -0500 Subject: [PATCH] fix(extract-version): pass inputs through the environment and check them The composite action put the release tag, the PR number, the repository, the suffix and the workflow type straight into its script with ${{ }}, so a release tag name holding shell syntax ran in the calling job, which in a release holds id-token and packages write. Every input now reaches the script through env:, and each is checked before use: the release tag is v.. with an optional -prerelease and +build, the PR number is digits, the repository is owner/name, the suffix is empty or -. A value with a line break can no longer add an output. Build metadata is dropped from the image tag only, since a container tag cannot hold "+". This repository's own release workflow interpolated the tag the same way, and validated it only after the script had run and written its outputs; it now takes it through env: and validates before writing anything. Tested by running the action's script with 14 inputs: valid release, release candidate, build metadata, PR and main values give the same outputs as before; $( ), ";", a line break, a missing v, a non-numeric PR number, a bad suffix and a bad repository are refused, and nothing in them runs. Every caller's existing release tags (bindy, bindcar, forage, kube-condition, sceau) pass the new check. Signed-off-by: Erick Bourgeois --- .github/workflows/release.yml | 13 +++-- versioning/extract-version/action.yml | 75 ++++++++++++++++----------- 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1fdf675..cbd8aca 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -25,21 +25,24 @@ jobs: steps: - name: Parse release tag id: parse + # The tag name reaches the script through the environment, never by `${{ }}` in it: a + # tag name may hold shell syntax. It is validated before anything is written. + env: + TAG_NAME: ${{ github.event.release.tag_name }} run: | - TAG_NAME="${{ github.event.release.tag_name }}" echo "Release tag: $TAG_NAME" # Remove 'v' prefix if present VERSION="${TAG_NAME#v}" - echo "version=$TAG_NAME" >> $GITHUB_OUTPUT - echo "version-without-v=$VERSION" >> $GITHUB_OUTPUT - # Validate version format (semver) - if ! echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?(\+[a-zA-Z0-9.]+)?$'; then + # Validate version format (semver), before any output is written + if ! printf '%s' "$VERSION" | grep -qxE '[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?(\+[a-zA-Z0-9.]+)?'; then echo "ERROR: Invalid version format: $VERSION" echo "Expected format: X.Y.Z or X.Y.Z-prerelease+metadata" exit 1 fi + echo "version=$TAG_NAME" >> $GITHUB_OUTPUT + echo "version-without-v=$VERSION" >> $GITHUB_OUTPUT # Extract major.minor.patch (strip prerelease/metadata) BASE_VERSION=$(echo "$VERSION" | sed -E 's/(-.*)?(\+.*)?$//') diff --git a/versioning/extract-version/action.yml b/versioning/extract-version/action.yml index 7db19df..89310b2 100644 --- a/versioning/extract-version/action.yml +++ b/versioning/extract-version/action.yml @@ -50,55 +50,72 @@ runs: - name: Extract version information id: extract shell: bash + # Every input reaches the script through the environment, never by `${{ }}` in the script: + # a release tag's name, a repository name or a suffix may hold shell syntax, and a value + # with a line break would add an output of its own choosing. Each is checked before use. + env: + GIT_SHA: ${{ github.sha }} + RUN_NUMBER: ${{ github.run_number }} + IMAGE_REPOSITORY: ${{ inputs.repository }} + IMAGE_TAG_SUFFIX: ${{ inputs.image-tag-suffix }} + WORKFLOW_TYPE: ${{ inputs.workflow-type }} + RELEASE_TAG: ${{ inputs.release-tag }} + PR_NUMBER: ${{ inputs.pr-number }} run: | - # Extract short SHA (first 7 characters) - SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7) - echo "short-sha=${SHORT_SHA}" >> $GITHUB_OUTPUT + set -euo pipefail + fail() { echo "::error::$1"; exit 1; } - IMAGE_REPOSITORY="${{ inputs.repository }}" - IMAGE_TAG_SUFFIX="${{ inputs.image-tag-suffix }}" - echo "image-repository=${IMAGE_REPOSITORY}" >> $GITHUB_OUTPUT + # A repository is owner/name (letters, digits, '.', '_', '-'); a suffix is empty or + # starts with '-' and holds what a container image tag may. + [[ "$IMAGE_REPOSITORY" =~ ^[A-Za-z0-9._-]+/[A-Za-z0-9._-]+$ ]] \ + || fail "repository is not owner/name: refusing it" + [[ -z "$IMAGE_TAG_SUFFIX" || "$IMAGE_TAG_SUFFIX" =~ ^-[A-Za-z0-9._-]{1,64}$ ]] \ + || fail "image-tag-suffix is not empty or -: refusing it" - case "${{ inputs.workflow-type }}" in + SHORT_SHA="${GIT_SHA:0:7}" + { + echo "short-sha=${SHORT_SHA}" + echo "image-repository=${IMAGE_REPOSITORY}" + } >> "$GITHUB_OUTPUT" + + case "$WORKFLOW_TYPE" in release) - # Release workflow: extract from release tag - # Tag format: v0.2.0 (no date) - TAG_NAME="${{ inputs.release-tag }}" + # Release workflow: the version is the tag without its leading v. + # Tag format: v1.2.3, with an optional -prerelease and +build. + [[ "$RELEASE_TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]] \ + || fail "release-tag is not v..[-pre][+build]: refusing it" + TAG_NAME="$RELEASE_TAG" VERSION="${TAG_NAME#v}" - IMAGE_TAG="${TAG_NAME}${IMAGE_TAG_SUFFIX}" - echo "version=${VERSION}" >> $GITHUB_OUTPUT - echo "tag-name=${TAG_NAME}" >> $GITHUB_OUTPUT - echo "image-tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT - echo "Release: version=${VERSION}, tag=${TAG_NAME}, image=${IMAGE_REPOSITORY}:${IMAGE_TAG}" + # A container tag cannot hold '+': build metadata is dropped from the image tag only. + IMAGE_TAG="${TAG_NAME%%+*}${IMAGE_TAG_SUFFIX}" ;; pr) # PR workflow: use pr-NUMBER format # Tag format: pr-42 - VERSION="pr-${{ inputs.pr-number }}" - TAG_NAME="pr-${{ inputs.pr-number }}" - IMAGE_TAG="pr-${{ inputs.pr-number }}${IMAGE_TAG_SUFFIX}" - echo "version=${VERSION}" >> $GITHUB_OUTPUT - echo "tag-name=${TAG_NAME}" >> $GITHUB_OUTPUT - echo "image-tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT - echo "PR: version=${VERSION}, tag=${TAG_NAME}, image=${IMAGE_REPOSITORY}:${IMAGE_TAG}" + [[ "$PR_NUMBER" =~ ^[0-9]+$ ]] || fail "pr-number is not a number: refusing it" + VERSION="pr-${PR_NUMBER}" + TAG_NAME="pr-${PR_NUMBER}" + IMAGE_TAG="pr-${PR_NUMBER}${IMAGE_TAG_SUFFIX}" ;; main) # Main branch: use main-YYYY.MM.DD format # Tag format: main-2025.12.17 DATE=$(date +%Y.%m.%d) - VERSION="0.0.0-main.${DATE}.${{ github.run_number }}" + VERSION="0.0.0-main.${DATE}.${RUN_NUMBER}" TAG_NAME="main-${DATE}" IMAGE_TAG="main-${DATE}${IMAGE_TAG_SUFFIX}" - echo "version=${VERSION}" >> $GITHUB_OUTPUT - echo "tag-name=${TAG_NAME}" >> $GITHUB_OUTPUT - echo "image-tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT - echo "Main: version=${VERSION}, tag=${TAG_NAME}, image=${IMAGE_REPOSITORY}:${IMAGE_TAG}" ;; *) - echo "Error: Invalid workflow-type '${{ inputs.workflow-type }}'. Must be: main, pr, or release" - exit 1 + fail "invalid workflow-type: must be main, pr, or release" ;; esac + + { + echo "version=${VERSION}" + echo "tag-name=${TAG_NAME}" + echo "image-tag=${IMAGE_TAG}" + } >> "$GITHUB_OUTPUT" + echo "${WORKFLOW_TYPE}: version=${VERSION}, tag=${TAG_NAME}, image=${IMAGE_REPOSITORY}:${IMAGE_TAG}"