-
Notifications
You must be signed in to change notification settings - Fork 321
158 lines (140 loc) · 5.54 KB
/
Copy pathsecurity.yml
File metadata and controls
158 lines (140 loc) · 5.54 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
name: Security
on:
pull_request:
types: [opened, reopened, synchronize, ready_for_review]
branches: ['**']
push:
branches: [main]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
permissions:
contents: read
jobs:
semgrep:
name: Semgrep SAST
runs-on: ubuntu-latest
container:
image: semgrep/semgrep@sha256:a3d49dc967b8534a6a76628e50c51cbfe33eb7195dc2feab1fdc0f100852c8ef
permissions:
contents: read
pull-requests: write
security-events: write
if: github.actor != 'dependabot[bot]'
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Run Semgrep (full scan)
if: github.event_name != 'pull_request'
run: |
semgrep scan \
--config p/typescript \
--config p/react \
--config p/nodejs \
--config p/owasp-top-ten \
--config p/security-audit \
--config p/secrets \
--config p/jwt \
--sarif-output semgrep.sarif \
--json-output semgrep.json
- name: Run Semgrep (diff-aware)
if: github.event_name == 'pull_request'
run: |
semgrep scan \
--config p/typescript \
--config p/react \
--config p/nodejs \
--config p/owasp-top-ten \
--config p/security-audit \
--config p/secrets \
--config p/jwt \
--baseline-commit ${{ github.event.pull_request.base.sha }} \
--sarif-output semgrep.sarif \
--json-output semgrep.json
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@99df26d4f13ea111d4ec1a7dddef6063f76b97e9 # v4.37.0
if: always() && hashFiles('semgrep.sarif') != ''
continue-on-error: true
with:
sarif_file: semgrep.sarif
# Post findings as a PR comment using curl + jq (no Node.js needed in Alpine).
# Security: semgrep output is read from files and escaped by jq — never
# interpolated through ${{ }} or shell expansion — preventing injection.
- name: Post Semgrep results as PR comment
if: always() && github.event_name == 'pull_request' && hashFiles('semgrep.json') != ''
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
shell: sh
run: |
apk add --no-cache curl jq > /dev/null 2>&1 || true
MARKER="<!-- semgrep-scan-results -->"
FINDING_COUNT=$(jq '.results | length' semgrep.json)
# Build the comment body in a file (never in a shell variable)
{
echo "$MARKER"
echo "## Semgrep Security Scan"
echo ""
if [ "$FINDING_COUNT" -eq 0 ]; then
echo "No security issues found."
else
echo "**Found $FINDING_COUNT issue(s).**"
echo ""
echo "| # | Severity | Rule | File | Line |"
echo "|---|----------|------|------|------|"
# Use jq to produce pre-escaped table rows from the JSON file.
# jq --raw-output safely handles any characters in the fields.
jq -r '
.results | to_entries[] |
"\(.key + 1) | " +
"`\(.value.extra.severity // "UNKNOWN")` | " +
"`\(.value.check_id | split(".")[-1])` | " +
"`\(.value.path)` | " +
"L\(.value.start.line)"
' semgrep.json | while IFS= read -r row; do
echo "| $row |"
done
echo ""
echo "<details>"
echo "<summary>Finding details</summary>"
echo ""
jq -r '
.results[] |
"### `\(.check_id | split(".")[-1])` — \(.path):\(.start.line)\n" +
"**Severity:** \(.extra.severity // "UNKNOWN") \n" +
"**Message:** \(.extra.message)\n" +
"```\n\(.extra.lines)\n```\n"
' semgrep.json
echo "</details>"
fi
} > comment_body.md
# Use jq to build the JSON payload from the file — jq handles all escaping.
PAYLOAD=$(jq -n --rawfile body comment_body.md '{ body: $body }')
# Find existing comment by marker
COMMENT_ID=$(
curl -sf \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$REPO/issues/$PR_NUMBER/comments?per_page=100" \
| jq ".[] | select(.body | startswith(\"$MARKER\")) | .id" \
| head -1
)
if [ -n "$COMMENT_ID" ]; then
# Update existing comment
curl -sf -X PATCH \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$REPO/issues/comments/$COMMENT_ID" \
-d "$PAYLOAD" > /dev/null
echo "Updated existing comment $COMMENT_ID"
else
# Create new comment
curl -sf -X POST \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$REPO/issues/$PR_NUMBER/comments" \
-d "$PAYLOAD" > /dev/null
echo "Created new comment"
fi