18
18
pull-requests : write
19
19
outputs :
20
20
met : ${{ steps.label.outputs.found || steps.workflow_dispatch.outputs.found }}
21
+ active_branch_name_sanitized : ${{ steps.sanitize.outputs.branch }}
21
22
steps :
22
23
- uses : actions/checkout@v4
23
24
with :
27
28
# This machine account is only for this PAT, pwd was created and thrown away
28
29
# If an update is needed, create a new account, add access to the repo and generate a new PAT
29
30
token : ${{ secrets.MACHINE_ACCOUNT_PAT }}
31
+ # Sanitize branch name for any subsequent git checkout to prevent branch name exploits
32
+ - name : Sanitize branch name
33
+ id : sanitize
34
+ if : ${{ github.event_name == 'pull_request' }}
35
+ env :
36
+ BRANCH_REF : ${{ github.event.pull_request.head.ref }}
37
+ run : |
38
+ # Sanitize branch name to prevent command injection
39
+ SAFE_BRANCH=$(echo "$BRANCH_REF" | sed 's/[^a-zA-Z0-9._\/-]//g')
40
+ echo "branch=$SAFE_BRANCH" >> $GITHUB_OUTPUT
41
+ # After sanitizing the branchname, confirm it exists (it won't if exploit code was stripped)
42
+ # After sanitizing the branchname, confirm it exists (it won't if exploit code was stripped)
43
+ - name : Check sanitized branchname exists
44
+ if : ${{ github.event_name == 'pull_request' }}
45
+ env :
46
+ SAFE_BRANCH : ${{ steps.sanitize.outputs.branch }}
47
+ run : |
48
+ if git ls-remote --heads origin "$SAFE_BRANCH" | grep -q "$SAFE_BRANCH"; then
49
+ echo "Branch '$SAFE_BRANCH' found."
50
+ else
51
+ echo "Error: Branch '$SAFE_BRANCH' not found on origin" >&2
52
+ exit 1
53
+ fi
30
54
- name : Found workflow disptach
31
55
id : workflow_dispatch
32
56
if : ${{ github.event_name == 'workflow_dispatch' }}
37
61
env :
38
62
# Required for running `gh`.
39
63
GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
64
+ PR_NUMBER : ${{ github.event.number }}
40
65
run : |
41
66
echo "found=true" >> $GITHUB_OUTPUT
42
- gh pr edit ${{ github.event.number }} --remove-label update_snapshots
67
+ gh pr edit "$PR_NUMBER" --remove-label update_snapshots
43
68
44
69
get_matrix :
45
70
name : Set CI flavors
@@ -79,9 +104,12 @@ jobs:
79
104
token : ${{ secrets.MACHINE_ACCOUNT_PAT }}
80
105
- name : Checkout branch if on a PR
81
106
if : ${{ github.event_name != 'workflow_dispatch' }}
107
+ # Use sanitized branch name from precondition job
108
+ env :
109
+ SAFE_BRANCH : ${{ needs.precondition.outputs.active_branch_name_sanitized }}
82
110
# On the `pull_request` event, actions/checkout@v4 leaves the local checkout in a detached head state.
83
111
# Explicitly checkout the target branch so we can push later.
84
- run : git checkout ${{ github.event.pull_request.head.ref }}
112
+ run : git checkout "$SAFE_BRANCH"
85
113
- name : Setup bot git information
86
114
# User id of github actions bot. See https://api.github.com/users/better-informatics%5Bbot%5D
87
115
run : |
@@ -136,11 +164,12 @@ jobs:
136
164
echo "hasChanged=false" >> $GITHUB_OUTPUT
137
165
else
138
166
echo "hasChanged=true" >> $GITHUB_OUTPUT
139
- exit
140
167
fi
141
168
- name : Push new snapshots, if any
142
169
if : ${{ steps.changescheck.outputs.hasChanged == 'true' }}
143
- # Before pushing changes to origin, merge any intervening changes on the upstream branch.
170
+ env :
171
+ SAFE_BRANCH : ${{ needs.precondition.outputs.active_branch_name_sanitized }}
172
+ # Before pushing changes to origin, merge any intervening changes on the upstream branch
144
173
# This allows multiple snapshot update jobs in this action to run concurrently.
145
174
# - The only files updated locally are UI snapshots
146
175
# - Each job is responsible for a unique set of UI snapshots
@@ -153,7 +182,7 @@ jobs:
153
182
run : |
154
183
git add samples/tests/*.png
155
184
git commit -m 'Update component examples snapshots'
156
- git pull origin ${{ needs.get_target_branch.outputs.target }} --no-rebase --no-edit
185
+ git pull origin "$SAFE_BRANCH" --no-rebase --no-edit
157
186
git push
158
187
159
188
html_bundle :
@@ -175,9 +204,11 @@ jobs:
175
204
token : ${{ secrets.MACHINE_ACCOUNT_PAT }}
176
205
- name : Checkout branch if on a PR
177
206
if : ${{ github.event_name != 'workflow_dispatch' }}
207
+ env :
208
+ SAFE_BRANCH : ${{ needs.precondition.outputs.active_branch_name_sanitized }}
178
209
# On the `pull_request` event, actions/checkout@v4 leaves the local checkout in a detached head state.
179
210
# Explicitly checkout the target branch so we can push later.
180
- run : git checkout ${{ github.event.pull_request.head.ref }}
211
+ run : git checkout "$SAFE_BRANCH"
181
212
- name : Setup bot git information
182
213
# User id of github actions bot. See https://api.github.com/users/better-informatics%5Bbot%5D
183
214
run : |
@@ -232,11 +263,12 @@ jobs:
232
263
echo "hasChanged=false" >> $GITHUB_OUTPUT
233
264
else
234
265
echo "hasChanged=true" >> $GITHUB_OUTPUT
235
- exit
236
266
fi
237
267
- name : Push new snapshots, if any
238
268
if : ${{ steps.changescheck.outputs.hasChanged == 'true' }}
239
- # Before pushing changes to origin, merge any intervening changes on the upstream branch.
269
+ env :
270
+ SAFE_BRANCH : ${{ needs.precondition.outputs.active_branch_name_sanitized }}
271
+ # Before pushing changes to origin, merge any intervening changes on the upstream branch
240
272
# This allows multiple snapshot update jobs in this action to run concurrently.
241
273
# - The only files updated locally are UI snapshots
242
274
# - Each job is responsible for a unique set of UI snapshots
@@ -249,7 +281,7 @@ jobs:
249
281
run : |
250
282
git add samples/tests/*.png
251
283
git commit -m 'Update embed html bundle snapshots'
252
- git pull origin ${{ needs.get_target_branch.outputs.target }} --no-rebase --no-edit
284
+ git pull origin "$SAFE_BRANCH" --no-rebase --no-edit
253
285
git push
254
286
255
287
call_composite :
@@ -271,9 +303,11 @@ jobs:
271
303
token : ${{ secrets.MACHINE_ACCOUNT_PAT }}
272
304
- name : Checkout branch if on a PR
273
305
if : ${{ github.event_name != 'workflow_dispatch' }}
306
+ env :
307
+ SAFE_BRANCH : ${{ needs.precondition.outputs.active_branch_name_sanitized }}
274
308
# On the `pull_request` event, actions/checkout@v4 leaves the local checkout in a detached head state.
275
309
# Explicitly checkout the target branch so we can push later.
276
- run : git checkout ${{ github.event.pull_request.head.ref }}
310
+ run : git checkout "$SAFE_BRANCH"
277
311
- name : Setup bot git information
278
312
# User id of github actions bot. See https://api.github.com/users/better-informatics%5Bbot%5D
279
313
run : |
@@ -332,7 +366,9 @@ jobs:
332
366
fi
333
367
- name : Push new snapshots, if any
334
368
if : ${{ steps.changescheck.outputs.hasChanged == 'true' }}
335
- # Before pushing changes to origin, merge any intervening changes on the upstream branch.
369
+ env :
370
+ SAFE_BRANCH : ${{ needs.precondition.outputs.active_branch_name_sanitized }}
371
+ # Before pushing changes to origin, merge any intervening changes on the upstream branch
336
372
# This allows multiple snapshot update jobs in this action to run concurrently.
337
373
# - The only files updated locally are UI snapshots
338
374
# - Each job is responsible for a unique set of UI snapshots
@@ -345,7 +381,7 @@ jobs:
345
381
run : |
346
382
git add packages/react-composites/*.png
347
383
git commit -m 'Update packages/react-composites CallComposite browser test snapshots'
348
- git pull origin ${{ needs.get_target_branch.outputs.target }} --no-rebase --no-edit
384
+ git pull origin "$SAFE_BRANCH" --no-rebase --no-edit
349
385
git push
350
386
351
387
chat_composite :
@@ -367,9 +403,11 @@ jobs:
367
403
token : ${{ secrets.MACHINE_ACCOUNT_PAT }}
368
404
- name : Checkout branch if on a PR
369
405
if : ${{ github.event_name != 'workflow_dispatch' }}
406
+ env :
407
+ SAFE_BRANCH : ${{ needs.precondition.outputs.active_branch_name_sanitized }}
370
408
# On the `pull_request` event, actions/checkout@v4 leaves the local checkout in a detached head state.
371
409
# Explicitly checkout the target branch so we can push later.
372
- run : git checkout ${{ github.event.pull_request.head.ref }}
410
+ run : git checkout "$SAFE_BRANCH"
373
411
- name : Setup bot git information
374
412
# User id of github actions bot. See https://api.github.com/users/better-informatics%5Bbot%5D
375
413
run : |
@@ -428,7 +466,9 @@ jobs:
428
466
fi
429
467
- name : Push new snapshots, if any
430
468
if : ${{ steps.changescheck.outputs.hasChanged == 'true' }}
431
- # Before pushing changes to origin, merge any intervening changes on the upstream branch.
469
+ env :
470
+ SAFE_BRANCH : ${{ needs.precondition.outputs.active_branch_name_sanitized }}
471
+ # Before pushing changes to origin, merge any intervening changes on the upstream branch
432
472
# This allows multiple snapshot update jobs in this action to run concurrently.
433
473
# - The only files updated locally are UI snapshots
434
474
# - Each job is responsible for a unique set of UI snapshots
@@ -441,7 +481,7 @@ jobs:
441
481
run : |
442
482
git add packages/react-composites/*.png
443
483
git commit -m 'Update packages/react-composites ChatComposite browser test snapshots'
444
- git pull origin ${{ needs.get_target_branch.outputs.target }} --no-rebase --no-edit
484
+ git pull origin "$SAFE_BRANCH" --no-rebase --no-edit
445
485
git push
446
486
447
487
callwithchat_composite :
@@ -463,9 +503,11 @@ jobs:
463
503
token : ${{ secrets.MACHINE_ACCOUNT_PAT }}
464
504
- name : Checkout branch if on a PR
465
505
if : ${{ github.event_name != 'workflow_dispatch' }}
506
+ env :
507
+ SAFE_BRANCH : ${{ needs.precondition.outputs.active_branch_name_sanitized }}
466
508
# On the `pull_request` event, actions/checkout@v4 leaves the local checkout in a detached head state.
467
509
# Explicitly checkout the target branch so we can push later.
468
- run : git checkout ${{ github.event.pull_request.head.ref }}
510
+ run : git checkout "$SAFE_BRANCH"
469
511
- name : Setup bot git information
470
512
# User id of github actions bot. See https://api.github.com/users/better-informatics%5Bbot%5D
471
513
run : |
@@ -524,7 +566,9 @@ jobs:
524
566
fi
525
567
- name : Push new snapshots, if any
526
568
if : ${{ steps.changescheck.outputs.hasChanged == 'true' }}
527
- # Before pushing changes to origin, merge any intervening changes on the upstream branch.
569
+ env :
570
+ SAFE_BRANCH : ${{ needs.precondition.outputs.active_branch_name_sanitized }}
571
+ # Before pushing changes to origin, merge any intervening changes on the upstream branch
528
572
# This allows multiple snapshot update jobs in this action to run concurrently.
529
573
# - The only files updated locally are UI snapshots
530
574
# - Each job is responsible for a unique set of UI snapshots
@@ -537,7 +581,7 @@ jobs:
537
581
run : |
538
582
git add packages/react-composites/*.png
539
583
git commit -m 'Update packages/react-composites CallWithChatComposite browser test snapshots'
540
- git pull origin ${{ needs.get_target_branch.outputs.target }} --no-rebase --no-edit
584
+ git pull origin "$SAFE_BRANCH" --no-rebase --no-edit
541
585
git push
542
586
543
587
components :
@@ -559,9 +603,11 @@ jobs:
559
603
token : ${{ secrets.MACHINE_ACCOUNT_PAT }}
560
604
- name : Checkout branch if on a PR
561
605
if : ${{ github.event_name != 'workflow_dispatch' }}
606
+ env :
607
+ SAFE_BRANCH : ${{ needs.precondition.outputs.active_branch_name_sanitized }}
562
608
# On the `pull_request` event, actions/checkout@v4 leaves the local checkout in a detached head state.
563
609
# Explicitly checkout the target branch so we can push later.
564
- run : git checkout ${{ github.event.pull_request.head.ref }}
610
+ run : git checkout "$SAFE_BRANCH"
565
611
- name : Setup bot git information
566
612
# User id of github actions bot. See https://api.github.com/users/better-informatics%5Bbot%5D
567
613
run : |
@@ -617,7 +663,9 @@ jobs:
617
663
fi
618
664
- name : Push new snapshots, if any
619
665
if : ${{ steps.changescheck.outputs.hasChanged == 'true' }}
620
- # Before pushing changes to origin, merge any intervening changes on the upstream branch.
666
+ env :
667
+ SAFE_BRANCH : ${{ needs.precondition.outputs.active_branch_name_sanitized }}
668
+ # Before pushing changes to origin, merge any intervening changes on the upstream branch
621
669
# This allows multiple snapshot update jobs in this action to run concurrently.
622
670
# - The only files updated locally are UI snapshots
623
671
# - Each job is responsible for a unique set of UI snapshots
@@ -630,7 +678,7 @@ jobs:
630
678
run : |
631
679
git add packages/react-components/*.png
632
680
git commit -m 'Update packages/react-components browser test snapshots'
633
- git pull origin ${{ needs.get_target_branch.outputs.target }} --no-rebase --no-edit
681
+ git pull origin "$SAFE_BRANCH" --no-rebase --no-edit
634
682
git push
635
683
636
684
push_updated_snapshots :
0 commit comments