Skip to content

Commit 87eb7c2

Browse files
committed
Harden system calls to git
1 parent 4984767 commit 87eb7c2

2 files changed

Lines changed: 36 additions & 17 deletions

File tree

autoload/vundle/installer.vim

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ endf
343343
" return -- the URL for the origin remote (string)
344344
" ---------------------------------------------------------------------------
345345
func! s:get_current_origin_url(bundle) abort
346-
let cmd = 'cd '.vundle#installer#shellesc(a:bundle.path()).' && git config --get remote.origin.url'
347-
let cmd = vundle#installer#shellesc_cd(cmd)
346+
let cmd = s:make_git_command(a:bundle, ['config', '--get', 'remote.origin.url'])
348347
let out = s:strip(s:system(cmd))
349348
return out
350349
endf
@@ -357,12 +356,37 @@ endf
357356
" return -- A 15 character log sha for the current HEAD
358357
" ---------------------------------------------------------------------------
359358
func! s:get_current_sha(bundle)
360-
let cmd = 'cd '.vundle#installer#shellesc(a:bundle.path()).' && git rev-parse HEAD'
361-
let cmd = vundle#installer#shellesc_cd(cmd)
359+
let cmd = s:make_sync_command(a:bundle, ['rev-parse', 'HEAD'])
362360
let out = s:system(cmd)[0:15]
363361
return out
364362
endf
365363

364+
" ---------------------------------------------------------------------------
365+
" Build a safe (escaped) git command
366+
"
367+
" bundle -- A bundle object to get the path to the git dir
368+
" args -- A list of arguments to the git executable
369+
" return -- A string containing the escaped shell command
370+
" ---------------------------------------------------------------------------
371+
func! s:make_git_command(bundle, args) abort
372+
let workdir = a:bundle.path()
373+
let gitdir = workdir.'/.git/'
374+
375+
let git = ['git', '--git-dir='.gitdir, '--work-tree='.workdir]
376+
377+
return join(map(copy(git + args), 'vundle#installer#shellesc'))
378+
endf
379+
380+
" ---------------------------------------------------------------------------
381+
" Build a safe (escaped) command from list of git args
382+
"
383+
" bundle -- A bundle object to get the path to the git dir
384+
" argss -- A list of lists of arguments to successive git calls
385+
" return -- A string containing the escaped shell command
386+
" ---------------------------------------------------------------------------
387+
func! s:make_git_commands(bundle, argss) abort
388+
return join(map(a:argss, 's:make_git_command(s:bundle, {})'), ' && ')
389+
endf
366390

367391
" ---------------------------------------------------------------------------
368392
" Create the appropriate sync command to run according to the current state of
@@ -388,14 +412,12 @@ func! s:make_sync_command(bang, bundle) abort
388412
call s:log('> Plugin ' . a:bundle.name . ' new URI: ' . a:bundle.uri)
389413
" Directory names match but the origin remotes are not the same
390414
let cmd_parts = [
391-
\ 'cd '.vundle#installer#shellesc(a:bundle.path()) ,
392-
\ 'git remote set-url origin ' . vundle#installer#shellesc(a:bundle.uri),
393-
\ 'git fetch',
394-
\ 'git reset --hard origin/HEAD',
395-
\ 'git submodule update --init --recursive',
396-
\ ]
397-
let cmd = join(cmd_parts, ' && ')
398-
let cmd = vundle#installer#shellesc_cd(cmd)
415+
[ 'remote', 'set-url', 'origin', a:bundle.uri ],
416+
[ 'fetch' ],
417+
[ 'reset', '--hard', 'origin/HEAD' ],
418+
[ 'submodule', 'update', '--init', '--recursive' ],
419+
]
420+
let cmd = s:make_git_commands(a:bundle, cmd_parts)
399421
let initial_sha = ''
400422
return [cmd, initial_sha]
401423
endif

autoload/vundle/scripts.vim

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,8 @@ func! s:create_changelog() abort
8383
let updated_sha = bundle_data[1]
8484
let bundle = bundle_data[2]
8585

86-
let cmd = 'cd '.vundle#installer#shellesc(bundle.path()).
87-
\ ' && git log --pretty=format:"%s %an, %ar" --graph '.
88-
\ initial_sha.'..'.updated_sha
89-
90-
let cmd = vundle#installer#shellesc_cd(cmd)
86+
let cmd = s:make_git_command(bundle, ['log', '--pretty=format:"%s %an, %ar"',
87+
\ '--graph', initial_sha.'..'.updated_sha ])
9188

9289
let updates = system(cmd)
9390

0 commit comments

Comments
 (0)