vcs_info.zsh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Impacted versions go from v5.0.3 to v5.8 (v5.8.1 is the first patched version)
  2. autoload -Uz is-at-least
  3. if is-at-least 5.8.1 || ! is-at-least 5.0.3; then
  4. return
  5. fi
  6. # Quote necessary $hook_com[<field>] items just before they are used
  7. # in the line "VCS_INFO_hook 'post-backend'" of the VCS_INFO_formats
  8. # function, where <field> is:
  9. #
  10. # base: the full path of the repository's root directory.
  11. # base-name: the name of the repository's root directory.
  12. # branch: the name of the currently checked out branch.
  13. # misc: a string that may contain anything the vcs_info backend wants.
  14. # revision: an identifier of the currently checked out revision.
  15. # subdir: the path of the current directory relative to the
  16. # repository's root directory.
  17. #
  18. # This patch %-quotes these fields previous to their use in vcs_info hooks and
  19. # the zformat call and, eventually, when they get expanded in the prompt.
  20. # It's important to quote these here, and not later after hooks have modified the
  21. # fields, because then we could be quoting % characters from valid prompt sequences,
  22. # like %F{color}, %B, etc.
  23. #
  24. # 32 │ hook_com[subdir]="$(VCS_INFO_reposub ${hook_com[base]})"
  25. # 33 │ hook_com[subdir_orig]="${hook_com[subdir]}"
  26. # 34 │
  27. # 35 + │ for tmp in base base-name branch misc revision subdir; do
  28. # 36 + │ hook_com[$tmp]="${hook_com[$tmp]//\%/%%}"
  29. # 37 + │ done
  30. # 38 + │
  31. # 39 │ VCS_INFO_hook 'post-backend'
  32. #
  33. # This is especially important so that no command substitution is performed
  34. # due to malicious input as a consequence of CVE-2021-45444, which affects
  35. # zsh versions from 5.0.3 to 5.8.
  36. #
  37. autoload -Uz +X regexp-replace VCS_INFO_formats
  38. # We use $tmp here because it's already a local variable in VCS_INFO_formats
  39. typeset PATCH='for tmp (base base-name branch misc revision subdir) hook_com[$tmp]="${hook_com[$tmp]//\%/%%}"'
  40. # Unique string to avoid reapplying the patch if this code gets called twice
  41. typeset PATCH_ID=vcs_info-patch-9b9840f2-91e5-4471-af84-9e9a0dc68c1b
  42. # Only patch the VCS_INFO_formats function if not already patched
  43. if [[ "$functions[VCS_INFO_formats]" != *$PATCH_ID* ]]; then
  44. regexp-replace 'functions[VCS_INFO_formats]' \
  45. "VCS_INFO_hook 'post-backend'" \
  46. ': ${PATCH_ID}; ${PATCH}; ${MATCH}'
  47. fi
  48. unset PATCH PATCH_ID