Skip to content

Escape caller-supplied values in the actions' annotations #267

Closed

Description

@mmcky

Copilot's review of #265 pointed out that setup-environment's "Environment file not found" error prints the environment input inside a ::error:: line without escaping it (discussion_r4491500321). That is true, and #265 did not introduce it. Before #265, ${{ inputs.environment }} put the same text into the same echo. The same pattern is in 30 annotation lines across six actions and one script.

The runner reads an annotation's message as a workflow command's data. A %, CR or LF in it has to be written %25, %0D or %0A:

  • An unescaped line break ends the annotation early. What follows is a new log line, which the runner reads as a workflow command if it starts with ::.
  • An unescaped %0A in a value is shown as a line break.

The severity is low. Every value below is the calling workflow's own input, or derived from one, and a workflow can print any command it likes itself. It matters when an input is built from text the workflow does not control, such as a branch name or a pull request title. It is still the one place where the actions print caller text without the care the rest of the code takes, and three places already do it right.

Where values are already escaped

Where they are not (on ea01c01)

File Lines Value printed From input Line break already refused?
build-jupyter-cache/action.yml 189 $token builders yes: tokens are split on whitespace
build-jupyter-cache/action.yml 197, 204 $RECEIVED builders yes: CR and LF are turned into spaces
build-lectures/action.yml 149 $PDF_SOURCE output-dir no
build-lectures/action.yml 170 $NB_SOURCE output-dir no
build-lectures/action.yml 210 $SOURCE_DIR source-dir no
build-lectures/action.yml 226, 228 $TOP, the work tree's root as git reports it source-dir no
deploy-cloudflare/action.yml 132 $REQUIRE_ACCESS require-access no
deploy-cloudflare/action.yml 135 $VISIBILITY visibility no
deploy-cloudflare/action.yml 151 $TEAM_DOMAIN team-domain no
deploy-cloudflare/action.yml 172 $WORKER_NAME worker-name no
deploy-cloudflare/action.yml 177 $ACCOUNT_SUBDOMAIN account-subdomain no
deploy-cloudflare/action.yml 182, 184 $ALIAS alias no
deploy-cloudflare/action.yml 189, 191 $BUILD_DIR build-dir yes, at line 188
publish-gh-pages/action.yml 97 $MODE mode no
publish-gh-pages/action.yml 119 $ASSET_NAME asset-name no
restore-jupyter-cache/action.yml 307 $CACHE_PATH path yes, since #228
setup-environment/action.yml 116, 129, 147 $ENV_UPDATE; at 147 also $FILE_NAME, the name: read from that file environment-update no
setup-environment/action.yml 150, 218 $ENV_NAME environment-name no
setup-environment/action.yml 181 $ENV_FILE environment no
setup-environment/action.yml 278, 290 $LATEX_REQUIREMENTS_FILE latex-requirements-file no
scripts/package-release-assets.sh 46, 51 $BUILD_DIR publish-gh-pages' build-dir no

Values the actions build themselves are left out: exit codes, step outcomes, modes after validation, URLs made from validated names, and input names. So are the scripts that only this repository's own workflows run (check-base-pins.sh, delete-pr-images.sh, detect-changed-images.sh, create-ci-failure-issue.sh).

The annotations are not the whole of it. A plain echo of an input with a line break in it can start a workflow command too: publish-gh-pages' "Preparing build directory: …", for example, or build-lectures' build command line. Escaping is right for an annotation, but a log line should show the value as it is.

Proposal

  1. Refuse a line break in every single-line input, in each action's first step. A path, a name, a mode or a key never needs one. restore-jupyter-cache (Move the remaining ${{ }} expressions in run: scripts into env: (PLAN item 18) #228) and deploy-cloudflare's build-dir already do this. This closes the plain-echo case too, and most of the table's "no" rows become "yes".
  2. Escape every input printed in an annotation, with one helper. Either one sourced file, scripts/workflow-command.sh with esc(), found from $GITHUB_ACTION_PATH/../scripts/ as publish-gh-pages finds package-release-assets.sh and the preview actions find preview-comment.cjs, or the same four-line function inline in each step that needs it. The file is one copy to keep right, but adds a file each action must find at run time. The inline function needs no lookup, but is a copy per step. Both escape % first, then CR, then LF, as the existing copies do. Note: do each substitution as a plain assignment. Inside double quotes, "${s//$'\n'/'%0A'}" keeps the single quotes literally in some bash versions, and local s=${…} can word-split in old ones.
  3. Keep each message readable. Brackets or quotes around the value, as most messages already have.

Harness

  • A job like unit-line-break for each action whose first step gains the line-break check: a single-line control that passes, then each input with a line break failing the action before anything runs.
  • A test of the helper itself, if it is a file: %, CR, LF, %0A in the input, and a value with spaces and a glob, each coming out as one escaped line.
  • Optionally, a check in scripts/lint-shell.py (ci(harness): shellcheck the actions' run: scripts and scripts/*.sh in the gate (#100) #266) that flags an annotation echo printing a $VAR that does not go through the helper.

Not in scope

  • Annotations whose values the actions build themselves, listed above.
  • The repository's own workflows and their scripts.

Related: #228, #236, #265, #266.

Claude Code (on behalf of mmcky), drafted by the mini:actions-issue228 session and filed by the organiser

Metadata

Metadata

Assignees

No one assigned

    Labels

    low-prioritymaintenancesecurity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions