robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqMerge pull request #41069 from zeertzjq/vim-b0e0b229d7c004ade

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