Repository navigation
142 lines (125 loc) · 4.92 KB
/
Copy pathcounter-stack-check.yml
File metadata and controls
142 lines (125 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
name: Counter-Stack Check
# t2763: CI lint gate that detects the grep -c counter-stacking bug class.
# Scans only PR-changed .sh and .yml files (diff-scoped — Option A) so existing
# violations on main don't block. Phase 2 sweep (t2762) will clear them.
#
# Bug class: `count=$(... grep -c 'pat' ... || echo "N")` produces multi-line
# "0\nN" output on zero-match path because grep -c exits 1. Canonical failure:
# parent issue #20402 rendered "Progress: **0\n0 done**".
#
# Violations posted as a PR comment linking to the style guide.
on:
pull_request:
types: [opened, synchronize, reopened]
paths:
- '.agents/scripts/**/*.sh'
- '.agents/hooks/**/*.sh'
- '.github/workflows/**/*.yml'
- '.github/workflows/**/*.yaml'
permissions:
pull-requests: write
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
check:
name: Counter-Stack Lint
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
fetch-depth: 0
- name: Get changed shell + workflow files
id: changed
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
set -euo pipefail
changed=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" -- \
'.agents/scripts/**/*.sh' \
'.agents/hooks/**/*.sh' \
'.github/workflows/**/*.yml' \
'.github/workflows/**/*.yaml' || true)
if [ -z "$changed" ]; then
echo "No counter-stack-scannable files changed."
echo "has_files=false" >> "$GITHUB_OUTPUT"
exit 0
fi
printf 'Changed files:\n%s\n' "$changed"
echo "has_files=true" >> "$GITHUB_OUTPUT"
printf '%s\n' "$changed" > /tmp/changed-cs-files.txt
- name: Run counter-stack check
if: steps.changed.outputs.has_files == 'true'
id: scan
run: |
set +e
files=$(tr '\n' ' ' < /tmp/changed-cs-files.txt)
# shellcheck disable=SC2086
output=$(bash .agents/scripts/counter-stack-check.sh --scan-files $files 2>&1)
exit_code=$?
set -e
echo "exit_code=$exit_code" >> "$GITHUB_OUTPUT"
printf '%s\n' "$output"
printf '%s\n' "$output" > /tmp/counter-stack-report.txt
- name: Post PR comment on failure
if: steps.changed.outputs.has_files == 'true' && steps.scan.outputs.exit_code == '1'
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
run: |
set -euo pipefail
report=$(cat /tmp/counter-stack-report.txt)
marker="<!-- counter-stack-check -->"
body=$(cat <<EOF
${marker}
## Counter-Stacking Violations (grep -c safety)
The following files introduce or modify \`grep -c … || echo "N"\` patterns
that produce multi-line output on the zero-match path (see
[style guide](.agents/reference/shell-style-guide.md#counter-safety-grep--c)
and canonical failure #20402):
\`\`\`
${report}
\`\`\`
**Fix:** Source \`shared-constants.sh\` and use \`safe_grep_count\`:
\`\`\`bash
count=\$(safe_grep_count 'pat' file)
\`\`\`
Or, in YAML workflow steps and bootstrap scripts that cannot source
\`shared-constants.sh\`, use the inline guard form:
\`\`\`bash
count=\$(grep -c 'pat' file 2>/dev/null || true)
[[ "\$count" =~ ^[0-9]+\$ ]] || count=0
\`\`\`
Run \`counter-stack-check.sh --fix-hint\` for remediation snippets.
EOF
)
existing=$(gh api "repos/${REPO}/issues/${PR_NUMBER}/comments" \
--jq "[.[] | select(.body | contains(\"$marker\")) | .id] | .[0] // empty" \
2>/dev/null || true)
if [ -n "$existing" ]; then
gh api "repos/${REPO}/issues/comments/${existing}" \
--method PATCH -F body="$body" >/dev/null 2>&1 || true
else
gh pr comment "$PR_NUMBER" --repo "$REPO" --body "$body" \
>/dev/null 2>&1 || true
fi
- name: Enforce gate
if: steps.changed.outputs.has_files == 'true'
env:
EXIT_CODE: ${{ steps.scan.outputs.exit_code }}
run: |
set -euo pipefail
if [ "${EXIT_CODE:-0}" = "0" ]; then
echo "Counter-stack check: clean."
exit 0
fi
if [ "$EXIT_CODE" = "1" ]; then
echo "::error::Counter-stack violations found. See PR comment for details."
exit 1
fi
echo "::error::Counter-stack check failed with unexpected exit code: $EXIT_CODE"
exit 1