Skip to content

Commit ab6b5b1

Browse files
committed
Added less broken, more secure test environment variable handling for python-uv-ci.yml
1 parent 30cac0b commit ab6b5b1

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

.github/workflows/python-uv-ci.yml

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,46 @@ jobs:
161161
- name: Test
162162
if: ${{ inputs.run-tests }}
163163
run: |
164-
# Parse and export environment variables from JSON
165-
# Handle empty or null TEST_SECRETS
166-
if [ -z "$TEST_SECRETS" ] || [ "$TEST_SECRETS" = "null" ]; then
167-
TEST_SECRETS='{}'
168-
fi
164+
# Function to safely process and export environment variables from JSON
165+
process_env_vars() {
166+
local json_data="$1"
167+
local source_name="$2"
168+
local mask_secrets="$3"
169+
170+
if [ -n "$json_data" ] && [ "$json_data" != "null" ] && [ "$json_data" != "{}" ]; then
171+
# Validate JSON first to prevent jq injection
172+
if ! echo "$json_data" | jq -e . >/dev/null 2>&1; then
173+
echo "Error: Invalid JSON in $source_name"
174+
exit 1
175+
fi
176+
177+
# Use process substitution to avoid subshell export issues
178+
local entry_num=0
179+
while IFS= read -r key && IFS= read -r value; do
180+
entry_num=$((entry_num + 1))
181+
182+
# Validate key contains only safe characters
183+
if [[ "$key" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
184+
# Mask secrets in GitHub Actions logs
185+
if [ "$mask_secrets" = "true" ]; then
186+
echo "::add-mask::$value"
187+
fi
188+
export "$key"="$value"
189+
else
190+
echo "Warning: Skipping invalid environment variable name at entry $entry_num in $source_name"
191+
fi
192+
done < <(echo "$json_data" | jq -r 'to_entries[] | select(.value != null) | .key, .value') || {
193+
echo "Error: Failed to process JSON in $source_name"
194+
exit 1
195+
}
196+
fi
197+
}
169198
170-
# Use jq to merge JSON objects, with TEST_SECRETS taking precedence
171-
echo "$TEST_ENV_VARS $TEST_SECRETS" | jq -s '.[0] * .[1]' | jq -r 'to_entries[] | "export \(.key)=\(.value)"' | while IFS= read -r line; do
172-
eval "$line"
173-
done
199+
# Process test environment variables first
200+
process_env_vars "$TEST_ENV_VARS" "test-environment-variables" "false"
201+
202+
# Process test secrets (with precedence over env vars, and mask them in logs)
203+
process_env_vars "$TEST_SECRETS" "TEST_SECRETS" "true"
174204
175205
# Run the test script
176206
${{ inputs.test-script }}

0 commit comments

Comments
 (0)