robin/laufey

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

zeertzjqMerge pull request #40317 from zeertzjq/vim-9.2.06730639f6e41a

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