robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
0639f6e41a
master
1 2CheckExecutable tar 3CheckNotMSWindows 4 5runtime plugin/tarPlugin.vim 6 7func s: CopyFile ( source ) 8if ! filecopy ( $ "samples/{a:source}", "X.tar") 9call assert_report ( $ "Can't copy samples/{a:source}") 10endif 11endfunc 12 13func Test_tar_basic () 14call s: CopyFile ( "sample.tar" ) 15defer delete("X.tar") 16defer delete("./testtar",'rf' ) 17e X.tar 18 19"## Check header 20call assert_match ( '^" tar\.vim version v\d\+' , getline ( 1 )) 21call assert_match ( '^" Browsing tarfile .*/X.tar' , getline ( 2 )) 22call assert_match ( '^" Select a file with cursor and press ENTER, "x" to extract a file' , getline ( 3 )) 23call assert_match ( '^$' , getline ( 4 )) 24call assert_equal ( 'testtar/' , getline ( 5 )) 25call assert_equal ( 'testtar/file1.txt' , getline ( 6 )) 26 27"## Check ENTER on header 28: 1 29exe ":normal \<cr>" 30call assert_equal ( "X.tar" , @%) 31 32"## Check ENTER on file 33: 6 34exe ":normal \<cr>" 35call assert_equal ( "tarfile::testtar/file1.txt" , @%) 36 37 38"## Check editing file 39"## Note: deleting entries not supported on BSD 40if has ( "mac" ) 41return 42endif 43if has ( "bsd" ) 44return 45endif 46s /. * /some-content/ 47call assert_equal ( "some-content" , getline ( 1 )) 48w ! 49call assert_equal ( "tarfile::testtar/file1.txt" , @%) 50bw ! 51close 52bw ! 53 54e X.tar 55: 6 56exe "normal \<cr>" 57call assert_equal ( "some-content" , getline ( 1 )) 58bw ! 59close 60 61"## Check extracting file 62: 5 63normal x 64call assert_true ( filereadable ( "./testtar/file1.txt" )) 65bw ! 66endfunc 67 68func Test_tar_evil () 69" On s390x, tar outputs its full path in warning messages (e.g. /usr/bin/tar: Removing leading '/') 70" which tar.vim doesn't handle, causing path traversal detection to fail. 71CheckNotS390 72call s: CopyFile ( "evil.tar" ) 73defer delete("X.tar") 74defer delete("./etc",'rf' ) 75e X.tar 76 77"## Check header 78call assert_match ( '^" tar\.vim version v\d\+' , getline ( 1 )) 79call assert_match ( '^" Browsing tarfile .*/X.tar' , getline ( 2 )) 80call assert_match ( '^" Select a file with cursor and press ENTER, "x" to extract a file' , getline ( 3 )) 81call assert_match ( '^" Note: Path Traversal Attack detected' , getline ( 4 )) 82call assert_match ( '^$' , getline ( 5 )) 83call assert_equal ( '/etc/ax-pwn' , getline ( 6 )) 84 85"## Check ENTER on header 86: 1 87exe ":normal \<cr>" 88call assert_equal ( "X.tar" , @%) 89call assert_equal ( 1 , b: leading_slash ) 90 91"## Press x to extract 92: 6 93let mess = execute ( ":normal x" , '' ) 94call assert_match ( '(tar#Extract) Path Traversal Attack detected, not extracting!' , mess ) 95 96"## Check ENTER on file 97: 6 98exe ":normal \<cr>" 99call assert_equal ( 1 , b: leading_slash ) 100call assert_equal ( "tarfile::/etc/ax-pwn" , @%) 101 102 103"## Check editing file 104"## Note: deleting entries not supported on BSD 105if has ( "mac" ) 106return 107endif 108if has ( "bsd" ) 109return 110endif 111s /. * /none/ 112call assert_equal ( "none" , getline ( 1 )) 113w ! 114call assert_equal ( 1 , b: leading_slash ) 115call assert_equal ( "tarfile::/etc/ax-pwn" , @%) 116bw ! 117close 118bw ! 119 120" Writing was aborted 121e X.tar 122call assert_match ( '^" Note: Path Traversal Attack detected' , getline ( 4 )) 123: 6 124exe "normal \<cr>" 125call assert_equal ( "something" , getline ( 1 )) 126bw ! 127close 128 129"## Check extracting file 130: 5 131normal x 132call assert_true ( filereadable ( "./etc/ax-pwn" )) 133 134bw ! 135endfunc 136 137func Test_tar_path_traversal_with_nowrapscan () 138CheckNotS390 139call s: CopyFile ( "evil.tar" ) 140defer delete("X.tar") 141" Make sure we still find the tar warning (or leading slashes) even when 142" wrapscan is off 143set nowrapscan 144e X.tar 145 146"## Check header 147call assert_match ( '^" tar\.vim version v\d\+' , getline ( 1 )) 148call assert_match ( '^" Browsing tarfile .*/X.tar' , getline ( 2 )) 149call assert_match ( '^" Select a file with cursor and press ENTER, "x" to extract a file' , getline ( 3 )) 150call assert_match ( '^" Note: Path Traversal Attack detected' , getline ( 4 )) 151call assert_match ( '^$' , getline ( 5 )) 152call assert_equal ( '/etc/ax-pwn' , getline ( 6 )) 153 154call assert_equal ( 1 , b: leading_slash ) 155 156bw ! 157endfunc 158 159func s: CreateTar ( archivename , content , outputdir ) 160let tempdir = tempname () 161call mkdir ( tempdir , 'R' ) 162call writefile ([ a: content ], tempdir .. '/X.txt' ) 163call assert_true ( filereadable ( tempdir .. '/X.txt' )) 164call system ( 'tar -C ' .. tempdir .. ' -cf ' .. a: outputdir .. '/' .. a: archivename .. ' X.txt' ) 165call assert_equal ( 0 , v: shell_error ) 166endfunc 167 168func s: CreateTgz ( archivename , content , outputdir ) 169let tempdir = tempname () 170call mkdir ( tempdir , 'R' ) 171call writefile ([ a: content ], tempdir .. '/X.txt' ) 172call assert_true ( filereadable ( tempdir .. '/X.txt' )) 173call system ( 'tar -C ' .. tempdir .. ' -czf ' .. a: outputdir .. '/' .. a: archivename .. ' X.txt' ) 174call assert_equal ( 0 , v: shell_error ) 175endfunc 176 177func s: CreateTbz ( archivename , content , outputdir ) 178let tempdir = tempname () 179call mkdir ( tempdir , 'R' ) 180call writefile ([ a: content ], tempdir .. '/X.txt' ) 181call assert_true ( filereadable ( tempdir .. '/X.txt' )) 182call system ( 'tar -C ' .. tempdir .. ' -cjf ' .. a: outputdir .. '/' .. a: archivename .. ' X.txt' ) 183call assert_equal ( 0 , v: shell_error ) 184endfunc 185 186func s: CreateTxz ( archivename , content , outputdir ) 187let tempdir = tempname () 188call mkdir ( tempdir , 'R' ) 189call writefile ([ a: content ], tempdir .. '/X.txt' ) 190call assert_true ( filereadable ( tempdir .. '/X.txt' )) 191call system ( 'tar -C ' .. tempdir .. ' -cJf ' .. a: outputdir .. '/' .. a: archivename .. ' X.txt' ) 192call assert_equal ( 0 , v: shell_error ) 193endfunc 194 195func s: CreateTzst ( archivename , content , outputdir ) 196let tempdir = tempname () 197call mkdir ( tempdir , 'R' ) 198call writefile ([ a: content ], tempdir .. '/X.txt' ) 199call assert_true ( filereadable ( tempdir .. '/X.txt' )) 200call system ( 'tar --zstd -C ' .. tempdir .. ' -cf ' .. a: outputdir .. '/' .. a: archivename .. ' X.txt' ) 201call assert_equal ( 0 , v: shell_error ) 202endfunc 203 204func s: CreateTlz4 ( archivename , content , outputdir ) 205let tempdir = tempname () 206call mkdir ( tempdir , 'R' ) 207call writefile ([ a: content ], tempdir .. '/X.txt' ) 208call assert_true ( filereadable ( tempdir .. '/X.txt' )) 209call system ( 'tar -C ' .. tempdir .. ' -cf ' .. tempdir .. '/Xarchive.tar X.txt' ) 210call assert_equal ( 0 , v: shell_error ) 211call assert_true ( filereadable ( tempdir .. '/Xarchive.tar' )) 212call system ( 'lz4 -z ' .. tempdir .. '/Xarchive.tar ' .. a: outputdir .. '/' .. a: archivename ) 213call assert_equal ( 0 , v: shell_error ) 214endfunc 215 216" XXX: Add test for .tar.bz3 217func Test_extraction () 218let control = [ 219 \#{ create : function ( 's:CreateTar' ), 220 \archive : 'Xarchive.tar' }, 221 \#{ create : function ( 's:CreateTgz' ), 222 \archive : 'Xarchive.tgz' }, 223 \#{ create : function ( 's:CreateTgz' ), 224 \archive : 'Xarchive.tar.gz' }, 225 \#{ create : function ( 's:CreateTbz' ), 226 \archive : 'Xarchive.tbz' }, 227 \#{ create : function ( 's:CreateTbz' ), 228 \archive : 'Xarchive.tar.bz2' }, 229 \#{ create : function ( 's:CreateTxz' ), 230 \archive : 'Xarchive.txz' }, 231 \#{ create : function ( 's:CreateTxz' ), 232 \archive : 'Xarchive.tar.xz' }, 233 \] 234 235if executable ( 'lz4' ) == 1 236eval control ->add (#{ 237 \create : function ( 's:CreateTlz4' ), 238 \archive : 'Xarchive.tar.lz4' 239 \}) 240eval control ->add (#{ 241 \create : function ( 's:CreateTlz4' ), 242 \archive : 'Xarchive.tlz4' 243 \}) 244endif 245if executable ( 'zstd' ) == 1 246eval control ->add (#{ 247 \create : function ( 's:CreateTzst' ), 248 \archive : 'Xarchive.tar.zst' 249 \}) 250eval control ->add (#{ 251 \create : function ( 's:CreateTzst' ), 252 \archive : 'Xarchive.tzst' 253 \}) 254endif 255 256for c in control 257let dir = tempname () 258call mkdir ( dir , 'R' ) 259call call ( c . create , [ c . archive , 'hello' , dir ]) 260 261call delete ( 'X.txt' ) 262execute 'edit ' .. dir .. '/' .. c . archive 263call assert_equal ( 'X.txt' , getline ( 5 ), 'line 5 wrong in archive: ' .. c . archive ) 264: 5 265normal x 266call assert_equal ( 0 , v: shell_error , 'vshell error not 0' ) 267call assert_true ( filereadable ( 'X.txt' ), 'X.txt not readable for archive: ' .. c . archive ) 268call assert_equal ([ 'hello' ], readfile ( 'X.txt' ), 'X.txt wrong contents for archive: ' .. c . archive ) 269call delete ( 'X.txt' ) 270call delete ( dir .. '/' .. c . archive ) 271bw ! 272endfor 273endfunc 274 275func Test_extract_with_dotted_dir () 276call delete ( 'X.txt' ) 277call writefile ([ 'when they kiss they spit white noise' ], 'X.txt' ) 278 279let dirname = tempname () 280call mkdir ( dirname , 'R' ) 281let dirname = dirname .. '/foo.bar' 282call mkdir ( dirname , 'R' ) 283let tarpath = dirname .. '/Xarchive.tar.gz' 284call system ( 'tar -czf ' .. tarpath .. ' X.txt' ) 285call assert_true ( filereadable ( tarpath )) 286call assert_equal ( 0 , v: shell_error ) 287 288call delete ( 'X.txt' ) 289defer delete(tarpath) 290 291execute 'e ' .. tarpath 292call assert_equal ( 'X.txt' , getline ( 5 )) 293: 5 294normal x 295call assert_true ( filereadable ( 'X.txt' )) 296call assert_equal ([ 'when they kiss they spit white noise' ], readfile ( 'X.txt' )) 297call delete ( 'X.txt' ) 298bw ! 299endfunc 300 301func Test_extract_with_dotted_filename () 302call delete ( 'X.txt' ) 303call writefile ([ 'holiday inn' ], 'X.txt' ) 304 305let dirname = tempname () 306call mkdir ( dirname , 'R' ) 307let tarpath = dirname .. '/Xarchive.foo.tar.gz' 308call system ( 'tar -czf ' .. tarpath .. ' X.txt' ) 309call assert_true ( filereadable ( tarpath )) 310call assert_equal ( 0 , v: shell_error ) 311 312call delete ( 'X.txt' ) 313defer delete(tarpath) 314 315execute 'e ' .. tarpath 316call assert_equal ( 'X.txt' , getline ( 5 )) 317: 5 318normal x 319call assert_true ( filereadable ( 'X.txt' )) 320call assert_equal ([ 'holiday inn' ], readfile ( 'X.txt' )) 321call delete ( 'X.txt' ) 322bw ! 323endfunc 324 325func Test_extract_command_injection () 326throw 'SKipped: Nvim does not support :Vimuntar' 327CheckExecutable gunzip 328CheckExecutable touch 329var tgz = eval('0z1F8B08087795056A000364756D6D792E74617200EDCE2B12C2300004D01C254' .. 330 '7480269 CE534080A8495BD1DBF3996106C3A08A7ACFACD8157B59A7690BFB4A0FC3707C666E357D ' .. 331 'E65 BC8B5A47CC8A5D61A522EA5B510D3CEBF5ED679197B8CE17CEDB7F9D4C76FBB5F3D000000000 ' .. 332 '000000000 FCD11D32415E2C00280000 ') 333var dirname = tempname() 334 335mkdir (dirname,'R' ) 336var tar = dirname .."/';%$(touch pwned)'.tgz" 337writefile (tgz, tar) 338new 339exe "e " .. fnameescape ( tar ) 340exe ":Vimuntar " .. dirname 341assert _false(filereadable(dirname .."/pwned" )) 342bw ! 343endfunc