-
Notifications
You must be signed in to change notification settings - Fork 115
/
action.yml
110 lines (98 loc) · 3.87 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright 2023 SLSA Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: "Secure folder download"
description: "Download a folder uploaded by secure-upload-folder and verify its SHA256"
inputs:
name:
description: "Artifact name. (Note: this is a name given to an upload, not the path or filename)."
required: true
path:
description: "The path to download the folder into. (Must be under the GITHUB_WORKSPACE)"
required: false
default: "."
sha256:
description: "SHA256 of the file for verification."
required: true
runs:
using: "composite"
steps:
- name: Compute a random value
id: rng
uses: slsa-framework/slsa-github-generator/.github/actions/rng@v1.9.1-rc.0
- name: Download the artifact
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: "${{ inputs.name }}"
path: "${{ steps.rng.outputs.random }}"
- name: Compute the hash
id: compute
uses: slsa-framework/slsa-github-generator/.github/actions/compute-sha256@v1.9.1-rc.0
with:
path: "${{ steps.rng.outputs.random }}/folder.tgz"
- name: Verify the SHA256
env:
UNTRUSTED_EXPECTED_HASH: "${{ inputs.sha256 }}"
UNTRUSTED_COMPUTED_HASH: "${{ steps.compute.outputs.sha256 }}"
TRUSTED_FOLDER: "${{ steps.rng.outputs.random }}/folder.tgz"
shell: bash
run: |
set -euo pipefail
if ! [[ -f "${TRUSTED_FOLDER}" ]]; then
echo "File '${TRUSTED_FOLDER}' not present"
exit 5
fi
echo "expected hash is ${UNTRUSTED_EXPECTED_HASH}"
echo "computed hash is ${UNTRUSTED_COMPUTED_HASH}"
if [[ "${UNTRUSTED_COMPUTED_HASH}" != "${UNTRUSTED_EXPECTED_HASH}" ]]; then
echo "hashes do not match"
rm -rf "${TRUSTED_FOLDER}"
exit -2
fi
echo "hashes match"
- name: Extract tarball content
shell: bash
env:
TRUSTED_FOLDER: "${{ steps.rng.outputs.random }}"
UNTRUSTED_PATH: "${{ inputs.path }}"
run: |
set -euo pipefail
tempdir=$(mktemp -d)
tar -xzvf "${TRUSTED_FOLDER}/folder.tgz" --directory "${tempdir}"
rm -rf "${TRUSTED_FOLDER}"
# Ensure no directory traversal.
# Remove possible trailing '/'.
resolved_dir=$(readlink -m "${UNTRUSTED_PATH}")
wd=$(readlink -m "${GITHUB_WORKSPACE}")
if [[ "${wd}" != "" ]] && [[ "${resolved_dir}" != "${wd}"/* ]] && [[ "${resolved_dir}" != "${wd}" ]]; then
if [[ "${RUNNER_TEMP}" != "" ]] && [[ "${resolved_dir}" != "${RUNNER_TEMP}"/* ]] && [[ "${resolved_dir}" != "${RUNNER_TEMP}" ]]; then
if [[ "${resolved_dir}" != /tmp/* ]] && [[ "${resolved_dir}" != "/tmp" ]]; then
echo "Path is not in the current directory: ${UNTRUSTED_PATH}"
exit 1
fi
fi
fi
# Create the destination directory if it does not exist.
if ! [[ -e "${UNTRUSTED_PATH}" ]]; then
mkdir -p "${UNTRUSTED_PATH}"
fi
# Move all entries (file or folder).
for file in "${tempdir}"/*; do
# Ensure we don't overwrite existing files.
dst="${resolved_dir}"/$(basename "${file}")
if [ -e "${dst}" ]; then
echo "Path '${dst}' already exists"
exit 5
fi
mv "${file}" "${dst}"
done