refactor: split common URL logic into download_with_args

This commit is contained in:
amyavi 2024-12-20 16:00:38 -03:00
parent 00956436dc
commit d73ce8a1f6
Signed by: amy
SSH key fingerprint: SHA256:CoLIqZWDYPZEhs1j1HQWwV0j1JhC3BFWcaUF7ZLZHJA
5 changed files with 12 additions and 10 deletions

View file

@ -41,12 +41,11 @@ check_path() {
case "$1" in
"/"*) echo "Attempted path traversal: $1 is absolute"
return 1;;
*);; # Safe
esac
case "$rpath" in
"$_EXEC_PATH/"*);; # Safe
*) echo "Attempted path traversal: $1 is outside current directory"
return 1;;
esac

View file

@ -42,12 +42,20 @@ require_args() {
tmp=""
done
}
download_with_args() {
require_args url
download "${arg_url:?}" "$1"
}
download_type() {
# Calling the function with _download_type_"$1" opens up the
# possibility for users to run arbitrary commands, so we must
# manually handle the type.
#
# Since the args are part of the function's stdin, they will be
# be propagated into the _download_type_... functions.
case "$1" in
"url") _download_type_url "$2";;
"zip") _download_type_zip "$2";;

View file

@ -2,7 +2,5 @@
_download_type_url() {
read_args url
require_args url
download "${arg_url:?}" "$1"
download_with_args "$@"
}

View file

@ -6,7 +6,7 @@ _download_type_zip() {
zip_path="$(mktemp --suffix=.zip)"
exitcode=0
download "${arg_url:?}" "$zip_path" || exitcode=$?
download_with_args "$zip_path" || exitcode=$?
if [ $exitcode != 0 ]; then
rm -f "$zip_path" 2>/dev/null
return $exitcode

View file

@ -52,8 +52,5 @@ _parse_downloads | while read -r path; read -r type; read -r args; do
fi
debug "download_type: type=$type; args=$args"
# Since the args are part of the function's stdin, they will be
# be propagated up.
echo "$args" | download_type "$type" "$path"
done