Skip to content

Commit 8e639df

Browse files
alanfallooncopybara-github
authored andcommitted
Don't follow symlinks when deleting test outputs
Update tools/test/test-setup.sh to preserve symlinks when performing the zip of `$TEST_UNDECLARED_OUTPUTS_DIR`. This fixes a serious bug where an absolute symlink generated in the test could delete files anywhere on the filesystem. For example, a `sh_test` containing a line like: ln -s "$HOME" "$TEST_UNDECLARED_OUTPUTS_DIR/home" would have caused the users home directory to be deleted after copying it in to the output.zip. With this change, the output.zip only contains a (possibly dangling) symlink, but more importantly the deletions are limited to the `$TEST_UNDECLARED_OUTPUTS_DIR`. RELNOTES: `--zip_undeclared_test_outputs` now preserves symlinks when zipping `$TEST_UNDECLARED_OUTPUTS_DIR`. Closes #19948. Change-Id: Ia4a8a9699e4e2f40498342af55babc5554a9ac93 PiperOrigin-RevId: 587696908
1 parent a03ec7f commit 8e639df

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

tools/test/test-setup.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,16 @@ fi
421421

422422
# Zip up undeclared outputs.
423423
if [[ -n "$TEST_UNDECLARED_OUTPUTS_ZIP" ]] && cd "$TEST_UNDECLARED_OUTPUTS_DIR"; then
424-
shopt -s dotglob
425-
if [[ "$(echo *)" != "*" ]]; then
426-
# If * found nothing, echo printed the literal *.
427-
# Otherwise echo printed the top-level files and directories.
428-
# Pass files to zip with *, so paths with spaces aren't broken up.
429-
# Remove original files after zipping them.
430-
if ! zip_output="$(zip -qrm "$TEST_UNDECLARED_OUTPUTS_ZIP" -- * 2>&1)"; then
424+
shopt -s dotglob nullglob
425+
# Capture the contents of TEST_UNDECLARED_OUTPUTS_DIR prior to creating the output.zip
426+
UNDECLARED_OUTPUTS=(*)
427+
if [[ "${#UNDECLARED_OUTPUTS[@]}" != 0 ]]; then
428+
if ! zip_output="$(zip -qr "$TEST_UNDECLARED_OUTPUTS_ZIP" -- "${UNDECLARED_OUTPUTS[@]}")" ; then
431429
echo >&2 "Could not create \"$TEST_UNDECLARED_OUTPUTS_ZIP\": $zip_output"
432430
fi
431+
# Use 'rm' instead of 'zip -m' so that we don't follow symlinks when deleting the
432+
# contents.
433+
rm -r "${UNDECLARED_OUTPUTS[@]}"
433434
fi
434435
fi
435436

0 commit comments

Comments
 (0)