Skip to content

Commit b78dbcf

Browse files
committed
Fix set-export glob expansion without eval
1 parent ffe0388 commit b78dbcf

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

.github/scripts/set-export

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,47 @@
22

33
VAR_NAME="$1"
44
ARTIFACT_PATH="$2"
5-
REAL_PATH=$(printf '%s' "$ARTIFACT_PATH")
65

7-
if [ -d "$REAL_PATH" ]; then
6+
expand_env_vars() {
7+
local input="$1"
8+
local output=""
9+
local prefix suffix var_name
10+
11+
while [[ "$input" == *'$'* ]]; do
12+
prefix="${input%%\$*}"
13+
suffix="${input#*\$}"
14+
output+="$prefix"
15+
16+
if [[ "$suffix" =~ ^\{([A-Za-z_][A-Za-z0-9_]*)\}(.*)$ ]]; then
17+
var_name="${BASH_REMATCH[1]}"
18+
output+="${!var_name-}"
19+
input="${BASH_REMATCH[2]}"
20+
elif [[ "$suffix" =~ ^([A-Za-z_][A-Za-z0-9_]*)(.*)$ ]]; then
21+
var_name="${BASH_REMATCH[1]}"
22+
output+="${!var_name-}"
23+
input="${BASH_REMATCH[2]}"
24+
else
25+
output+='$'
26+
input="$suffix"
27+
fi
28+
done
29+
30+
printf '%s' "$output$input"
31+
}
32+
33+
ARTIFACT_PATH=$(expand_env_vars "$ARTIFACT_PATH")
34+
35+
shopt -s nullglob
36+
OLD_IFS="$IFS"
37+
IFS=
38+
# Intentionally unquoted: expand artifact path globs after variable substitution.
39+
# shellcheck disable=SC2206
40+
MATCHES=( $ARTIFACT_PATH )
41+
IFS="$OLD_IFS"
42+
shopt -u nullglob
43+
44+
if [ "${#MATCHES[@]}" -eq 1 ] && [ -d "${MATCHES[0]}" ]; then
45+
REAL_PATH="${MATCHES[0]}"
846
export "$VAR_NAME"="$REAL_PATH"
947
echo "$VAR_NAME"="$REAL_PATH" >> "$GITHUB_ENV"
1048
fi

0 commit comments

Comments
 (0)