Repository navigation
Expand file tree
/
Copy pathaction.yml
More file actions
235 lines (221 loc) · 10.2 KB
/
Copy pathaction.yml
File metadata and controls
235 lines (221 loc) · 10.2 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
name: "Shingan — AI Agent Workflow Static Analyzer"
description: "AIエージェントワークフローを実行前に静的解析し、無限ループ・コスト爆発・PII漏洩経路を検出"
author: "hatyibei"
branding:
icon: "eye"
color: "purple"
inputs:
format:
description: "Input format: auto / json / adk-go / samurai / langgraph / n8n / crewai / langgraph-js / pydantic-graph / llamaindex / autogen / mastra / openai-agents ('auto' infers from extension + content)"
required: false
default: "adk-go"
input:
description: "Workflow file or directory to analyze"
required: true
output:
description: "Output format (json / markdown / sarif)"
required: false
default: "sarif"
output-file:
description: "Optional output file path. Default: stdout for json/markdown, shingan.sarif for sarif"
required: false
default: ""
fail-on:
description: "Exit non-zero on this Severity or higher (critical / warning / info / never)"
required: false
default: "critical"
baseline-file:
description: "Optional path to a baseline JSON produced by --save-baseline. When set, findings already present in the baseline are suppressed (progressive adoption)."
required: false
default: ""
since:
description: "Optional git ref (e.g. main). When set, only files changed since this ref are analyzed."
required: false
default: ""
policy-file:
description: "Optional path to .shingan.yaml policy file. Default: walk up from CWD looking for .shingan.yaml"
required: false
default: ""
pr-comment:
description: "Post a sticky markdown comment on the PR summarising findings. Requires `permissions: pull-requests: write` and a pull_request trigger."
required: false
default: "false"
pr-comment-mode:
description: "Controls when the PR comment is posted: 'always' / 'on-findings' (only when total>0) / 'on-failure' (only when fail-on threshold tripped)."
required: false
default: "on-findings"
outputs:
exit-code:
description: "0=no findings or Info only, 1=Warning detected, 2=Critical detected"
value: ${{ steps.analyze.outputs.exit-code }}
findings-count:
description: "Total number of findings"
value: ${{ steps.analyze.outputs.findings-count }}
runs:
using: "composite"
steps:
- name: Setup Go
uses: actions/setup-go@v5
with:
# Pin to a patch level that includes the most recent
# security fixes. A bare minor (`1.26`) floats to whatever setup-go
# decides; the exact patch matches go.mod's `toolchain` line, the
# build that clears the govulncheck baseline (crypto/tls, crypto/x509,
# net/http, html/template fixes in go1.26.9+).
go-version: "1.26.11"
cache: true
- name: Install Shingan
shell: bash
# Pin the installed CLI to the action ref the caller selected, NOT
# @latest. A workflow that pins `uses: hatyibei/shingan@v0.9.0` must get
# the v0.9.0 CLI — otherwise a pinned action tag still silently runs the
# newest CLI (non-reproducible CI, and a supply-chain footgun if a bad
# release ships). `github.action_ref` is the tag/branch/SHA the caller
# referenced; it stays in sync automatically as releases are cut.
# It is empty for a local `uses: ./` ref, so fall back to the pinned
# current release (kept in lockstep with npm/package.json's version).
# The ref is routed through env: (not inline ${{ }}) so it is an opaque
# string, matching the injection-hardening used in the analyze step.
env:
ACTION_REF: ${{ github.action_ref }}
run: |
VERSION="${ACTION_REF:-v0.9.0}"
echo "shingan: installing CLI @ ${VERSION}"
go install "github.com/hatyibei/shingan/cmd/shingan@${VERSION}"
- name: Run Shingan analyze
id: analyze
shell: bash
# All `${{ inputs.* }}` values are routed through the env block so
# an attacker-controlled value (e.g. `input: foo.py"; curl evil$IFS`)
# cannot break out of the shell context. Direct `${{ }}` expansion
# inside `run:` is template-time substitution — values with `"`,
# `$()`, backticks etc. become live shell tokens. Threading them
# through `env:` keeps the values as opaque strings.
env:
INPUT_FORMAT: ${{ inputs.format }}
INPUT_INPUT: ${{ inputs.input }}
INPUT_OUTPUT: ${{ inputs.output }}
INPUT_OUTPUT_FILE: ${{ inputs.output-file }}
INPUT_BASELINE_FILE: ${{ inputs.baseline-file }}
INPUT_SINCE: ${{ inputs.since }}
INPUT_POLICY_FILE: ${{ inputs.policy-file }}
INPUT_FAIL_ON: ${{ inputs.fail-on }}
run: |
OUT="$INPUT_OUTPUT_FILE"
if [ -z "$OUT" ] && [ "$INPUT_OUTPUT" = "sarif" ]; then
OUT="shingan.sarif"
fi
FLAGS=(--format "$INPUT_FORMAT" --input "$INPUT_INPUT" --output "$INPUT_OUTPUT")
if [ -n "$OUT" ]; then
FLAGS+=(--output-file "$OUT")
fi
if [ -n "$INPUT_BASELINE_FILE" ]; then
FLAGS+=(--baseline "$INPUT_BASELINE_FILE")
fi
if [ -n "$INPUT_SINCE" ]; then
FLAGS+=(--since "$INPUT_SINCE")
fi
if [ -n "$INPUT_POLICY_FILE" ]; then
FLAGS+=(--policy "$INPUT_POLICY_FILE")
fi
# Always also produce a JSON copy so the PR-comment formatter
# downstream has structured input regardless of the user's
# primary --output choice.
JSON_OUT="$RUNNER_TEMP/shingan-findings.json"
JSON_FLAGS=(--format "$INPUT_FORMAT" --input "$INPUT_INPUT" --output json --output-file "$JSON_OUT")
if [ -n "$INPUT_BASELINE_FILE" ]; then JSON_FLAGS+=(--baseline "$INPUT_BASELINE_FILE"); fi
if [ -n "$INPUT_SINCE" ]; then JSON_FLAGS+=(--since "$INPUT_SINCE"); fi
if [ -n "$INPUT_POLICY_FILE" ]; then JSON_FLAGS+=(--policy "$INPUT_POLICY_FILE"); fi
shingan analyze "${JSON_FLAGS[@]}" >/dev/null 2>&1 || true
set +e
shingan analyze "${FLAGS[@]}"
EC=$?
set -e
echo "exit-code=$EC" >> "$GITHUB_OUTPUT"
echo "json-output=$JSON_OUT" >> "$GITHUB_OUTPUT"
if [ -f "$JSON_OUT" ]; then
# Python script reads $JSON_OUT via os.environ to avoid string
# interpolation injection — even though $JSON_OUT is a fixed
# $RUNNER_TEMP path right now, this hardens the call against
# accidental future changes.
COUNT=$(JSON_OUT="$JSON_OUT" python3 -c 'import json,os;d=json.load(open(os.environ["JSON_OUT"]));print(d.get("summary",{}).get("total",0))' 2>/dev/null || echo 0)
else
COUNT=0
fi
echo "findings-count=$COUNT" >> "$GITHUB_OUTPUT"
# Fail behavior
case "$INPUT_FAIL_ON" in
critical) [ $EC -ge 2 ] && exit 2 || exit 0 ;;
warning) [ $EC -ge 1 ] && exit $EC || exit 0 ;;
info) exit $EC ;;
never) exit 0 ;;
*) echo "Invalid fail-on: $INPUT_FAIL_ON"; exit 3 ;;
esac
- name: Post sticky PR comment
if: ${{ always() && inputs.pr-comment == 'true' && github.event_name == 'pull_request' }}
shell: bash
env:
GH_TOKEN: ${{ github.token }}
JSON_OUT: ${{ steps.analyze.outputs.json-output }}
EC: ${{ steps.analyze.outputs.exit-code }}
MODE: ${{ inputs.pr-comment-mode }}
REPO: ${{ github.repository }}
PR: ${{ github.event.pull_request.number }}
run: |
case "$MODE" in
always) ;;
on-findings)
COUNT=$(JSON_OUT="$JSON_OUT" python3 -c 'import json,os;d=json.load(open(os.environ["JSON_OUT"]));print(d.get("summary",{}).get("total",0))' 2>/dev/null || echo 0)
[ "$COUNT" = "0" ] && { echo "no findings; skipping PR comment"; exit 0; }
;;
on-failure)
[ "$EC" = "0" ] && { echo "no failure; skipping PR comment"; exit 0; }
;;
*) echo "Invalid pr-comment-mode: $MODE"; exit 3 ;;
esac
MARKER="<!-- shingan-pr-comment-marker -->"
BODY="$RUNNER_TEMP/shingan-pr-body.md"
{
echo "$MARKER"
echo "## 🔍 Shingan static analysis"
echo
if [ -f "$JSON_OUT" ]; then
python3 - "$JSON_OUT" <<'PY'
import json, sys
with open(sys.argv[1]) as fp: d = json.load(fp)
s = d.get('summary', {}); total = s.get('total', 0)
if total == 0:
print('✅ **No findings.**'); sys.exit(0)
crit, warn, info = s.get('critical',0), s.get('warning',0), s.get('info',0)
print(f'**Summary**: {total} findings — {crit} Critical · {warn} Warning · {info} Info')
print()
findings = d.get('findings', [])
top = findings[:10]
if top:
print('| Severity | Rule | Node | Confidence | File:Line |')
print('|---|---|---|---|---|')
for f in top:
pos = f.get('pos') or {}
file = pos.get('file') or f.get('source_file') or ''
line = pos.get('line') or 0
src = f"{file}:{line}" if line else file
conf = int(f.get('confidence', 0) * 100)
print(f"| {f['severity']} | `{f['rule']}` | `{f['node_id'][:48]}` | {conf}% | `{src}` |")
if len(findings) > 10:
print(f"\n…and {len(findings)-10} more — see the workflow log.")
PY
else
echo "_(no JSON output — see the workflow logs)_"
fi
echo
echo "<sub>Posted by [shingan](https://github.com/hatyibei/shingan) · use [\`# shingan: ignore <rule>\`](https://github.com/hatyibei/shingan/blob/main/docs/ignore-comments.md) to suppress an individual finding · [\`.shingan.yaml\`](https://github.com/hatyibei/shingan/blob/main/docs/severity-policy.md) for repo-level overrides</sub>"
} > "$BODY"
EXISTING=$(gh api "repos/$REPO/issues/$PR/comments" --paginate \
--jq ".[] | select(.body | contains(\"$MARKER\")) | .id" | head -1)
if [ -n "$EXISTING" ]; then
gh api -X PATCH "repos/$REPO/issues/comments/$EXISTING" -F "body=@$BODY"
echo "Updated existing PR comment $EXISTING"
else
gh api -X POST "repos/$REPO/issues/$PR/comments" -F "body=@$BODY"
echo "Posted new PR comment"
fi