Skip to content

Commit cd8a3ea

Browse files
authored
runtime(dist): centralize safe executable check and add vim library (#13413)
Follow up to 816fbcc (patch 9.0.1833: [security] runtime file fixes, 2023-08-31) and f7ac0ef (runtime: don't execute external commands when loading ftplugins, 2023-09-06). This puts the logic for safe executable checks in a single place, by introducing a central vim library, so all filetypes benefit from consistency. Notable changes: - dist#vim because the (autoload) namespace for a new runtime support library. Supporting functions should get documentation. It might make life easier for NeoVim devs to make the documentation a new file rather than cram it into existing files, though we may want cross-references to it somewhere… - The gzip and zip plugins need to be opted into by enabling execution of those programs (or the global plugin_exec). This needs documentation or discussion. - This fixes a bug in the zig plugin: code setting s:tmp_cwd was removed in f7ac0ef (runtime: don't execute external commands when loading ftplugins, 2023-09-06), but the variable was still referenced. Since the new function takes care of that automatically, the variable is no longer needed. Signed-off-by: D. Ben Knoble <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 08b1c61 commit cd8a3ea

7 files changed

Lines changed: 25 additions & 23 deletions

File tree

runtime/autoload/dist/vim.vim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
vim9script
2+
3+
# Vim runtime support library
4+
#
5+
# Maintainer: The Vim Project <https://github.com/vim/vim>
6+
# Last Change: 2023 Oct 25
7+
8+
export def IsSafeExecutable(filetype: string, executable: string): bool
9+
var cwd = getcwd()
10+
return get(g:, filetype .. '_exec', get(g:, 'plugin_exec', 0))
11+
&& (fnamemodify(exepath(executable), ':p:h') !=# cwd
12+
|| (split($PATH, has('win32') ? ';' : ':')->index(cwd) != -1
13+
&& cwd != '.'))
14+
enddef
15+
16+
# Uncomment this line to check for compilation errors early
17+
# defcompile

runtime/autoload/gzip.vim

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ fun s:check(cmd)
1111
let name = substitute(a:cmd, '\(\S*\).*', '\1', '')
1212
if !exists("s:have_" . name)
1313
" safety check, don't execute anything from the current directory
14-
let s:tmp_cwd = getcwd()
15-
let f = (fnamemodify(exepath(name), ":p:h") !=# s:tmp_cwd
16-
\ || (index(split($PATH,has("win32")? ';' : ':'), s:tmp_cwd) != -1 && s:tmp_cwd != '.'))
17-
unlet s:tmp_cwd
14+
let f = dist#vim#IsSafeExecutable('gzip', name)
1815
if !f
1916
echoerr "Warning: NOT executing " .. name .. " from current directory!"
2017
endif

runtime/autoload/zip.vim

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,10 @@ if !exists("g:zip_extractcmd")
5757
let g:zip_extractcmd= g:zip_unzipcmd
5858
endif
5959

60-
let s:tmp_cwd = getcwd()
61-
if (fnamemodify(exepath(g:zip_unzipcmd), ":p:h") ==# getcwd()
62-
\ && (index(split($PATH,has("win32")? ';' : ':'), s:tmp_cwd) == -1 || s:tmp_cwd == '.'))
63-
unlet s:tmp_cwd
60+
if !dist#vim#IsSafeExecutable('zip', g:zip_unzipcmd)
6461
echoerr "Warning: NOT executing " .. g:zip_unzipcmd .. " from current directory!"
6562
finish
6663
endif
67-
unlet s:tmp_cwd
6864

6965
" ----------------
7066
" Functions: {{{1

runtime/ftplugin/awk.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ if exists("g:awk_is_gawk")
3737
let b:undo_ftplugin .= " | setl fp<"
3838
endif
3939

40-
" Disabled by default for security reasons.
41-
if get(g:, 'awk_exec', get(g:, 'plugin_exec', 0))
40+
" Disabled by default for security reasons.
41+
if dist#vim#IsSafeExecutable('awk', 'gawk')
4242
let path = system("gawk 'BEGIN { printf ENVIRON[\"AWKPATH\"] }'")
4343
let path = substitute(path, '^\.\=:\|:\.\=$\|:\.\=:', ',,', 'g') " POSIX cwd
4444
let path = substitute(path, ':', ',', 'g')

runtime/ftplugin/changelog.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ if &filetype == 'changelog'
5757
endif
5858
let s:default_login = 'unknown'
5959

60-
" Disabled by default for security reasons.
61-
if get(g:, 'changelog_exec', get(g:, 'plugin_exec', 0))
60+
" Disabled by default for security reasons.
61+
if dist#vim#IsSafeExecutable('changelog', 'whoami')
6262
let login = s:login()
6363
else
6464
let login = s:default_login

runtime/ftplugin/perl.vim

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,8 @@ endif
5656

5757
" Set this once, globally.
5858
if !exists("perlpath")
59-
let s:tmp_cwd = getcwd()
6059
" safety check: don't execute perl binary by default
61-
if executable("perl") && get(g:, 'perl_exec', get(g:, 'plugin_exec', 0))
62-
\ && (fnamemodify(exepath("perl"), ":p:h") != s:tmp_cwd
63-
\ || (index(split($PATH, has("win32") ? ';' : ':'), s:tmp_cwd) != -1
64-
\ && s:tmp_cwd != '.'))
60+
if dist#vim#IsSafeExecutable('perl', 'perl')
6561
try
6662
if &shellxquote != '"'
6763
let perlpath = system('perl -e "print join(q/,/,@INC)"')
@@ -77,7 +73,6 @@ if !exists("perlpath")
7773
" current directory and the directory of the current file.
7874
let perlpath = ".,,"
7975
endif
80-
unlet! s:tmp_cwd
8176
endif
8277

8378
" Append perlpath to the existing path value, if it is set. Since we don't

runtime/ftplugin/zig.vim

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,13 @@ let &l:define='\v(<fn>|<const>|<var>|^\s*\#\s*define)'
4141

4242
" Safety check: don't execute zig from current directory
4343
if !exists('g:zig_std_dir') && exists('*json_decode') &&
44-
\ executable('zig') && get(g:, 'zig_exec', get(g:, 'plugin_exec', 0))
45-
\ && (fnamemodify(exepath("zig"), ":p:h") != s:tmp_cwd
46-
\ || (index(split($PATH,has("win32")? ';' : ':'), s:tmp_cwd) != -1 && s:tmp_cwd != '.'))
44+
\ executable('zig') && dist#vim#IsSafeExecutable('zig', 'zig')
4745
silent let s:env = system('zig env')
4846
if v:shell_error == 0
4947
let g:zig_std_dir = json_decode(s:env)['std_dir']
5048
endif
5149
unlet! s:env
5250
endif
53-
unlet! s:tmp_cwd
5451

5552
if exists('g:zig_std_dir')
5653
let &l:path = g:zig_std_dir . ',' . &l:path

0 commit comments

Comments
 (0)