1 # Don't skip this file until a Zsh release does the necessary quoting.
2 # This is because even though 5.8.1 undid recursive prompt_subst inside
3 # prompt sequences, % characters in relevant fields will still be rendered
4 # incorrectly in vcs_info, on all Zsh releases up to writing this.
6 # There is no release yet that does this right, since it requires changing
7 # how what vcs_info hooks expect to receive. Even so, I'd rather be correct
8 # and break custom vcs_info hooks than have a broken prompt.
10 # Quote necessary $hook_com[<field>] items just before they are used
11 # in the line "VCS_INFO_hook 'post-backend'" of the VCS_INFO_formats
12 # function, where <field> is:
14 # base: the full path of the repository's root directory.
15 # base-name: the name of the repository's root directory.
16 # branch: the name of the currently checked out branch.
17 # misc: a string that may contain anything the vcs_info backend wants.
18 # revision: an identifier of the currently checked out revision.
19 # subdir: the path of the current directory relative to the
20 # repository's root directory.
22 # This patch %-quotes these fields previous to their use in vcs_info hooks and
23 # the zformat call and, eventually, when they get expanded in the prompt.
24 # It's important to quote these here, and not later after hooks have modified the
25 # fields, because then we could be quoting % characters from valid prompt sequences,
26 # like %F{color}, %B, etc.
28 # 32 │ hook_com[subdir]="$(VCS_INFO_reposub ${hook_com[base]})"
29 # 33 │ hook_com[subdir_orig]="${hook_com[subdir]}"
31 # 35 + │ for tmp in base base-name branch misc revision subdir; do
32 # 36 + │ hook_com[$tmp]="${hook_com[$tmp]//\%/%%}"
35 # 39 │ VCS_INFO_hook 'post-backend'
37 # This is especially important so that no command substitution is performed
38 # due to malicious input as a consequence of CVE-2021-45444, which affects
39 # zsh versions from 5.0.3 to 5.8.
41 autoload -Uz +X regexp-replace VCS_INFO_formats 2>/dev/null || return
43 # We use $tmp here because it's already a local variable in VCS_INFO_formats
44 typeset PATCH='for tmp (base base-name branch misc revision subdir) hook_com[$tmp]="${hook_com[$tmp]//\%/%%}"'
45 # Unique string to avoid reapplying the patch if this code gets called twice
46 typeset PATCH_ID=vcs_info-patch-9b9840f2-91e5-4471-af84-9e9a0dc68c1b
47 # Only patch the VCS_INFO_formats function if not already patched
48 if [[ "$functions[VCS_INFO_formats]" != *$PATCH_ID* ]]; then
49 regexp-replace 'functions[VCS_INFO_formats]' \
50 "VCS_INFO_hook 'post-backend'" \
51 ': ${PATCH_ID}; ${PATCH}; ${MATCH}'