robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
9d7c004ade
master
1so check.vim 2 3CheckExecutable unzip 4 5if 0 " Find uncovered line 6profile start zip_profile 7profile ! file */zip*.vim 8endif 9 10packadd old-zip 11 12func s: CopyZipFile ( source ) 13if ! filecopy ( $ "samples/{a:source}", "X.zip") 14call assert_report ( $ "Can't copy samples/{a:source}.zip") 15endif 16endfunc 17 18func Test_zip_basic () 19call s: CopyZipFile ( "test.zip" ) 20defer delete("X.zip") 21 22e X.zip 23 24"## Check header 25call assert_match ( '^" zip\.vim version v\d\+' , getline ( 1 )) 26call assert_match ( '^" Browsing zipfile .*/X\.zip' , getline ( 2 )) 27call assert_match ( '^" Select a file with cursor and press ENTER' , getline ( 3 )) 28call assert_match ( '^$' , getline ( 4 )) 29 30"## Check files listing 31call assert_equal ([ "Xzip/" , "Xzip/dir/" , "Xzip/file.txt" ], getline ( 5 , 7 )) 32 33"## Check ENTER on header 34: 1 35exe ":normal \<cr>" 36call assert_equal ( "X.zip" , @%) 37 38"## Check ENTER on directory 39: 1 |: / ^$ // dir / 40call assert_match ( 'Please specify a file, not a directory' , 41 \execute ( "normal \<CR>" )) 42 43"## Check ENTER on file 44: 1 45call search ( 'file.txt' ) 46exe ":normal \<cr>" 47call assert_match ( 'zipfile://.*/X\.zip::Xzip/file\.txt' , @%) 48call assert_equal ( 'one' , getline ( 1 )) 49 50"## Check editing file 51if executable ( "zip" ) 52s /one/two/ 53call assert_equal ( "two" , getline ( 1 )) 54w 55bw |bw 56e X.zip 57 58: 1 |: / ^$ // file / 59exe "normal \<cr>" 60call assert_equal ( "two" , getline ( 1 )) 61endif 62 63only 64e X.zip 65 66"## Check extracting file 67: 1 |: / ^$ // file / 68normal x 69call assert_true ( filereadable ( "Xzip/file.txt" )) 70 71"## Check not overwriting existing file 72call assert_match ( '<Xzip/file\.txt> .* not overwriting!' , execute ( "normal x" )) 73 74call delete ( "Xzip" , "rf" ) 75 76"## Check extracting directory 77: 1 |: / ^$ // dir / 78call assert_match ( 'Please specify a file, not a directory' , execute ( "normal x" )) 79call assert_equal ( "X.zip" , @%) 80 81"## Check "x" on header 82: 1 83normal x 84call assert_equal ( "X.zip" , @%) 85bw 86 87"## Check opening zip when "unzip" program is missing 88let save_zip_unzipcmd = g: zip_unzipcmd 89let g: zip_unzipcmd = "/" 90call assert_match ( '(zip#Browse) sorry, your system doesn''t appear to have the / program' , execute ( "e X.zip" )) 91 92"## Check when "unzip" don't work 93if executable ( "false" ) 94let g: zip_unzipcmd = "false" 95call assert_match ( 'X\.zip is not a zip file' , execute ( "e X.zip" )) 96endif 97bw 98 99let g: zip_unzipcmd = save_zip_unzipcmd 100e X.zip 101 102"## Check opening file when "unzip" is missing 103let g: zip_unzipcmd = "/" 104call assert_match ( 'sorry, your system doesn''t appear to have the / program' , 105 \execute ( "normal \<CR>" )) 106 107bw |bw 108let g: zip_unzipcmd = save_zip_unzipcmd 109e X.zip 110 111"## Check :write when "zip" program is missing 112: 1 |: / ^$ // file / 113exe "normal \<cr>Goanother\<esc>" 114let save_zip_zipcmd = g: zip_zipcmd 115let g: zip_zipcmd = "/" 116call assert_match ( 'sorry, your system doesn''t appear to have the / program' , 117 \execute ( "write" )) 118 119"## Check when "zip" report failure 120if executable ( "false" ) 121let g: zip_zipcmd = "false" 122call assert_match ( 'sorry, unable to update .*/X\.zip with Xzip/file\.txt' , 123 \execute ( "write" )) 124endif 125bw !|bw 126 127let g: zip_zipcmd = save_zip_zipcmd 128 129"## Check opening an no zipfile 130call writefile ([ "qsdf" ], "Xcorupt.zip" , "D" ) 131e ! Xcorupt.zip 132call assert_equal ( "qsdf" , getline ( 1 )) 133 134bw 135 136"## Check no existing zipfile 137call assert_match ( 'File not readable' , execute ( "e Xnot_exists.zip" )) 138 139bw 140endfunc 141 142func Test_zip_glob_fname () 143CheckNotMSWindows 144" does not work on Windows, why? 145 146call s: CopyZipFile ( "testa.zip" ) 147defer delete("X.zip") 148defer delete('zipglob','rf' ) 149 150e X.zip 151 152"## 1) Check extracting strange files 153: 1 154let fname = 'a[a].txt' 155call search ( '\V' .. fname ) 156normal x 157call assert_true ( filereadable ( 'zipglob/' .. fname )) 158call delete ( 'zipglob' , 'rf' ) 159 160: 1 161let fname = 'a*.txt' 162call search ( '\V' .. fname ) 163normal x 164call assert_true ( filereadable ( 'zipglob/' .. fname )) 165call delete ( 'zipglob' , 'rf' ) 166 167: 1 168let fname = 'a?.txt' 169call search ( '\V' .. fname ) 170normal x 171call assert_true ( filereadable ( 'zipglob/' .. fname )) 172call delete ( 'zipglob' , 'rf' ) 173 174: 1 175let fname = 'a\.txt' 176call search ( '\V' .. escape ( fname , '\\' )) 177normal x 178call assert_true ( filereadable ( 'zipglob/' .. fname )) 179call delete ( 'zipglob' , 'rf' ) 180 181: 1 182let fname = 'a\\.txt' 183call search ( '\V' .. escape ( fname , '\\' )) 184normal x 185call assert_true ( filereadable ( 'zipglob/' .. fname )) 186call delete ( 'zipglob' , 'rf' ) 187 188"## 2) Check entering strange file names 189: 1 190let fname = 'a[a].txt' 191call search ( '\V' .. fname ) 192exe ":normal \<cr>" 193call assert_match ( 'zipfile://.*/X\.zip::zipglob/a\[a\]\.txt' , @%) 194call assert_equal ( 'a test file with []' , getline ( 1 )) 195bw 196 197e X.zip 198: 1 199let fname = 'a*.txt' 200call search ( '\V' .. fname ) 201exe ":normal \<cr>" 202call assert_match ( 'zipfile://.*/X\.zip::zipglob/a\*\.txt' , @%) 203call assert_equal ( 'a test file with a*' , getline ( 1 )) 204bw 205 206e X.zip 207: 1 208let fname = 'a?.txt' 209call search ( '\V' .. fname ) 210exe ":normal \<cr>" 211call assert_match ( 'zipfile://.*/X\.zip::zipglob/a?\.txt' , @%) 212call assert_equal ( 'a test file with a?' , getline ( 1 )) 213bw 214 215e X.zip 216: 1 217let fname = 'a\.txt' 218call search ( '\V' .. escape ( fname , '\\' )) 219exe ":normal \<cr>" 220call assert_match ( 'zipfile://.*/X\.zip::zipglob/a\\\.txt' , @%) 221call assert_equal ( 'a test file with a\' , getline ( 1 )) 222bw 223 224e X.zip 225: 1 226let fname = 'a\\.txt' 227call search ( '\V' .. escape ( fname , '\\' )) 228exe ":normal \<cr>" 229call assert_match ( 'zipfile://.*/X\.zip::zipglob/a\\\\\.txt' , @%) 230call assert_equal ( 'a test file with a double \' , getline ( 1 )) 231bw 232 233bw 234endfunc 235 236func Test_zip_fname_leading_hyphen () 237CheckNotMSWindows 238 239"## copy sample zip file 240call s: CopyZipFile ( "poc.zip" ) 241defer delete("X.zip") 242defer delete('-d','rf' ) 243defer delete('/tmp/pwned','rf' ) 244 245e X.zip 246 247: 1 248let fname = '-d/tmp' 249call search ( '\V' .. fname ) 250normal x 251call assert_true ( filereadable ( '-d/tmp' )) 252call assert_false ( filereadable ( '/tmp/pwned' )) 253bw 254endfunc 255 256func Test_zip_fname_evil_path () 257CheckNotMSWindows 258" needed for writing the zip file 259CheckExecutable zip 260 261messages clear 262call s: CopyZipFile ( "evil.zip" ) 263defer delete("X.zip") 264e X.zip 265 266: 1 267let fname = 'pwn' 268call search ( '\V' .. fname ) 269normal x 270call assert_false ( filereadable ( '/etc/ax-pwn' )) 271let mess = execute ( ':mess' ) 272call assert_match ( 'Path Traversal Attack' , mess ) 273 274exe ":normal \<cr>" 275: w 276call assert_match ( 'zipfile://.*::etc/ax-pwn' , @%) 277bw 278endfunc 279 280func Test_zip_fname_evil_path2 () 281CheckNotMSWindows 282" needed for writing the zip file 283CheckExecutable zip 284 285messages clear 286call s: CopyZipFile ( "evil.zip" ) 287defer delete("X.zip") 288e X.zip 289 290: 1 291let fname = 'foobar' 292call search ( '\V' .. fname ) 293exe "normal \<cr>" 294normal x 295call assert_false ( filereadable ( '/tmp/foobar' )) 296: w 297let mess = execute ( ':mess' ) 298call assert_match ( 'Path Traversal Attack' , mess ) 299call assert_match ( 'zipfile://.*::.*tmp/foobar' , @%) 300bw ! 301endfunc 302 303func Test_zip_fname_evil_path3 () 304CheckNotMSWindows 305" needed for writing the zip file 306CheckExecutable zip 307 308messages clear 309call s: CopyZipFile ( "evil.zip" ) 310defer delete("X.zip") 311e X.zip 312 313: 1 314let fname = 'payload.txt' 315call search ( '\V' .. fname ) 316exe "normal \<cr>" 317: w ! 318let mess = execute ( ':mess' ) 319call assert_match ( 'Path Traversal Attack' , mess ) 320bw ! 321endfunc