-
-
Notifications
You must be signed in to change notification settings - Fork 5
203 lines (187 loc) · 6.44 KB
/
security-scan.yml
File metadata and controls
203 lines (187 loc) · 6.44 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
name: 'Security Code Scanner'
# Minimal permissions - jobs override as needed
permissions:
contents: read
on:
workflow_call:
inputs:
repo:
description: 'Repository that requested the scan'
required: false
type: string
default: ${{ github.repository }}
languages-config:
description: |
Optional: JSON array of language configurations to override/supplement repo config file.
Auto-detects languages and loads repo-specific config from packages/codeql-action/repo-configs/${repo}.cjs.
Workflow input takes precedence over file config for same languages.
Example:
[
{
"language": "java",
"build_mode": "manual",
"build_command": "./gradlew build",
"version": "21",
"distribution": "temurin"
},
{
"language": "javascript",
"build_mode": "none"
}
]
required: false
type: string
default: ''
scanner-ref:
description: 'The git ref of the scanner to use'
required: true
type: string
paths-ignored:
description: 'Multi-line list of paths to ignore during scan (one path per line, supports glob patterns)'
required: false
type: string
default: ''
rules-excluded:
description: 'Comma delimited IDs of rules to exclude'
required: false
type: string
default: ''
secrets:
project-metrics-token:
description: 'Analytics token to log failed builds'
required: false
slack-webhook:
description: 'Slack webhook for notifications'
required: false
env:
MONOREPO_PATH: .security-scanner
jobs:
# Detect languages and create matrix
setup:
name: Setup
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.detect-languages.outputs.matrix }}
steps:
- name: Checkout monorepo (for local actions)
uses: actions/checkout@v6
with:
repository: MetaMask/action-security-code-scanner
ref: ${{ inputs.scanner-ref }}
path: ${{ env.MONOREPO_PATH }}
- name: Checkout target repository
uses: actions/checkout@v6
with:
repository: ${{ inputs.repo }}
path: ${{ inputs.repo }}
- name: Detect languages and create matrix
id: detect-languages
uses: ./.security-scanner/packages/language-detector
with:
repo: ${{ inputs.repo }}
languages_config: ${{ inputs.languages-config }}
# Run CodeQL analysis for each detected language
codeql-analysis:
name: CodeQL analysis
needs: setup
if: ${{ fromJSON(needs.setup.outputs.matrix).include[0] != null }}
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.setup.outputs.matrix) }}
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
steps:
- name: Checkout monorepo (for local actions)
uses: actions/checkout@v6
with:
repository: MetaMask/action-security-code-scanner
ref: ${{ inputs.scanner-ref }}
path: ${{ env.MONOREPO_PATH }}
- name: Checkout target repository
uses: actions/checkout@v6
with:
repository: ${{ inputs.repo }}
path: ${{ inputs.repo }}
- name: Run CodeQL Analysis
uses: ./.security-scanner/packages/codeql-action
with:
repo: ${{ inputs.repo }}
language: ${{ matrix.language }}
paths_ignored: ${{ inputs.paths-ignored }}
rules_excluded: ${{ inputs.rules-excluded }}
build_mode: ${{ matrix.build_mode }}
build_command: ${{ matrix.build_command }}
version: ${{ matrix.version }}
distribution: ${{ matrix.distribution }}
# Run Semgrep analysis once for all languages
semgrep-analysis:
name: Semgrep analysis
needs: setup
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
steps:
- name: Checkout monorepo (for local actions)
uses: actions/checkout@v6
with:
repository: MetaMask/action-security-code-scanner
ref: ${{ inputs.scanner-ref }}
path: ${{ env.MONOREPO_PATH }}
- name: Checkout target repository
uses: actions/checkout@v6
with:
repository: ${{ inputs.repo }}
path: ${{ inputs.repo }}
- name: Run Semgrep Analysis
uses: ./.security-scanner/packages/semgrep-action
with:
paths_ignored: ${{ inputs.paths-ignored }}
# Collect results and handle notifications
finalize:
name: Finalize scans and notify
needs: [codeql-analysis, semgrep-analysis]
if: always()
runs-on: ubuntu-latest
env:
SLACK_WEBHOOK: ${{ secrets.slack-webhook }}
PROJECT_METRICS_TOKEN: ${{ secrets.project-metrics-token }}
REPO: ${{ inputs.repo }}
steps:
- name: Determine overall scan result
id: scan-result
env:
CODEQL_RESULT: ${{ needs.codeql-analysis.result }}
SEMGREP_RESULT: ${{ needs.semgrep-analysis.result }}
run: |
if [[ "$CODEQL_RESULT" == "failure" || "$SEMGREP_RESULT" == "failure" ]]; then
echo "result=failure" >> "$GITHUB_OUTPUT"
echo "SCAN_RESULT=failure" >> "$GITHUB_ENV"
else
echo "result=success" >> "$GITHUB_OUTPUT"
echo "SCAN_RESULT=success" >> "$GITHUB_ENV"
fi
- name: Post to Slack channel
if: ${{ steps.scan-result.outputs.result == 'failure' && env.SLACK_WEBHOOK != '' }}
uses: slackapi/slack-github-action@007b2c3c751a190b6f0f040e47ed024deaa72844
with:
payload: |
{
"text": "Security scan failed for ${{ env.REPO }} - Run: https://github.com/${{ env.REPO }}/actions/runs/${{ github.run_id }}",
"channel": "#security-notifications"
}
env:
SLACK_WEBHOOK_URL: ${{ env.SLACK_WEBHOOK }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
- name: Log metrics
if: ${{ env.PROJECT_METRICS_TOKEN != '' }}
run: |
# TODO: Implement metrics logging
echo "Logging scan result: $SCAN_RESULT for repo: ${{ env.REPO }}"
- name: Fail if security issues found
if: ${{ steps.scan-result.outputs.result == 'failure' }}
run: exit 1