-
Notifications
You must be signed in to change notification settings - Fork 115
/
action.yml
121 lines (110 loc) · 4.53 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
111
112
113
114
115
116
117
118
119
120
121
# 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 artifact download"
description: "Download an artifact 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 of the artifact. (Note: should be the same as the `path` argument passed to secure-upload-artifact or actions/upload-artifact GHA)."
required: true
sha256:
description: "SHA256 of the file for verification."
required: true
set-executable:
description: "Set the artifact as executable."
required: false
runs:
using: "composite"
steps:
- name: Validate the artifact path
id: validate-path
shell: bash
env:
UNTRUSTED_PATH: "${{ inputs.path }}"
run: |
set -euo pipefail
# Ensure no directory traversal.
# NOTE: the actions/download-artifact Action only creates files
# in the workspace directory, but this may change in the future.
# TODO(#1893): Consolidate directory traversal checks
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 workspace or temp directory: $UNTRUSTED_PATH"
exit 1
fi
fi
fi
# Check that the name of the path does not clash with the
# directory where the builder code is stored.
results=$(
echo "$UNTRUSTED_PATH" | grep __BUILDER_CHECKOUT_DIR__ || true
)
if [[ "$results" != "" ]]; then
echo "\"$UNTRUSTED_PATH\" contains '__BUILDER_CHECKOUT_DIR__'"
echo "$results"
exit 1
fi
# Ensure we don't overwrite existing files.
if [ -e "$UNTRUSTED_PATH" ]; then
echo "Path $UNTRUSTED_PATH already exists"
exit 5
fi
# path has been validated. Explicitly trust it and set as an output.
file_path="${UNTRUSTED_PATH}"
folder_path="$(dirname ${UNTRUSTED_PATH})"
echo "file_path=${file_path}" >> "${GITHUB_OUTPUT}"
echo "folder_path=${folder_path}" >> "${GITHUB_OUTPUT}"
- name: Download the artifact
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: "${{ inputs.name }}"
path: "${{ steps.validate-path.outputs.folder_path }}"
- name: Compute the hash
id: compute
uses: slsa-framework/slsa-github-generator/.github/actions/compute-sha256@v1.9.1-rc.0
with:
path: "${{ steps.validate-path.outputs.file_path }}"
# Note: this assumes to top-level re-usable workflow
# has checkout'ed the builder repository using
# `.github/actions/secure-builder-checkout`.
- name: Verify the SHA256
env:
UNTRUSTED_EXPECTED_HASH: "${{ inputs.sha256 }}"
UNTRUSTED_COMPUTED_HASH: "${{ steps.compute.outputs.sha256 }}"
TRUSTED_PATH: "${{ steps.validate-path.outputs.file_path }}"
SET_EXECUTABLE: "${{ inputs.set-executable }}"
shell: bash
run: |
set -euo pipefail
if ! [[ -f "$TRUSTED_PATH" ]]; then
echo "File $TRUSTED_PATH 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_PATH}"
exit -2
fi
echo "hashes match"
if [[ "$SET_EXECUTABLE" == "true" ]]; then
echo "Setting $TRUSTED_PATH as executable"
chmod u+x "$TRUSTED_PATH"
fi