Skip to content
Merged
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
19 changes: 16 additions & 3 deletions scripts/check-lock-sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,19 @@ FILENAME == lockfile {
}

# ---------- pass 2: the workflow YAML ----------
FNR == 1 { wf = FILENAME }
FNR == 1 { wf = FILENAME; stepind = -1 }
{
line = $0
sub(/[[:space:]]+#.*$/, "", line) # strip trailing comment
# Track whether this line sits inside a job's `steps:` block, by
# indentation: a `uses:` there is a step action, whatever its path looks
# like (an action directory may be named tool.yml). Only a `uses:` outside
# every `steps:` block is a job-level reusable-workflow call.
if (line ~ /[^[:space:]]/) {
match(line, /^[[:space:]]*/); ind = RLENGTH
if (stepind >= 0 && ind <= stepind) stepind = -1

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve step context for indentless YAML sequences

When a valid workflow formats a step sequence without extra indentation (steps: followed by - name: at the same column), this condition clears stepind on the sequence item. A later uses: key in that step has no leading dash and is therefore misclassified as a job-level reusable call when its action path resembles .github/workflows/*.yml. In that scenario, the checker incorrectly permits a self-SHA ref: for an action and exits successfully, defeating the guard this change is intended to add.

Useful? React with 👍 / 👎.

if (line ~ /^[[:space:]]*steps:/) stepind = ind
}
if (line ~ /^[[:space:]]*-?[[:space:]]*uses:[[:space:]]*.+$/) {
raw = line
sub(/^[[:space:]]*-?[[:space:]]*uses:[[:space:]]*/, "", raw)
Expand All @@ -264,7 +273,7 @@ FNR == 1 { wf = FILENAME }
# Clause 5's exception needs to know HOW a ref is used: as the target of a
# job-level reusable-workflow call, or as an action.
if (n != "") {
if (raw ~ /\/\.github\/workflows\/[^\/@]+\.ya?ml@/) reusetgt[ck(n)] = 1
if (stepind < 0 && line !~ /^[[:space:]]*-/ && raw ~ /\/\.github\/workflows\/[^\/@]+\.ya?ml@/) reusetgt[ck(n)] = 1
else actiontgt[ck(n)] = 1
}
}
Expand Down Expand Up @@ -374,7 +383,11 @@ END {
k = ck(s)
# Only the target of job-level reusable calls, never an action and never a
# nested uses: of another record (see the header, clause 5).
if ((k in reusetgt) && !(k in actiontgt) && index(wantsrc[k], " dependencies:") == 0) continue
# The record's key must itself be pinned to that same SHA: a mutable key
# (@v1) or a key at a different SHA is not "its own SHA".
keyref = substr(s, index(s, "@") + 1)
if (fullsha(keyref) && tolower(keyref) == tolower(selfsha[s]) &&
(k in reusetgt) && !(k in actiontgt) && index(wantsrc[k], " dependencies:") == 0) continue
nself++
selflist = selflist sprintf("\n %s (ref: %s)", s, selfsha[s])
}
Expand Down
58 changes: 58 additions & 0 deletions tests/workflows/check_lock_sync_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,61 @@
sed -i "s#Owner/Repo/.github/workflows/check.yml@#Owner/Repo/.github/actions/check@#" "$work/current/ci.yml"
self_ref
expect 1 'SELF-SHA REFS' 'a path outside .github/workflows is not a reusable workflow'

# The exception needs the key itself pinned to the same full SHA as ref:.
fresh
reusable_only
self_ref
expect 1 'SELF-SHA REFS' 'a mutable key (@v1) with a SHA ref is not covered by the exception'
fresh
reusable_only
sha_key
sed -i "s/ref: 'v1'/ref: '2222222222222222222222222222222222222222'/" "$work/current/actions.lock"
expect 1 'SELF-SHA REFS' 'a SHA key whose ref names a different SHA is not covered'

# Job-level vs step position is read from the YAML, not inferred from the
# path: a step action may live in a directory named like a workflow file.
fresh
cat > "$work/current/ci.yml" <<'YML'
name: Test
on: pull_request
jobs:
local:
runs-on: ubuntu-latest
steps:
- uses: Owner/Repo/.github/workflows/tool.yml@v1
YML
sha_key
self_ref
expect 1 'SELF-SHA REFS' 'a step action with a workflow-shaped path is not a reusable call'
fresh
cat > "$work/current/ci.yml" <<'YML'
name: Test
on: pull_request
jobs:
local:
runs-on: ubuntu-latest
steps:
- name: build
uses: Owner/Repo/.github/workflows/tool.yml@v1
reusable:
uses: Owner/Repo/.github/workflows/check.yml@v1
YML
sha_key
self_ref
expect 1 'SELF-SHA REFS' 'a non-first step key with a workflow-shaped path still counts as an action'

Check warning on line 408 in tests/workflows/check_lock_sync_test.sh

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of using the literal 'SELF-SHA REFS' 11 times.

See more on https://sonarcloud.io/project/issues?id=hyperpolymath_secret-types&issues=AaGpO0556Lviy4OhlOID&open=AaGpO0556Lviy4OhlOID&pullRequest=19
fresh
cat > "$work/current/ci.yml" <<'YML'
name: Test
on: pull_request
jobs:
local:
runs-on: ubuntu-latest
steps:
- run: echo build
reusable:
uses: Owner/Repo/.github/workflows/check.yml@v1
YML
sha_key
self_ref
expect 0 'OK' 'a job-level call after another job with steps is still a reusable call'
Loading