Skip to content

Commit d2cad79

Browse files
committed
feat: Secure PR workflows by switching to pull_request_target with explicit head checkout and permission checks.
1 parent 36bb083 commit d2cad79

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

.github/workflows/test.yml

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ name: Tests
44
# This GitHub action runs your tests for each pull request and push.
55
# Optionally, you can turn it on using a schedule for regular testing.
66
on:
7-
pull_request:
8-
branches:
9-
- main
7+
pull_request_target:
8+
types: [opened, synchronize]
109
paths-ignore:
1110
- "README.md"
1211
push:
@@ -33,7 +32,22 @@ jobs:
3332
runs-on: ubuntu-latest
3433
timeout-minutes: 5
3534
steps:
35+
- id: check-access
36+
if: github.event_name == 'pull_request_target'
37+
uses: actions-cool/check-user-permission@v2
38+
with:
39+
require: write
40+
username: ${{ github.triggering_actor }}
41+
- name: Check User Permission
42+
if: github.event_name == 'pull_request_target' && steps.check-access.outputs.require-result != 'true'
43+
run: |
44+
echo "User ${{ github.triggering_actor }} lacks the necessary rights on this repository."
45+
echo "Their current access level: ${{ steps.checkAccess.outputs.user-permission }}"
46+
echo "Initial trigger for this job: ${{ github.actor }}"
47+
exit 1
3648
- uses: actions/checkout@v5
49+
with:
50+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
3751
- uses: actions/setup-go@v6
3852
with:
3953
go-version-file: "go.mod"
@@ -48,7 +62,22 @@ jobs:
4862
generate:
4963
runs-on: ubuntu-latest
5064
steps:
65+
- id: check-access
66+
if: github.event_name == 'pull_request_target'
67+
uses: actions-cool/check-user-permission@v2
68+
with:
69+
require: write
70+
username: ${{ github.triggering_actor }}
71+
- name: Check User Permission
72+
if: github.event_name == 'pull_request_target' && steps.check-access.outputs.require-result != 'true'
73+
run: |
74+
echo "User ${{ github.triggering_actor }} lacks the necessary rights on this repository."
75+
echo "Their current access level: ${{ steps.checkAccess.outputs.user-permission }}"
76+
echo "Initial trigger for this job: ${{ github.actor }}"
77+
exit 1
5178
- uses: actions/checkout@v5
79+
with:
80+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
5281
- uses: actions/setup-go@v6
5382
with:
5483
go-version-file: "go.mod"
@@ -77,7 +106,22 @@ jobs:
77106
terraform:
78107
- "1.13.*"
79108
steps:
109+
- id: check-access
110+
if: github.event_name == 'pull_request_target'
111+
uses: actions-cool/check-user-permission@v2
112+
with:
113+
require: write
114+
username: ${{ github.triggering_actor }}
115+
- name: Check User Permission
116+
if: github.event_name == 'pull_request_target' && steps.check-access.outputs.require-result != 'true'
117+
run: |
118+
echo "User ${{ github.triggering_actor }} lacks the necessary rights on this repository."
119+
echo "Their current access level: ${{ steps.checkAccess.outputs.user-permission }}"
120+
echo "Initial trigger for this job: ${{ github.actor }}"
121+
exit 1
80122
- uses: actions/checkout@v5
123+
with:
124+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
81125
- uses: actions/setup-go@v6
82126
with:
83127
go-version-file: "go.mod"

0 commit comments

Comments
 (0)