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