Skip to content

Commit 39f4144

Browse files
committed
fix(internal): 💚 Use pull_request_target and manual checks of permissions to be able to access secrets for DB branching
1 parent b81fbba commit 39f4144

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

‎.github/workflows/turso_branching.yaml‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Automatically create and cleanup Turso DB branches for each PR
22

33
on:
4-
pull_request:
4+
pull_request_target:
55
types:
66
- opened
77
- reopened
@@ -16,11 +16,27 @@ jobs:
1616
permissions:
1717
contents: read
1818
steps:
19+
- name: Get user permissions
20+
id: permissions
21+
uses: actions-cool/check-user-permission@v2
22+
with:
23+
require: write
24+
username: ${{ github.triggering_actor }}
25+
env:
26+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
- name: Check user permissions
28+
if: steps.permissions.outputs.require-result == 'false'
29+
run: |
30+
echo "${{ github.triggering_actor }} does not have permissions on this repo."
31+
echo "A user with write permissions should very carefully check the diff of this PR and then re-run the workflow if it is safe"
32+
exit 1
1933
- name: Get branch name
2034
id: branch_name
2135
uses: tj-actions/branch-names@v8
2236
- name: Checkout
2337
uses: actions/checkout@v4
38+
with:
39+
ref: ${{ github.event.pull_request.head.sha }} # This is dangerous without the first access check
2440
- name: Generate branched DB ID
2541
id: branch_db_id
2642
uses: actions/github-script@v7
@@ -62,7 +78,6 @@ jobs:
6278
fi
6379
fi
6480
65-
echo $RESPONSE
6681
HOSTNAME=$(echo $RESPONSE | jq -r '.database.Hostname')
6782
if [ -z "$HOSTNAME" ]; then
6883
echo "Hostname not found in response"
@@ -72,6 +87,8 @@ jobs:
7287
echo "hostname=$HOSTNAME" >> $GITHUB_OUTPUT
7388
- name: Checkout
7489
uses: actions/checkout@v4
90+
with:
91+
ref: ${{ github.event.pull_request.head.sha }} # This is dangerous without the first access check
7592
- name: Run migrations
7693
env:
7794
DATABASE_URL: "libsql://${{ steps.create_db_branch.hostname }}"
@@ -93,6 +110,8 @@ jobs:
93110
- name: Checkout
94111
if: github.event.pull_request.merged == true
95112
uses: actions/checkout@v4
113+
with:
114+
ref: ${{ github.event.pull_request.head.sha }} # This is dangerous without the first access check
96115
- name: Apply migrations to production
97116
if: github.event.pull_request.merged == true
98117
env:

0 commit comments

Comments
 (0)