Make detection of command line flags that prevent sudo from overriding TERMINFO more robust

This commit is contained in:
Kovid Goyal
2026-05-05 09:38:07 +05:30
parent 4ab166c9ac
commit fd7e43ab0c
4 changed files with 164 additions and 32 deletions

View File

@@ -18,23 +18,33 @@ if [[ -n "$KITTY_BASH_INJECT" ]]; then
if [[ -n "$ksi_val" && "$ksi_val" != *no-sudo* && -n "$TERMINFO" && ! ( -r "/usr/share/terminfo/x/xterm-kitty" || -r "/usr/share/terminfo/78/xterm-kitty" ) ]]; then
# this must be done before sourcing user bashrc otherwise aliasing of sudo does not work
sudo() {
# Ensure terminfo is available in sudo
builtin local is_sudoedit="n"
for arg; do
if [[ "$arg" == "-e" || $arg == "--edit" ]]; then
is_sudoedit="y"
builtin break;
sudo() {
# Ensure terminfo is available in sudo
builtin local cannot_set_env_var="n"
builtin local ignore_arg="n"
for arg; do
if [[ "$ignore_arg" == "y" ]]; then ignore_arg="n"; builtin continue; fi
case "$arg" in
--) builtin break ;; # end of options
# sudo -e disallows setting env vars
--edit | -e* | -[!-]*e*) cannot_set_env_var="y"; builtin break ;;
# sudo-rs -v disallows setting env vars
--validate | -v* | -[!-]*v*) cannot_set_env_var="y"; builtin break ;;
# flags that require a following argument
--user|--group|--host|--chdir|--chroot|--role|--type|--command-timeout|--auth-type|--login-class|--prompt|--close-from|--other-user) ignore_arg="y" ;;
--*) ;; # misc long opt
-*a|-*C|-*c|-*D|-*g|-*h|-*p|-*R|-*r|-*t|-*T|-*u) ignore_arg="y" ;;
# flag or env var setting
-* | *=*) ;;
*) builtin break ;; # command found
esac
done
if [[ "$cannot_set_env_var" == "y" ]]; then
builtin command sudo "$@";
else
builtin command sudo TERMINFO="$TERMINFO" "$@";
fi
[[ "$arg" == "--" ]] && builtin break # end of options
[[ "$arg" != -* && "$arg" != *=* ]] && builtin break # command found
done
if [[ "$is_sudoedit" == "y" ]]; then
builtin command sudo "$@";
else
builtin command sudo TERMINFO="$TERMINFO" "$@";
fi
}
}
fi
if [[ "$kitty_bash_inject" == *"posix"* ]]; then

View File

@@ -144,20 +144,42 @@ function __ksi_schedule --on-event fish_prompt -d "Setup kitty integration after
and not test -r "/usr/share/terminfo/x/xterm-kitty" -o -r "/usr/share/terminfo/78/xterm-kitty"
# Ensure terminfo is available in sudo
function sudo
set --local is_sudoedit "n"
set --local cannot_set_env_var "n"
set --local ignore_arg "n"
for arg in $argv
if string match -q -- "-e" "$arg" or string match -q -- "--edit" "$arg"
set is_sudoedit "y"
break
if string match -q -- "$ignore_arg" "y"
set ignore_arg "n"
continue
end
if string match -q -- "--" "$arg"
if string match -r -q -- '^--$' "$arg"
break # end of options
end
if not string match -r -q -- "^-" "$arg" and not string match -r -q -- "=" "$arg"
break # reached the command
if string match -r -q -- '^-[^-]*e' "$arg" or string match -q -- "--edit" "$arg"
set cannot_set_env_var "y" # sudo -e
break
end
if string match -r -q -- '^-[^-]*v' "$arg" or string match -q -- "--validate" "$arg"
set cannot_set_env_var "y" # sudo-rs -e
break
end
if string match -r -q -- '--user|--group|--host|--chdir|--chroot|--role|--type|--command-timeout|--auth-type|--login-class|--prompt|--close-from|--other-user' "$arg"
set ignore_arg="y"
continue
end
if string match -r -q -- '--.+' "$arg"
continue
end
if string match -r -q -- '-.*a|-.*C|-.*c|-.*D|-.*g|-.*h|-.*p|-.*R|-.*r|-.*t|-.*T|-.*u)' "$arg"
set ignore_arg="y"
continue
end
if string match -r -q -- '-.+' "$arg" or string match -r -q -- '.+=.+' "$arg"
continue
end
break # command found
end
if string match -q -- "$is_sudoedit" "y"
if string match -q -- "$cannot_set_env_var" "y"
command sudo $argv
else
command sudo TERMINFO="$TERMINFO" $argv

View File

@@ -406,16 +406,26 @@ _ksi_deferred_init() {
if [[ -n "$TERMINFO" && ! ( -r "/usr/share/terminfo/x/xterm-kitty" || -r "/usr/share/terminfo/78/xterm-kitty" ) ]]; then
sudo() {
# Ensure terminfo is available in sudo
builtin local is_sudoedit="n"
builtin local cannot_set_env_var="n"
builtin local ignore_arg="n"
for arg; do
if [[ "$arg" == "-e" || $arg == "--edit" ]]; then
is_sudoedit="y"
builtin break;
fi
[[ "$arg" == "--" ]] && builtin break # end of options
[[ "$arg" != -* && "$arg" != *=* ]] && builtin break # command found
if [[ "$ignore_arg" == "y" ]]; then ignore_arg="n"; builtin continue; fi
case "$arg" in
--) builtin break ;; # end of options
# sudo -e disallows setting env vars
--edit | -e* | -[!-]*e*) cannot_set_env_var="y"; builtin break ;;
# sudo-rs -v disallows setting env vars
--validate | -v* | -[!-]*v*) cannot_set_env_var="y"; builtin break ;;
# flags that require a following argument
--user|--group|--host|--chdir|--chroot|--role|--type|--command-timeout|--auth-type|--login-class|--prompt|--close-from|--other-user) ignore_arg="y" ;;
--*) ;; # misc long opt
-*a|-*C|-*c|-*D|-*g|-*h|-*p|-*R|-*r|-*t|-*T|-*u) ignore_arg="y" ;;
# flag or env var setting
-* | *=*) ;;
*) builtin break ;; # command found
esac
done
if [[ "$is_sudoedit" == "y" ]]; then
if [[ "$cannot_set_env_var" == "y" ]]; then
builtin command sudo "$@";
else
builtin command sudo TERMINFO="$TERMINFO" "$@";