bin/brew: improve sudo handling

- prioritise `/usr/bin/sudo` instead of any random `sudo` in `PATH`
This commit is contained in:
Carlo Cabrera 2024-08-25 17:59:35 +08:00
parent 36a0e539e4
commit cd4f6eb2c0
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0

View File

@ -36,12 +36,6 @@ then
exit 1 exit 1
fi fi
# Reset sudo timestamp to avoid running unauthorized sudo commands
if command -v sudo >/dev/null
then
sudo --reset-timestamp 2>/dev/null || true
fi
quiet_cd() { quiet_cd() {
CDPATH='' cd -- "$@" &>/dev/null || return CDPATH='' cd -- "$@" &>/dev/null || return
} }
@ -63,6 +57,22 @@ do
done done
unset cmd unset cmd
# Avoid picking up any random `sudo` in `PATH`.
if [[ -x /usr/bin/sudo ]]
then
SUDO=/usr/bin/sudo
else
# Do this after ensuring we're using default Bash builtins.
SUDO="$(command -v sudo 2>/dev/null)"
fi
# Reset sudo timestamp to avoid running unauthorized sudo commands
if [[ -n "${SUDO}" ]]
then
"${SUDO}" --reset-timestamp 2>/dev/null || true
fi
unset SUDO
# Take the HOMEBREW_PATH if we are running brew within brew, otherwise we would lose the original path. # Take the HOMEBREW_PATH if we are running brew within brew, otherwise we would lose the original path.
if [[ -n "${HOMEBREW_BREW_FILE:-}" && -n "${HOMEBREW_PATH:-}" ]] if [[ -n "${HOMEBREW_BREW_FILE:-}" && -n "${HOMEBREW_PATH:-}" ]]
then then