Skip to content

Commit 352aa9d

Browse files
committed
Harden ci workflow
1 parent 7ee0634 commit 352aa9d

File tree

1 file changed

+67
-10
lines changed

1 file changed

+67
-10
lines changed

.github/workflows/ci.yml

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,53 @@ name: CI
22

33
on:
44
push:
5-
pull_request:
5+
branches: [main]
6+
pull_request_target:
7+
types: [opened, synchronize, reopened, labeled]
68

79
jobs:
10+
# Security check for fork PRs - validates PR source before running workflows with secrets
11+
security_check:
12+
name: Security Check
13+
runs-on: ubuntu-latest
14+
if: github.event_name == 'pull_request_target'
15+
outputs:
16+
is_fork: ${{ steps.check.outputs.is_fork }}
17+
is_safe: ${{ steps.check.outputs.is_safe }}
18+
steps:
19+
- name: Check PR source
20+
id: check
21+
run: |
22+
IS_FORK="false"
23+
IS_SAFE="false"
24+
25+
if [ "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]; then
26+
IS_FORK="true"
27+
echo "Fork PR detected from: ${{ github.event.pull_request.head.repo.full_name }}"
28+
fi
29+
30+
# Check if PR has 'safe-to-test' label (maintainer approval)
31+
if [ "${{ contains(github.event.pull_request.labels.*.name, 'safe-to-test') }}" = "true" ]; then
32+
IS_SAFE="true"
33+
echo "PR marked safe-to-test by maintainer"
34+
fi
35+
36+
# Non-fork PRs are always safe
37+
if [ "$IS_FORK" = "false" ]; then
38+
IS_SAFE="true"
39+
fi
40+
41+
echo "is_fork=$IS_FORK" >> $GITHUB_OUTPUT
42+
echo "is_safe=$IS_SAFE" >> $GITHUB_OUTPUT
43+
844
test:
945
name: Test
1046
runs-on: ubuntu-latest
47+
needs: [security_check]
48+
if: |
49+
always() &&
50+
(github.event_name == 'push' ||
51+
(github.event_name == 'pull_request_target' && needs.security_check.outputs.is_safe == 'true'))
1152
permissions:
1253
contents: read
1354
strategy:
@@ -16,15 +57,17 @@ jobs:
1657

1758
steps:
1859
- name: Checkout code
19-
uses: actions/checkout@v4
60+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
61+
with:
62+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
2063

2164
- name: Set up Go
22-
uses: actions/setup-go@v5
65+
uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.2.0
2366
with:
2467
go-version: ${{ matrix.go-version }}
2568

2669
- name: Cache Go modules
27-
uses: actions/cache@v4
70+
uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
2871
with:
2972
path: |
3073
~/.cache/go-build
@@ -49,7 +92,7 @@ jobs:
4992
5093
- name: Upload coverage to Codecov
5194
if: matrix.go-version == '1.25.x'
52-
uses: codecov/codecov-action@v4
95+
uses: codecov/codecov-action@015f24e6818733317a2da2edd6290ab26238649a # v4.6.0
5396
with:
5497
files: ./coverage.out
5598
flags: unittests
@@ -59,36 +102,50 @@ jobs:
59102
lint:
60103
name: Lint
61104
runs-on: ubuntu-latest
105+
needs: [security_check]
106+
if: |
107+
always() &&
108+
(github.event_name == 'push' ||
109+
(github.event_name == 'pull_request_target' && needs.security_check.outputs.is_safe == 'true'))
62110
permissions:
63111
contents: read
64112

65113
steps:
66114
- name: Checkout code
67-
uses: actions/checkout@v4
115+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
116+
with:
117+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
68118

69119
- name: Set up Go
70-
uses: actions/setup-go@v5
120+
uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.2.0
71121
with:
72122
go-version: '1.25.x'
73123

74124
- name: Run golangci-lint
75-
uses: golangci/golangci-lint-action@v8
125+
uses: golangci/golangci-lint-action@971e284b6050e8a5849b72094c50ab08da042db8 # v8.3.1
76126
with:
77127
version: v2.5
78128
args: --timeout=5m
79129

80130
security:
81131
name: Security
82132
runs-on: ubuntu-latest
133+
needs: [security_check]
134+
if: |
135+
always() &&
136+
(github.event_name == 'push' ||
137+
(github.event_name == 'pull_request_target' && needs.security_check.outputs.is_safe == 'true'))
83138
permissions:
84139
contents: read
85140

86141
steps:
87142
- name: Checkout code
88-
uses: actions/checkout@v4
143+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
144+
with:
145+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
89146

90147
- name: Set up Go
91-
uses: actions/setup-go@v5
148+
uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.2.0
92149
with:
93150
go-version: '1.25.x'
94151

0 commit comments

Comments
 (0)