Skip to content

Commit e64e71e

Browse files
chore(workflow): enhance artifact security with validation and sanitization (#5343)
## 📄 Description Add comprehensive validation and sanitization for artifact upload/download actions: - Validate folder existence and input formats - Sanitize event IDs and actions before output - Use temporary directories for safer operations - Improve error handling with clear messages ## 🚀 Demo If applicable, please add a screenshot or video to illustrate the changes. --- ## 📝 Checklist - ✅ My code follows the style guidelines of this project - 🛠️ I have performed a self-review of my own code - 📄 I have made corresponding changes to the documentation - ⚠️ My changes generate no new warnings or errors - 🧪 I have added tests that prove my fix is effective or that my feature works - ✔️ New and existing unit tests pass locally with my changes
1 parent c1a5ec5 commit e64e71e

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

.github/actions/artifact-download/action.yaml

+39-4
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,59 @@ outputs:
3131
runs:
3232
using: composite
3333
steps:
34+
- name: Create temporary directory
35+
shell: bash
36+
run: mkdir -p ${{ runner.temp }}/artifact_download
37+
3438
- name: Download artifact
3539
uses: dawidd6/action-download-artifact@07ab29fd4a977ae4d2b275087cf67563dfdf0295
3640
with:
3741
name: ${{ inputs.name }}
3842
run_id: ${{ github.event.workflow_run.id }}
3943
workflow_conclusion: success
44+
path: ${{ runner.temp }}/artifact_download
4045

46+
- name: Ensure target directory exists
47+
shell: bash
48+
run: mkdir -p ${{ inputs.folder }}
49+
4150
- name: Unzip artifacts
4251
shell: bash
43-
run: unzip artifacts.zip -d ${{ inputs.folder }}
52+
run: unzip ${{ runner.temp }}/artifact_download/artifacts.zip -d ${{ inputs.folder }}
53+
54+
- name: Validate artifact contents
55+
shell: bash
56+
run: |
57+
if [[ ! -f "${{ inputs.folder }}/GHA-EVENT-ID" ]]; then
58+
echo "Error: Event ID file missing"
59+
exit 1
60+
fi
61+
62+
if [[ ! -f "${{ inputs.folder }}/GHA-EVENT-ACTION" ]]; then
63+
echo "Error: Event Action file missing"
64+
exit 1
65+
fi
66+
67+
# Validate ID is numeric
68+
EVENT_ID=$(cat ${{ inputs.folder }}/GHA-EVENT-ID)
69+
if ! [[ "$EVENT_ID" =~ ^[0-9]*$ ]]; then
70+
echo "Error: Invalid Event ID format"
71+
exit 1
72+
fi
4473
4574
- name: Clean up
4675
shell: bash
47-
run: rm -r artifacts.zip
76+
run: rm -rf ${{ runner.temp }}/artifact_download
4877

4978
- name: Create outputs
5079
id: build
5180
shell: bash
5281
run: |
53-
echo "id=$(cat ${{ inputs.folder }}/GHA-EVENT-ID)" >> $GITHUB_OUTPUT
54-
echo "action=$(cat ${{ inputs.folder }}/GHA-EVENT-ACTION)" >> $GITHUB_OUTPUT
82+
EVENT_ID=$(cat ${{ inputs.folder }}/GHA-EVENT-ID)
83+
EVENT_ACTION=$(cat ${{ inputs.folder }}/GHA-EVENT-ACTION)
84+
85+
SANITIZED_ID=$(echo "$EVENT_ID" | tr -cd '[:digit:]')
86+
SANITIZED_ACTION=$(echo "$EVENT_ACTION" | tr -cd '[:alnum:]-_')
87+
88+
echo "id=$SANITIZED_ID" >> $GITHUB_OUTPUT
89+
echo "action=$SANITIZED_ACTION" >> $GITHUB_OUTPUT

.github/actions/artifact-upload/action.yaml

+18-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,27 @@ inputs:
2020
runs:
2121
using: composite
2222
steps:
23+
- name: Validate folder exists
24+
shell: bash
25+
run: |
26+
if [[ ! -d "${{ inputs.folder }}" ]]; then
27+
echo "Error: Folder ${{ inputs.folder }} does not exist"
28+
exit 1
29+
fi
30+
2331
- name: Save Event Infos into the artifact folder
2432
shell: bash
2533
run: |
26-
echo ${{ github.event.number }} > ${{ inputs.folder }}/GHA-EVENT-ID
27-
echo ${{ github.event.action }} > ${{ inputs.folder }}/GHA-EVENT-ACTION
34+
if ! [[ "${{ github.event.number }}" =~ ^[0-9]*$ ]]; then
35+
echo "Warning: Invalid event number format, using default"
36+
echo "0" > ${{ inputs.folder }}/GHA-EVENT-ID
37+
else
38+
echo "${{ github.event.number }}" > ${{ inputs.folder }}/GHA-EVENT-ID
39+
fi
40+
41+
ACTION="${{ github.event.action }}"
42+
SANITIZED_ACTION=$(echo "$ACTION" | tr -cd '[:alnum:]-_')
43+
echo "$SANITIZED_ACTION" > ${{ inputs.folder }}/GHA-EVENT-ACTION
2844
2945
- name: Zip artifact folder
3046
shell: bash

0 commit comments

Comments
 (0)