Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --follow_symlink to ynh_secure_remove #1716

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Add --follow_symlink to ynh_secure_remove. Fixes #2253
  • Loading branch information
Salamandar committed Sep 28, 2023
commit 0e21f4e6524100cfd76a380e909c9f86523ad558
27 changes: 18 additions & 9 deletions helpers/utils
Original file line number Diff line number Diff line change
Expand Up @@ -854,34 +854,43 @@ _acceptable_path_to_delete() {
# Remove a file or a directory securely
#
# usage: ynh_secure_remove --file=path_to_remove
# | arg: -f, --file= - File or directory to remove
# | arg: -f, --file= - File or directory to remove
# | arg: -l, --follow-symlink= - If file is a symlink, delete its target instead. Is **not** recursive.
#
# Requires YunoHost version 2.6.4 or higher.
ynh_secure_remove() {
# Declare an array to define the options of this helper.
local legacy_args=f
local -A args_array=([f]=file=)
local -A args_array=([f]=file= [l]=follow_symlink)
local file
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
set +o xtrace # set +x
follow_symlink="${follow_symlink:-0}"

# Let's hope no one deletes files with ; in their name...
if [[ "$file" == *";"* ]]; then
ynh_print_err --message="/!\ Packager ! You provided more than one file to ynh_secure_remove but it will be ignored... Use this helper with one argument at time."
file=${file%;*}
fi

if [[ -z "$file" ]]; then
ynh_print_warn --message="ynh_secure_remove called with empty argument, ignoring."
elif [[ ! -e $file ]]; then
return
fi
if [[ "$follow_symlink" -eq 1 ]]; then
file=$(readlink -f "$file")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we don't remove the link?

# file is now NOT a symlink.
fi
# This tests for file existence and, if not, symlink existence.
if [[ ! -e "$file" ]] && [[ ! -h "$file" ]]; then
ynh_print_info --message="'$file' wasn't deleted because it doesn't exist."
elif ! _acceptable_path_to_delete "$file"; then
return
fi
if ! _acceptable_path_to_delete "$file"; then
ynh_print_warn --message="Not deleting '$file' because it is not an acceptable path to delete."
else
rm --recursive "$file"
return
fi

set -o xtrace # set -x
rm --recursive "$file"
}

# Read the value of a key in a ynh manifest file
Expand Down