forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 7
136 lines (118 loc) · 4.46 KB
/
Copy pathcodeql.yaml
File metadata and controls
136 lines (118 loc) · 4.46 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
name: CodeQL
on:
push:
branches:
- main
pull_request:
types:
- opened
- synchronize
- reopened
jobs:
analyze:
name: Analyze
runs-on: ubuntu-24.04
permissions:
actions: read
contents: read
security-events: write
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
language: ['actions']
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Initialize CodeQL
uses: github/codeql-action/init@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
with:
languages: ${{ matrix.language }}
- name: Autobuild
uses: github/codeql-action/autobuild@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
with:
category: '/language:${{matrix.language}}'
output: sarif-results
upload: false
- name: Checking for missing-workflow-permissions issues
run: |
set -euo pipefail
SARIF_FILE="sarif-results/${{ matrix.language }}.sarif"
if [[ ! -f "$SARIF_FILE" ]]; then
echo "No SARIF file found"
touch /tmp/affected_files.txt
exit 0
fi
AFFECTED_FILES=$(jq -r '
.runs[].results // [] |
.[] |
select(.ruleId == "actions/missing-workflow-permissions") |
.locations[0].physicalLocation.artifactLocation.uri
' "$SARIF_FILE" | sort -u)
if [[ -z "$AFFECTED_FILES" ]]; then
echo "✅ No issues found"
touch /tmp/affected_files.txt
exit 0
fi
echo "$AFFECTED_FILES" > /tmp/affected_files.txt
echo "Found issues in $(echo "$AFFECTED_FILES" | wc -l) files"
- name: Get modified files
run: |
set -euo pipefail
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
MODIFIED_FILES=$(git diff --name-only "$BASE_SHA"..."$HEAD_SHA")
else
MODIFIED_FILES=$(git diff --name-only HEAD~1)
fi
echo "$MODIFIED_FILES" > /tmp/modified_files.txt
echo "Found $(echo "$MODIFIED_FILES" | wc -l) modified files"
- name: Check for issues in modified files
run: |
set -euo pipefail
SARIF_FILE="sarif-results/${{ matrix.language }}.sarif"
AFFECTED_FILES=$(cat /tmp/affected_files.txt)
MODIFIED_FILES=$(cat /tmp/modified_files.txt)
# If no affected files, skip check
if [[ -z "$AFFECTED_FILES" ]]; then
echo "✅ No issues in modified files"
exit 0
fi
# Check for issues in modified files
FAILED=false
while IFS= read -r file; do
[[ -z "$file" ]] && continue
if echo "$MODIFIED_FILES" | grep -qx "$file"; then
echo "❌ Missing permissions in modified file: $file"
jq -r --arg file "$file" '
.runs[].results // [] |
.[] |
select(.ruleId == "actions/missing-workflow-permissions" and
.locations[0].physicalLocation.artifactLocation.uri == $file) |
" Line \(.locations[0].physicalLocation.region.startLine): \(.message.text)"
' "$SARIF_FILE"
FAILED=true
fi
done <<< "$AFFECTED_FILES"
if [[ "$FAILED" == "true" ]]; then
echo ""
echo "❌ Pipeline failed: Modified files have missing workflow permissions"
exit 1
fi
echo "✅ No issues in modified files"
- name: Upload SARIF as artifact
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: codeql-sarif-${{ matrix.language }}
path: sarif-results/${{ matrix.language }}.sarif
retention-days: 7
- name: Upload SARIF to GitHub Security
uses: github/codeql-action/upload-sarif@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
with:
sarif_file: sarif-results/${{ matrix.language }}.sarif
category: '/language:${{matrix.language}}'