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

Highlight non-matching globs as errors or normal #599

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion highlighters/main/main-highlighter.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ _zsh_highlight_main_highlighter_check_path()
_zsh_highlight_main_highlighter_highlight_argument()
{
local base_style=default i=$1 option_eligible=${2:-1} path_eligible=1 ret start style
local -a highlights
local -a globs highlights

local -a match mbegin mend
local MATCH; integer MBEGIN MEND
Expand Down Expand Up @@ -1087,6 +1087,7 @@ _zsh_highlight_main_highlighter_highlight_argument()
highlights+=($(( start_pos + i - 1 )) $(( start_pos + i + $#MATCH - 1)) globbing)
(( i += $#MATCH - 1 ))
path_eligible=0
globs+=($#highlights)
else
continue
fi
Expand All @@ -1098,6 +1099,20 @@ _zsh_highlight_main_highlighter_highlight_argument()
base_style=$REPLY
_zsh_highlight_main_highlighter_highlight_path_separators $base_style
highlights+=($reply)
elif (( $#globs )); then
local glob
local -a files; files=($~arg(NY1))
Copy link
Member

Choose a reason for hiding this comment

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

(Y1) is new in 5.x. In versions that don't have it, any ** in there could be very expensive to compute (and of course it'll be recomputed every self-insert).

$~foo performs filename generation which can eval arbitrary code via *(+foo). We specifically avoid it in _zsh_highlight_main_highlighter_expand_path.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep. The globs we match are all safe, but we don't parse any of the unsafe globs. This feature can't happen until all unsafe globs are recognized. Sorry, must have been tired.

Copy link
Member

Choose a reason for hiding this comment

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

Not sure I follow. We shouldn't blacklist dangerous globs, but whitelist known-good ones.

Copy link
Member Author

Choose a reason for hiding this comment

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

While I agree whitelisting is better, we would first have to recognize all globs in order to only allow whitelisted ones. Or we could whitelist safe globs and non-globbing characters and assume anything else is dangerous.

if ! [[ -e $files[1] ]]; then
Copy link
Member

@danielshahaf danielshahaf Jan 7, 2019

Choose a reason for hiding this comment

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

Minor issue, but we could avoid the stat by simply checking $#files (it'll 0 or 1 so no need for the faster $+files[1]).

if [[ $zsyh_user_options[nomatch] == on ]]; then
for glob in $globs; do
highlights[glob]=unknown-token
done
else
for glob in $globs; do
unset "highlights[$glob - 2]" "highlights[glob - 1]" "highlights[glob]"
done
fi
fi
fi

highlights=($(( start_pos + $1 - 1 )) $end_pos $base_style $highlights)
Expand Down